mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 07:36:39 +08:00
140 lines
3.9 KiB
TypeScript
140 lines
3.9 KiB
TypeScript
"use client";
|
|
// azure and openai, using same models. so using same LLMApi.
|
|
import {
|
|
ApiPath,
|
|
DEFAULT_API_HOST,
|
|
DEFAULT_MODELS,
|
|
Stepfun,
|
|
REQUEST_TIMEOUT_MS,
|
|
ServiceProvider,
|
|
} from "@/app/constant";
|
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
|
import { collectModelsWithDefaultModel } from "@/app/utils/model";
|
|
import { preProcessImageContent } from "@/app/utils/chat";
|
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
|
|
|
import {
|
|
ChatOptions,
|
|
getHeaders,
|
|
LLMApi,
|
|
LLMModel,
|
|
LLMUsage,
|
|
MultimodalContent,
|
|
} from "../api";
|
|
import Locale from "../../locales";
|
|
import {
|
|
EventStreamContentType,
|
|
fetchEventSource,
|
|
} from "@fortaine/fetch-event-source";
|
|
import { prettyObject } from "@/app/utils/format";
|
|
import { getClientConfig } from "@/app/config/client";
|
|
import { getMessageTextContent } from "@/app/utils";
|
|
|
|
import { OpenAIListModelResponse, RequestPayload } from "./openai";
|
|
|
|
export class StepfunApi implements LLMApi {
|
|
private disableListModels = true;
|
|
|
|
path(path: string): string {
|
|
const accessStore = useAccessStore.getState();
|
|
let baseUrl = accessStore.useCustomConfig ? accessStore.stepfunUrl : "";
|
|
|
|
if (!baseUrl) {
|
|
const isApp = !!getClientConfig()?.isApp;
|
|
const apiPath = ApiPath.Stepfun;
|
|
baseUrl = isApp ? `${DEFAULT_API_HOST}/proxy${apiPath}` : apiPath;
|
|
}
|
|
|
|
baseUrl = baseUrl.replace(/\/$/, ""); // Remove trailing slash
|
|
if (!/^https?:\/\//.test(baseUrl) && !baseUrl.startsWith(ApiPath.Stepfun)) {
|
|
baseUrl = `https://${baseUrl}`;
|
|
}
|
|
|
|
console.log("[Proxy Endpoint] ", baseUrl, path);
|
|
return `${baseUrl}/${path}`;
|
|
}
|
|
|
|
extractMessage(res: any) {
|
|
return res.choices?.at(0)?.message?.content ?? "";
|
|
}
|
|
|
|
async chat(options: ChatOptions) {
|
|
const messages: ChatOptions["messages"] = [];
|
|
for (const v of options.messages) {
|
|
const content = getMessageTextContent(v);
|
|
messages.push({ role: v.role, content });
|
|
}
|
|
|
|
const modelConfig = {
|
|
...useAppConfig.getState().modelConfig,
|
|
...useChatStore.getState().currentSession().mask.modelConfig,
|
|
...{
|
|
model: options.config.model,
|
|
providerName: options.config.providerName,
|
|
},
|
|
};
|
|
|
|
const requestPayload: RequestPayload = {
|
|
messages,
|
|
stream: options.config.stream,
|
|
model: modelConfig.model,
|
|
temperature: modelConfig.temperature,
|
|
presence_penalty: modelConfig.presence_penalty,
|
|
frequency_penalty: modelConfig.frequency_penalty,
|
|
top_p: modelConfig.top_p,
|
|
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
|
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
|
};
|
|
|
|
console.log("[Request] openai payload: ", requestPayload);
|
|
|
|
const shouldStream = !!options.config.stream;
|
|
const controller = new AbortController();
|
|
options.onController?.(controller);
|
|
|
|
try {
|
|
const chatPath = this.path(Stepfun.ChatPath);
|
|
const chatPayload = {
|
|
method: "POST",
|
|
body: JSON.stringify(requestPayload),
|
|
signal: controller.signal,
|
|
headers: getHeaders(),
|
|
};
|
|
|
|
// make a fetch request
|
|
const requestTimeoutId = setTimeout(
|
|
() => controller.abort(),
|
|
REQUEST_TIMEOUT_MS,
|
|
);
|
|
|
|
if (shouldStream) {
|
|
// Streaming logic
|
|
} else {
|
|
const res = await fetch(chatPath, chatPayload);
|
|
clearTimeout(requestTimeoutId);
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Request failed with status ${res.status}`);
|
|
}
|
|
|
|
const resJson = await res.json();
|
|
const message = this.extractMessage(resJson);
|
|
options.onFinish(message);
|
|
}
|
|
} catch (e) {
|
|
console.error("[Request] failed to make a chat request", e);
|
|
options.onError?.(e as Error);
|
|
}
|
|
}
|
|
async usage() {
|
|
return {
|
|
used: 0,
|
|
total: 0,
|
|
};
|
|
}
|
|
|
|
async models(): Promise<LLMModel[]> {
|
|
return [];
|
|
}
|
|
}
|