Repository navigation
Expand file tree
/
Copy pathChatTemplate.php
More file actions
166 lines (141 loc) · 4.01 KB
/
Copy pathChatTemplate.php
File metadata and controls
166 lines (141 loc) · 4.01 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace ClaudeAgents\Prompts;
use ClaudeAgents\Exceptions\ValidationException;
/**
* Template for multi-turn chat conversations.
*/
class ChatTemplate
{
/**
* @var array<array{role: string, content: string}>
*/
private array $messages = [];
/**
* @var array<string> Extracted variables
*/
private array $variables = [];
private function __construct()
{
}
/**
* Create a new chat template.
*/
public static function create(): self
{
return new self();
}
/**
* Add a system message.
*/
public function system(string $content): self
{
$this->messages[] = ['role' => 'system', 'content' => $content];
$this->extractVariables($content);
return $this;
}
/**
* Add a user message.
*/
public function user(string $content): self
{
$this->messages[] = ['role' => 'user', 'content' => $content];
$this->extractVariables($content);
return $this;
}
/**
* Add an assistant message.
*/
public function assistant(string $content): self
{
$this->messages[] = ['role' => 'assistant', 'content' => $content];
$this->extractVariables($content);
return $this;
}
/**
* Add a message.
*
* @param string $role The role (system, user, assistant)
* @param string $content The content
*/
public function message(string $role, string $content): self
{
$this->messages[] = ['role' => $role, 'content' => $content];
$this->extractVariables($content);
return $this;
}
/**
* Format the template with values.
*
* @param array<string, mixed> $values Map of variable names to values
* @return array<array<string, mixed>> Formatted messages
*/
public function format(array $values): array
{
return array_map(function ($msg) use ($values) {
$content = $msg['content'];
foreach ($values as $key => $value) {
$placeholder = '{' . $key . '}';
$content = str_replace($placeholder, (string) $value, $content);
}
return ['role' => $msg['role'], 'content' => $content];
}, $this->messages);
}
/**
* Get all messages without formatting.
*
* @return array<array<string, string>>
*/
public function getMessages(): array
{
return $this->messages;
}
/**
* Get required variables.
*
* @return array<string>
*/
public function getVariables(): array
{
return array_unique($this->variables);
}
/**
* Extract variables from content.
*/
private function extractVariables(string $content): void
{
if (preg_match_all('/\{(\w+)\}/', $content, $matches)) {
$this->variables = array_merge($this->variables, $matches[1]);
}
}
/**
* Validate that all variables are provided.
*
* @param array<string, mixed> $values
* @throws ValidationException If required variables missing
*/
public function validate(array $values): void
{
$missing = [];
$required = $this->getVariables();
foreach ($required as $var) {
if (! isset($values[$var])) {
$missing[] = $var;
}
}
if (! empty($missing)) {
$available = array_keys($values);
throw new ValidationException(
sprintf(
'ChatTemplate validation failed. Missing required variable(s): %s. ' .
'Available variables: %s. ' .
'Required variables are extracted from your chat messages. ' .
"Please provide: ['%s' => 'value']",
implode(', ', $missing),
empty($available) ? '(none)' : implode(', ', $available),
implode("' => 'value', '", $missing)
)
);
}
}
}