Repository navigation
Expand file tree
/
Copy pathPromptBuilder.php
More file actions
148 lines (123 loc) · 2.8 KB
/
Copy pathPromptBuilder.php
File metadata and controls
148 lines (123 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace ClaudeAgents\Prompts;
/**
* Fluent builder for constructing prompts.
*
* Provides a clean API for building complex prompts with context, examples, and formatting.
*
* @example
* ```php
* $prompt = PromptBuilder::create()
* ->addContext('You are a helpful assistant')
* ->addTask('Solve this problem')
* ->addExample('Input: 1+1', 'Output: 2')
* ->addConstraint('Use step-by-step reasoning')
* ->build();
* ```
*/
class PromptBuilder
{
/**
* @var array<string>
*/
private array $sections = [];
/**
* Create a new prompt builder.
*/
public static function create(): self
{
return new self();
}
/**
* Add context/background information.
*/
public function addContext(string $context): self
{
$this->sections[] = $context;
return $this;
}
/**
* Add the main task description.
*/
public function addTask(string $task): self
{
$this->sections[] = "Task: {$task}";
return $this;
}
/**
* Add an example.
*/
public function addExample(string $input, string $output): self
{
$this->sections[] = "Example:\nInput: {$input}\nOutput: {$output}";
return $this;
}
/**
* Add multiple examples.
*
* @param array<array{input: string, output: string}> $examples
*/
public function addExamples(array $examples): self
{
foreach ($examples as $example) {
$this->addExample($example['input'], $example['output']);
}
return $this;
}
/**
* Add a constraint or requirement.
*/
public function addConstraint(string $constraint): self
{
$this->sections[] = "Requirement: {$constraint}";
return $this;
}
/**
* Add instructions.
*/
public function addInstructions(string $instructions): self
{
$this->sections[] = "Instructions:\n{$instructions}";
return $this;
}
/**
* Add a custom section.
*/
public function addSection(string $title, string $content): self
{
$this->sections[] = "{$title}:\n{$content}";
return $this;
}
/**
* Add raw text.
*/
public function addRaw(string $text): self
{
$this->sections[] = $text;
return $this;
}
/**
* Add a separator line.
*/
public function addSeparator(): self
{
$this->sections[] = '---';
return $this;
}
/**
* Build the final prompt.
*/
public function build(): string
{
return implode("\n\n", $this->sections);
}
/**
* Clear all sections.
*/
public function clear(): self
{
$this->sections = [];
return $this;
}
}