mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-18 16:44:33 -04:00
feat: Add toolcall callbacks to agent workflows (#2137)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@llamaindex/workflow": patch
|
||||
"@llamaindex/core": patch
|
||||
---
|
||||
|
||||
Add toolcall callbacks to agent workflows
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
} from "../llms";
|
||||
import { baseToolWithCallSchema } from "../schema";
|
||||
import {
|
||||
assertIsJSONValue,
|
||||
isAsyncIterable,
|
||||
prettifyError,
|
||||
stringifyJSONToMessageContent,
|
||||
@@ -227,6 +228,7 @@ export async function callTool(
|
||||
`Tool ${tool.metadata.name} (remote:${toolCall.name}) succeeded.`,
|
||||
);
|
||||
logger.log(`Output: ${JSON.stringify(output)}`);
|
||||
assertIsJSONValue(output);
|
||||
const toolOutput: ToolOutput = {
|
||||
tool,
|
||||
input,
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { callTool } from "@llamaindex/core/agent";
|
||||
import type { JSONValue } from "@llamaindex/core/global";
|
||||
import type { ChatMessage, MessageContent } from "@llamaindex/core/llms";
|
||||
import { createMemory, Memory } from "@llamaindex/core/memory";
|
||||
import { PromptTemplate } from "@llamaindex/core/prompts";
|
||||
import { tool } from "@llamaindex/core/tools";
|
||||
import {
|
||||
assertIsJSONValue,
|
||||
stringifyJSONToMessageContent,
|
||||
} from "@llamaindex/core/utils";
|
||||
import { stringifyJSONToMessageContent } from "@llamaindex/core/utils";
|
||||
import { consoleLogger, emptyLogger, type Logger } from "@llamaindex/env";
|
||||
import {
|
||||
createWorkflow,
|
||||
@@ -600,12 +598,22 @@ export class AgentWorkflow implements Workflow {
|
||||
const tool = this.agents
|
||||
.get(toolCall.agentName)
|
||||
?.tools.find((t) => t.metadata.name === toolCall.toolName);
|
||||
if (!tool) {
|
||||
throw new Error(`Tool ${toolCall.toolName} not found`);
|
||||
|
||||
const toolOutput = await callTool(
|
||||
tool,
|
||||
{
|
||||
name: toolCall.toolName,
|
||||
input: toolCall.toolKwargs,
|
||||
id: toolCall.toolId,
|
||||
},
|
||||
this.logger,
|
||||
);
|
||||
|
||||
if (toolOutput.isError) {
|
||||
throw new Error(String(toolOutput.output));
|
||||
}
|
||||
const output = await tool.call(toolCall.toolKwargs);
|
||||
assertIsJSONValue(output);
|
||||
return output;
|
||||
|
||||
return toolOutput.output;
|
||||
}
|
||||
|
||||
private createInitialState(): AgentWorkflowState {
|
||||
|
||||
Reference in New Issue
Block a user