Learning Hub

Explore tutorials, examples, and best practices for building AI agent systems

Categories

  • All Articles
  • LangChain Basics
  • Agent Architecture
  • LangGraph Tutorials
  • Evaluation Systems
  • Case Studies
LangChainJune 5, 2023

Building a Simple ReAct Agent with LangChain

In this tutorial, we'll explore how to create a ReAct agent using LangChain's agent framework. ReAct (Reasoning + Acting) combines reasoning and action to enhance the problem-solving capabilities of language models.

from langchain.agents import AgentType, initialize_agent
from langchain.tools import BaseTool
from langchain.llms import OpenAI

# Define your custom tools
class Calculator(BaseTool):
    name = "Calculator"
    description = "Useful for performing mathematical calculations"
    
    def _run(self, query):
        try:
            return eval(query)
        except Exception as e:
            return f"Error: {e}"
            
    def _arun(self, query):
        raise NotImplementedError("This tool does not support async")

# Initialize the LLM
llm = OpenAI(temperature=0)

# Create the agent
tools = [Calculator()]
agent = initialize_agent(
    tools, 
    llm, 
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)
Read Full Article →
LangGraphMay 22, 2023

Creating a Multi-Agent System with LangGraph

LangGraph extends LangChain with state management and cyclic graphs to create complex agent systems. In this example, we'll create a system with multiple specialized agents that collaborate to solve a complex task.

from langgraph.graph import StateGraph, END
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, AIMessage

# Define our state
class AgentState(TypedDict):
    messages: List[Union[HumanMessage, AIMessage]]
    next: str

# Initialize the state graph
workflow = StateGraph(AgentState)

# Define nodes for the state graph
def researcher_agent(state):
    messages = state["messages"]
    model = ChatOpenAI(temperature=0)
    response = model.invoke(messages)
    return {"messages": messages + [response], "next": "planner"}

workflow.add_node("researcher", researcher_agent)
workflow.add_node("planner", planner_agent)

# Add edges
workflow.add_edge("researcher", "planner")
workflow.add_conditional_edges(
    "planner",
    lambda x: x["next"],
    {
        "executor": "executor",
        "researcher": "researcher",
        "END": END
    }
)
Read Full Article →
LangSmithMay 10, 2023

Building Evaluation Systems with LangSmith

In this tutorial, we'll explore how to use LangSmith to create comprehensive evaluation frameworks for your AI agents, allowing you to catch regressions and improve performance over time.

from langsmith import Client
from langsmith.evaluation import RunEvalConfig
import os

# Initialize the LangSmith client
client = Client()

# Define custom evaluators
def factual_accuracy(run, example):
    # Check if the model's output aligns with known facts
    reference = example.get("reference")
    output = run.outputs.get("output")
    # ... your evaluation logic ...
    return {
        "score": score,
        "reasoning": reasoning
    }

# Create evaluation config
eval_config = RunEvalConfig(
    evaluators=[
        factual_accuracy,
        "correctness",
        "harmfulness"
    ],
    custom_evaluators={
        "factual_accuracy": factual_accuracy
    }
)

# Run the evaluation
results = client.run_evaluation(
    dataset_name="agent_benchmark",
    llm_or_chain=my_agent,
    evaluation=eval_config
)
Read Full Article →
123