fix: gemini

This commit is contained in:
Hk-Gosuto 2024-02-09 12:58:54 +08:00
parent 0ed7402494
commit fee45f7b23
4 changed files with 16 additions and 31 deletions

View File

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { getServerSideConfig } from "../config/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 { collectModelTable } from "../utils/model";
import { makeAzurePath } from "../azure"; import { makeAzurePath } from "../azure";

View File

@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth"; import { auth } from "../../auth";
import { getServerSideConfig } from "@/app/config/server"; 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( async function handle(
req: NextRequest, req: NextRequest,
@ -17,7 +17,7 @@ async function handle(
const serverConfig = getServerSideConfig(); const serverConfig = getServerSideConfig();
let baseUrl = serverConfig.googleBaseUrl || GEMINI_BASE_URL; let baseUrl = serverConfig.googleBaseUrl || GOOGLE_BASE_URL;
if (!baseUrl.startsWith("http")) { if (!baseUrl.startsWith("http")) {
baseUrl = `https://${baseUrl}`; baseUrl = `https://${baseUrl}`;

View File

@ -13,8 +13,7 @@ import {
LLMUsage, LLMUsage,
} from "../api"; } from "../api";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; 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 { 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.");
@ -29,7 +28,7 @@ export class GeminiProApi implements LLMApi {
); );
} }
async chat(options: ChatOptions): Promise<void> { async chat(options: ChatOptions): Promise<void> {
// const apiClient = this; const apiClient = this;
const messages = options.messages.map((v) => ({ const messages = options.messages.map((v) => ({
role: v.role.replace("assistant", "model").replace("system", "user"), role: v.role.replace("assistant", "model").replace("system", "user"),
parts: [{ text: v.content }], parts: [{ text: v.content }],
@ -87,27 +86,13 @@ export class GeminiProApi implements LLMApi {
], ],
}; };
const accessStore = useAccessStore.getState(); console.log("[Request] google payload: ", requestPayload);
let baseUrl = accessStore.googleBaseUrl;
const isApp = !!getClientConfig()?.isApp;
let shouldStream = !!options.config.stream; const shouldStream = !!options.config.stream;
const controller = new AbortController(); const controller = new AbortController();
options.onController?.(controller); options.onController?.(controller);
try { try {
let chatPath = this.path(Google.ChatPath); const 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 chatPayload = { const chatPayload = {
method: "POST", method: "POST",
body: JSON.stringify(requestPayload), body: JSON.stringify(requestPayload),
@ -123,6 +108,10 @@ 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[] = [];
@ -152,11 +141,7 @@ export class GeminiProApi implements LLMApi {
// start animaion // start animaion
animateResponseText(); animateResponseText();
fetch(streamChatPath, chatPayload)
fetch(
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();
@ -207,9 +192,11 @@ export class GeminiProApi implements LLMApi {
console.error("Error:", error); console.error("Error:", error);
}); });
} 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) {
// being blocked // being blocked
options.onError?.( options.onError?.(

View File

@ -12,8 +12,6 @@ export const DEFAULT_API_HOST = "https://api.nextchat.dev";
export const OPENAI_BASE_URL = "https://api.openai.com"; export const OPENAI_BASE_URL = "https://api.openai.com";
export const GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com"; export const GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com";
export const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/";
export enum Path { export enum Path {
Home = "/", Home = "/",
Chat = "/chat", Chat = "/chat",