Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,36 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from typing import Sequence, Annotated, TypedDict, Union
|
| 9 |
-
from langgraph.graph import StateGraph, END
|
| 10 |
-
from langgraph.graph.message import add_messages
|
| 11 |
-
from langgraph.prebuilt import ToolNode
|
| 12 |
-
from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage
|
| 13 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 14 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 15 |
-
from langchain.tools import tool
|
| 16 |
-
from dotenv import load_dotenv
|
| 17 |
-
load_dotenv()
|
| 18 |
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
| 21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
messages: Annotated[Sequence[BaseMessage], add_messages]
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# ----------- Math Tools ------------
|
| 29 |
-
@tool
|
| 30 |
-
def add(a: int, b: int):
|
| 31 |
-
"""Adds two numbers."""
|
| 32 |
-
return a + b
|
| 33 |
-
|
| 34 |
-
@tool
|
| 35 |
-
def subtract(a: int, b: int):
|
| 36 |
-
"""Subtracts two numbers."""
|
| 37 |
-
return a - b
|
| 38 |
-
|
| 39 |
-
@tool
|
| 40 |
-
def multiply(a: int, b: int):
|
| 41 |
-
"""Multiplies two numbers."""
|
| 42 |
-
return a * b
|
| 43 |
|
| 44 |
-
# ----------- DuckDuckGo Tool (LangChain built-in) -----------
|
| 45 |
-
ddg_tool = DuckDuckGoSearchRun(name="duckduckgo_search")
|
| 46 |
|
| 47 |
-
# ----------- Combine all tools -----------
|
| 48 |
-
tools = [add, subtract, multiply, ddg_tool]
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# ----------- BasicAgent Class -----------
|
| 52 |
class BasicAgent:
|
|
|
|
| 53 |
def __init__(self):
|
| 54 |
-
print("
|
| 55 |
-
|
| 56 |
-
self.model = ChatGoogleGenerativeAI(model="gemini-2.0-flash").bind_tools(tools)
|
| 57 |
-
|
| 58 |
-
def model_call(state: AgentState) -> AgentState:
|
| 59 |
-
system_prompt = SystemMessage(
|
| 60 |
-
content="""
|
| 61 |
-
You are an AI assistant. Use tools like math functions and web search (DuckDuckGo) to answer queries.
|
| 62 |
-
You don't have to explain or give reasons just you have to answer the question
|
| 63 |
-
Just reply the question eg.
|
| 64 |
-
if asked about what is capital of France just answer paris nothing more
|
| 65 |
-
"""
|
| 66 |
-
)
|
| 67 |
-
response = self.model.invoke([system_prompt] + state["messages"])
|
| 68 |
-
return {"messages": [response]}
|
| 69 |
-
|
| 70 |
-
def should_continue(state: AgentState):
|
| 71 |
-
last_message = state["messages"][-1]
|
| 72 |
-
if not getattr(last_message, "tool_calls", None):
|
| 73 |
-
return "end"
|
| 74 |
-
return "continue"
|
| 75 |
-
|
| 76 |
-
graph = StateGraph(AgentState)
|
| 77 |
-
graph.add_node("our_agent", model_call)
|
| 78 |
-
graph.add_node("tools", ToolNode(tools=tools))
|
| 79 |
-
graph.set_entry_point("our_agent")
|
| 80 |
-
graph.add_conditional_edges("our_agent", should_continue, {
|
| 81 |
-
"continue": "tools",
|
| 82 |
-
"end": END
|
| 83 |
-
})
|
| 84 |
-
graph.add_edge("tools", "our_agent")
|
| 85 |
-
|
| 86 |
-
self.agent = graph.compile()
|
| 87 |
|
| 88 |
-
def __call__(self,
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
result = self.agent.invoke({"messages": messages})
|
| 97 |
-
last_message = result["messages"][-1]
|
| 98 |
-
return last_message.content if hasattr(last_message, "content") else str(last_message)
|
| 99 |
|
| 100 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 101 |
"""
|
|
|
|
| 1 |
+
""" Basic Agent Evaluation Runner"""
|
| 2 |
import os
|
| 3 |
+
import inspect
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from agent import build_graph
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
+
# (Keep Constants as is)
|
| 13 |
+
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
+
# --- Basic Agent Definition ---
|
| 17 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class BasicAgent:
|
| 21 |
+
"""A langgraph agent."""
|
| 22 |
def __init__(self):
|
| 23 |
+
print("BasicAgent initialized.")
|
| 24 |
+
self.graph = build_graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def __call__(self, question: str) -> str:
|
| 27 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 29 |
+
messages = [HumanMessage(content=question)]
|
| 30 |
+
messages = self.graph.invoke({"messages": messages})
|
| 31 |
+
answer = messages['messages'][-1].content
|
| 32 |
+
return answer[14:]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 36 |
"""
|