Spaces:
Sleeping
Sleeping
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,180 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
LangGraph Agent Implementation
|
| 3 |
-
This file contains the build_graph() function that creates your agent.
|
| 4 |
-
Customize this with your own tools, logic, and LLM integration.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from langgraph.graph import StateGraph, START, END
|
| 8 |
-
from langchain_core.messages import BaseMessage, AIMessage
|
| 9 |
-
from typing import Annotated
|
| 10 |
-
from typing_extensions import TypedDict
|
| 11 |
-
import operator
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Define the state schema for your agent
|
| 15 |
-
class AgentState(TypedDict):
|
| 16 |
-
"""State passed through the agent graph."""
|
| 17 |
-
messages: Annotated[list[BaseMessage], operator.add]
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def build_graph():
|
| 21 |
-
"""
|
| 22 |
-
Build and compile your LangGraph agent graph.
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
A compiled LangGraph graph that can process messages.
|
| 26 |
-
|
| 27 |
-
The graph should:
|
| 28 |
-
- Accept input: {"messages": [HumanMessage(...)]}
|
| 29 |
-
- Return output: {"messages": [HumanMessage(...), AIMessage(...)]}
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
# Create the state graph
|
| 33 |
-
graph = StateGraph(AgentState)
|
| 34 |
-
|
| 35 |
-
# ============================================
|
| 36 |
-
# DEFINE YOUR NODES HERE
|
| 37 |
-
# ============================================
|
| 38 |
-
|
| 39 |
-
def agent_node(state: AgentState) -> dict:
|
| 40 |
-
"""
|
| 41 |
-
Main agent processing node.
|
| 42 |
-
This is where your agent logic goes.
|
| 43 |
-
|
| 44 |
-
Args:
|
| 45 |
-
state: Current conversation state with messages
|
| 46 |
-
|
| 47 |
-
Returns:
|
| 48 |
-
Updated state with agent's response
|
| 49 |
-
"""
|
| 50 |
-
messages = state["messages"]
|
| 51 |
-
last_message = messages[-1]
|
| 52 |
-
|
| 53 |
-
# TODO: Replace this with your actual agent logic
|
| 54 |
-
# Examples:
|
| 55 |
-
# - Call an LLM
|
| 56 |
-
# - Process the question
|
| 57 |
-
# - Use tools
|
| 58 |
-
# - Return a response
|
| 59 |
-
|
| 60 |
-
response_text = f"Echo: {last_message.content}"
|
| 61 |
-
|
| 62 |
-
# Create the agent's response
|
| 63 |
-
agent_response = AIMessage(content=response_text)
|
| 64 |
-
|
| 65 |
-
return {"messages": [agent_response]}
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# ============================================
|
| 69 |
-
# OPTIONAL: ADD MORE NODES FOR COMPLEX LOGIC
|
| 70 |
-
# ============================================
|
| 71 |
-
|
| 72 |
-
# Example: Tool execution node
|
| 73 |
-
def tool_node(state: AgentState) -> dict:
|
| 74 |
-
"""
|
| 75 |
-
Optional node for executing tools.
|
| 76 |
-
Implement if your agent uses external tools.
|
| 77 |
-
"""
|
| 78 |
-
# Placeholder for tool execution
|
| 79 |
-
return {"messages": []}
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# ============================================
|
| 83 |
-
# BUILD THE GRAPH
|
| 84 |
-
# ============================================
|
| 85 |
-
|
| 86 |
-
# Add nodes to the graph
|
| 87 |
-
graph.add_node("agent", agent_node)
|
| 88 |
-
# graph.add_node("tools", tool_node) # Uncomment if using tools
|
| 89 |
-
|
| 90 |
-
# Define edges (connections between nodes)
|
| 91 |
-
graph.add_edge(START, "agent")
|
| 92 |
-
graph.add_edge("agent", END)
|
| 93 |
-
# graph.add_edge("agent", "tools") # Uncomment if using tools
|
| 94 |
-
# graph.add_edge("tools", "agent")
|
| 95 |
-
|
| 96 |
-
# Compile the graph
|
| 97 |
-
return graph.compile()
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# ============================================
|
| 101 |
-
# EXAMPLE IMPLEMENTATIONS (uncomment to use)
|
| 102 |
-
# ============================================
|
| 103 |
-
|
| 104 |
-
def build_graph_with_llm():
|
| 105 |
-
"""
|
| 106 |
-
Example: Agent with LLM integration.
|
| 107 |
-
Requires: pip install langchain-openai (or another LLM provider)
|
| 108 |
-
"""
|
| 109 |
-
from langchain_openai import ChatOpenAI
|
| 110 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 111 |
-
|
| 112 |
-
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
| 113 |
-
|
| 114 |
-
graph = StateGraph(AgentState)
|
| 115 |
-
|
| 116 |
-
def llm_agent(state: AgentState) -> dict:
|
| 117 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 118 |
-
("system", "You are a helpful assistant."),
|
| 119 |
-
("user", "{input}"),
|
| 120 |
-
])
|
| 121 |
-
|
| 122 |
-
chain = prompt | llm
|
| 123 |
-
last_message = state["messages"][-1]
|
| 124 |
-
|
| 125 |
-
response = chain.invoke({"input": last_message.content})
|
| 126 |
-
|
| 127 |
-
return {"messages": [response]}
|
| 128 |
-
|
| 129 |
-
graph.add_node("llm", llm_agent)
|
| 130 |
-
graph.add_edge(START, "llm")
|
| 131 |
-
graph.add_edge("llm", END)
|
| 132 |
-
|
| 133 |
-
return graph.compile()
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def build_graph_with_tools():
|
| 137 |
-
"""
|
| 138 |
-
Example: Agent with tool use.
|
| 139 |
-
Requires: pip install langchain-openai
|
| 140 |
-
"""
|
| 141 |
-
from langchain_openai import ChatOpenAI
|
| 142 |
-
from langchain_core.tools import tool
|
| 143 |
-
from langgraph.prebuilt import create_react_agent
|
| 144 |
-
|
| 145 |
-
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
| 146 |
-
|
| 147 |
-
@tool
|
| 148 |
-
def add(a: int, b: int) -> int:
|
| 149 |
-
"""Add two numbers."""
|
| 150 |
-
return a + b
|
| 151 |
-
|
| 152 |
-
@tool
|
| 153 |
-
def multiply(a: int, b: int) -> int:
|
| 154 |
-
"""Multiply two numbers."""
|
| 155 |
-
return a * b
|
| 156 |
-
|
| 157 |
-
tools = [add, multiply]
|
| 158 |
-
|
| 159 |
-
# Use the pre-built ReAct agent
|
| 160 |
-
return create_react_agent(llm, tools)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
# ============================================
|
| 164 |
-
# FOR TESTING (run this file directly)
|
| 165 |
-
# ============================================
|
| 166 |
-
|
| 167 |
-
if __name__ == "__main__":
|
| 168 |
-
from langchain_core.messages import HumanMessage
|
| 169 |
-
|
| 170 |
-
# Build your graph
|
| 171 |
-
graph = build_graph()
|
| 172 |
-
|
| 173 |
-
# Test with a sample question
|
| 174 |
-
test_question = "What is 2 + 2?"
|
| 175 |
-
messages = [HumanMessage(content=test_question)]
|
| 176 |
-
|
| 177 |
-
result = graph.invoke({"messages": messages})
|
| 178 |
-
|
| 179 |
-
print(f"Question: {test_question}")
|
| 180 |
-
print(f"Agent Response: {result['messages'][-1].content}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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