Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,64 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
Copied
|
| 8 |
+
from typing import TypedDict, Annotated
|
| 9 |
+
from langgraph.graph.message import add_messages
|
| 10 |
+
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
| 11 |
+
from langgraph.graph import StateGraph, START, END
|
| 12 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 13 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 14 |
+
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
+
class AgentState(TypedDict):
|
| 22 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 23 |
+
|
| 24 |
class BasicAgent:
|
| 25 |
def __init__(self):
|
| 26 |
print("BasicAgent initialized.")
|
| 27 |
+
|
| 28 |
+
llm = HuggingFaceEndpoint(
|
| 29 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 30 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
search_tool = DuckDuckGoSearchRun()
|
| 34 |
+
tools = [search_tool]
|
| 35 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 36 |
+
|
| 37 |
+
def assistant(state: AgentState):
|
| 38 |
+
return {
|
| 39 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
## The graph
|
| 43 |
+
builder = StateGraph(AgentState)
|
| 44 |
+
|
| 45 |
+
# Define nodes: these do the work
|
| 46 |
+
builder.add_node("assistant", assistant)
|
| 47 |
+
builder.add_node("tools", ToolNode(tools))
|
| 48 |
+
|
| 49 |
+
# Define edges: these determine how the control flow moves
|
| 50 |
+
builder.add_edge(START, "assistant")
|
| 51 |
+
builder.add_conditional_edges(
|
| 52 |
+
"assistant",
|
| 53 |
+
# If the latest message requires a tool, route to tools
|
| 54 |
+
# Otherwise, provide a direct response
|
| 55 |
+
tools_condition,
|
| 56 |
+
)
|
| 57 |
+
builder.add_edge("tools", "assistant")
|
| 58 |
+
self.agent = builder.compile()
|
| 59 |
+
|
| 60 |
def __call__(self, question: str) -> str:
|
| 61 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 62 |
+
|
| 63 |
+
messages = [HumanMessage(content=question)]
|
| 64 |
+
answer = self.agent.invoke({"messages": messages})
|
| 65 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 66 |
return fixed_answer
|
| 67 |
|