Spaces:
Sleeping
Sleeping
Commit ·
8e91397
1
Parent(s): 167f5fb
prompt change
Browse files
app.py
CHANGED
|
@@ -38,12 +38,11 @@ OPENAI_MODEL = os.getenv ('OPENAI_MODEL')
|
|
| 38 |
def current_events_news_search_tool(query: str) -> str:
|
| 39 |
"""
|
| 40 |
General web search tool for current events, news, or trending topics not yet on Wikipedia.
|
| 41 |
-
Use this tool only if the Wikipedia tool is not suitable.
|
| 42 |
Returns relevant context and source URL if available.
|
| 43 |
"""
|
| 44 |
url = f"https://api.duckduckgo.com/?q={query}&format=json&no_html=1"
|
| 45 |
try:
|
| 46 |
-
resp = requests.get(url, timeout=
|
| 47 |
resp.raise_for_status()
|
| 48 |
data = resp.json()
|
| 49 |
# Check main answer fields
|
|
@@ -54,32 +53,35 @@ def current_events_news_search_tool(query: str) -> str:
|
|
| 54 |
else:
|
| 55 |
answer = None
|
| 56 |
|
| 57 |
-
#
|
| 58 |
if not answer:
|
| 59 |
related = data.get("RelatedTopics")
|
| 60 |
-
if related and isinstance(related, list)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if not answer:
|
| 67 |
results = data.get("Results")
|
| 68 |
-
if results and isinstance(results, list)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
if answer:
|
| 76 |
-
|
| 77 |
-
if source_url:
|
| 78 |
-
result += f"\nSource: {source_url}"
|
| 79 |
-
return result.strip()
|
| 80 |
return "no_answer"
|
| 81 |
-
except Exception:
|
| 82 |
-
return "error"
|
| 83 |
|
| 84 |
# when you use the @tool decorator from langchain.tools, the tool.name and tool.description are automatically extracted from your function
|
| 85 |
# tool.name is set to the function name (e.g., `search_tool`), and
|
|
@@ -801,6 +803,11 @@ Final Answer: Python, JavaScript, Java
|
|
| 801 |
Q: What is the maximum number os birds in the video https://www.youtube.com/watch?v=example?
|
| 802 |
Final Answer: 12
|
| 803 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
If after 12 iterations also a tool usage is not useful then try to answer directly based on your knowledge. If you cannot answer then just say "no_answer" as YOUR FINAL ANSWER.
|
| 805 |
|
| 806 |
"""
|
|
|
|
| 38 |
def current_events_news_search_tool(query: str) -> str:
|
| 39 |
"""
|
| 40 |
General web search tool for current events, news, or trending topics not yet on Wikipedia.
|
|
|
|
| 41 |
Returns relevant context and source URL if available.
|
| 42 |
"""
|
| 43 |
url = f"https://api.duckduckgo.com/?q={query}&format=json&no_html=1"
|
| 44 |
try:
|
| 45 |
+
resp = requests.get(url, timeout=30)
|
| 46 |
resp.raise_for_status()
|
| 47 |
data = resp.json()
|
| 48 |
# Check main answer fields
|
|
|
|
| 53 |
else:
|
| 54 |
answer = None
|
| 55 |
|
| 56 |
+
# Try to extract more from RelatedTopics
|
| 57 |
if not answer:
|
| 58 |
related = data.get("RelatedTopics")
|
| 59 |
+
if related and isinstance(related, list):
|
| 60 |
+
for topic in related:
|
| 61 |
+
if isinstance(topic, dict) and topic.get("Text"):
|
| 62 |
+
answer = topic["Text"].strip()
|
| 63 |
+
# Optionally, add the URL
|
| 64 |
+
if topic.get("FirstURL"):
|
| 65 |
+
answer += f"\nSource: {topic['FirstURL']}"
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
# Try to extract from Results
|
| 69 |
if not answer:
|
| 70 |
results = data.get("Results")
|
| 71 |
+
if results and isinstance(results, list):
|
| 72 |
+
for result in results:
|
| 73 |
+
if isinstance(result, dict) and result.get("Text"):
|
| 74 |
+
answer = result["Text"].strip()
|
| 75 |
+
if result.get("FirstURL"):
|
| 76 |
+
answer += f"\nSource: {result['FirstURL']}"
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
# Fallback: return "no_answer"
|
| 80 |
if answer:
|
| 81 |
+
return answer
|
|
|
|
|
|
|
|
|
|
| 82 |
return "no_answer"
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"error: {e}"
|
| 85 |
|
| 86 |
# when you use the @tool decorator from langchain.tools, the tool.name and tool.description are automatically extracted from your function
|
| 87 |
# tool.name is set to the function name (e.g., `search_tool`), and
|
|
|
|
| 803 |
Q: What is the maximum number os birds in the video https://www.youtube.com/watch?v=example?
|
| 804 |
Final Answer: 12
|
| 805 |
|
| 806 |
+
Rules for YOUR FINAL ANSWER:
|
| 807 |
+
- Don't include explanations, thoughts, or tool calls in YOUR FINAL ANSWER.
|
| 808 |
+
- YOUR FINAL ANSWER should be a single value (number, string, or comma-separated list).
|
| 809 |
+
- If your example thought and Final Answer is something like 'Thought:Final Answer: The country with the least number of athletes at the 1928 Summer Olympics was Luxembourg, which had only 2 athletes.' then your output should be just: 'Luxembourg'
|
| 810 |
+
|
| 811 |
If after 12 iterations also a tool usage is not useful then try to answer directly based on your knowledge. If you cannot answer then just say "no_answer" as YOUR FINAL ANSWER.
|
| 812 |
|
| 813 |
"""
|