kingkw1 commited on
Commit
ff8b7ff
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files

first pass with websearch

Files changed (1) hide show
  1. app.py +46 -3
app.py CHANGED
@@ -3,6 +3,15 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -10,14 +19,48 @@ 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
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from langchain_core.tools import tool
7
+ from langchain_core.messages import add_message
8
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
9
+ from langchain_community.tools import DuckDuckGoSearchRun
10
+ from typing import TypedDict, Annotated
11
+ from langgraph.graph.message import add_message
12
+ from langgraph.graph import START, StateGraph
13
+ from langgraph.prebuilt import tools_condition
14
+
15
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
 
19
 
20
  # --- Basic Agent Definition ---
21
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
22
+ llm = HuggingFaceEndpoint(
23
+ repo_id = "Qwen/Qwen2.5-Coder-32B-Instruct",
24
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
25
+ )
26
+
27
+ chat = ChatHuggingFace(llm=llm, verbose=True)
28
+
29
+ search_tool = DuckDuckGoSearchRun()
30
+
31
+ tools = [search_tool]
32
+ chat_with_tools = chat.bind_tools(tools)
33
+
34
+ class AgentState(TypedDict):
35
+ messages:Annotated[list[AnyMessage], add_message]
36
+
37
+ def assistant(state:AgentState):
38
+ return {
39
+ "messages": [chat_with_tools.invoke(state["messages"])]
40
+ }
41
+
42
+ # Graph
43
+ builder = StateGraph(AgentState)
44
+ # Graph Nodes
45
+ builder.add_node("assistant", assistant)
46
+ builder.add_node("tools", ToolNode(toools))
47
+ #Graph Edges
48
+ builder.add_edge(START, "assistant")
49
+ builder.add_conditional_edges(
50
+ "assistant",
51
+ tools_condition
52
+ )
53
+ builder.add_edge("tools", "assistant")
54
+ my_agent = builder.complie()
55
+
56
  class BasicAgent:
57
  def __init__(self):
58
  print("BasicAgent initialized.")
59
  def __call__(self, question: str) -> str:
60
  print(f"Agent received question (first 50 chars): {question[:50]}...")
61
+ response = my_agent.invoke({"messages": messages})
62
+
63
+ return response
64
 
65
  def run_and_submit_all( profile: gr.OAuthProfile | None):
66
  """