qver3nc1a commited on
Commit
f41c0d7
·
verified ·
1 Parent(s): df527ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -52
app.py CHANGED
@@ -21,68 +21,62 @@ import uuid
21
  # (Keep Constants as is)
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
24
 
25
 
26
-
27
- wikipedia = WikipediaAPIWrapper()
28
- def wikipedia_search(query: str) -> str:
29
- return wikipedia.run(query)
30
-
31
  # --- Basic Agent Definition ---
32
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
33
- def web_search(query: str):
34
- """Search the web for information."""
35
- return DuckDuckGoSearchRun().run(query)
36
 
37
- def wikipedia_search(query: str):
38
- """Search Wikipedia for information."""
39
- return wikipedia.run(query)
40
-
41
- HUGGINGFACEHUB_API_TOKEN=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
42
-
43
- class BasicAgent:
44
- def __init__(self, model_id="ollama/qwen2:7b"):
45
- print('BasicAgent initialized.')
46
- self.system_prompt = (
47
- "You are a general AI assistant. I will ask you a question. "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  "Report your thoughts, and finish your answer with the following template: "
49
- "FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
50
  "If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
51
  "If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
52
- "If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
53
- )
54
- web_search_tool = DuckDuckGoSearchRun()
55
- #wikipedia_search_tool = wikipedia_search()
56
- tools = [web_search_tool]
57
- llm = HuggingFaceEndpoint(repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN)
58
-
59
- chat = ChatHuggingFace(llm=llm, verbose=True)
60
- chat_with_tools = chat.bind_tools(tools)
61
-
62
- class AgentState(TypedDict):
63
- messages: Annotated[list[AnyMessage], add_messages]
64
-
65
- def assistant(state: AgentState):
66
- return {
67
- "messages": [chat_with_tools.invoke(state["messages"])],
68
- }
69
-
70
- builder = StateGraph(AgentState)
71
- builder.add_node("assistant", assistant)
72
- builder.add_node("tools", ToolNode(tools))
73
- builder.add_edge(START, "assistant")
74
- builder.add_conditional_edges("assistant", tools_condition)
75
- builder.add_edge("tools", "assistant")
76
- self.alfred = builder.compile()
77
 
78
  def __call__(self, question: str) -> str:
79
- messages = [
80
- SystemMessage(content=self.system_prompt),
81
- HumanMessage(content=question)
82
- ]
83
- response = self.alfred.invoke({"messages": messages})
84
- model_answer = response["messages"][-1].content
85
- return model_answer
86
 
87
 
88
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
21
  # (Keep Constants as is)
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
+ SERPER_API_KEY=os.environ.get("SERPER_API_KEY")
25
+ OPENAI_KEY=os.environ.get("OPENAI_KEY")
26
 
27
 
 
 
 
 
 
28
  # --- Basic Agent Definition ---
29
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
30
+ serper_tool = GoogleSerperAPIWrapper()
 
 
31
 
32
+ def wikipedia_search(query: str) -> str:
33
+ return WikipediaAPIWrapper().run(query)
34
+
35
+ serper_tool = GoogleSerperAPIWrapper()
36
+
37
+ tools = [
38
+ Tool(
39
+ name = 'wikipedia_search',
40
+ func = wikipedia_search,
41
+ description = 'Search Wikipedia for factual information.'
42
+ ),
43
+ Tool(
44
+ name = 'google_search',
45
+ func = serper_tool.run,
46
+ description = 'Search the web using Google Serper.'
47
+ )
48
+ ]
49
+
50
+ llm = ChatOpenAI(
51
+ model = 'gpt-4o-mini',
52
+ temperature = 0,
53
+ openai_api_key = OPENAI_KEY,
54
+ max_tokens = 4096
55
+ )
56
+
57
+ agent = initialize_agent(
58
+ tools=tools,
59
+ llm=llm,
60
+ agent=AgentType.OPENAI_FUNCTIONS,
61
+ verbose=True,
62
+ agent_kwargs={
63
+ "system_message": SystemMessage(content="You are a general AI assistant. I will ask you a question. "
64
  "Report your thoughts, and finish your answer with the following template: "
65
+ "[YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
66
  "If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
67
  "If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
68
+ "If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.")
69
+ }
70
+ )
71
+
72
+
73
+ class BasicAgent:
74
+ def __init__(self):
75
+ self.agent = agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  def __call__(self, question: str) -> str:
78
+ print(f"\nQuestion: {question}")
79
+ return self.agent.run(question)
 
 
 
 
 
80
 
81
 
82
  def run_and_submit_all( profile: gr.OAuthProfile | None):