|
@@ -1,4 +1,4 @@
|
|
|
-import { Google, REQUEST_TIMEOUT_MS } from "@/app/constant";
|
|
|
|
|
|
|
+import { ApiPath, Google, REQUEST_TIMEOUT_MS } from "@/app/constant";
|
|
|
import { ChatOptions, getHeaders, LLMApi, LLMModel, LLMUsage } from "../api";
|
|
import { ChatOptions, getHeaders, LLMApi, LLMModel, LLMUsage } from "../api";
|
|
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
|
|
import { getClientConfig } from "@/app/config/client";
|
|
import { getClientConfig } from "@/app/config/client";
|
|
@@ -16,6 +16,34 @@ import {
|
|
|
} from "@/app/utils";
|
|
} from "@/app/utils";
|
|
|
|
|
|
|
|
export class GeminiProApi implements LLMApi {
|
|
export class GeminiProApi implements LLMApi {
|
|
|
|
|
+ path(path: string): string {
|
|
|
|
|
+ const accessStore = useAccessStore.getState();
|
|
|
|
|
+
|
|
|
|
|
+ let baseUrl = "";
|
|
|
|
|
+ if (accessStore.useCustomConfig) {
|
|
|
|
|
+ baseUrl = accessStore.googleUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (baseUrl.length === 0) {
|
|
|
|
|
+ const isApp = !!getClientConfig()?.isApp;
|
|
|
|
|
+ baseUrl = isApp
|
|
|
|
|
+ ? DEFAULT_API_HOST + `/api/proxy/google?key=${accessStore.googleApiKey}`
|
|
|
|
|
+ : ApiPath.Google;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (baseUrl.endsWith("/")) {
|
|
|
|
|
+ baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Google)) {
|
|
|
|
|
+ baseUrl = "https://" + baseUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ console.log("[Proxy Endpoint] ", baseUrl, path);
|
|
|
|
|
+
|
|
|
|
|
+ let chatPath = [baseUrl, path].join("/");
|
|
|
|
|
+
|
|
|
|
|
+ chatPath += chatPath.includes("?") ? "&alt=sse" : "?alt=sse";
|
|
|
|
|
+ return chatPath;
|
|
|
|
|
+ }
|
|
|
extractMessage(res: any) {
|
|
extractMessage(res: any) {
|
|
|
console.log("[Response] gemini-pro response: ", res);
|
|
console.log("[Response] gemini-pro response: ", res);
|
|
|
|
|
|
|
@@ -108,30 +136,13 @@ export class GeminiProApi implements LLMApi {
|
|
|
],
|
|
],
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const accessStore = useAccessStore.getState();
|
|
|
|
|
-
|
|
|
|
|
- let baseUrl = "";
|
|
|
|
|
-
|
|
|
|
|
- if (accessStore.useCustomConfig) {
|
|
|
|
|
- baseUrl = accessStore.googleUrl;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const isApp = !!getClientConfig()?.isApp;
|
|
|
|
|
-
|
|
|
|
|
let shouldStream = !!options.config.stream;
|
|
let shouldStream = !!options.config.stream;
|
|
|
const controller = new AbortController();
|
|
const controller = new AbortController();
|
|
|
options.onController?.(controller);
|
|
options.onController?.(controller);
|
|
|
try {
|
|
try {
|
|
|
- if (!baseUrl && isApp) {
|
|
|
|
|
- baseUrl = DEFAULT_API_HOST + "/api/proxy/google/";
|
|
|
|
|
- }
|
|
|
|
|
- baseUrl = `${baseUrl}/${Google.ChatPath(modelConfig.model)}`.replaceAll(
|
|
|
|
|
- "//",
|
|
|
|
|
- "/",
|
|
|
|
|
- );
|
|
|
|
|
- if (isApp) {
|
|
|
|
|
- baseUrl += `?key=${accessStore.googleApiKey}`;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/Streaming_REST.ipynb
|
|
|
|
|
+ const chatPath = this.path(Google.ChatPath(modelConfig.model));
|
|
|
|
|
+
|
|
|
const chatPayload = {
|
|
const chatPayload = {
|
|
|
method: "POST",
|
|
method: "POST",
|
|
|
body: JSON.stringify(requestPayload),
|
|
body: JSON.stringify(requestPayload),
|
|
@@ -181,10 +192,6 @@ export class GeminiProApi implements LLMApi {
|
|
|
|
|
|
|
|
controller.signal.onabort = finish;
|
|
controller.signal.onabort = finish;
|
|
|
|
|
|
|
|
- // https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/Streaming_REST.ipynb
|
|
|
|
|
- const chatPath =
|
|
|
|
|
- baseUrl.replace("generateContent", "streamGenerateContent") +
|
|
|
|
|
- (baseUrl.indexOf("?") > -1 ? "&alt=sse" : "?alt=sse");
|
|
|
|
|
fetchEventSource(chatPath, {
|
|
fetchEventSource(chatPath, {
|
|
|
...chatPayload,
|
|
...chatPayload,
|
|
|
async onopen(res) {
|
|
async onopen(res) {
|
|
@@ -259,7 +266,7 @@ export class GeminiProApi implements LLMApi {
|
|
|
openWhenHidden: true,
|
|
openWhenHidden: true,
|
|
|
});
|
|
});
|
|
|
} else {
|
|
} else {
|
|
|
- const res = await fetch(baseUrl, chatPayload);
|
|
|
|
|
|
|
+ const res = await fetch(chatPath, chatPayload);
|
|
|
clearTimeout(requestTimeoutId);
|
|
clearTimeout(requestTimeoutId);
|
|
|
const resJson = await res.json();
|
|
const resJson = await res.json();
|
|
|
if (resJson?.promptFeedback?.blockReason) {
|
|
if (resJson?.promptFeedback?.blockReason) {
|
|
@@ -285,14 +292,4 @@ export class GeminiProApi implements LLMApi {
|
|
|
async models(): Promise<LLMModel[]> {
|
|
async models(): Promise<LLMModel[]> {
|
|
|
return [];
|
|
return [];
|
|
|
}
|
|
}
|
|
|
- path(path: string): string {
|
|
|
|
|
- return "/api/google/" + path;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function ensureProperEnding(str: string) {
|
|
|
|
|
- if (str.startsWith("[") && !str.endsWith("]")) {
|
|
|
|
|
- return str + "]";
|
|
|
|
|
- }
|
|
|
|
|
- return str;
|
|
|
|
|
}
|
|
}
|