From fee45f7b23832ce6b5e02650757d2b073a9a4b0d Mon Sep 17 00:00:00 2001 From: Hk-Gosuto Date: Fri, 9 Feb 2024 12:58:54 +0800 Subject: [PATCH] fix: gemini --- app/api/common.ts | 2 +- app/api/google/[...path]/route.ts | 4 ++-- app/client/platforms/google.ts | 39 +++++++++++-------------------- app/constant.ts | 2 -- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index d931edc19..190f87805 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -1,6 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; import { getServerSideConfig } from "../config/server"; -import { DEFAULT_MODELS, OPENAI_BASE_URL, GEMINI_BASE_URL } from "../constant"; +import { DEFAULT_MODELS, OPENAI_BASE_URL } from "../constant"; import { collectModelTable } from "../utils/model"; import { makeAzurePath } from "../azure"; diff --git a/app/api/google/[...path]/route.ts b/app/api/google/[...path]/route.ts index 9f9aa96fc..3132f7b91 100644 --- a/app/api/google/[...path]/route.ts +++ b/app/api/google/[...path]/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { auth } from "../../auth"; import { getServerSideConfig } from "@/app/config/server"; -import { GEMINI_BASE_URL, ModelProvider } from "@/app/constant"; +import { GOOGLE_BASE_URL, ModelProvider } from "@/app/constant"; async function handle( req: NextRequest, @@ -17,7 +17,7 @@ async function handle( const serverConfig = getServerSideConfig(); - let baseUrl = serverConfig.googleBaseUrl || GEMINI_BASE_URL; + let baseUrl = serverConfig.googleBaseUrl || GOOGLE_BASE_URL; if (!baseUrl.startsWith("http")) { baseUrl = `https://${baseUrl}`; diff --git a/app/client/platforms/google.ts b/app/client/platforms/google.ts index be58a35c8..064590bf8 100644 --- a/app/client/platforms/google.ts +++ b/app/client/platforms/google.ts @@ -13,8 +13,7 @@ import { LLMUsage, } from "../api"; import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; -import { getClientConfig } from "@/app/config/client"; -import { DEFAULT_API_HOST } from "@/app/constant"; + export class GeminiProApi implements LLMApi { toolAgentChat(options: AgentChatOptions): Promise { throw new Error("Method not implemented."); @@ -29,7 +28,7 @@ export class GeminiProApi implements LLMApi { ); } async chat(options: ChatOptions): Promise { - // const apiClient = this; + const apiClient = this; const messages = options.messages.map((v) => ({ role: v.role.replace("assistant", "model").replace("system", "user"), parts: [{ text: v.content }], @@ -87,27 +86,13 @@ export class GeminiProApi implements LLMApi { ], }; - const accessStore = useAccessStore.getState(); - let baseUrl = accessStore.googleBaseUrl; - const isApp = !!getClientConfig()?.isApp; + console.log("[Request] google payload: ", requestPayload); - let shouldStream = !!options.config.stream; + const shouldStream = !!options.config.stream; const controller = new AbortController(); options.onController?.(controller); try { - let chatPath = this.path(Google.ChatPath); - - // let baseUrl = accessStore.googleUrl; - - if (!baseUrl) { - baseUrl = isApp - ? DEFAULT_API_HOST + "/api/proxy/google/" + Google.ChatPath - : chatPath; - } - - if (isApp) { - baseUrl += `?key=${accessStore.googleApiKey}`; - } + const chatPath = this.path(Google.ChatPath); const chatPayload = { method: "POST", body: JSON.stringify(requestPayload), @@ -123,6 +108,10 @@ export class GeminiProApi implements LLMApi { if (shouldStream) { let responseText = ""; let remainText = ""; + let streamChatPath = chatPath.replace( + "generateContent", + "streamGenerateContent", + ); let finished = false; let existingTexts: string[] = []; @@ -152,11 +141,7 @@ export class GeminiProApi implements LLMApi { // start animaion animateResponseText(); - - fetch( - baseUrl.replace("generateContent", "streamGenerateContent"), - chatPayload, - ) + fetch(streamChatPath, chatPayload) .then((response) => { const reader = response?.body?.getReader(); const decoder = new TextDecoder(); @@ -207,9 +192,11 @@ export class GeminiProApi implements LLMApi { console.error("Error:", error); }); } else { - const res = await fetch(baseUrl, chatPayload); + const res = await fetch(chatPath, chatPayload); clearTimeout(requestTimeoutId); + const resJson = await res.json(); + if (resJson?.promptFeedback?.blockReason) { // being blocked options.onError?.( diff --git a/app/constant.ts b/app/constant.ts index 716e950f2..ad08a2ea2 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -12,8 +12,6 @@ export const DEFAULT_API_HOST = "https://api.nextchat.dev"; export const OPENAI_BASE_URL = "https://api.openai.com"; export const GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com"; -export const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/"; - export enum Path { Home = "/", Chat = "/chat",