Skip to main content

Create a problem-solving agent

Developing agents in Langchain is complex.

The AgentComponent is a component for easily creating an AI agent capable of analyzing tasks using tools you provide.

The component contains all of the elements you'll need for creating an agent. Instead of managing LLM models and providers, pick your model and enter your API key. Instead of connecting a Prompt component, enter instructions in the component's Agent Instruction fields.

Prompt component

Learn how to build a flow starting with the Tool calling agent component, and see how it can help you solve problems.

Prerequisites​

Create a problem-solving agent with AgentComponent​

Create a problem-solving agent in Langflow, starting with the Tool calling agent.

  1. Click New Flow, and then click Blank Flow.
  2. Click and drag an Agent component to your workspace. The default settings are acceptable for now, so this guide assumes you're using Open AI for the LLM.
  3. Add your Open AI API Key to the Agent component.
  4. Add Chat input and Chat output components to your flow, and connect them to the tool calling agent.
Chat with agent component

This basic flow enables you to chat with the agent with the Playground after you've connected some Tools.

  1. Connect the Search API tool component to your agent.
  2. Add your Search API key to the component. Your agent can now query the Search API for information.
  3. Connect a Calculator tool for solving basic math problems.
  4. Connect an API Request component to the agent. This component is not in the Tools category, but the agent can still use it as a tool by enabling Tool Mode. Tool Mode makes a component into a tool by adding a Toolset port that can be connected to an agent's Tools port. To enable Tool Mode on the component, click Tool Mode. The component's fields change dynamically based on the mode it's in.
Chat with agent component

Solve problems with the agent​

Your agent now has tools for performing a web search, doing basic math, and performing API requests. You can solve many problems with just these capabilities.

  • Your tabletop game group cancelled, and you're stuck at home. Point API Request to an online rules document, tell your agent You are a fun game organizer who uses the tools at your disposal, and play a game.
  • You need to learn a new software language quickly. Point API Request to some docs, tell your agent You are a knowledgeable software developer who uses the tools at your disposal, and start learning.

See what problems you can solve with this flow. As your problem becomes more specialized, add a tool. For example, the simple agent starter project adds a Python REPL component to solve math problems that are too challenging for the calculator.

Use an agent as a tool​

The agent component itself also supports Tool Mode for creating multi-agent flows.

Add an agent to your problem-solving flow that uses a different OpenAI model for more specialized problem solving.

  1. Click and drag an Agent component to your workspace.
  2. Add your Open AI API Key to the Agent component.
  3. In the Model Name field, select gpt-4o.
  4. Click Tool Mode to use this new agent as a tool.
  5. Connect the new agent's Toolset port to the previously created agent's Tools port.
  6. Connect Search API and API Request to the new agent. The new agent will use gpt-4o for the larger tasks of scraping and searching information that requires large context windows. The problem-solving agent will now use this agent as a tool, with its unique LLM and toolset.
Chat with agent component

Add custom components as tools​

An agent can use custom components as tools.

  1. To add a custom component to the problem-solving agent flow, click New Custom Component.

  2. Add custom Python code to the custom component. Here's an example text analyzer for sentiment analysis.


_50
from langflow.custom import Component
_50
from langflow.io import MessageTextInput, Output
_50
from langflow.schema import Data
_50
import re
_50
_50
class TextAnalyzerComponent(Component):
_50
display_name = "Text Analyzer"
_50
description = "Analyzes and transforms input text."
_50
documentation: str = "http://docs.langflow.org/components/custom"
_50
icon = "chart-bar"
_50
name = "TextAnalyzerComponent"
_50
_50
inputs = [
_50
MessageTextInput(
_50
name="input_text",
_50
display_name="Input Text",
_50
info="Enter text to analyze",
_50
value="Hello, World!",
_50
tool_mode=True,
_50
),
_50
]
_50
_50
outputs = [
_50
Output(display_name="Analysis Result", name="output", method="analyze_text"),
_50
]
_50
_50
def analyze_text(self) -> Data:
_50
text = self.input_text
_50
_50
# Perform text analysis
_50
word_count = len(text.split())
_50
char_count = len(text)
_50
sentence_count = len(re.findall(r'\w+[.!?]', text))
_50
_50
# Transform text
_50
reversed_text = text[::-1]
_50
uppercase_text = text.upper()
_50
_50
analysis_result = {
_50
"original_text": text,
_50
"word_count": word_count,
_50
"character_count": char_count,
_50
"sentence_count": sentence_count,
_50
"reversed_text": reversed_text,
_50
"uppercase_text": uppercase_text
_50
}
_50
_50
data = Data(value=analysis_result)
_50
self.status = data
_50
return data

  1. To enable the custom component as a tool, click Tool Mode.
  2. Connect the tool output to the agent's tools input.
  3. Ask the agent, What tools are you using to answer my questions? Your response will be similar to the following, and will include your custom component.

_10
I have access to several tools that assist me in answering your questions, including:
_10
Search API: This allows me to search for recent information or results on the web.
_10
HTTP Requests: I can make HTTP requests to various URLs to retrieve data or interact with APIs.
_10
Calculator: I can evaluate basic arithmetic expressions.
_10
Text Analyzer: I can analyze and transform input text.
_10
Current Date and Time: I can retrieve the current date and time in various time zones.

  1. Click Check & Save.

Your component now has a Tool Mode button, and can be used by an agent.

Make any component a tool​

These components support Tool Mode:

  • URL
  • API request
  • Calculator
  • Current date

If the component you want to use as a tool doesn't have a Tool Mode button, add tool_mode=True to the component's code under MessageTextInput.

For example, in the components as tools example above, tool_mode=True, is added so the custom component can be used as a tool.

Tool Mode supports the MessageTextInput type.


_10
inputs = [
_10
MessageTextInput(
_10
name="input_text",
_10
display_name="Input Text",
_10
info="Enter text to analyze",
_10
value="Hello, World!",
_10
tool_mode=True,
_10
),
_10
]

Add flows as tools​

An agent can use flows that are saved in your workspace as tools.

  1. To add a Flow as tool component, click and drag a Flow as tool component to your workspace.
  2. Select the flow you want the agent to use as a tool.
  3. Connect the tool output to the agent's tools input.
  4. Ask the agent, What tools are you using to answer my questions? Your Flow as tool flow should be visible in the response.

Hi, how can I help you?