From 3628d68d9a7eaf6fa9e9f210f382cc88b6769bea Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 9 Jul 2024 19:56:52 +0800 Subject: [PATCH] update --- app/api/alibaba/[...path]/route.ts | 7 +------ app/client/api.ts | 4 ++++ app/client/platforms/alibaba.ts | 13 ++++--------- app/constant.ts | 6 +++--- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/app/api/alibaba/[...path]/route.ts b/app/api/alibaba/[...path]/route.ts index e30eacbdb..b2c42ac78 100644 --- a/app/api/alibaba/[...path]/route.ts +++ b/app/api/alibaba/[...path]/route.ts @@ -68,10 +68,7 @@ async function request(req: NextRequest) { const controller = new AbortController(); // alibaba use base url or just remove the path - let path = `${req.nextUrl.pathname}`.replaceAll( - ApiPath.Alibaba + "/" + Alibaba.ChatPath, - "", - ); + let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Alibaba, ""); let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL; @@ -153,11 +150,9 @@ async function request(req: NextRequest) { console.error(`[Alibaba] filter`, e); } } - console.log("[Alibaba request]", fetchOptions.headers, req.method); try { const res = await fetch(fetchUrl, fetchOptions); - console.log("[Alibaba response]", res.status, " ", res.headers, res.url); // to prevent browser prompt for credentials const newHeaders = new Headers(res.headers); newHeaders.delete("www-authenticate"); diff --git a/app/client/api.ts b/app/client/api.ts index 6f6ff6222..c0c71480c 100644 --- a/app/client/api.ts +++ b/app/client/api.ts @@ -185,6 +185,7 @@ export function getHeaders() { const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic; const isBaidu = modelConfig.providerName == ServiceProvider.Baidu; const isByteDance = modelConfig.providerName === ServiceProvider.ByteDance; + const isAlibaba = modelConfig.providerName === ServiceProvider.Alibaba; const isEnabledAccessControl = accessStore.enabledAccessControl(); const apiKey = isGoogle ? accessStore.googleApiKey @@ -194,6 +195,8 @@ export function getHeaders() { ? accessStore.anthropicApiKey : isByteDance ? accessStore.bytedanceApiKey + : isAlibaba + ? accessStore.alibabaApiKey : accessStore.openaiApiKey; return { isGoogle, @@ -201,6 +204,7 @@ export function getHeaders() { isAnthropic, isBaidu, isByteDance, + isAlibaba, apiKey, isEnabledAccessControl, }; diff --git a/app/client/platforms/alibaba.ts b/app/client/platforms/alibaba.ts index eefdb017f..72126d728 100644 --- a/app/client/platforms/alibaba.ts +++ b/app/client/platforms/alibaba.ts @@ -2,7 +2,7 @@ import { ApiPath, Alibaba, - DEFAULT_API_HOST, + ALIBABA_BASE_URL, REQUEST_TIMEOUT_MS, } from "@/app/constant"; import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; @@ -58,9 +58,7 @@ export class QwenApi implements LLMApi { if (baseUrl.length === 0) { const isApp = !!getClientConfig()?.isApp; - baseUrl = isApp - ? DEFAULT_API_HOST + "/api/proxy/alibaba" - : ApiPath.Alibaba; + baseUrl = isApp ? ALIBABA_BASE_URL : ApiPath.Alibaba; } if (baseUrl.endsWith("/")) { @@ -76,14 +74,13 @@ export class QwenApi implements LLMApi { } extractMessage(res: any) { - return res.choices?.at(0)?.message?.content ?? ""; + return res?.output?.choices?.at(0)?.message?.content ?? ""; } async chat(options: ChatOptions) { - const visionModel = isVisionModel(options.config.model); const messages = options.messages.map((v) => ({ role: v.role, - content: visionModel ? v.content : getMessageTextContent(v), + content: getMessageTextContent(v), })); const modelConfig = { @@ -104,8 +101,6 @@ export class QwenApi implements LLMApi { top_p: modelConfig.top_p, }; - console.log("[Request] Alibaba payload: ", requestPayload); - const shouldStream = !!options.config.stream; const controller = new AbortController(); options.onController?.(controller); diff --git a/app/constant.ts b/app/constant.ts index 8b5bd2306..c07adad25 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -19,8 +19,7 @@ export const BAIDU_OATUH_URL = `${BAIDU_BASE_URL}/oauth/2.0/token`; export const BYTEDANCE_BASE_URL = "https://ark.cn-beijing.volces.com"; -export const ALIBABA_BASE_URL = - "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"; +export const ALIBABA_BASE_URL = "https://dashscope.aliyuncs.com/api/"; export enum Path { Home = "/", @@ -144,7 +143,8 @@ export const ByteDance = { }; export const Alibaba = { - ChatPath: "chat/completions", + ExampleEndpoint: ALIBABA_BASE_URL, + ChatPath: "v1/services/aigc/text-generation/generation", }; export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang