|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function convertResponseToChatCompletion(resp) {
|
|
|
if (!resp || typeof resp !== 'object') {
|
|
|
throw new Error('Invalid response object')
|
|
|
}
|
|
|
|
|
|
const outputMsg = (resp.output || []).find(o => o.type === 'message')
|
|
|
const textBlocks = outputMsg?.content?.filter(c => c.type === 'output_text') || []
|
|
|
const content = textBlocks.map(c => c.text).join('')
|
|
|
|
|
|
const chatCompletion = {
|
|
|
id: resp.id ? resp.id.replace(/^resp_/, 'chatcmpl-') : `chatcmpl-${Date.now()}`,
|
|
|
object: 'chat.completion',
|
|
|
created: resp.created_at || Math.floor(Date.now() / 1000),
|
|
|
model: resp.model || 'unknown-model',
|
|
|
choices: [
|
|
|
{
|
|
|
index: 0,
|
|
|
message: {
|
|
|
role: outputMsg?.role || 'assistant',
|
|
|
content: content || ''
|
|
|
},
|
|
|
finish_reason: resp.status === 'completed' ? 'stop' : 'unknown'
|
|
|
}
|
|
|
],
|
|
|
usage: {
|
|
|
prompt_tokens: resp.usage?.input_tokens ?? 0,
|
|
|
completion_tokens: resp.usage?.output_tokens ?? 0,
|
|
|
total_tokens: resp.usage?.total_tokens ?? 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return chatCompletion
|
|
|
}
|
|
|
|
|
|
|