mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-21 14:55:36 -04:00
56689707d3
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import { OpenAI } from "@llamaindex/openai";
|
|
|
|
async function main() {
|
|
const llm = new OpenAI({ model: "gpt-4-turbo" });
|
|
const args: Parameters<typeof llm.chat>[0] = {
|
|
additionalChatOptions: {
|
|
tool_choice: "auto",
|
|
},
|
|
messages: [
|
|
{
|
|
content: "Who was Goethe?",
|
|
role: "user",
|
|
},
|
|
],
|
|
tools: [
|
|
{
|
|
metadata: {
|
|
name: "wikipedia_search",
|
|
description: "A tool that uses a query engine to search Wikipedia.",
|
|
parameters: {
|
|
type: "object",
|
|
properties: {
|
|
query: {
|
|
type: "string",
|
|
description: "The query to search for",
|
|
},
|
|
},
|
|
required: ["query"],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const stream = await llm.chat({ ...args, stream: true });
|
|
for await (const chunk of stream) {
|
|
process.stdout.write(chunk.delta);
|
|
if (chunk.options && "toolCall" in chunk.options) {
|
|
console.log("Tool call:");
|
|
console.log(chunk.options.toolCall);
|
|
}
|
|
}
|
|
}
|
|
|
|
(async function () {
|
|
await main();
|
|
console.log("Done");
|
|
})();
|