LangChain
This section contains implementation using LangGraph.
Getting Started
Prerequisites
Poetry (for dependency management)
Setup
Clone the repository:
git clone https://github.com/GDP-ADMIN/gen-ai-examples.git
Install dependencies:
poetry install
Setup OpenAPI key
export OPENAI_API_KEY='your-openai-api-key-here'
Basic implementation
We can use LangChainAgent to utilize LangChain's framework using our Agent Interface.
from gllm_agents.agent.langchain_agent import LangChainAgent
from langchain_openai import ChatOpenAI
from aip_agent_quickstart.config import CALCULATOR_AGENT_INSTRUCTION
from aip_agent_quickstart.tools import langchain_add_numbers
agent = LangChainAgent(
name="LangChainArithmeticAgent",
instruction=CALCULATOR_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
tools=[langchain_add_numbers],
)
response = agent.run(query="What is the sum of 23 and 47? And then add 10 to that, then add 5 more.")
print(response["output"])
Running the Agent
You can run the following to test running the agent;
poetry run python hello_world_langchain.py
You should see the following output:
The sum of 23 and 47 is 70. Adding 10 gives 80, and adding 5 more gives a final result of 85.
Multi-agent
We provide multi-agent capabilities out of the box for LangChain agents. The following example showcases:
How to define multiple specialized agents (
WeatherAgent
,MathAgent
).How to set up a
CoordinatorAgent
that can delegate to these specialized agents.How the
CoordinatorAgent
uses dynamically created tools to call sub-agents.How the
CoordinatorAgent
can delegate tasks to the appropriate sub-agents.
from gllm_agents.agent.langchain_agent import LangChainAgent
from langchain_openai import ChatOpenAI
from aip_agent_quickstart.config import (
COORDINATOR_MULTI_AGENT_INSTRUCTION,
MATH_AGENT_INSTRUCTION,
WEATHER_AGENT_INSTRUCTION,
)
from aip_agent_quickstart.tools import langchain_add_numbers, langchain_weather_tool
weather_agent = LangChainAgent(
name="WeatherAgent",
instruction=WEATHER_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
tools=[langchain_weather_tool],
)
math_agent = LangChainAgent(
name="MathAgent",
instruction=MATH_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
tools=[langchain_add_numbers],
)
coordinator_agent = LangChainAgent(
name="CoordinatorAgent",
instruction=COORDINATOR_MULTI_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
agents=[weather_agent, math_agent],
)
response = coordinator_agent.run(query="What is the weather in Tokyo and what is 5 + 7?")
print(response.get("output"))
Running the Agent
You can run the following to test the capability:
poetry run python hello_world_multi_agent_langchain.py
You should see the following output:
[INFO] Coordinator asynchronously delegating to agent 'WeatherAgent' with query: What is the weather in Tokyo?
[INFO] Coordinator asynchronously delegating to agent 'MathAgent' with query: What is 5 + 7?
[INFO] Agent 'WeatherAgent' asynchronously responded: The weather in Tokyo is currently 25°C with clear skies.
[INFO] Agent 'MathAgent' asynchronously responded: 5 + 7 is 12.
Response: The weather in Tokyo is currently 25°C with clear skies. Also, 5 + 7 is 12.
MCP
We have the capability to connect to MCP client for LangChain agents.
Start the MCP server
In a new terminal window, execute this
poetry run python aip_agent_quickstart/mcp_servers/mcp_server_stdio.py
Upon success, the window remains open without displaying any output.
Run agent using MCP-based tools
The following is how we can configure our LangChain Agent to run using MCP-based tools.
from gllm_agents.agent.langchain_agent import LangChainAgent
from langchain_openai import ChatOpenAI
from aip_agent_quickstart.config import DEFAULT_AGENT_INSTRUCTION
from aip_agent_quickstart.mcp_configs.configs import mcp_config_stdio
langchain_agent = LangChainAgent(
name="langchain_mcp_example",
instruction=DEFAULT_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
)
langchain_agent.add_mcp_server(mcp_config_stdio)
response = langchain_agent.run(query="What's the weather forecast for monday?")
print(f"Response: {response.get('output')}")
To test the agent, in the current terminal window run the following command:
poetry run python hello_world_langchain_mcp_stdio.py
You should see the following output:
Attempting to connect to MCP server 'weather_tools'...
Successfully connected to MCP server 'weather_tools'...
Agent 'langchain_mcp_example': Retrieved 1 tools from MCP server.
Response: The weather forecast for Monday is sunny, with temperatures ranging between 28°C and 32°C.
A2A (Agent-to-Agent)
LangChain agents can act as A2A agents and can also coordinate agents hosted on remote A2A servers.
Deploy Agent as A2A server
To initiate the specialized Weather Agent server, follow one of these instruction(s):
Start the Server
To start the Weather Agent server using Python and Poetry, first we need to open a new terminal window. Then export OpenAI API key:
export OPENAI_API_KEY='your-openai-api-key-here'
export framework='langchain'
Then run this command from your project's root directory:
poetry run python aip_agent_quickstart/agents/weather_agent/server.py
By default, the server will start and listen on http://localhost:8001
You can customize the host and port using the --host
and --port
command-line options.
This script performs the following key actions:
Imports
weather_tool
.Builds a
Agent Card
with metadata.Wraps a LangChain agent as an A2A app.
Call Agents via the A2A client
Once your Weather Agent server runs, then attempt one of the following approaches:
The following is how we can call A2A agent with non-streaming mode
from gllm_agents.agent.langchain_agent import LangChainAgent
from gllm_agents.agent.types import A2AClientConfig
from langchain_openai import ChatOpenAI
from aip_agent_quickstart.config import DEFAULT_AGENT_INSTRUCTION
agent = LangChainAgent(
name="AssistantAgentLangChain",
instruction=DEFAULT_AGENT_INSTRUCTION,
model=ChatOpenAI(model="gpt-4.1", temperature=0),
)
client_a2a_config = A2AClientConfig(discovery_urls=["http://localhost:8001"])
agent_cards = agent.discover_agents(client_a2a_config)
response = agent.send_to_agent(agent_cards[0], message="What is the weather in Jakarta?")
print(response.get("content", str(response)))
Test the script by running
poetry run python hello_world_a2a_langchain_client.py
These scripts will do the following:
Instantiate
LangChainAgent
Discover the Weather Agent at its URL.
Send
What is the weather in Jakarta City?
Receive the full response when it's ready.
Expected Output:
Discovered agents (1 Agents):
1. WeatherAgent
...
The weather in Jakarta City is currently 32°C, partly cloudy, and there is high humidity
Stopping the A2A Server
If the server is running in the foreground (as it will with the command above), press Ctrl+C
in the terminal where it's running.
Last updated