fix: fix gemini stream output

This commit is contained in:
Hk-Gosuto 2023-12-29 14:10:51 +08:00
parent 9e86aa0901
commit 78bc819742
4 changed files with 40 additions and 17 deletions

View File

@ -46,7 +46,7 @@ async function handle(
}); });
} }
const bearToken = req.headers.get("Authorization") ?? ""; const bearToken = req.headers.get("x-goog-api-key") ?? "";
const token = bearToken.trim().replaceAll("Bearer ", "").trim(); const token = bearToken.trim().replaceAll("Bearer ", "").trim();
const key = token ? token : serverConfig.googleApiKey; const key = token ? token : serverConfig.googleApiKey;
@ -63,7 +63,7 @@ async function handle(
); );
} }
const fetchUrl = `${baseUrl}/${path}?key=${key}&alt=sse`; const fetchUrl = `${baseUrl}/${path}?key=${key}`;
const fetchOptions: RequestInit = { const fetchOptions: RequestInit = {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",

View File

@ -174,20 +174,25 @@ export function getHeaders(ignoreHeaders?: boolean) {
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-requested-with": "XMLHttpRequest", "x-requested-with": "XMLHttpRequest",
"Accept": "application/json", Accept: "application/json",
}; };
} }
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig; const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
const isGoogle = modelConfig.model === "gemini-pro"; const isGoogle = modelConfig.model === "gemini-pro";
const isAzure = accessStore.provider === ServiceProvider.Azure; const isAzure = accessStore.provider === ServiceProvider.Azure;
const authHeader = isAzure ? "api-key" : "Authorization"; const authHeader = isGoogle
? "x-goog-api-key"
: isAzure
? "api-key"
: "Authorization";
const apiKey = isGoogle const apiKey = isGoogle
? accessStore.googleApiKey ? accessStore.googleApiKey
: isAzure : isAzure
? accessStore.azureApiKey ? accessStore.azureApiKey
: accessStore.openaiApiKey; : accessStore.openaiApiKey;
const makeBearer = (s: string) => `${isAzure ? "" : "Bearer "}${s.trim()}`; const makeBearer = (s: string) =>
`${isGoogle || isAzure ? "" : "Bearer "}${s.trim()}`;
const validString = (x: string) => x && x.length > 0; const validString = (x: string) => x && x.length > 0;
// use user's api key first // use user's api key first

View File

@ -1,4 +1,9 @@
import { Google, REQUEST_TIMEOUT_MS } from "@/app/constant"; import {
ApiPath,
Google,
REQUEST_TIMEOUT_MS,
ServiceProvider,
} from "@/app/constant";
import { import {
AgentChatOptions, AgentChatOptions,
ChatOptions, ChatOptions,
@ -8,14 +13,7 @@ import {
LLMUsage, LLMUsage,
} from "../api"; } from "../api";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
import {
EventStreamContentType,
fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client";
import Locale from "../../locales";
import { getServerSideConfig } from "@/app/config/server";
export class GeminiProApi implements LLMApi { export class GeminiProApi implements LLMApi {
toolAgentChat(options: AgentChatOptions): Promise<void> { toolAgentChat(options: AgentChatOptions): Promise<void> {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
@ -188,7 +186,28 @@ export class GeminiProApi implements LLMApi {
return []; return [];
} }
path(path: string): string { path(path: string): string {
return "/api/google/" + path; const accessStore = useAccessStore.getState();
const isGoogle = accessStore.provider === ServiceProvider.Google;
if (isGoogle && !accessStore.isValidGoogle()) {
throw Error(
"incomplete google config, please check it in your settings page",
);
}
let baseUrl = isGoogle ? accessStore.googleBaseUrl : ApiPath.GoogleAI;
if (baseUrl.length === 0) {
baseUrl = ApiPath.GoogleAI;
}
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
}
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.GoogleAI)) {
baseUrl = "https://" + baseUrl;
}
return [baseUrl, path].join("/");
} }
} }

View File

@ -93,8 +93,7 @@ export const Azure = {
}; };
export const Google = { export const Google = {
ExampleEndpoint: ExampleEndpoint: "https://generativelanguage.googleapis.com",
"https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent",
ChatPath: "v1/models/gemini-pro:generateContent", ChatPath: "v1/models/gemini-pro:generateContent",
// /api/openai/v1/chat/completions // /api/openai/v1/chat/completions