Skip to content
Snippets Groups Projects
Commit 1da51b49 authored by thucpn's avatar thucpn
Browse files

move tool call to tools package

parent 8f56465a
No related branches found
No related tags found
No related merge requests found
export * from "./server"; export * from "./server";
export * from "./workflow/stream"; export * from "./workflow/stream";
export * from "./workflow/tools";
export * from "./workflow/type"; export * from "./workflow/type";
...@@ -7,3 +7,5 @@ export * from "./tools/interpreter"; ...@@ -7,3 +7,5 @@ export * from "./tools/interpreter";
export * from "./tools/openapi-action"; export * from "./tools/openapi-action";
export * from "./tools/weather"; export * from "./tools/weather";
export * from "./tools/wikipedia"; export * from "./tools/wikipedia";
export * from "./tool-call";
import { callTool } from "@llamaindex/core/agent";
import { import {
type BaseToolWithCall, type BaseToolWithCall,
callTool,
type ChatMessage, type ChatMessage,
type ChatResponse, type ChatResponse,
type ChatResponseChunk, type ChatResponseChunk,
type HandlerContext,
type PartialToolCall, type PartialToolCall,
type ToolCall, type ToolCall,
ToolCallLLM, ToolCallLLM,
type ToolCallLLMMessageOptions, type ToolCallLLMMessageOptions,
} from "llamaindex"; } from "@llamaindex/core/llms";
import crypto from "node:crypto";
import { AgentRunEvent } from "./type";
/** export async function callTools({
* Call multiple tools and return the tool messages
*/
export const callTools = async <T>({
tools, tools,
toolCalls, toolCalls,
ctx, writeEvent,
agentName,
writeEvent = true,
}: { }: {
toolCalls: ToolCall[]; toolCalls: ToolCall[];
tools: BaseToolWithCall[]; tools: BaseToolWithCall[];
ctx: HandlerContext<T>; writeEvent?: (text: string, step: number, totalSteps: number) => void;
agentName: string; }): Promise<ChatMessage[]> {
writeEvent?: boolean; if (toolCalls.length === 0) return [];
}): Promise<ChatMessage[]> => {
const toolMsgs: ChatMessage[] = []; const toolMsgs: ChatMessage[] = [];
if (toolCalls.length === 0) {
return toolMsgs;
}
if (toolCalls.length === 1 && toolCalls[0]) {
const tool = tools.find(
(tool) => tool.metadata.name === toolCalls[0]!.name,
);
if (!tool) {
throw new Error(`Tool ${toolCalls[0].name} not found`);
}
return [
await callSingleTool(
tool,
toolCalls[0],
writeEvent
? (msg: string) => {
ctx.sendEvent(
new AgentRunEvent({
agent: agentName,
text: msg,
type: "text",
}),
);
}
: undefined,
),
];
}
// Multiple tool calls, show events in progress
const progressId = crypto.randomUUID();
const totalSteps = toolCalls.length; const totalSteps = toolCalls.length;
let currentStep = 0; for (let step = 0; step < totalSteps; step++) {
for (const toolCall of toolCalls) { const toolCall = toolCalls[step]!;
const tool = tools.find((tool) => tool.metadata.name === toolCall.name); const tool = tools.find((tool) => tool.metadata.name === toolCall.name);
if (!tool) { if (!tool) throw new Error(`Tool ${toolCall.name} not found`);
throw new Error(`Tool ${toolCall.name} not found`);
} const toolMsg = await callSingleTool(tool, toolCall, (text) => {
const toolMsg = await callSingleTool(tool, toolCall, (msg: string) => { writeEvent?.(text, step, totalSteps);
ctx.sendEvent(
new AgentRunEvent({
agent: agentName,
text: msg,
type: "progress",
data: {
id: progressId,
total: totalSteps,
current: currentStep,
},
}),
);
currentStep++;
}); });
toolMsgs.push(toolMsg); toolMsgs.push(toolMsg);
} }
return toolMsgs; return toolMsgs;
}; }
export const callSingleTool = async ( export async function callSingleTool(
tool: BaseToolWithCall, tool: BaseToolWithCall,
toolCall: ToolCall, toolCall: ToolCall,
eventEmitter?: (msg: string) => void, eventEmitter?: (msg: string) => void,
): Promise<ChatMessage> => { ): Promise<ChatMessage> {
if (eventEmitter) { if (eventEmitter) {
eventEmitter( eventEmitter(
`Calling tool ${toolCall.name} with input: ${JSON.stringify(toolCall.input)}`, `Calling tool ${toolCall.name} with input: ${JSON.stringify(toolCall.input)}`,
...@@ -135,7 +86,7 @@ export const callSingleTool = async ( ...@@ -135,7 +86,7 @@ export const callSingleTool = async (
}, },
}, },
}; };
}; }
class ChatWithToolsResponse { class ChatWithToolsResponse {
toolCalls: ToolCall[]; toolCalls: ToolCall[];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment