Spaces:
Runtime error
Runtime error
abtsousa
commited on
Commit
·
f37e95b
1
Parent(s):
8a46dc1
Add initial implementation of LangGraph agent with model and tools integration.
Browse files- agent/agent.py +47 -0
- tools/__init__.py +23 -0
agent/agent.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal
|
| 2 |
+
from typing_extensions import TypedDict
|
| 3 |
+
from langgraph.graph import StateGraph, START, END
|
| 4 |
+
from langgraph.prebuilt import tools_condition
|
| 5 |
+
from agent.nodes import call_model, tool_node
|
| 6 |
+
from agent.state import State
|
| 7 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_agent():
|
| 11 |
+
"""
|
| 12 |
+
Get our LangGraph agent with the given model and tools.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
class GraphConfig(TypedDict):
|
| 16 |
+
app_name: str
|
| 17 |
+
|
| 18 |
+
graph = StateGraph(State, context_schema=GraphConfig)
|
| 19 |
+
|
| 20 |
+
# Add nodes
|
| 21 |
+
graph.add_node("agent", call_model)
|
| 22 |
+
graph.add_node("tools", tool_node)
|
| 23 |
+
|
| 24 |
+
graph.add_edge(START, "agent")
|
| 25 |
+
graph.add_conditional_edges("agent", tools_condition)
|
| 26 |
+
graph.add_edge("tools", "agent")
|
| 27 |
+
|
| 28 |
+
return graph.compile()
|
| 29 |
+
|
| 30 |
+
# test
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
import os
|
| 33 |
+
|
| 34 |
+
question = "When was Andrej Karpathy born?"
|
| 35 |
+
messages = [HumanMessage(content=question)]
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
graph = get_agent()
|
| 39 |
+
from agent.config import create_agent_config
|
| 40 |
+
config = create_agent_config("OracleBot")
|
| 41 |
+
result = graph.invoke({"messages": messages}, config)
|
| 42 |
+
print("\n=== Agent Response ===")
|
| 43 |
+
for m in result["messages"]:
|
| 44 |
+
m.pretty_print()
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error running agent: {e}")
|
| 47 |
+
print("Make sure your API key is correct and the service is accessible.")
|
tools/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .wikipedia import wiki_search
|
| 2 |
+
from langchain_core.tools import StructuredTool
|
| 3 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 4 |
+
from langchain_core.tools import render_text_description_and_args
|
| 5 |
+
from langchain_core.tools import tool
|
| 6 |
+
|
| 7 |
+
def get_all_tools() -> list[StructuredTool]:
|
| 8 |
+
"""
|
| 9 |
+
Get all available tools as a list of LangChain StructuredTool instances.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
List of StructuredTool instances ready for use with LangChain agents
|
| 13 |
+
"""
|
| 14 |
+
tools = []
|
| 15 |
+
|
| 16 |
+
# Add Wikipedia tool
|
| 17 |
+
wikipedia_tool = wiki_search
|
| 18 |
+
tools.append(wikipedia_tool)
|
| 19 |
+
|
| 20 |
+
return tools
|
| 21 |
+
|
| 22 |
+
def list_tools() -> str:
|
| 23 |
+
return render_text_description_and_args(get_all_tools()) # type: ignore
|