From 5b1f1018be2a0fbeaa1f2c8fd73effa2a1a72a0e Mon Sep 17 00:00:00 2001 From: tegnike Date: Tue, 27 Aug 2024 21:33:53 +0200 Subject: [PATCH] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/features/chat/openAiChat.ts | 120 ++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 53 deletions(-) diff --git a/src/features/chat/openAiChat.ts b/src/features/chat/openAiChat.ts index 56cbe269..51cc9c4b 100644 --- a/src/features/chat/openAiChat.ts +++ b/src/features/chat/openAiChat.ts @@ -5,16 +5,25 @@ export async function getOpenAIChatResponse( apiKey: string, model: string ) { - const response = await fetch('/api/openai', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ messages, apiKey, model }), - }) + try { + const response = await fetch('/api/openai', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ messages, apiKey, model }), + }) - const data = await response.json() - return { message: data.message } + if (!response.ok) { + throw new Error('Failed to fetch OpenAI API response') + } + + const data = await response.json() + return { message: data.message } + } catch (error) { + console.error('Error fetching OpenAI API response:', error) + throw error + } } export async function getOpenAIChatResponseStream( @@ -22,60 +31,65 @@ export async function getOpenAIChatResponseStream( apiKey: string, model: string ) { - const response = await fetch('/api/openai', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ messages, apiKey, model, stream: true }), - }) + try { + const response = await fetch('/api/openai', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ messages, apiKey, model, stream: true }), + }) - if (!response.ok) { - throw new Error('OpenAI APIリクエストに失敗しました') - } + if (!response.ok) { + throw new Error('OpenAI APIリクエストに失敗しました') + } - if (!response.body) { - throw new Error('OpenAI APIレスポンスが空です') - } + if (!response.body) { + throw new Error('OpenAI APIレスポンスが空です') + } - const reader = response.body.getReader() - const decoder = new TextDecoder('utf-8') + const reader = response.body.getReader() + const decoder = new TextDecoder('utf-8') - return new ReadableStream({ - async start(controller) { - while (true) { - const { done, value } = await reader.read() + return new ReadableStream({ + async start(controller) { + while (true) { + const { done, value } = await reader.read() - if (done) { - break - } + if (done) { + break + } - const chunk = decoder.decode(value) - const lines = chunk.split('\n') + const chunk = decoder.decode(value) + const lines = chunk.split('\n') - for (const line of lines) { - if (line.startsWith('data:')) { - const data = line.substring(5).trim() - if (data !== '[DONE]') { - const event = JSON.parse(data) - switch (event.type) { - case 'content_block_delta': - controller.enqueue(event.text) - break - case 'error': - throw new Error( - `OpenAI API error: ${JSON.stringify(event.error)}` - ) - case 'message_stop': - controller.close() - return + for (const line of lines) { + if (line.startsWith('data:')) { + const data = line.substring(5).trim() + if (data !== '[DONE]') { + const event = JSON.parse(data) + switch (event.type) { + case 'content_block_delta': + controller.enqueue(event.text) + break + case 'error': + throw new Error( + `OpenAI API error: ${JSON.stringify(event.error)}` + ) + case 'message_stop': + controller.close() + return + } } } } } - } - controller.close() - }, - }) + controller.close() + }, + }) + } catch (error) { + console.error('Error fetching OpenAI API response stream:', error) + throw error + } }