From f466cf4b17766370e7f00d1654e70d011d0b1064 Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 7 Feb 2024 13:35:27 +0800 Subject: [PATCH 1/2] fix: support custom api endpoint --- app/client/platforms/google.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/app/client/platforms/google.ts b/app/client/platforms/google.ts index 6e335e7fd2f..aeb91ba7c02 100644 --- a/app/client/platforms/google.ts +++ b/app/client/platforms/google.ts @@ -72,23 +72,26 @@ export class GeminiProApi implements LLMApi { ], }; + const accessStore = useAccessStore.getState(); + let baseUrl = accessStore.googleUrl; const isApp = !!getClientConfig()?.isApp; - const shouldStream = !!options.config.stream; + let shouldStream = !!options.config.stream; const controller = new AbortController(); options.onController?.(controller); - const accessStore = useAccessStore.getState(); try { let chatPath = this.path(Google.ChatPath); // let baseUrl = accessStore.googleUrl; - chatPath = isApp - ? DEFAULT_API_HOST + - "/api/proxy/google/" + - Google.ChatPath + - `?key=${accessStore.googleApiKey}` - : chatPath; + if (!baseUrl) { + baseUrl = isApp + ? DEFAULT_API_HOST + + "/api/proxy/google/" + + Google.ChatPath + + `?key=${accessStore.googleApiKey}` + : chatPath; + } const chatPayload = { method: "POST", @@ -96,7 +99,7 @@ export class GeminiProApi implements LLMApi { signal: controller.signal, headers: getHeaders(), }; - console.log("[Request] google chatPath: ", chatPath, isApp); + // make a fetch request const requestTimeoutId = setTimeout( () => controller.abort(), @@ -105,10 +108,6 @@ export class GeminiProApi implements LLMApi { if (shouldStream) { let responseText = ""; let remainText = ""; - let streamChatPath = chatPath.replace( - "generateContent", - "streamGenerateContent", - ); let finished = false; let existingTexts: string[] = []; @@ -139,8 +138,10 @@ export class GeminiProApi implements LLMApi { // start animaion animateResponseText(); - console.log("[Proxy Endpoint] ", streamChatPath); - fetch(streamChatPath, chatPayload) + fetch( + baseUrl.replace("generateContent", "streamGenerateContent"), + chatPayload, + ) .then((response) => { const reader = response?.body?.getReader(); const decoder = new TextDecoder(); @@ -191,7 +192,7 @@ export class GeminiProApi implements LLMApi { console.error("Error:", error); }); } else { - const res = await fetch(chatPath, chatPayload); + const res = await fetch(baseUrl, chatPayload); clearTimeout(requestTimeoutId); const resJson = await res.json(); if (resJson?.promptFeedback?.blockReason) { From a10d6f58d384817f0efa23a0151620cccb7623d0 Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 7 Feb 2024 13:45:07 +0800 Subject: [PATCH 2/2] fix: attach api key to google gemini --- app/client/platforms/google.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/client/platforms/google.ts b/app/client/platforms/google.ts index aeb91ba7c02..6832400ca58 100644 --- a/app/client/platforms/google.ts +++ b/app/client/platforms/google.ts @@ -86,13 +86,13 @@ export class GeminiProApi implements LLMApi { if (!baseUrl) { baseUrl = isApp - ? DEFAULT_API_HOST + - "/api/proxy/google/" + - Google.ChatPath + - `?key=${accessStore.googleApiKey}` + ? DEFAULT_API_HOST + "/api/proxy/google/" + Google.ChatPath : chatPath; } + if (isApp) { + baseUrl += `?key=${accessStore.googleApiKey}`; + } const chatPayload = { method: "POST", body: JSON.stringify(requestPayload),