NextChat-U/app/client/platforms/openai.ts

433 lines
12 KiB
TypeScript
Raw Normal View History

"use client";
2024-07-05 20:59:45 +09:00
// azure and openai, using same models. so using same LLMApi.
import {
2023-11-10 03:43:30 +09:00
ApiPath,
DEFAULT_API_HOST,
2023-07-11 00:19:43 +09:00
DEFAULT_MODELS,
OpenaiPath,
2024-07-05 20:59:45 +09:00
Azure,
REQUEST_TIMEOUT_MS,
2023-11-10 03:43:30 +09:00
ServiceProvider,
} from "@/app/constant";
2024-08-29 01:21:26 +09:00
import {
ChatMessageTool,
useAccessStore,
useAppConfig,
useChatStore,
2024-08-29 20:55:09 +09:00
usePluginStore,
2024-08-29 01:21:26 +09:00
} from "@/app/store";
2024-07-05 20:59:45 +09:00
import { collectModelsWithDefaultModel } from "@/app/utils/model";
2024-08-02 21:58:21 +09:00
import {
preProcessImageContent,
uploadImage,
base64Image2Blob,
stream,
2024-08-02 21:58:21 +09:00
} from "@/app/utils/chat";
2024-07-12 13:00:25 +09:00
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
import { DalleSize, DalleQuality, DalleStyle } from "@/app/typing";
2024-02-20 19:04:32 +09:00
import {
ChatOptions,
getHeaders,
LLMApi,
LLMModel,
LLMUsage,
MultimodalContent,
} from "../api";
import Locale from "../../locales";
2023-05-17 09:50:50 +09:00
import {
EventStreamContentType,
fetchEventSource,
2023-05-20 20:58:12 +09:00
} from "@fortaine/fetch-event-source";
2023-05-16 02:58:58 +09:00
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client";
2024-02-20 19:04:32 +09:00
import {
getMessageTextContent,
getMessageImages,
isVisionModel,
2024-08-02 19:00:42 +09:00
isDalle3 as _isDalle3,
2024-02-20 19:04:32 +09:00
} from "@/app/utils";
2023-05-15 00:00:17 +09:00
export interface OpenAIListModelResponse {
object: string;
data: Array<{
id: string;
object: string;
root: string;
}>;
}
2024-07-07 22:59:56 +09:00
export interface RequestPayload {
messages: {
role: "system" | "user" | "assistant";
content: string | MultimodalContent[];
}[];
stream?: boolean;
model: string;
temperature: number;
presence_penalty: number;
frequency_penalty: number;
top_p: number;
max_tokens?: number;
}
2024-08-02 19:00:42 +09:00
export interface DalleRequestPayload {
model: string;
prompt: string;
2024-08-02 21:58:21 +09:00
response_format: "url" | "b64_json";
2024-08-02 19:00:42 +09:00
n: number;
2024-08-02 19:50:48 +09:00
size: DalleSize;
quality: DalleQuality;
style: DalleStyle;
2024-08-02 19:00:42 +09:00
}
2023-05-15 00:00:17 +09:00
export class ChatGPTApi implements LLMApi {
2023-07-11 00:19:43 +09:00
private disableListModels = true;
2023-05-15 00:00:17 +09:00
path(path: string): string {
2023-11-10 03:43:30 +09:00
const accessStore = useAccessStore.getState();
let baseUrl = "";
2023-11-10 03:43:30 +09:00
2024-07-05 21:15:56 +09:00
const isAzure = path.includes("deployments");
if (accessStore.useCustomConfig) {
if (isAzure && !accessStore.isValidAzure()) {
throw Error(
"incomplete azure config, please check it in your settings page",
);
}
baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl;
}
2023-11-10 03:43:30 +09:00
if (baseUrl.length === 0) {
const isApp = !!getClientConfig()?.isApp;
2024-07-05 21:15:56 +09:00
const apiPath = isAzure ? ApiPath.Azure : ApiPath.OpenAI;
baseUrl = isApp ? DEFAULT_API_HOST + "/proxy" + apiPath : apiPath;
}
2023-11-10 03:43:30 +09:00
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
}
2024-07-05 21:15:56 +09:00
if (
!baseUrl.startsWith("http") &&
!isAzure &&
!baseUrl.startsWith(ApiPath.OpenAI)
) {
2023-11-10 03:43:30 +09:00
baseUrl = "https://" + baseUrl;
2023-05-16 01:22:11 +09:00
}
2023-11-10 03:43:30 +09:00
console.log("[Proxy Endpoint] ", baseUrl, path);
2024-07-12 13:00:25 +09:00
// try rebuild url, when using cloudflare ai gateway in client
return cloudflareAIGatewayUrl([baseUrl, path].join("/"));
2023-05-15 00:00:17 +09:00
}
2024-08-02 21:58:21 +09:00
async extractMessage(res: any) {
2024-08-02 19:00:42 +09:00
if (res.error) {
return "```\n" + JSON.stringify(res, null, 4) + "\n```";
}
2024-08-02 21:58:21 +09:00
// dalle3 model return url, using url create image message
2024-08-02 19:00:42 +09:00
if (res.data) {
2024-08-02 21:58:21 +09:00
let url = res.data?.at(0)?.url ?? "";
const b64_json = res.data?.at(0)?.b64_json ?? "";
if (!url && b64_json) {
// uploadImage
url = await uploadImage(base64Image2Blob(b64_json, "image/png"));
}
2024-08-02 19:00:42 +09:00
return [
{
type: "image_url",
image_url: {
url,
},
},
];
}
2024-08-02 23:16:08 +09:00
return res.choices?.at(0)?.message?.content ?? res;
2023-05-15 00:00:17 +09:00
}
async chat(options: ChatOptions) {
const modelConfig = {
...useAppConfig.getState().modelConfig,
...useChatStore.getState().currentSession().mask.modelConfig,
...{
model: options.config.model,
2024-07-05 20:59:45 +09:00
providerName: options.config.providerName,
2023-05-15 00:00:17 +09:00
},
};
2024-08-02 19:00:42 +09:00
let requestPayload: RequestPayload | DalleRequestPayload;
2023-05-15 00:00:17 +09:00
2024-08-02 19:00:42 +09:00
const isDalle3 = _isDalle3(options.config.model);
if (isDalle3) {
2024-08-02 19:50:48 +09:00
const prompt = getMessageTextContent(
options.messages.slice(-1)?.pop() as any,
);
2024-08-02 19:00:42 +09:00
requestPayload = {
model: options.config.model,
prompt,
2024-08-02 21:58:21 +09:00
// URLs are only valid for 60 minutes after the image has been generated.
response_format: "b64_json", // using b64_json, and save image in CacheStorage
2024-08-02 19:00:42 +09:00
n: 1,
size: options.config?.size ?? "1024x1024",
quality: options.config?.quality ?? "standard",
style: options.config?.style ?? "vivid",
2024-08-02 19:00:42 +09:00
};
} else {
const visionModel = isVisionModel(options.config.model);
const messages: ChatOptions["messages"] = [];
for (const v of options.messages) {
const content = visionModel
? await preProcessImageContent(v.content)
: getMessageTextContent(v);
messages.push({ role: v.role, content });
}
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.
};
// add max_tokens to vision model
if (visionModel && modelConfig.model.includes("preview")) {
requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000);
}
}
2023-05-15 00:00:17 +09:00
console.log("[Request] openai payload: ", requestPayload);
2024-08-02 19:00:42 +09:00
const shouldStream = !isDalle3 && !!options.config.stream;
2023-05-15 00:00:17 +09:00
const controller = new AbortController();
options.onController?.(controller);
2023-05-15 00:00:17 +09:00
try {
2024-07-05 20:59:45 +09:00
let chatPath = "";
2024-07-06 00:56:10 +09:00
if (modelConfig.providerName === ServiceProvider.Azure) {
2024-07-05 20:59:45 +09:00
// find model, and get displayName as deployName
const { models: configModels, customModels: configCustomModels } =
useAppConfig.getState();
2024-07-06 01:05:59 +09:00
const {
defaultModel,
customModels: accessCustomModels,
useCustomConfig,
} = useAccessStore.getState();
2024-07-05 20:59:45 +09:00
const models = collectModelsWithDefaultModel(
configModels,
[configCustomModels, accessCustomModels].join(","),
defaultModel,
);
const model = models.find(
(model) =>
2024-07-06 00:56:10 +09:00
model.name === modelConfig.model &&
model?.provider?.providerName === ServiceProvider.Azure,
2024-07-05 20:59:45 +09:00
);
2024-07-05 21:15:56 +09:00
chatPath = this.path(
2024-08-02 19:00:42 +09:00
(isDalle3 ? Azure.ImagePath : Azure.ChatPath)(
2024-07-05 21:20:21 +09:00
(model?.displayName ?? model?.name) as string,
2024-07-06 01:05:59 +09:00
useCustomConfig ? useAccessStore.getState().azureApiVersion : "",
2024-07-05 21:15:56 +09:00
),
);
2024-07-05 20:59:45 +09:00
} else {
2024-08-02 19:00:42 +09:00
chatPath = this.path(
isDalle3 ? OpenaiPath.ImagePath : OpenaiPath.ChatPath,
);
2024-07-05 20:59:45 +09:00
}
2023-05-15 00:00:17 +09:00
if (shouldStream) {
2024-08-30 18:31:20 +09:00
const [tools, funcs] = usePluginStore
2024-08-29 20:55:09 +09:00
.getState()
2024-08-31 00:39:08 +09:00
.getAsTools(
useChatStore.getState().currentSession().mask?.plugin as string[],
);
2024-09-02 22:45:47 +09:00
// console.log("getAsTools", tools, funcs);
stream(
chatPath,
requestPayload,
getHeaders(),
2024-08-31 00:39:08 +09:00
tools as any,
2024-08-30 18:31:20 +09:00
funcs,
controller,
2024-08-29 18:28:15 +09:00
// parseSSE
(text: string, runTools: ChatMessageTool[]) => {
2024-08-29 18:28:15 +09:00
// console.log("parseSSE", text, runTools);
const json = JSON.parse(text);
const choices = json.choices as Array<{
delta: {
content: string;
tool_calls: ChatMessageTool[];
2024-08-29 00:58:46 +09:00
};
}>;
const tool_calls = choices[0]?.delta?.tool_calls;
if (tool_calls?.length > 0) {
const index = tool_calls[0]?.index;
const id = tool_calls[0]?.id;
const args = tool_calls[0]?.function?.arguments;
if (id) {
runTools.push({
id,
type: tool_calls[0]?.type,
2024-08-29 00:58:46 +09:00
function: {
name: tool_calls[0]?.function?.name as string,
arguments: args,
2024-08-29 00:58:46 +09:00
},
});
} else {
// @ts-ignore
runTools[index]["function"]["arguments"] += args;
}
}
return choices[0]?.delta?.content;
},
2024-08-29 18:28:15 +09:00
// processToolMessage, include tool_calls message and tool call results
(
requestPayload: RequestPayload,
toolCallMessage: any,
toolCallResult: any[],
) => {
// @ts-ignore
requestPayload?.messages?.splice(
// @ts-ignore
requestPayload?.messages?.length,
0,
toolCallMessage,
...toolCallResult,
);
},
options,
);
} else {
const chatPayload = {
method: "POST",
body: JSON.stringify(requestPayload),
signal: controller.signal,
headers: getHeaders(),
};
2024-08-29 00:58:46 +09:00
// make a fetch request
const requestTimeoutId = setTimeout(
() => controller.abort(),
isDalle3 ? REQUEST_TIMEOUT_MS * 2 : REQUEST_TIMEOUT_MS, // dalle3 using b64_json is slow.
);
2024-08-29 00:58:46 +09:00
2023-05-15 00:00:17 +09:00
const res = await fetch(chatPath, chatPayload);
clearTimeout(requestTimeoutId);
2023-05-15 00:00:17 +09:00
const resJson = await res.json();
2024-08-02 21:58:21 +09:00
const message = await this.extractMessage(resJson);
2023-05-15 00:00:17 +09:00
options.onFinish(message);
}
} catch (e) {
2023-08-01 11:16:36 +09:00
console.log("[Request] failed to make a chat request", e);
options.onError?.(e as Error);
2023-05-15 00:00:17 +09:00
}
}
async usage() {
const formatDate = (d: Date) =>
`${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, "0")}-${d
.getDate()
.toString()
.padStart(2, "0")}`;
const ONE_DAY = 1 * 24 * 60 * 60 * 1000;
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const startDate = formatDate(startOfMonth);
const endDate = formatDate(new Date(Date.now() + ONE_DAY));
const [used, subs] = await Promise.all([
fetch(
this.path(
`${OpenaiPath.UsagePath}?start_date=${startDate}&end_date=${endDate}`,
),
{
method: "GET",
headers: getHeaders(),
},
),
fetch(this.path(OpenaiPath.SubsPath), {
method: "GET",
headers: getHeaders(),
}),
]);
if (used.status === 401) {
throw new Error(Locale.Error.Unauthorized);
}
if (!used.ok || !subs.ok) {
throw new Error("Failed to query usage from openai");
}
const response = (await used.json()) as {
total_usage?: number;
error?: {
type: string;
message: string;
};
};
const total = (await subs.json()) as {
hard_limit_usd?: number;
};
if (response.error && response.error.type) {
throw Error(response.error.message);
}
if (response.total_usage) {
response.total_usage = Math.round(response.total_usage) / 100;
}
if (total.hard_limit_usd) {
total.hard_limit_usd = Math.round(total.hard_limit_usd * 100) / 100;
}
2023-05-15 00:00:17 +09:00
return {
used: response.total_usage,
total: total.hard_limit_usd,
2023-05-15 00:00:17 +09:00
} as LLMUsage;
}
async models(): Promise<LLMModel[]> {
2023-07-11 00:19:43 +09:00
if (this.disableListModels) {
return DEFAULT_MODELS.slice();
}
const res = await fetch(this.path(OpenaiPath.ListModelPath), {
method: "GET",
headers: {
...getHeaders(),
},
});
const resJson = (await res.json()) as OpenAIListModelResponse;
const chatModels = resJson.data?.filter((m) => m.id.startsWith("gpt-"));
console.log("[Models]", chatModels);
if (!chatModels) {
return [];
}
//由于目前 OpenAI 的 disableListModels 默认为 true所以当前实际不会运行到这场
let seq = 1000; //同 Constant.ts 中的排序保持一致
return chatModels.map((m) => ({
name: m.id,
available: true,
sorted: seq++,
2023-12-24 03:39:06 +09:00
provider: {
id: "openai",
providerName: "OpenAI",
providerType: "openai",
sorted: 1,
2023-12-24 03:39:06 +09:00
},
}));
}
2023-05-15 00:00:17 +09:00
}
export { OpenaiPath };