33namespace Utopia \Agents \Adapters ;
44
55use Utopia \Agents \Adapter ;
6- use Utopia \Agents \Conversation ;
76use Utopia \Agents \Message ;
87use Utopia \Agents \Messages \Text ;
98use Utopia \Fetch \Chunk ;
@@ -88,38 +87,41 @@ public function send(array $messages, ?callable $listener = null): Message
8887 throw new \Exception ('Agent not set ' );
8988 }
9089
91- $ client = new \ Utopia \ Fetch \ Client ();
90+ $ client = new Client ();
9291 $ client
9392 ->setTimeout (90 )
9493 ->addHeader ('x-api-key ' , $ this ->apiKey )
9594 ->addHeader ('anthropic-version ' , '2023-06-01 ' )
96- ->addHeader ('content-type ' , ' application/json ' );
95+ ->addHeader ('content-type ' , Client:: CONTENT_TYPE_APPLICATION_JSON );
9796
97+ $ formattedMessages = [];
9898 foreach ($ messages as $ message ) {
99- $ messages [] = [
100- 'role ' => $ message[ ' role ' ] ,
101- 'content ' => $ message[ ' content ' ] ,
99+ $ formattedMessages [] = [
100+ 'role ' => $ message-> getRole () ,
101+ 'content ' => $ message-> getContent () ,
102102 ];
103103 }
104104
105105 $ instructions = [];
106106 foreach ($ this ->getAgent ()->getInstructions () as $ name => $ content ) {
107- $ instructions [] = " # " . $ name . "\n\n" . $ content ;
107+ $ instructions [] = ' # ' . $ name. "\n\n" . $ content ;
108108 }
109109
110+ $ payload = [
111+ 'model ' => $ this ->model ,
112+ 'system ' => $ this ->getAgent ()->getDescription ().
113+ (empty ($ instructions ) ? '' : "\n\n" .implode ("\n\n" , $ instructions )),
114+ 'messages ' => $ formattedMessages ,
115+ 'max_tokens ' => $ this ->maxTokens ,
116+ 'temperature ' => $ this ->temperature ,
117+ 'stream ' => true ,
118+ ];
119+
110120 $ content = '' ;
111121 $ response = $ client ->fetch (
112122 'https://api.anthropic.com/v1/messages ' ,
113123 Client::METHOD_POST ,
114- [
115- 'model ' => $ this ->model ,
116- 'system ' => $ this ->getAgent ()->getDescription () .
117- (empty ($ instructions ) ? '' : "\n\n" . implode ("\n\n" , $ instructions )),
118- 'messages ' => $ messages ,
119- 'max_tokens ' => $ this ->maxTokens ,
120- 'temperature ' => $ this ->temperature ,
121- 'stream ' => true ,
122- ],
124+ $ payload ,
123125 [],
124126 function ($ chunk ) use (&$ content , $ listener ) {
125127 $ content .= $ this ->process ($ chunk , $ listener );
@@ -130,16 +132,13 @@ function ($chunk) use (&$content, $listener) {
130132 throw new \Exception ('Anthropic API error ( ' .$ response ->getStatusCode ().'): ' .$ response ->getBody ());
131133 }
132134
133- $ message = new Text ($ content );
134-
135- return $ message ;
135+ return new Text ($ content );
136136 }
137137
138138 /**
139139 * Process a stream chunk from the Anthropic API
140140 *
141141 * @param \Utopia\Fetch\Chunk $chunk
142- * @param Conversation $conversation
143142 * @param callable|null $listener
144143 * @return string
145144 *
@@ -152,7 +151,6 @@ protected function process(Chunk $chunk, ?callable $listener): string
152151 $ lines = explode ("\n" , $ data );
153152
154153 foreach ($ lines as $ line ) {
155-
156154 if (empty (trim ($ line ))) {
157155 continue ;
158156 }
@@ -199,7 +197,7 @@ protected function process(Chunk $chunk, ?callable $listener): string
199197 $ block = $ json ['delta ' ]['text ' ];
200198 }
201199
202- if (!empty ($ block )) {
200+ if (! empty ($ block )) {
203201 if ($ listener !== null ) {
204202 $ listener ($ block );
205203 }
0 commit comments