ai问片支持非流式响应
This commit is contained in:
@@ -61,6 +61,7 @@ export async function POST(request: NextRequest) {
|
||||
Temperature,
|
||||
MaxTokens,
|
||||
SystemPrompt,
|
||||
EnableStreaming,
|
||||
DefaultMessageNoVideo,
|
||||
DefaultMessageWithVideo,
|
||||
} = body as {
|
||||
@@ -96,6 +97,7 @@ export async function POST(request: NextRequest) {
|
||||
Temperature?: number;
|
||||
MaxTokens?: number;
|
||||
SystemPrompt?: string;
|
||||
EnableStreaming?: boolean;
|
||||
DefaultMessageNoVideo?: string;
|
||||
DefaultMessageWithVideo?: string;
|
||||
};
|
||||
@@ -133,7 +135,8 @@ export async function POST(request: NextRequest) {
|
||||
typeof AllowRegularUsers !== 'boolean' ||
|
||||
(Temperature !== undefined && typeof Temperature !== 'number') ||
|
||||
(MaxTokens !== undefined && typeof MaxTokens !== 'number') ||
|
||||
(SystemPrompt !== undefined && typeof SystemPrompt !== 'string')
|
||||
(SystemPrompt !== undefined && typeof SystemPrompt !== 'string') ||
|
||||
(EnableStreaming !== undefined && typeof EnableStreaming !== 'boolean')
|
||||
) {
|
||||
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
|
||||
}
|
||||
@@ -182,6 +185,7 @@ export async function POST(request: NextRequest) {
|
||||
Temperature,
|
||||
MaxTokens,
|
||||
SystemPrompt,
|
||||
EnableStreaming,
|
||||
DefaultMessageNoVideo,
|
||||
DefaultMessageWithVideo,
|
||||
};
|
||||
|
||||
@@ -34,8 +34,9 @@ async function streamOpenAIChat(
|
||||
model: string;
|
||||
temperature: number;
|
||||
maxTokens: number;
|
||||
}
|
||||
): Promise<ReadableStream> {
|
||||
},
|
||||
enableStreaming: boolean = true
|
||||
): Promise<ReadableStream | Response> {
|
||||
const response = await fetch(`${config.baseURL}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -47,7 +48,7 @@ async function streamOpenAIChat(
|
||||
messages,
|
||||
temperature: config.temperature,
|
||||
max_tokens: config.maxTokens,
|
||||
stream: true,
|
||||
stream: enableStreaming,
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -57,7 +58,7 @@ async function streamOpenAIChat(
|
||||
);
|
||||
}
|
||||
|
||||
return response.body!;
|
||||
return enableStreaming ? response.body! : response;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,6 +266,7 @@ export async function POST(request: NextRequest) {
|
||||
// 6. 调用自定义API
|
||||
const temperature = aiConfig.Temperature ?? 0.7;
|
||||
const maxTokens = aiConfig.MaxTokens ?? 1000;
|
||||
const enableStreaming = aiConfig.EnableStreaming !== false; // 默认启用流式响应
|
||||
|
||||
if (!aiConfig.CustomApiKey || !aiConfig.CustomBaseURL) {
|
||||
return NextResponse.json(
|
||||
@@ -273,24 +275,34 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
const stream = await streamOpenAIChat(messages, {
|
||||
const result = await streamOpenAIChat(messages, {
|
||||
apiKey: aiConfig.CustomApiKey,
|
||||
baseURL: aiConfig.CustomBaseURL,
|
||||
model: aiConfig.CustomModel || 'gpt-3.5-turbo',
|
||||
temperature,
|
||||
maxTokens,
|
||||
});
|
||||
}, enableStreaming);
|
||||
|
||||
// 7. 转换为SSE格式并返回
|
||||
const sseStream = transformToSSE(stream, 'openai');
|
||||
// 7. 根据是否启用流式响应返回不同格式
|
||||
if (enableStreaming) {
|
||||
// 流式响应:转换为SSE格式并返回
|
||||
const sseStream = transformToSSE(result as ReadableStream, 'openai');
|
||||
|
||||
return new NextResponse(sseStream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
});
|
||||
return new NextResponse(sseStream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// 非流式响应:等待完整响应后返回JSON
|
||||
const response = result as Response;
|
||||
const data = await response.json();
|
||||
const content = data.choices?.[0]?.message?.content || '';
|
||||
|
||||
return NextResponse.json({ content });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ AI聊天API错误:', error);
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in New Issue
Block a user