Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

Providers.php

190 lines5.0 KB

1<?php
2​
3declare(strict_types=1);
4​
5function stream_openai(
6 string $model,
7 string $system,
8 array $messages,
9 callable $emit
10): void {
11 $apiKey = getenv('OPENAI_API_KEY') ?: '';
12 if ($apiKey === '') {
13 throw new RuntimeException('OPENAI_API_KEY is not set');
14 }
15​
16 $openaiMessages = [];
17 if ($system !== '') {
18 $openaiMessages[] = ['role' => 'system', 'content' => $system];
19 }
20 foreach ($messages as $message) {
21 $openaiMessages[] = [
22 'role' => $message['role'],
23 'content' => $message['content'],
24 ];
25 }
26​
27 $payload = json_encode([
28 'model' => $model,
29 'messages' => $openaiMessages,
30 'stream' => true,
31 ], JSON_UNESCAPED_UNICODE);
32​
33 if ($payload === false) {
34 throw new RuntimeException('failed to encode OpenAI request');
35 }
36​
37 stream_provider_sse(
38 'https://api.openai.com/v1/chat/completions',
39 [
40 'Authorization: Bearer ' . $apiKey,
41 'Content-Type: application/json',
42 ],
43 $payload,
44 static function (array $parsed) use ($emit): void {
45 $delta = $parsed['choices'][0]['delta']['content'] ?? null;
46 if (is_string($delta) && $delta !== '') {
47 $emit($delta);
48 }
49 }
50 );
52​
53function stream_anthropic(
54 string $model,
55 string $system,
56 array $messages,
57 callable $emit
58): void {
59 $apiKey = getenv('ANTHROPIC_API_KEY') ?: '';
60 if ($apiKey === '') {
61 throw new RuntimeException('ANTHROPIC_API_KEY is not set');
62 }
63​
64 $anthropicMessages = [];
65 foreach ($messages as $message) {
66 $anthropicMessages[] = [
67 'role' => $message['role'],
68 'content' => $message['content'],
69 ];
70 }
71​
72 $body = [
73 'model' => $model,
74 'max_tokens' => 4096,
75 'messages' => $anthropicMessages,
76 'stream' => true,
77 ];
78 if ($system !== '') {
79 $body['system'] = $system;
80 }
81​
82 $payload = json_encode($body, JSON_UNESCAPED_UNICODE);
83 if ($payload === false) {
84 throw new RuntimeException('failed to encode Anthropic request');
85 }
86​
87 stream_provider_sse(
88 'https://api.anthropic.com/v1/messages',
89 [
90 'x-api-key: ' . $apiKey,
91 'anthropic-version: 2023-06-01',
92 'Content-Type: application/json',
93 ],
94 $payload,
95 static function (array $parsed) use ($emit): void {
96 if (($parsed['type'] ?? '') !== 'content_block_delta') {
97 return;
98 }
99 $delta = $parsed['delta'] ?? null;
100 if (!is_array($delta) || ($delta['type'] ?? '') !== 'text_delta') {
101 return;
102 }
103 $text = $delta['text'] ?? null;
104 if (is_string($text) && $text !== '') {
105 $emit($text);
106 }
107 }
108 );
110​
111function stream_completion(
112 array $config,
113 string $system,
114 array $messages,
115 callable $emit
116): void {
117 switch ($config['provider']) {
118 case 'openai':
119 stream_openai($config['model'], $system, $messages, $emit);
120 return;
121 case 'anthropic':
122 stream_anthropic($config['model'], $system, $messages, $emit);
123 return;
124 default:
125 throw new RuntimeException(
126 'unsupported provider "' . $config['provider'] . '" (expected openai or anthropic)'
127 );
128 }
130​
131function stream_provider_sse(
132 string $url,
133 array $headers,
134 string $payload,
135 callable $handleEvent
136): void {
137 $buffer = '';
138​
139 $ch = curl_init($url);
140 if ($ch === false) {
141 throw new RuntimeException('failed to initialize curl');
142 }
143​
144 curl_setopt_array($ch, [
145 CURLOPT_POST => true,
146 CURLOPT_HTTPHEADER => $headers,
147 CURLOPT_POSTFIELDS => $payload,
148 CURLOPT_RETURNTRANSFER => false,
149 CURLOPT_WRITEFUNCTION => static function ($handle, string $chunk) use (&$buffer, $handleEvent): int {
150 $buffer .= $chunk;
151​
152 while (($lineEnd = strpos($buffer, "\n")) !== false) {
153 $line = rtrim(substr($buffer, 0, $lineEnd), "\r");
154 $buffer = substr($buffer, $lineEnd + 1);
155​
156 if (!str_starts_with($line, 'data: ')) {
157 continue;
158 }
159​
160 $data = substr($line, 6);
161 if ($data === '[DONE]') {
162 return strlen($chunk);
163 }
164​
165 $parsed = json_decode($data, true);
166 if (!is_array($parsed)) {
167 continue;
168 }
169​
170 $handleEvent($parsed);
171 }
172​
173 return strlen($chunk);
174 },
175 ]);
176​
177 $ok = curl_exec($ch);
178 if ($ok === false) {
179 $error = curl_error($ch);
180 curl_close($ch);
181 throw new RuntimeException($error !== '' ? $error : 'provider request failed');
182 }
183​
184 $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
185 curl_close($ch);
186​
187 if ($status >= 400) {
188 throw new RuntimeException('provider request failed (' . $status . ')');
189 }