Duke-911 commited on
Commit
0b54083
·
1 Parent(s): 658fc52

Refactor app.py to integrate AgentWorkflow and update LLM model; add Wikipedia tool to requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +35 -17
  2. requirements.txt +1 -0
app.py CHANGED
@@ -3,31 +3,48 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
 
 
6
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
 
 
 
 
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
 
13
- llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen2.5-Coder-32B-Instruct')
 
 
14
 
15
- class TofuAgent:
16
- def __init__(self):
17
- print("Agent initialized.")
 
 
 
 
 
 
 
18
 
19
- def __call__(self, question: str) -> str:
20
- print(f"Agent received question (first 50 chars): {question[:50]}...")
21
- try:
22
- # Use the llm to generate an answer
23
- response = llm.complete(question)
24
- print(f"Agent generated response: {response}")
25
- return response
26
- except Exception as e:
27
- print(f"Error generating response: {e}")
28
- return f"Error: {e}"
 
 
 
29
 
30
- def run_and_submit_all( profile: gr.OAuthProfile | None):
31
  """
32
  Fetches all questions, runs the BasicAgent on them, submits all answers,
33
  and displays the results.
@@ -48,7 +65,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
48
 
49
  # 1. Instantiate Agent ( modify this part to create your agent)
50
  try:
51
- agent = TofuAgent()
52
  except Exception as e:
53
  print(f"Error instantiating agent: {e}")
54
  return f"Error initializing agent: {e}", None
@@ -88,7 +105,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
88
  print(f"Skipping item with missing task_id or question: {item}")
89
  continue
90
  try:
91
- submitted_answer = agent(question_text)
 
92
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
93
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
94
  except Exception as e:
 
3
  import requests
4
  import pandas as pd
5
 
6
+ from llama_index.core.agent.workflow import AgentWorkflow
7
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
8
+ from llama_index.tools.wikipedia import WikipediaToolSpec
9
+ from llama_index.core.tools import FunctionTool
10
+ from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
11
+
12
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
 
18
+ llm = HuggingFaceInferenceAPI(model_name='Qwen/Qwen3-32B', num_output=2048)
19
+ wikipedia_tools = WikipediaToolSpec().to_tool_list()
20
+ search_tool = FunctionTool.from_defaults(DuckDuckGoSearchToolSpec().duckduckgo_full_search)
21
 
22
+ agent = AgentWorkflow.from_tools_or_functions(
23
+ tools_or_functions=[search_tool],
24
+ llm=llm,
25
+ system_prompt="""You are a concise answer engine.
26
+ ALWAYS output *only* the single entity requested—no explanations, no punctuation beyond the entity itself.
27
+ If unsure, reply “Unknown”.
28
+ Output must be a single word or name or number. Do not output anything else.
29
+ Examples:
30
+ Q: What is the capital of France?
31
+ A: Paris
32
 
33
+ Q: Who nominated the only Featured Article on English Wikipedia about a dinosaur promoted in November 2016?
34
+ A: Ian Rose
35
+
36
+ Q: How many legs does a spider have?
37
+ A: 8
38
+
39
+ Do not think step by step. Do not show any internal reasoning. Only respond with the exact entity.
40
+
41
+ You can use the Wikipedia tool to search for information on Wikipedia, and the DuckDuckGo tool to search the web.
42
+ Some of the questions might be valid sentences but with characters reversed.
43
+ """,
44
+ verbose=False,
45
+ )
46
 
47
+ async def run_and_submit_all( profile: gr.OAuthProfile | None):
48
  """
49
  Fetches all questions, runs the BasicAgent on them, submits all answers,
50
  and displays the results.
 
65
 
66
  # 1. Instantiate Agent ( modify this part to create your agent)
67
  try:
68
+ agent = agent
69
  except Exception as e:
70
  print(f"Error instantiating agent: {e}")
71
  return f"Error initializing agent: {e}", None
 
105
  print(f"Skipping item with missing task_id or question: {item}")
106
  continue
107
  try:
108
+ r = await agent.run(question_text)
109
+ submitted_answer = r.response.blocks[0].text
110
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
111
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
112
  except Exception as e:
requirements.txt CHANGED
@@ -7,3 +7,4 @@ llama-index-llms-huggingface
7
  llama-index-llms-huggingface-api
8
  llama-index-tools-duckduckgo
9
  llama-index-retrievers-bm25
 
 
7
  llama-index-llms-huggingface-api
8
  llama-index-tools-duckduckgo
9
  llama-index-retrievers-bm25
10
+ llama-index-tools-wikipedia