feat: 重构对话功能
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import { BaseProvider } from "./BaseProvider";
|
||||
import { BaseProvider, ChatOptions, GatewayChatMessage } from "./BaseProvider";
|
||||
|
||||
import OpenAI from "openai";
|
||||
import logManager from "@electron/service/logger"
|
||||
|
||||
|
||||
function _transformChunk(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): UniversalChunk {
|
||||
const choice = chunk.choices[0];
|
||||
return {
|
||||
isEnd: choice?.finish_reason === 'stop',
|
||||
isEnd: choice?.finish_reason != null,
|
||||
result: choice?.delta?.content ?? '',
|
||||
}
|
||||
}
|
||||
@@ -15,12 +14,12 @@ function _transformChunk(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): Un
|
||||
export class OpenAIProvider extends BaseProvider {
|
||||
private client: OpenAI;
|
||||
|
||||
constructor(apiKey: string, baseURL: string) {
|
||||
constructor(apiKey: string, baseURL: string, headers?: Record<string, string>) {
|
||||
super();
|
||||
this.client = new OpenAI({ apiKey, baseURL });
|
||||
this.client = new OpenAI({ apiKey, baseURL, defaultHeaders: headers });
|
||||
}
|
||||
|
||||
async chat(messages: DialogueMessageProps[], model: string): Promise<AsyncIterable<UniversalChunk>> {
|
||||
async chat(messages: GatewayChatMessage[], model: string, options?: ChatOptions): Promise<AsyncIterable<UniversalChunk>> {
|
||||
const startTime = Date.now();
|
||||
|
||||
const lastMessage = messages[messages.length - 1];
|
||||
@@ -34,17 +33,25 @@ export class OpenAIProvider extends BaseProvider {
|
||||
try {
|
||||
const chunks = await this.client.chat.completions.create({
|
||||
model,
|
||||
messages,
|
||||
messages: messages as any,
|
||||
stream: true,
|
||||
}, {
|
||||
signal: options?.signal,
|
||||
});
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
logManager.logApiResponse('chat.completions.create', { success: true }, 200, responseTime);
|
||||
// return chunk;
|
||||
return {
|
||||
async *[Symbol.asyncIterator]() {
|
||||
for await (const chunk of chunks) {
|
||||
yield _transformChunk(chunk);
|
||||
try {
|
||||
for await (const chunk of chunks) {
|
||||
if (options?.signal?.aborted) break;
|
||||
yield _transformChunk(chunk);
|
||||
}
|
||||
const responseTime = Date.now() - startTime;
|
||||
logManager.logApiResponse('chat.completions.create', { success: true }, 200, responseTime);
|
||||
} catch (error) {
|
||||
const responseTime = Date.now() - startTime;
|
||||
logManager.logApiResponse('chat.completions.create', { error: error instanceof Error ? error.message : String(error) }, 500, responseTime);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user