mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { OpenAI } from "openai";
|
|
import ChatSdk from "../lib/chat-sdk.ts";
|
|
import { StreamParams } from "../services/ChatService.ts";
|
|
import { BaseChatProvider, CommonProviderParams } from "./chat-stream-provider.ts";
|
|
import {ProviderRepository} from "./_ProviderRepository";
|
|
|
|
export class GoogleChatProvider extends BaseChatProvider {
|
|
getOpenAIClient(param: CommonProviderParams): OpenAI {
|
|
return new OpenAI({
|
|
baseURL: ProviderRepository.OPENAI_COMPAT_ENDPOINTS.google,
|
|
apiKey: param.env.GEMINI_API_KEY,
|
|
});
|
|
}
|
|
|
|
getStreamParams(param: CommonProviderParams, safeMessages: any[]): any {
|
|
return {
|
|
model: param.model,
|
|
messages: safeMessages,
|
|
stream: true,
|
|
};
|
|
}
|
|
|
|
async processChunk(chunk: any, dataCallback: (data: any) => void): Promise<boolean> {
|
|
if (chunk.choices?.[0]?.finish_reason === "stop") {
|
|
dataCallback({
|
|
type: "chat",
|
|
data: {
|
|
choices: [
|
|
{
|
|
delta: { content: chunk.choices[0].delta.content || "" },
|
|
finish_reason: "stop",
|
|
index: chunk.choices[0].index,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
return true;
|
|
} else {
|
|
dataCallback({
|
|
type: "chat",
|
|
data: {
|
|
choices: [
|
|
{
|
|
delta: { content: chunk.choices?.[0]?.delta?.content || "" },
|
|
finish_reason: null,
|
|
index: chunk.choices?.[0]?.index || 0,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
export class GoogleChatSdk {
|
|
private static provider = new GoogleChatProvider();
|
|
|
|
static async handleGoogleStream(
|
|
param: StreamParams,
|
|
dataCallback: (data) => void,
|
|
) {
|
|
return this.provider.handleStream(
|
|
{
|
|
systemPrompt: param.systemPrompt,
|
|
preprocessedContext: param.preprocessedContext,
|
|
maxTokens: param.maxTokens,
|
|
messages: param.messages,
|
|
model: param.model,
|
|
env: param.env,
|
|
},
|
|
dataCallback,
|
|
);
|
|
}
|
|
}
|