File size: 1,402 Bytes
f37e95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Literal
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import tools_condition
from agent.nodes import call_model, tool_node
from agent.state import State
from langchain_core.messages import AIMessage, HumanMessage


def get_agent():
    """
    Get our LangGraph agent with the given model and tools.
    """
   
    class GraphConfig(TypedDict):
        app_name: str

    graph = StateGraph(State, context_schema=GraphConfig)
    
    # Add nodes
    graph.add_node("agent", call_model)
    graph.add_node("tools", tool_node)
    
    graph.add_edge(START, "agent")
    graph.add_conditional_edges("agent", tools_condition)
    graph.add_edge("tools", "agent")
        
    return graph.compile()

# test
if __name__ == "__main__":
    import os
    
    question = "When was Andrej Karpathy born?"
    messages = [HumanMessage(content=question)]
    
    try:
        graph = get_agent()
        from agent.config import create_agent_config
        config = create_agent_config("OracleBot")
        result = graph.invoke({"messages": messages}, config)
        print("\n=== Agent Response ===")
        for m in result["messages"]:
            m.pretty_print()
    except Exception as e:
        print(f"Error running agent: {e}")
        print("Make sure your API key is correct and the service is accessible.")