Files
20260512-skg-tk/web/canvas-app/src/api/chat.js
2026-05-26 10:08:22 +08:00

41 lines
1.0 KiB
JavaScript

/**
* Chat API | 对话 API
*/
import { request } from '@/utils'
// 对话补全
export const chatCompletions = (data) =>
request({
url: `/chat/completions`,
method: 'post',
data
})
// 流式对话补全
export const streamChatCompletions = async function* (data, signal, options = {}) {
const text = data?.messages?.at?.(-1)?.content || data?.goal || ''
const systemPrompt = data?.messages?.find?.((message) => message?.role === 'system')?.content || ''
const response = await fetch('/api/prompt/polish', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: typeof text === 'string' ? text : JSON.stringify(text),
system_prompt: systemPrompt,
mode: 'chat',
target_language: 'keep'
}),
signal
})
if (!response.ok) {
const error = await response.json().catch(() => ({}))
throw new Error(error?.detail || error?.message || '提示词助手请求失败')
}
const json = await response.json()
yield json.text || ''
}