From 2048698f77b91555bae48950805969b95105c328 Mon Sep 17 00:00:00 2001 From: Marcus Schiesser <mail@marcusschiesser.de> Date: Tue, 5 Mar 2024 11:15:50 +0700 Subject: [PATCH] docs: add interactive chat for anthropic --- examples/anthropic/chat_interactive.ts | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/anthropic/chat_interactive.ts diff --git a/examples/anthropic/chat_interactive.ts b/examples/anthropic/chat_interactive.ts new file mode 100644 index 000000000..88bac3544 --- /dev/null +++ b/examples/anthropic/chat_interactive.ts @@ -0,0 +1,34 @@ +import { Anthropic, SimpleChatEngine, SimpleChatHistory } from "llamaindex"; +import { stdin as input, stdout as output } from "node:process"; +import readline from "node:readline/promises"; + +(async () => { + const llm = new Anthropic({ + apiKey: process.env.ANTHROPIC_API_KEY, + model: "claude-3-opus", + }); + // chatHistory will store all the messages in the conversation + const chatHistory = new SimpleChatHistory({ + messages: [ + { + content: "You want to talk in rhymes.", + role: "system", + }, + ], + }); + const chatEngine = new SimpleChatEngine({ + llm, + chatHistory, + }); + const rl = readline.createInterface({ input, output }); + + while (true) { + const query = await rl.question("User: "); + process.stdout.write("Assistant: "); + const stream = await chatEngine.chat({ message: query, stream: true }); + for await (const chunk of stream) { + process.stdout.write(chunk.response); + } + process.stdout.write("\n"); + } +})(); -- GitLab