useChatuseChat/api/chatuseChatuseChathttps://www.php.cn/link/a4fe3e4de8ad631acb028b271b1fc271Authorization: Bearer api-keymodeldeepseek-chatdeepseek-r1gpt-4response_formattool_choice400 Bad Requestdata:choices[0].message.contentapp/api/chat/route.tsisLoadinguseChatapp/api/chat/route.tschatfetchhttps://www.php.cn/link/a4fe3e4de8ad631acb028b271b1fc271req.json()messagesmodeltemperatureresponse_format{"Content-Type": "application/json", "Authorization": Bearer ${process.env.DEEPSEEK_API_KEY}choices[0].message.content{ id, object, created, model, choices: [...] }
export async function POST(req: Request) {
const { messages, model = 'deepseek-chat', temperature = 0.7 } = await req.json();
const response = await fetch('https://www.php.cn/link/a4fe3e4de8ad631acb028b271b1fc271', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': Bearer ${process.env.DEEPSEEK_API_KEY},
},
body: JSON.stringify({
messages,
model,
temperature,
// 不要加 stream: true —— useChat 自己处理流式,后端统一走非流式即可
}),
});
const data = await response.json();
稿定在线PS
PS软件网页版
下载
return Response.json({
id: data.id || Date.now().toString(),
object: 'chat.completion',
created: data.created || Math.floor(Date.now() / 1000),
model: data.model || model,
choices: [{
index: 0,
message: { role: 'assistant', content: data.choices?.[0]?.message?.content || '' },
finish_reason: data.choices?.[0]?.finish_reason || 'stop',
}],
});
}
stream: trueuseChattext/event-streamuseChatapp/chat/page.tsx'use client'inputmessagesuseChatuseStatehandleSubmithandleSubmituseChatfetchisLoadingstatus === 'pending'messagerole'user''assistant'role
'use client';
import { useChat } from '@ai-sdk/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
return (
{messages.map(m => (
{m.role}: {m.content}
))}
.env.localDEEPSEEK_API_KEYroute.tsnpm run devfetchroute.tsundefined