fix: support custom api endpoint (#4016)

This commit is contained in:
fred-bf 2024-02-07 13:40:30 +08:00 committed by GitHub
parent 0869455612
commit b8f0822214
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -72,23 +72,26 @@ export class GeminiProApi implements LLMApi {
], ],
}; };
const accessStore = useAccessStore.getState();
let baseUrl = accessStore.googleUrl;
const isApp = !!getClientConfig()?.isApp; const isApp = !!getClientConfig()?.isApp;
const shouldStream = !!options.config.stream; let shouldStream = !!options.config.stream;
const controller = new AbortController(); const controller = new AbortController();
options.onController?.(controller); options.onController?.(controller);
const accessStore = useAccessStore.getState();
try { try {
let chatPath = this.path(Google.ChatPath); let chatPath = this.path(Google.ChatPath);
// let baseUrl = accessStore.googleUrl; // let baseUrl = accessStore.googleUrl;
chatPath = isApp if (!baseUrl) {
? DEFAULT_API_HOST + baseUrl = isApp
"/api/proxy/google/" + ? DEFAULT_API_HOST +
Google.ChatPath + "/api/proxy/google/" +
`?key=${accessStore.googleApiKey}` Google.ChatPath +
: chatPath; `?key=${accessStore.googleApiKey}`
: chatPath;
}
const chatPayload = { const chatPayload = {
method: "POST", method: "POST",
@ -96,7 +99,7 @@ export class GeminiProApi implements LLMApi {
signal: controller.signal, signal: controller.signal,
headers: getHeaders(), headers: getHeaders(),
}; };
console.log("[Request] google chatPath: ", chatPath, isApp);
// make a fetch request // make a fetch request
const requestTimeoutId = setTimeout( const requestTimeoutId = setTimeout(
() => controller.abort(), () => controller.abort(),
@ -105,10 +108,6 @@ export class GeminiProApi implements LLMApi {
if (shouldStream) { if (shouldStream) {
let responseText = ""; let responseText = "";
let remainText = ""; let remainText = "";
let streamChatPath = chatPath.replace(
"generateContent",
"streamGenerateContent",
);
let finished = false; let finished = false;
let existingTexts: string[] = []; let existingTexts: string[] = [];
@ -139,8 +138,10 @@ export class GeminiProApi implements LLMApi {
// start animaion // start animaion
animateResponseText(); animateResponseText();
console.log("[Proxy Endpoint] ", streamChatPath); fetch(
fetch(streamChatPath, chatPayload) baseUrl.replace("generateContent", "streamGenerateContent"),
chatPayload,
)
.then((response) => { .then((response) => {
const reader = response?.body?.getReader(); const reader = response?.body?.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
@ -191,7 +192,7 @@ export class GeminiProApi implements LLMApi {
console.error("Error:", error); console.error("Error:", error);
}); });
} else { } else {
const res = await fetch(chatPath, chatPayload); const res = await fetch(baseUrl, chatPayload);
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);
const resJson = await res.json(); const resJson = await res.json();
if (resJson?.promptFeedback?.blockReason) { if (resJson?.promptFeedback?.blockReason) {