Skip to content

Commit 06acf61

Browse files
committed
Improved structure
1 parent 0cc1ca3 commit 06acf61

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

src/Agents/Adapter.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ abstract class Adapter
1111
*/
1212
protected ?Agent $agent = null;
1313

14+
/**
15+
* Input tokens count
16+
*
17+
* @var int
18+
*/
19+
protected int $inputTokens = 0;
20+
21+
/**
22+
* Output tokens count
23+
*
24+
* @var int
25+
*/
26+
protected int $outputTokens = 0;
27+
1428
/**
1529
* Get the adapter name
1630
*
@@ -21,12 +35,13 @@ abstract public function getName(): string;
2135
/**
2236
* Send a message to the AI model
2337
*
24-
* @param Conversation $conversation The conversation instance containing messages and tracking tokens
38+
* @param array<Message> $messages The messages to send to the AI model
39+
* @param callable|null $listener The listener to call when the message is sent
2540
* @return Message Response from the AI model
2641
*
2742
* @throws \Exception
2843
*/
29-
abstract public function send(Conversation $conversation): Message;
44+
abstract public function send(array $messages, ?callable $listener = null): Message;
3045

3146
/**
3247
* Get available models for this adapter
@@ -74,4 +89,61 @@ public function setAgent(Agent $agent): self
7489

7590
return $this;
7691
}
92+
93+
94+
/**
95+
* Get input tokens count
96+
*
97+
* @return int
98+
*/
99+
public function getInputTokens(): int
100+
{
101+
return $this->inputTokens;
102+
}
103+
104+
/**
105+
* Add to input tokens count
106+
*
107+
* @param int $tokens
108+
* @return self
109+
*/
110+
public function countInputTokens(int $tokens): self
111+
{
112+
$this->inputTokens += $tokens;
113+
114+
return $this;
115+
}
116+
117+
/**
118+
* Get output tokens count
119+
*
120+
* @return int
121+
*/
122+
public function getOutputTokens(): int
123+
{
124+
return $this->outputTokens;
125+
}
126+
127+
/**
128+
* Add to output tokens count
129+
*
130+
* @param int $tokens
131+
* @return self
132+
*/
133+
public function countOutputTokens(int $tokens): self
134+
{
135+
$this->outputTokens += $tokens;
136+
137+
return $this;
138+
}
139+
140+
/**
141+
* Get total tokens count
142+
*
143+
* @return int
144+
*/
145+
public function getTotalTokens(): int
146+
{
147+
return $this->inputTokens + $this->outputTokens;
148+
}
77149
}

src/Agents/Adapters/Anthropic.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Utopia\Agents\Conversation;
77
use Utopia\Agents\Message;
88
use Utopia\Agents\Messages\Text;
9-
use Utopia\Agents\Roles\Assistant;
109
use Utopia\Fetch\Chunk;
1110
use Utopia\Fetch\Client;
1211

@@ -77,12 +76,13 @@ public function __construct(
7776
/**
7877
* Send a message to the Anthropic API
7978
*
80-
* @param Conversation $conversation
79+
* @param array<Message> $messages
80+
* @param callable|null $listener
8181
* @return Message
8282
*
8383
* @throws \Exception
8484
*/
85-
public function send(Conversation $conversation): Message
85+
public function send(array $messages, ?callable $listener = null): Message
8686
{
8787
if ($this->getAgent() === null) {
8888
throw new \Exception('Agent not set');
@@ -94,8 +94,7 @@ public function send(Conversation $conversation): Message
9494
->addHeader('anthropic-version', '2023-06-01')
9595
->addHeader('content-type', 'application/json');
9696

97-
$messages = [];
98-
foreach ($conversation->getMessages() as $message) {
97+
foreach ($messages as $message) {
9998
$messages[] = [
10099
'role' => $message['role'],
101100
'content' => $message['content'],
@@ -121,8 +120,8 @@ public function send(Conversation $conversation): Message
121120
'stream' => true,
122121
],
123122
[],
124-
function ($chunk) use ($conversation, &$content) {
125-
$content .= $this->process($chunk, $conversation, $conversation->getListener());
123+
function ($chunk) use (&$content, $listener) {
124+
$content .= $this->process($chunk, $listener);
126125
}
127126
);
128127

@@ -145,7 +144,7 @@ function ($chunk) use ($conversation, &$content) {
145144
*
146145
* @throws \Exception
147146
*/
148-
protected function process(Chunk $chunk, Conversation $conversation, ?callable $listener): string
147+
protected function process(Chunk $chunk, ?callable $listener): string
149148
{
150149
$block = '';
151150
$data = $chunk->getData();
@@ -176,10 +175,10 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
176175
if (isset($json['message']['usage'])) {
177176
$usage = $json['message']['usage'];
178177
if (isset($usage['input_tokens']) && is_int($usage['input_tokens'])) {
179-
$conversation->countInputTokens($usage['input_tokens']);
178+
$this->countInputTokens($usage['input_tokens']);
180179
}
181180
if (isset($usage['output_tokens']) && is_int($usage['output_tokens'])) {
182-
$conversation->countOutputTokens($usage['output_tokens']);
181+
$this->countOutputTokens($usage['output_tokens']);
183182
}
184183
}
185184
break;
@@ -211,13 +210,13 @@ protected function process(Chunk $chunk, Conversation $conversation, ?callable $
211210
break;
212211

213212
case 'message_delta':
214-
if (isset($json['message']['usage'])) {
215-
$usage = $json['message']['usage'];
213+
if (isset($json['usage'])) {
214+
$usage = $json['usage'];
216215
if (isset($usage['input_tokens']) && is_int($usage['input_tokens'])) {
217-
$conversation->countInputTokens($usage['input_tokens']);
216+
$this->countInputTokens($usage['input_tokens']);
218217
}
219218
if (isset($usage['output_tokens']) && is_int($usage['output_tokens'])) {
220-
$conversation->countOutputTokens($usage['output_tokens']);
219+
$this->countOutputTokens($usage['output_tokens']);
221220
}
222221
}
223222
break;

src/Agents/Conversation.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ public function message(Role $from, Message $message): self
8585
*/
8686
public function send(): Message
8787
{
88-
$message = $this->agent->getAdapter()->send($this);
88+
$message = $this->agent->getAdapter()->send($this->messages, $this->listener);
89+
90+
$this->countInputTokens($this->agent->getAdapter()->getInputTokens());
91+
$this->countOutputTokens($this->agent->getAdapter()->getOutputTokens());
92+
8993
$from = new Assistant($this->agent->getAdapter()->getModel(), 'Assistant');
9094
$this->message($from, $message);
9195

0 commit comments

Comments
 (0)