FeaturesAgents

Introduction to Agents

In AgentForge, agents are the core components that drive the functionality of your AI applications. An agent is essentially a combination of a Large Language Model (LLM) and various tools that enable it to perform specific tasks. Agents are designed to accept a given state, process it, and return a partial state that merges with the original, allowing for dynamic and flexible workflows.

Creating Agents

Creating agents in AgentForge is straightforward, thanks to the provided helper functions. Let's dive into an example to understand how to create and utilize an agent effectively.

Example: Researcher Agent

The Researcher agent is designed to search the web for useful information using the Tavily search tool. This agent's primary goal is to gather information that other team members can utilize.
Here's the complete example of creating a Researcher agent:

ai/agents/researcher.ts

1import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
2import { HumanMessage } from '@langchain/core/messages';
3import { Agent, createAgent } from './base';
4import { AgentStateChannels } from '../state';
5import llm from './llm';
6
7const tavilyTool = new TavilySearchResults();
8const name = 'Researcher';
9
10export const createResearcher = async (): Promise<Agent> => {
11    const researcherAgent = await createAgent(
12        llm,
13        [tavilyTool],
14        'You are a web researcher. You may use the Tavily search engine to search the web for important information, so that other members in your team can make use of the information.'
15    );
16
17    return {
18        name,
19        goal: 'Searches the web for useful information, and provides results so they can be used by other team members',
20        runnable: async (state: AgentStateChannels, config: any) => {
21            const result = await researcherAgent.invoke(state, config);
22            return {
23                messages: [new HumanMessage({ content: result.output, name })],
24            };
25        },
26    };
27};

Key Components of the Agent

  1. Tools Integration: The Researcher agent utilizes the Tavily search tool to perform web searches. This tool is integrated by creating an instance of TavilySearchResults.
  1. Agent Creation: The createAgent helper function combines the LLM and the Tavily search tool to form the Researcher agent. It also includes a prompt that guides the agent's behavior.
  1. Agent Definition: The agent is defined with a name, goal, and a runnable function. The runnable function is responsible for processing the passed state and returning a partial state.
  1. State Processing: Within the runnable function, the agent invokes the LLM and tool using the passed state and configuration. The result is then formatted as a HumanMessage and returned as part of the partial state.

Using Agents in Workflows

Agents in AgentForge are designed to be modular and reusable. They can be easily incorporated into workflows where they process the current state and contribute to the overall goal. By accepting and returning state, agents facilitate seamless integration and interaction within complex workflows.

Workflow Integration

In a typical workflow, the state is passed to the agent, which processes it and returns a partial state. This partial state is then merged with the original state, allowing the workflow to progress dynamically based on the agent's output.

Conclusion

The Agents feature in AgentForge provides a powerful and flexible way to build AI-driven applications. By combining LLMs with various tools, agents can perform a wide range of tasks, from web searching to data analysis. The ease of creating and integrating agents into workflows makes AgentForge a robust platform for developing sophisticated AI solutions.
Explore the full potential of agents in AgentForge and see how they can enhance your AI applications. Happy building with AgentForge!