Browse Source
Introduce a new demo for maintaining complete conversation history in the xllm package. This feature allows the accumulation of user and assistant messages across multiple turns, enabling both generate and stream functionalities to utilize the same history. Update the demo catalog to include this new functionality.main
2 changed files with 68 additions and 0 deletions
@ -0,0 +1,60 @@ |
|||||
|
import type { XMessage } from "@dm/xllm"; |
||||
|
import type { DemoLog, XllmClient } from "./types"; |
||||
|
|
||||
|
/** |
||||
|
* 维护完整对话历史:每轮把 user / assistant 按顺序压入 `messages`, |
||||
|
* 下一请求原样传入 `generate`(或 `stream`),模型才能看到前文。 |
||||
|
*/ |
||||
|
export async function runConversationHistoryDemo(client: XllmClient, log: DemoLog): Promise<void> { |
||||
|
log("========== 多轮对话 + 完整 history(generate)=========="); |
||||
|
|
||||
|
const history: XMessage[] = [ |
||||
|
{ |
||||
|
role: "system", |
||||
|
content: [{ type: "text", text: "你是简洁助手,回答尽量短。" }], |
||||
|
}, |
||||
|
]; |
||||
|
|
||||
|
const userTurns = [ |
||||
|
"我叫阿林,请记住。", |
||||
|
"我刚才说我叫什么?只回答名字。", |
||||
|
]; |
||||
|
|
||||
|
for (let i = 0; i < userTurns.length; i += 1) { |
||||
|
const userText = userTurns[i]!; |
||||
|
log(`\n--- 第 ${i + 1} 轮 user: ${userText} ---`); |
||||
|
history.push({ role: "user", content: [{ type: "text", text: userText }] }); |
||||
|
|
||||
|
const result = await client.generate({ messages: history }); |
||||
|
const assistant: XMessage = { |
||||
|
role: "assistant", |
||||
|
content: [{ type: "text", text: result.text }], |
||||
|
...(result.reasoning ? { reasoningContent: result.reasoning } : {}), |
||||
|
}; |
||||
|
history.push(assistant); |
||||
|
|
||||
|
log(`[assistant] ${result.text}`); |
||||
|
if (result.reasoning) { |
||||
|
log(`[reasoning 已写入 history.reasoningContent,长度 ${result.reasoning.length}]`); |
||||
|
} |
||||
|
log(`[当前 history 条数] ${history.length}(含 system)`); |
||||
|
} |
||||
|
|
||||
|
log("\n========== 完整 history(JSON,可供持久化 / 下次会话恢复)=========="); |
||||
|
log(JSON.stringify(history, null, 2)); |
||||
|
|
||||
|
log("\n--- 可选:用同一条 history 再流式问一轮(演示 stream 与 generate 共用历史)---"); |
||||
|
history.push({ |
||||
|
role: "user", |
||||
|
content: [{ type: "text", text: "用一句话总结我们聊了什么。" }], |
||||
|
}); |
||||
|
let streamed = ""; |
||||
|
for await (const event of client.stream({ messages: history })) { |
||||
|
if (event.type === "text.delta") { |
||||
|
streamed += event.text; |
||||
|
} |
||||
|
} |
||||
|
history.push({ role: "assistant", content: [{ type: "text", text: streamed }] }); |
||||
|
log(`[stream 本轮正文] ${streamed}`); |
||||
|
log(`[最终 history 条数] ${history.length}`); |
||||
|
} |
||||
Loading…
Reference in new issue