You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.9 KiB
82 lines
2.9 KiB
import { createXllm, type XProviderName } from "@dm-pkg/xllm";
|
|
import { hasPrimaryConfig, primaryApiKey, primaryBaseUrl, primaryModel, primaryProvider } from "../env";
|
|
import { runConversationHistoryDemo } from "./conversation-history";
|
|
import { runGenerateDemo } from "./generate";
|
|
import { runRequestSwitchDemo } from "./request-switch";
|
|
import { runStreamDemo } from "./stream";
|
|
import { runStreamWithToolsDemo } from "./stream-with-tools";
|
|
import { runThinkingDemo } from "./thinking";
|
|
import { runToolsMultimodalDemo } from "./tools-multimodal";
|
|
import type { DemoLog, XllmClient } from "./types";
|
|
|
|
export type DemoDefinition = {
|
|
id: string;
|
|
title: string;
|
|
description: string;
|
|
run: (client: XllmClient, log: DemoLog, ctx: { provider: XProviderName; model: string }) => Promise<void>;
|
|
};
|
|
|
|
function createDefaultClient(): XllmClient | null {
|
|
if (!hasPrimaryConfig || !primaryApiKey) {
|
|
return null;
|
|
}
|
|
return createXllm({
|
|
provider: primaryProvider,
|
|
model: primaryModel,
|
|
apiKey: primaryApiKey,
|
|
baseURL: primaryBaseUrl,
|
|
});
|
|
}
|
|
|
|
export const xllmDemoCatalog: DemoDefinition[] = [
|
|
{
|
|
id: "stream",
|
|
title: "stream() 流式输出",
|
|
description: "逐 token 消费 SSE 风格事件,适合打字机 UI。",
|
|
run: (client, log) => runStreamDemo(client, log),
|
|
},
|
|
{
|
|
id: "generate",
|
|
title: "generate() 一次性响应",
|
|
description: "非流式,直接拿到完整文本与 usage。",
|
|
run: (client, log) => runGenerateDemo(client, log),
|
|
},
|
|
{
|
|
id: "conversation-history",
|
|
title: "完整对话历史",
|
|
description:
|
|
"用 XMessage[] 累积多轮 user/assistant;generate 与 stream 共用同一条 history,并打印可持久化的 JSON。",
|
|
run: (client, log) => runConversationHistoryDemo(client, log),
|
|
},
|
|
{
|
|
id: "thinking",
|
|
title: "DeepSeek 思考模式",
|
|
description:
|
|
"thinking + reasoning_effort;流式区分 reasoning.delta 与 text.delta;generate 可读 reasoning。需 provider=deepseek(见 .env.example)。",
|
|
run: (client, log, ctx) => runThinkingDemo(client, log, ctx),
|
|
},
|
|
{
|
|
id: "request-switch",
|
|
title: "请求级切换 provider / model",
|
|
description: "单次请求覆盖默认 client 的 provider、model、apiKey(需配置 *_2 环境变量)。",
|
|
run: (client, log) => runRequestSwitchDemo(client, log),
|
|
},
|
|
{
|
|
id: "tools-multimodal",
|
|
title: "chatWithTools + 多模态",
|
|
description: "工具闭环;若模型支持视觉则附带示例图片 URL。",
|
|
run: (client, log, ctx) => runToolsMultimodalDemo(client, ctx.provider, ctx.model, log),
|
|
},
|
|
{
|
|
id: "stream-with-tools",
|
|
title: "streamWithTools()",
|
|
description: "流式输出同时跑工具调用闭环。",
|
|
run: (client, log) => runStreamWithToolsDemo(client, log),
|
|
},
|
|
];
|
|
|
|
export function getDefaultXllmClient(): XllmClient | null {
|
|
return createDefaultClient();
|
|
}
|
|
|
|
export { primaryModel, primaryProvider };
|
|
|