Skip to content

Commit

Permalink
エラーハンドリング追加
Browse files Browse the repository at this point in the history
  • Loading branch information
tegnike committed Aug 27, 2024
1 parent fac08d3 commit 5b1f101
Showing 1 changed file with 67 additions and 53 deletions.
120 changes: 67 additions & 53 deletions src/features/chat/openAiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,91 @@ 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(
messages: Message[],
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
}
}

0 comments on commit 5b1f101

Please sign in to comment.