Update app.py
Browse files
app.py
CHANGED
@@ -19,61 +19,37 @@ search_tool = DuckDuckGoSearchTool()
|
|
19 |
##Tool 2
|
20 |
import wikipedia
|
21 |
from smolagents import Tool
|
22 |
-
from smolagents.models import InferenceClientModel
|
23 |
|
24 |
-
class
|
25 |
-
name = "
|
26 |
description = (
|
27 |
-
"
|
28 |
-
"
|
|
|
29 |
)
|
30 |
|
31 |
inputs = {
|
32 |
-
"question": {
|
33 |
-
"type": "string",
|
34 |
-
"description": "The question that should be answered using Wikipedia."
|
35 |
-
},
|
36 |
"topic": {
|
37 |
"type": "string",
|
38 |
-
"description": "The
|
39 |
}
|
40 |
}
|
41 |
|
42 |
output_type = "string"
|
43 |
|
44 |
-
def
|
45 |
-
super().__init__()
|
46 |
-
self.model = model or InferenceClientModel(
|
47 |
-
model="mistralai/Magistral-Small-2506", provider="featherless-ai"
|
48 |
-
)
|
49 |
-
|
50 |
-
def forward(self, question: str, topic: str) -> str:
|
51 |
try:
|
52 |
page = wikipedia.page(topic)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
f"answer the question briefly and factually.\n\n"
|
59 |
-
f"=== Wikipedia Content ===\n{content}\n\n"
|
60 |
-
f"=== Question ===\n{question}\n\n"
|
61 |
-
f"Answer in a single line. Avoid any extra explanation.\n"
|
62 |
-
f"FINAL ANSWER:"
|
63 |
-
)
|
64 |
-
|
65 |
-
response = self.model(prompt)
|
66 |
-
return response.strip()
|
67 |
-
|
68 |
-
except wikipedia.DisambiguationError as e:
|
69 |
-
return f"Disambiguation error: multiple results found: {', '.join(e.options[:5])}"
|
70 |
-
except wikipedia.PageError:
|
71 |
-
return "Wikipedia page not found."
|
72 |
except Exception as e:
|
73 |
-
return f"
|
74 |
|
75 |
|
76 |
-
|
77 |
#excel_tool = ExcelAnalysisTool()
|
78 |
#yt_tool = YouTubeDialogueTool()
|
79 |
|
@@ -83,7 +59,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
83 |
try:
|
84 |
|
85 |
agent = ToolCallingAgent(
|
86 |
-
tools=[search_tool],
|
87 |
model=OpenAIServerModel(model_id="gpt-4o",
|
88 |
api_key=os.environ["OPENAI_API_KEY"],
|
89 |
temperature=0.0),
|
@@ -141,7 +117,10 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
141 |
|
142 |
If the answer is "st. petersberg" answer as "saint petersburg" (without abbreviations)
|
143 |
If the answer is "three" answer as "3".
|
144 |
-
If the answer is "yamsaki, uehara" answer as "YAMASAKI, UEHARA" (capital letters)
|
|
|
|
|
|
|
145 |
"""
|
146 |
)
|
147 |
full_prompt = system_prompt + f"Question: {question_text.strip()}"
|
|
|
19 |
##Tool 2
|
20 |
import wikipedia
|
21 |
from smolagents import Tool
|
|
|
22 |
|
23 |
+
class WikipediaReaderTool(Tool):
|
24 |
+
name = "wikipedia_reader"
|
25 |
description = (
|
26 |
+
"Use this tool to retrieve the full text of a Wikipedia article given a topic. "
|
27 |
+
"Useful when a question involves factual, historical, or biographical knowledge "
|
28 |
+
"that is likely found in Wikipedia. Input must be a single word or phrase representing the topic."
|
29 |
)
|
30 |
|
31 |
inputs = {
|
|
|
|
|
|
|
|
|
32 |
"topic": {
|
33 |
"type": "string",
|
34 |
+
"description": "The Wikipedia article title to look up"
|
35 |
}
|
36 |
}
|
37 |
|
38 |
output_type = "string"
|
39 |
|
40 |
+
def forward(self, topic: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
try:
|
42 |
page = wikipedia.page(topic)
|
43 |
+
return page.content[:3000] # return first 3000 characters (within LLM token limit)
|
44 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
45 |
+
return f"Disambiguation error: Be more specific. Options: {', '.join(e.options[:5])}"
|
46 |
+
except wikipedia.exceptions.PageError:
|
47 |
+
return f"Error: No Wikipedia page found for '{topic}'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
except Exception as e:
|
49 |
+
return f"Unexpected error: {str(e)}"
|
50 |
|
51 |
|
52 |
+
wiki_tool = WikipediaReaderTool()
|
53 |
#excel_tool = ExcelAnalysisTool()
|
54 |
#yt_tool = YouTubeDialogueTool()
|
55 |
|
|
|
59 |
try:
|
60 |
|
61 |
agent = ToolCallingAgent(
|
62 |
+
tools=[search_tool, wiki_tool],
|
63 |
model=OpenAIServerModel(model_id="gpt-4o",
|
64 |
api_key=os.environ["OPENAI_API_KEY"],
|
65 |
temperature=0.0),
|
|
|
117 |
|
118 |
If the answer is "st. petersberg" answer as "saint petersburg" (without abbreviations)
|
119 |
If the answer is "three" answer as "3".
|
120 |
+
If the answer is "yamsaki, uehara" answer as "YAMASAKI, UEHARA" (capital letters).
|
121 |
+
|
122 |
+
If the user asks a question like "who played Ray in the Polish-language version of Everybody Loves Raymond", use the `wikipedia_reader` tool with topic='Wszyscy kochają Romana, Magda M'.
|
123 |
+
If you are unsure of the answer, or believe the question requires external information, call the relevant tool first.
|
124 |
"""
|
125 |
)
|
126 |
full_prompt = system_prompt + f"Question: {question_text.strip()}"
|