feat: support gemini pro

This commit is contained in:
Hk-Gosuto
2023-12-20 19:36:59 +08:00
parent 4044891f85
commit ee08ab4070
14 changed files with 817 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ import { getClientConfig } from "../config/client";
import { ACCESS_CODE_PREFIX, Azure, ServiceProvider } from "../constant";
import { ChatMessage, ModelType, useAccessStore } from "../store";
import { ChatGPTApi } from "./platforms/openai";
import { GeminiApi } from "./platforms/google";
import { FileApi } from "./platforms/utils";
export const ROLES = ["system", "user", "assistant"] as const;
@@ -105,6 +106,11 @@ export class ClientApi {
this.file = new FileApi();
}
switch(model: string) {
if (model.startsWith("gemini")) this.llm = new GeminiApi();
else this.llm = new ChatGPTApi();
}
config() {}
prompts() {}
@@ -207,3 +213,31 @@ export function getHeaders() {
return headers;
}
export function getGeminiHeaders() {
const accessStore = useAccessStore.getState();
const headers: Record<string, string> = {
"Content-Type": "application/json",
"x-requested-with": "XMLHttpRequest",
};
const authHeader = "Authorization";
const apiKey = accessStore.googleApiKey;
const makeBearer = (s: string) => `${"Bearer "}${s.trim()}`;
const validString = (x: string) => x && x.length > 0;
// use user's api key first
if (validString(apiKey)) {
headers[authHeader] = makeBearer(apiKey);
} else if (
accessStore.enabledAccessControl() &&
validString(accessStore.accessCode)
) {
headers[authHeader] = makeBearer(
ACCESS_CODE_PREFIX + accessStore.accessCode,
);
}
return headers;
}