also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,201 @@ | ||
| 1 | import type { AIProvider, ChatOptions, ChatResponse, EmbeddingResponse, Message, StreamChunk } from '../types' | |
| 2 | ||
| 3 | interface OpenAIConfig { | |
| 4 | apiKey: string | |
| 5 | model?: string | |
| 6 | baseUrl?: string | |
| 7 | } | |
| 8 | ||
| 9 | export function openaiProvider(config: OpenAIConfig): AIProvider { | |
| 10 | const defaultModel = config.model ?? 'gpt-4o' | |
| 11 | const baseUrl = config.baseUrl ?? 'https://api.openai.com/v1' | |
| 12 | ||
| 13 | return { | |
| 14 | name: 'openai', | |
| 15 | ||
| 16 | async chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse> { | |
| 17 | if (!config.apiKey) { | |
| 18 | throw new Error('OpenAI API key is required.') | |
| 19 | } | |
| 20 | ||
| 21 | const model = options?.model ?? defaultModel | |
| 22 | const allMessages = [...messages] | |
| 23 | ||
| 24 | if (options?.system) { | |
| 25 | allMessages.unshift({ role: 'system', content: options.system }) | |
| 26 | } | |
| 27 | ||
| 28 | const body: Record<string, unknown> = { | |
| 29 | model, | |
| 30 | messages: allMessages.map(m => ({ role: m.role, content: m.content })), | |
| 31 | max_tokens: options?.maxTokens ?? 1024, | |
| 32 | } | |
| 33 | ||
| 34 | if (options?.temperature !== undefined) { | |
| 35 | body.temperature = options.temperature | |
| 36 | } | |
| 37 | if (options?.topP !== undefined) { | |
| 38 | body.top_p = options.topP | |
| 39 | } | |
| 40 | if (options?.stop) { | |
| 41 | body.stop = options.stop | |
| 42 | } | |
| 43 | ||
| 44 | const response = await fetch(`${baseUrl}/chat/completions`, { | |
| 45 | method: 'POST', | |
| 46 | headers: { | |
| 47 | 'Content-Type': 'application/json', | |
| 48 | 'Authorization': `Bearer ${config.apiKey}`, | |
| 49 | }, | |
| 50 | body: JSON.stringify(body), | |
| 51 | }) | |
| 52 | ||
| 53 | if (!response.ok) { | |
| 54 | throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`) | |
| 55 | } | |
| 56 | ||
| 57 | const data = await response.json() as { | |
| 58 | choices: Array<{ message: { content: string }, finish_reason: string }> | |
| 59 | model: string | |
| 60 | usage: { prompt_tokens: number, completion_tokens: number, total_tokens: number } | |
| 61 | } | |
| 62 | ||
| 63 | const choice = data.choices[0] | |
| 64 | ||
| 65 | return { | |
| 66 | content: choice.message.content, | |
| 67 | model: data.model, | |
| 68 | usage: { | |
| 69 | inputTokens: data.usage.prompt_tokens, | |
| 70 | outputTokens: data.usage.completion_tokens, | |
| 71 | totalTokens: data.usage.total_tokens, | |
| 72 | }, | |
| 73 | finishReason: choice.finish_reason, | |
| 74 | } | |
| 75 | }, | |
| 76 | ||
| 77 | async *stream(messages: Message[], options?: ChatOptions): AsyncIterable<StreamChunk> { | |
| 78 | if (!config.apiKey) { | |
| 79 | throw new Error('OpenAI API key is required.') | |
| 80 | } | |
| 81 | ||
| 82 | const model = options?.model ?? defaultModel | |
| 83 | const allMessages = [...messages] | |
| 84 | ||
| 85 | if (options?.system) { | |
| 86 | allMessages.unshift({ role: 'system', content: options.system }) | |
| 87 | } | |
| 88 | ||
| 89 | const body: Record<string, unknown> = { | |
| 90 | model, | |
| 91 | messages: allMessages.map(m => ({ role: m.role, content: m.content })), | |
| 92 | max_tokens: options?.maxTokens ?? 1024, | |
| 93 | stream: true, | |
| 94 | } | |
| 95 | ||
| 96 | if (options?.temperature !== undefined) { | |
| 97 | body.temperature = options.temperature | |
| 98 | } | |
| 99 | if (options?.topP !== undefined) { | |
| 100 | body.top_p = options.topP | |
| 101 | } | |
| 102 | if (options?.stop) { | |
| 103 | body.stop = options.stop | |
| 104 | } | |
| 105 | ||
| 106 | const response = await fetch(`${baseUrl}/chat/completions`, { | |
| 107 | method: 'POST', | |
| 108 | headers: { | |
| 109 | 'Content-Type': 'application/json', | |
| 110 | 'Authorization': `Bearer ${config.apiKey}`, | |
| 111 | }, | |
| 112 | body: JSON.stringify(body), | |
| 113 | }) | |
| 114 | ||
| 115 | if (!response.ok) { | |
| 116 | throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`) | |
| 117 | } | |
| 118 | ||
| 119 | const reader = response.body?.getReader() | |
| 120 | if (!reader) { | |
| 121 | throw new Error('No response body') | |
| 122 | } | |
| 123 | ||
| 124 | const decoder = new TextDecoder() | |
| 125 | let buffer = '' | |
| 126 | ||
| 127 | while (true) { | |
| 128 | const { done, value } = await reader.read() | |
| 129 | if (done) | |
| 130 | break | |
| 131 | ||
| 132 | buffer += decoder.decode(value, { stream: true }) | |
| 133 | const lines = buffer.split('\n') | |
| 134 | buffer = lines.pop() ?? '' | |
| 135 | ||
| 136 | for (const line of lines) { | |
| 137 | if (!line.startsWith('data: ')) | |
| 138 | continue | |
| 139 | const data = line.slice(6).trim() | |
| 140 | if (data === '[DONE]') { | |
| 141 | yield { content: '', done: true, finishReason: 'stop' } | |
| 142 | return | |
| 143 | } | |
| 144 | ||
| 145 | try { | |
| 146 | const event = JSON.parse(data) as { | |
| 147 | choices: Array<{ delta: { content?: string }, finish_reason?: string }> | |
| 148 | } | |
| 149 | const choice = event.choices[0] | |
| 150 | if (choice.delta.content) { | |
| 151 | yield { content: choice.delta.content, done: false } | |
| 152 | } | |
| 153 | if (choice.finish_reason) { | |
| 154 | yield { content: '', done: true, finishReason: choice.finish_reason } | |
| 155 | return | |
| 156 | } | |
| 157 | } | |
| 158 | catch { | |
| 159 | // skip malformed JSON | |
| 160 | } | |
| 161 | } | |
| 162 | } | |
| 163 | }, | |
| 164 | ||
| 165 | async embed(text: string, model?: string): Promise<EmbeddingResponse> { | |
| 166 | if (!config.apiKey) { | |
| 167 | throw new Error('OpenAI API key is required.') | |
| 168 | } | |
| 169 | ||
| 170 | const embeddingModel = model ?? 'text-embedding-3-small' | |
| 171 | ||
| 172 | const response = await fetch(`${baseUrl}/embeddings`, { | |
| 173 | method: 'POST', | |
| 174 | headers: { | |
| 175 | 'Content-Type': 'application/json', | |
| 176 | 'Authorization': `Bearer ${config.apiKey}`, | |
| 177 | }, | |
| 178 | body: JSON.stringify({ | |
| 179 | model: embeddingModel, | |
| 180 | input: text, | |
| 181 | }), | |
| 182 | }) | |
| 183 | ||
| 184 | if (!response.ok) { | |
| 185 | throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`) | |
| 186 | } | |
| 187 | ||
| 188 | const data = await response.json() as { | |
| 189 | data: Array<{ embedding: number[] }> | |
| 190 | model: string | |
| 191 | usage: { total_tokens: number } | |
| 192 | } | |
| 193 | ||
| 194 | return { | |
| 195 | embedding: data.data[0].embedding, | |
| 196 | model: data.model, | |
| 197 | usage: { totalTokens: data.usage.total_tokens }, | |
| 198 | } | |
| 199 | }, | |
| 200 | } | |
| 201 | } | |