Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from typing import TypedDict, List, Dict, Any, Optional
|
|
| 11 |
from langgraph.graph import StateGraph, END
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langchain_core.messages import HumanMessage
|
|
|
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
|
@@ -99,6 +100,33 @@ def web_search(query: str) -> str:
|
|
| 99 |
result=search_tool(query)
|
| 100 |
return f"Search results for '{query}' : {result}."
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 103 |
|
| 104 |
|
|
@@ -114,9 +142,10 @@ class BasicAgent:
|
|
| 114 |
Always return a valid answer after validating the source.
|
| 115 |
|
| 116 |
Your final answer should be in the following format:
|
| 117 |
-
"
|
| 118 |
|
| 119 |
If you are asked question related to numbers,return the answer in numeric format.
|
|
|
|
| 120 |
If you want to perform web search to find the answer, use the web_search tool.
|
| 121 |
If you are given a task to create an image, you can use the image_generation_tool.
|
| 122 |
|
|
|
|
| 11 |
from langgraph.graph import StateGraph, END
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langchain_core.messages import HumanMessage
|
| 14 |
+
import wikipedia
|
| 15 |
|
| 16 |
# (Keep Constants as is)
|
| 17 |
# --- Constants ---
|
|
|
|
| 100 |
result=search_tool(query)
|
| 101 |
return f"Search results for '{query}' : {result}."
|
| 102 |
|
| 103 |
+
@tool
|
| 104 |
+
def wikipedia_tool(query: str, llm=openrouter_inference) -> str:
|
| 105 |
+
"""
|
| 106 |
+
Searches Wikipedia for the given query and uses the LLM to extract a direct answer.
|
| 107 |
+
Args:
|
| 108 |
+
query (str): The search term or question.
|
| 109 |
+
llm (callable, optional): The language model to extract the answer.
|
| 110 |
+
Returns:
|
| 111 |
+
str: A direct answer or summary from Wikipedia.
|
| 112 |
+
"""
|
| 113 |
+
try:
|
| 114 |
+
summary = wikipedia.summary(query, sentences=5)
|
| 115 |
+
if llm:
|
| 116 |
+
# Ask the LLM to extract a direct answer from the summary
|
| 117 |
+
prompt = (
|
| 118 |
+
f"Given the following Wikipedia summary, answer the question as directly as possible:\n"
|
| 119 |
+
f"Question: {query}\n"
|
| 120 |
+
f"Summary: {summary}\n"
|
| 121 |
+
f"Answer:"
|
| 122 |
+
)
|
| 123 |
+
answer = llm(prompt)
|
| 124 |
+
return answer.strip()
|
| 125 |
+
else:
|
| 126 |
+
return summary
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return f"Wikipedia search error: {e}"
|
| 129 |
+
|
| 130 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 131 |
|
| 132 |
|
|
|
|
| 142 |
Always return a valid answer after validating the source.
|
| 143 |
|
| 144 |
Your final answer should be in the following format:
|
| 145 |
+
"your answer here".
|
| 146 |
|
| 147 |
If you are asked question related to numbers,return the answer in numeric format.
|
| 148 |
+
If you want more details about a topic,person, you can use the wikipedia tool.
|
| 149 |
If you want to perform web search to find the answer, use the web_search tool.
|
| 150 |
If you are given a task to create an image, you can use the image_generation_tool.
|
| 151 |
|