Spaces:
Sleeping
Sleeping
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
from typing import List
|
| 2 |
-
from langgraph.graph import StateGraph, MessagesState, END
|
| 3 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 4 |
-
|
| 5 |
-
from langchain_core.messages import SystemMessage, HumanMessage
|
| 6 |
-
from langchain_core.tools import tool
|
| 7 |
-
|
| 8 |
-
from duckduckgo_search import DDGS
|
| 9 |
-
|
| 10 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 11 |
-
from langchain_groq import ChatGroq
|
| 12 |
-
|
| 13 |
-
import os
|
| 14 |
-
|
| 15 |
-
# ---------------------------------------------------------
|
| 16 |
-
# TOOLS
|
| 17 |
-
# ---------------------------------------------------------
|
| 18 |
-
|
| 19 |
-
@tool
|
| 20 |
-
def web_search(query: str) -> str:
|
| 21 |
-
"""Search the web using DuckDuckGo."""
|
| 22 |
-
with DDGS() as ddgs:
|
| 23 |
-
results = list(ddgs.text(query, max_results=3))
|
| 24 |
-
if not results:
|
| 25 |
-
return "No results found."
|
| 26 |
-
return "\n\n".join(r["body"] for r in results)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@tool
|
| 30 |
-
def calculator(expression: str) -> str:
|
| 31 |
-
"""Evaluate a math expression."""
|
| 32 |
-
try:
|
| 33 |
-
return str(eval(expression))
|
| 34 |
-
except Exception:
|
| 35 |
-
return "Error evaluating expression."
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
TOOLS = [web_search, calculator]
|
| 39 |
-
|
| 40 |
-
# ---------------------------------------------------------
|
| 41 |
-
# BUILD GRAPH
|
| 42 |
-
# ---------------------------------------------------------
|
| 43 |
-
|
| 44 |
-
def build_graph(provider: str = "google"):
|
| 45 |
-
"""
|
| 46 |
-
Build and return the LangGraph agent.
|
| 47 |
-
Supported providers: 'google', 'groq'
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
-
# -------------------------------
|
| 51 |
-
# LLM SELECTION
|
| 52 |
-
# -------------------------------
|
| 53 |
-
llm = ChatHuggingFace(
|
| 54 |
-
llm=HuggingFaceEndpoint(
|
| 55 |
-
repo_id="HuggingFaceH4/zephyr-7b-beta",
|
| 56 |
-
temperature=0,
|
| 57 |
-
max_new_tokens=512,
|
| 58 |
-
)
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# -------------------------------
|
| 62 |
-
# ASSISTANT NODE
|
| 63 |
-
# -------------------------------
|
| 64 |
-
def assistant(state: MessagesState):
|
| 65 |
-
return {
|
| 66 |
-
"messages": [
|
| 67 |
-
llm_with_tools.invoke(state["messages"])
|
| 68 |
-
]
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
# -------------------------------
|
| 72 |
-
# GRAPH
|
| 73 |
-
# -------------------------------
|
| 74 |
-
builder = StateGraph(MessagesState)
|
| 75 |
-
|
| 76 |
-
builder.add_node("assistant", assistant)
|
| 77 |
-
builder.add_node("tools", ToolNode(TOOLS))
|
| 78 |
-
|
| 79 |
-
builder.set_entry_point("assistant")
|
| 80 |
-
|
| 81 |
-
builder.add_conditional_edges(
|
| 82 |
-
"assistant",
|
| 83 |
-
tools_condition
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
builder.add_edge("tools", "assistant")
|
| 87 |
-
builder.add_edge("assistant", END)
|
| 88 |
-
|
| 89 |
-
return builder.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|