agent_with_tools.py


Overview

The agent_with_tools.py file implements an Agent component designed to perform complex task-oriented conversations by leveraging a Large Language Model (LLM) combined with multiple tool plugins. This agent orchestrates multi-turn dialogue sessions, interacts with external tools or microservices, manages conversation memory, and generates context-aware responses for user prompts.

Key functionalities include:

This file integrates tightly with other system components such as LLM services, tenant-specific LLM configurations, MCP servers, and the canvas framework that manages component graphs and memory.


Detailed Explanation of Classes and Methods

Class: AgentParam(LLMParam, ToolParamBase)

Defines the parameters and metadata for configuring an Agent instance.

Attributes:

Usage Example:

param = AgentParam()
param.user_prompt = "Explain quantum computing."
param.reasoning = "User wants a detailed explanation."
param.context = "Relevant scientific background."
param.max_rounds = 7

Class: Agent(LLM, ToolBase)

The main agent class that extends LLM capabilities and integrates tool-based function calling.

Initialization: __init__(self, canvas, id, param: LLMParam)

Initialization Steps:


Method: _load_tool_obj(self, cpn: dict) -> object

Loads a tool component object given a component configuration dictionary.

Raises exceptions if the configuration is invalid.


Method: get_meta(self) -> dict[str, Any]

Returns the metadata dictionary for this agent, including function parameters and descriptions. Injects the current user prompt if available.


Method: get_input_form(self) -> dict[str, dict]

Returns a dictionary describing the input form schema this agent accepts, including inputs from all sub-tools that are LLM-based.


Method: _invoke(self, **kwargs)

Main entry point to execute the agent with the given inputs.

Returns the final answer string.

Timeout: Decorated with a configurable timeout (default 20 minutes).


Method: stream_output_with_tools(self, prompt, msg, user_defined_prompt={})

Streams agent responses incrementally while supporting tool calls.


Method: _gen_citations(self, text)

Generates citation text for the provided text using retrieved knowledge chunks.


Method: _react_with_tools_streamly(self, prompt, history: list[dict], use_tools, user_defined_prompt={})

Core interaction loop for multi-round reasoning with dynamic tool invocation.


Method: get_useful_memory(self, goal: str, sub_goal:str, topn=3, user_defined_prompt:dict={}) -> str

Retrieves and ranks conversational memories relevant to the current goal and sub-goal.


Important Implementation Details and Algorithms


System Interaction and Integration


Usage Example

from agent_with_tools import Agent, AgentParam

# Initialize agent parameters
param = AgentParam()
param.user_prompt = "Summarize the latest market trends."
param.reasoning = "User requires a concise and up-to-date summary."
param.context = "Provide background on recent stock movements."
param.max_rounds = 3

# Simulated canvas and component ID
canvas = ...  # Provided by execution environment
component_id = "agent_001"

# Create agent instance
agent = Agent(canvas, component_id, param)

# Invoke the agent with input parameters
response = agent._invoke(
    user_prompt=param.user_prompt,
    reasoning=param.reasoning,
    context=param.context
)

print("Agent response:", response)

Mermaid Class Diagram

classDiagram
    class AgentParam {
        +meta: ToolMeta
        +function_name: str
        +tools: list
        +mcp: list
        +max_rounds: int
        +description: str
        +__init__()
    }

    class Agent {
        +component_name: str
        +tools: dict
        +chat_mdl: LLMBundle
        +tool_meta: list
        +callback: function
        +toolcall_session: LLMToolPluginCallSession
        +__init__(canvas, id, param)
        +_load_tool_obj(cpn) object
        +get_meta() dict
        +get_input_form() dict
        +_invoke(**kwargs) string
        +stream_output_with_tools(prompt, msg, user_defined_prompt) generator
        +_gen_citations(text) generator
        +_react_with_tools_streamly(prompt, history, use_tools, user_defined_prompt) generator
        +get_useful_memory(goal, sub_goal, topn, user_defined_prompt) string
    }

    AgentParam <|-- Agent
    Agent --|> LLM
    Agent --|> ToolBase

Summary

The agent_with_tools.py file provides a powerful and extensible Agent implementation that integrates LLM-based reasoning with tool invocation capabilities. It is designed for complex multi-turn tasks requiring access to external knowledge, tools, and services, while maintaining a conversational memory and delivering streaming results. The architecture supports modular tool plugins, MCP microservices, and tenant-aware LLM configurations, making it a key component in the InfiniFlow AI orchestration platform.