Vinay Kerai commited on
Commit
c4236f6
·
1 Parent(s): 81917a3

quick and dirty test

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py CHANGED
@@ -1,15 +1,103 @@
1
  import os
 
2
  import gradio as gr
3
  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.")
 
1
  import os
2
+ import re
3
  import gradio as gr
4
  import requests
5
  import inspect
6
  import pandas as pd
7
 
8
+ from typing import TypedDict, Annotated
9
+ from langgraph.graph.message import add_messages
10
+ from langchain_core.messages import AnyMessage, HumanMessage, AIMessage, SystemMessage
11
+ from langgraph.prebuilt import ToolNode
12
+ from langgraph.graph import START, StateGraph
13
+ from langgraph.prebuilt import tools_condition
14
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
15
+ from langchain_community.tools import DuckDuckGoSearchRun
16
+
17
  # (Keep Constants as is)
18
  # --- Constants ---
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
 
21
  # --- Basic Agent Definition ---
22
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
23
+
24
+ # Generate the AgentState and Agent graph
25
+ class AgentState(TypedDict):
26
+ messages: Annotated[list[AnyMessage], add_messages]
27
+
28
+ # --- Agent ---
29
+ class GAIAAgent:
30
+ # --- LLM ---
31
+ llm = HuggingFaceEndpoint(
32
+ repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
33
+ huggingfacehub_api_token=os.getenv('HF_TOKEN'),
34
+ )
35
+ chat = ChatHuggingFace(llm=llm, verbose=True)
36
+
37
+ # --- Tools ---
38
+ search_tool = DuckDuckGoSearchRun()
39
+ tools = [search_tool]
40
+
41
+ # --- System Prompt ---
42
+ system = """
43
+ You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your
44
+ answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should
45
+ be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If
46
+ you are asked for a number, don't use comma to write your number neither use units such as $ or
47
+ percent sign unless specified otherwise. If you are asked for a string, don't use articles,
48
+ neither abbreviations (e.g. for cities), and write the digits in plain text unless specified
49
+ otherwise. If you are asked for a comma separated list, apply the above rules depending of
50
+ whether the element to be put in the list is a number or a string.
51
+ """.replace('\n', '')
52
+
53
+ def __init__(self):
54
+ self.build_graph()
55
+ print("GAIAAgent initialized.")
56
+
57
+ def build_graph(self):
58
+ self.chat_with_tools = self.chat.bind_tools(self.tools)
59
+
60
+ builder = StateGraph(AgentState)
61
+
62
+ def assistant(state: AgentState):
63
+ return {
64
+ "messages": [self.chat_with_tools.invoke(state["messages"])],
65
+ }
66
+
67
+ # Define nodes: these do the work
68
+ builder.add_node("assistant", assistant)
69
+ builder.add_node("tools", ToolNode(self.tools))
70
+
71
+ # Define edges: these determine how the control flow moves
72
+ builder.add_edge(START, "assistant")
73
+ builder.add_conditional_edges(
74
+ "assistant",
75
+ # If the latest message requires a tool, route to tools
76
+ # Otherwise, provide a direct response
77
+ tools_condition,
78
+ )
79
+ builder.add_edge("tools", "assistant")
80
+ self.graph = builder.compile()
81
+
82
+ def __call__(self, question: str) -> str:
83
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
84
+
85
+ answer = self.graph.invoke(
86
+ {"messages": [
87
+ SystemMessage(content=self.system),
88
+ HumanMessage(content=question)
89
+ ]})
90
+
91
+ # extract answer
92
+ match = re.search(r"(?<=FINAL ANSWER:\s).*", answer)
93
+ if match:
94
+ final_answer = match.group(0)
95
+ else:
96
+ final_answer = "I don't know"
97
+
98
+ print(f"Agent returning answer: {final_answer}")
99
+ return final_answer
100
+
101
  class BasicAgent:
102
  def __init__(self):
103
  print("BasicAgent initialized.")