import { createRefileTool } from "./refileTool.js";
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
// Initialize the OpenAI chat model
const model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4o", // You can change to a different model as needed
openAIApiKey: "<token>",
});
// Create the refile tool
const refileTool = createRefileTool("<token>");
// Bind the tool to the model
const llmWithTool = model.bindTools([refileTool]);
// Create a messages array to track conversation
const messages = [
new HumanMessage(
"Create a research report on the rainforests of the Amazon as a PDF. Include sections on biodiversity, conservation efforts, and threats. Reply with the URL."
),
];
// Get the initial response from the model
const aiMessage = await llmWithTool.invoke(messages);
console.log("Initial AI response:", aiMessage);
// Add the AI's response to the messages
messages.push(aiMessage);
// Check if there are tool calls to execute
if (aiMessage.tool_calls && aiMessage.tool_calls.length > 0) {
// Process each tool call
for (const toolCall of aiMessage.tool_calls) {
console.log(`Executing tool call: ${toolCall.name}`);
// Execute the tool with the tool call
const toolMessage = await refileTool.invoke(toolCall);
// Add the tool result to the messages
messages.push(toolMessage);
}
// Get the final response with the tool results
const finalResponse = await llmWithTool.invoke(messages);
console.log("Final response:", finalResponse);
} else {
// No tool calls were made
console.log("No tool calls were made.");
}