Creating Agents - janardhanhere/langgraph-deployment-kit GitHub Wiki

Creating Agents

This guide explains how to create and register new LangGraph agents in the Deployment Kit.

Structure

Agents are defined in the src/agents directory and registered in src/agents/agents.py.

Creating a New Agent

  1. Create a new Python file in src/agents (e.g., my_agent.py)
  2. Define your agent using LangGraph:
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain.chat_models import init_chat_model

# Define your agent's state
class State(TypedDict):
    messages: Annotated[list, add_messages]
    # Add any additional state fields

# Create the graph
graph_builder = StateGraph(State)

# Initialize your LLM
llm = init_chat_model("openai:gpt-4-turbo")  # Or your preferred model

# Define nodes
async def process(state: State):
    return {"messages": [await llm.ainvoke(state["messages"])]}

# Add nodes
graph_builder.add_node("process", process)

# Add edges
graph_builder.add_edge(START, "process")
graph_builder.add_edge("process", END)

# Compile the graph
my_agent = graph_builder.compile()

Registering Your Agent

Open src/agents/agents.py and register your agent:

from agents.my_agent import my_agent

# Update the agents dictionary
agents = {
    # Existing agents
    "research-assistant": Agent(
        description="A research assistant with web search and calculator.",
        graph=research_assistant
    ),
    # Your new agent
    "my-agent": Agent(
        description="Description of your agent", 
        graph=my_agent
    )
}

Setting as Default Agent

To make your agent the default, update the DEFAULT_AGENT variable in src/agents/agents.py:

DEFAULT_AGENT = "my-agent"

Testing Your Agent

You can test your agent directly:

python src/run_agent.py

Or start the service and test via API:

python src/run_service.py