mirror of
https://github.com/idootop/mi-gpt.git
synced 2025-04-15 04:23:06 +00:00
feat: add chatWithStreamResponse for MyBot
This commit is contained in:
parent
02a3203c03
commit
f31f64c286
|
@ -1,9 +1,12 @@
|
||||||
|
import { randomUUID } from "crypto";
|
||||||
import { jsonDecode, jsonEncode } from "../../utils/base";
|
import { jsonDecode, jsonEncode } from "../../utils/base";
|
||||||
import { buildPrompt, toUTC8Time } from "../../utils/string";
|
import { buildPrompt, toUTC8Time } from "../../utils/string";
|
||||||
import { openai } from "../openai";
|
import { ChatOptions, openai } from "../openai";
|
||||||
import { IBotConfig } from "./config";
|
import { IBotConfig } from "./config";
|
||||||
import { ConversationManager } from "./conversation";
|
import { ConversationManager } from "./conversation";
|
||||||
|
import { StreamResponse } from "../speaker/stream";
|
||||||
|
|
||||||
|
// todo JSON mode 下,无法使用 stream 应答模式(在应答完成之前,无法构造完整的JSON)
|
||||||
const systemTemplate = `
|
const systemTemplate = `
|
||||||
忽略所有之前的文字、文件和说明。现在,你将扮演一个名为“{{name}}”的人,并以这个新身份回复所有新消息。
|
忽略所有之前的文字、文件和说明。现在,你将扮演一个名为“{{name}}”的人,并以这个新身份回复所有新消息。
|
||||||
|
|
||||||
|
@ -84,4 +87,31 @@ export class MyBot {
|
||||||
});
|
});
|
||||||
return jsonDecode(result?.content)?.message;
|
return jsonDecode(result?.content)?.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async chatWithStreamResponse(
|
||||||
|
options: ChatOptions & {
|
||||||
|
onFinished?: (text: string) => void;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
const requestId = randomUUID();
|
||||||
|
const stream = new StreamResponse();
|
||||||
|
openai
|
||||||
|
.chatStream({
|
||||||
|
...options,
|
||||||
|
requestId,
|
||||||
|
onStream: (text) => {
|
||||||
|
if (stream.status === "canceled") {
|
||||||
|
return openai.abort(requestId);
|
||||||
|
}
|
||||||
|
stream.addResponse(text);
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((answer) => {
|
||||||
|
if (answer) {
|
||||||
|
stream.finish(answer);
|
||||||
|
options.onFinished?.(answer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
26
tests/bot.ts
Normal file
26
tests/bot.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import { MyBot } from "../src/services/bot";
|
||||||
|
import { AISpeaker } from "../src/services/speaker/ai";
|
||||||
|
|
||||||
|
export async function testMyBot() {
|
||||||
|
await testStreamResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testStreamResponse() {
|
||||||
|
const stream = await MyBot.chatWithStreamResponse({
|
||||||
|
user: "地球为什么是圆的?",
|
||||||
|
onFinished: (text) => {
|
||||||
|
console.log("\nFinal result 111:\n", text);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const config: any = {
|
||||||
|
userId: process.env.MI_USER!,
|
||||||
|
password: process.env.MI_PASS!,
|
||||||
|
did: process.env.MI_DID,
|
||||||
|
tts: "doubao",
|
||||||
|
};
|
||||||
|
const speaker = new AISpeaker(config);
|
||||||
|
await speaker.initMiServices();
|
||||||
|
await speaker.response({ stream });
|
||||||
|
const res = await stream.wasFinished();
|
||||||
|
console.log("\nFinal result 222:\n", res);
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import { runWithDB } from "../src/services/db";
|
||||||
import { testDB } from "./db";
|
import { testDB } from "./db";
|
||||||
import { testSpeaker } from "./speaker";
|
import { testSpeaker } from "./speaker";
|
||||||
import { testOpenAI } from "./openai";
|
import { testOpenAI } from "./openai";
|
||||||
|
import { testMyBot } from "./bot";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
@ -12,7 +13,8 @@ async function main() {
|
||||||
println(kBannerASCII);
|
println(kBannerASCII);
|
||||||
// testDB();
|
// testDB();
|
||||||
// testSpeaker();
|
// testSpeaker();
|
||||||
testOpenAI();
|
// testOpenAI();
|
||||||
|
testMyBot();
|
||||||
}
|
}
|
||||||
|
|
||||||
runWithDB(main);
|
runWithDB(main);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user