This commit is contained in:
lloydzhou 2024-07-09 19:56:52 +08:00
parent 23872086fa
commit 3628d68d9a
4 changed files with 12 additions and 18 deletions

View File

@ -68,10 +68,7 @@ async function request(req: NextRequest) {
const controller = new AbortController(); const controller = new AbortController();
// alibaba use base url or just remove the path // alibaba use base url or just remove the path
let path = `${req.nextUrl.pathname}`.replaceAll( let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Alibaba, "");
ApiPath.Alibaba + "/" + Alibaba.ChatPath,
"",
);
let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL; let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL;
@ -153,11 +150,9 @@ async function request(req: NextRequest) {
console.error(`[Alibaba] filter`, e); console.error(`[Alibaba] filter`, e);
} }
} }
console.log("[Alibaba request]", fetchOptions.headers, req.method);
try { try {
const res = await fetch(fetchUrl, fetchOptions); const res = await fetch(fetchUrl, fetchOptions);
console.log("[Alibaba response]", res.status, " ", res.headers, res.url);
// to prevent browser prompt for credentials // to prevent browser prompt for credentials
const newHeaders = new Headers(res.headers); const newHeaders = new Headers(res.headers);
newHeaders.delete("www-authenticate"); newHeaders.delete("www-authenticate");

View File

@ -185,6 +185,7 @@ export function getHeaders() {
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic; const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
const isBaidu = modelConfig.providerName == ServiceProvider.Baidu; const isBaidu = modelConfig.providerName == ServiceProvider.Baidu;
const isByteDance = modelConfig.providerName === ServiceProvider.ByteDance; const isByteDance = modelConfig.providerName === ServiceProvider.ByteDance;
const isAlibaba = modelConfig.providerName === ServiceProvider.Alibaba;
const isEnabledAccessControl = accessStore.enabledAccessControl(); const isEnabledAccessControl = accessStore.enabledAccessControl();
const apiKey = isGoogle const apiKey = isGoogle
? accessStore.googleApiKey ? accessStore.googleApiKey
@ -194,6 +195,8 @@ export function getHeaders() {
? accessStore.anthropicApiKey ? accessStore.anthropicApiKey
: isByteDance : isByteDance
? accessStore.bytedanceApiKey ? accessStore.bytedanceApiKey
: isAlibaba
? accessStore.alibabaApiKey
: accessStore.openaiApiKey; : accessStore.openaiApiKey;
return { return {
isGoogle, isGoogle,
@ -201,6 +204,7 @@ export function getHeaders() {
isAnthropic, isAnthropic,
isBaidu, isBaidu,
isByteDance, isByteDance,
isAlibaba,
apiKey, apiKey,
isEnabledAccessControl, isEnabledAccessControl,
}; };

View File

@ -2,7 +2,7 @@
import { import {
ApiPath, ApiPath,
Alibaba, Alibaba,
DEFAULT_API_HOST, ALIBABA_BASE_URL,
REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS,
} from "@/app/constant"; } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
@ -58,9 +58,7 @@ export class QwenApi implements LLMApi {
if (baseUrl.length === 0) { if (baseUrl.length === 0) {
const isApp = !!getClientConfig()?.isApp; const isApp = !!getClientConfig()?.isApp;
baseUrl = isApp baseUrl = isApp ? ALIBABA_BASE_URL : ApiPath.Alibaba;
? DEFAULT_API_HOST + "/api/proxy/alibaba"
: ApiPath.Alibaba;
} }
if (baseUrl.endsWith("/")) { if (baseUrl.endsWith("/")) {
@ -76,14 +74,13 @@ export class QwenApi implements LLMApi {
} }
extractMessage(res: any) { extractMessage(res: any) {
return res.choices?.at(0)?.message?.content ?? ""; return res?.output?.choices?.at(0)?.message?.content ?? "";
} }
async chat(options: ChatOptions) { async chat(options: ChatOptions) {
const visionModel = isVisionModel(options.config.model);
const messages = options.messages.map((v) => ({ const messages = options.messages.map((v) => ({
role: v.role, role: v.role,
content: visionModel ? v.content : getMessageTextContent(v), content: getMessageTextContent(v),
})); }));
const modelConfig = { const modelConfig = {
@ -104,8 +101,6 @@ export class QwenApi implements LLMApi {
top_p: modelConfig.top_p, top_p: modelConfig.top_p,
}; };
console.log("[Request] Alibaba payload: ", requestPayload);
const shouldStream = !!options.config.stream; const shouldStream = !!options.config.stream;
const controller = new AbortController(); const controller = new AbortController();
options.onController?.(controller); options.onController?.(controller);

View File

@ -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 BYTEDANCE_BASE_URL = "https://ark.cn-beijing.volces.com";
export const ALIBABA_BASE_URL = export const ALIBABA_BASE_URL = "https://dashscope.aliyuncs.com/api/";
"https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
export enum Path { export enum Path {
Home = "/", Home = "/",
@ -144,7 +143,8 @@ export const ByteDance = {
}; };
export const Alibaba = { 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 export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang