Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -17,33 +17,38 @@ from langchain_huggingface import (
|
|
| 17 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 18 |
from langchain_core.tools import tool
|
| 19 |
from langchain_openai import ChatOpenAI
|
| 20 |
-
from
|
| 21 |
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
### =============== SEARCH TOOLS =============== ###
|
| 25 |
|
| 26 |
@tool
|
| 27 |
-
def
|
| 28 |
-
"""Search the web using
|
| 29 |
Args:
|
| 30 |
query: The search query."""
|
| 31 |
try:
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
if not api_key:
|
| 35 |
-
return {"search_results": "Error: SERPAPI_API_KEY not found in environment variables."}
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
results = search.run(query)
|
| 42 |
-
|
| 43 |
-
if not results or results.strip() == "":
|
| 44 |
return {"search_results": "No search results found."}
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return {"search_results": f"Error performing search: {str(e)}"}
|
| 49 |
|
|
@@ -80,7 +85,7 @@ print(system_prompt)
|
|
| 80 |
sys_msg = SystemMessage(content=system_prompt)
|
| 81 |
|
| 82 |
tools = [
|
| 83 |
-
|
| 84 |
save_and_read_file,
|
| 85 |
]
|
| 86 |
|
|
@@ -137,7 +142,7 @@ def build_graph(provider: str = "openai"):
|
|
| 137 |
# test
|
| 138 |
if __name__ == "__main__":
|
| 139 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 140 |
-
graph = build_graph(provider="
|
| 141 |
messages = [HumanMessage(content=question)]
|
| 142 |
messages = graph.invoke({"messages": messages})
|
| 143 |
for m in messages["messages"]:
|
|
|
|
| 17 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 18 |
from langchain_core.tools import tool
|
| 19 |
from langchain_openai import ChatOpenAI
|
| 20 |
+
from duckduckgo_search import DDGS
|
| 21 |
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
### =============== SEARCH TOOLS =============== ###
|
| 25 |
|
| 26 |
@tool
|
| 27 |
+
def duckduckgo_search(query: str) -> str:
|
| 28 |
+
"""Search the web using DuckDuckGo.
|
| 29 |
Args:
|
| 30 |
query: The search query."""
|
| 31 |
try:
|
| 32 |
+
# Initialize DuckDuckGo search
|
| 33 |
+
ddgs = DDGS()
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Perform the search and get the first 5 results
|
| 36 |
+
results = list(ddgs.text(query, max_results=5))
|
| 37 |
|
| 38 |
+
if not results:
|
|
|
|
|
|
|
|
|
|
| 39 |
return {"search_results": "No search results found."}
|
| 40 |
|
| 41 |
+
# Format the results
|
| 42 |
+
formatted_results = []
|
| 43 |
+
for i, result in enumerate(results, 1):
|
| 44 |
+
formatted_result = f"{i}. {result.get('title', 'No title')}\n"
|
| 45 |
+
formatted_result += f" URL: {result.get('href', 'No URL')}\n"
|
| 46 |
+
formatted_result += f" {result.get('body', 'No description')}\n"
|
| 47 |
+
formatted_results.append(formatted_result)
|
| 48 |
+
|
| 49 |
+
search_output = "\n".join(formatted_results)
|
| 50 |
+
|
| 51 |
+
return {"search_results": search_output}
|
| 52 |
except Exception as e:
|
| 53 |
return {"search_results": f"Error performing search: {str(e)}"}
|
| 54 |
|
|
|
|
| 85 |
sys_msg = SystemMessage(content=system_prompt)
|
| 86 |
|
| 87 |
tools = [
|
| 88 |
+
duckduckgo_search,
|
| 89 |
save_and_read_file,
|
| 90 |
]
|
| 91 |
|
|
|
|
| 142 |
# test
|
| 143 |
if __name__ == "__main__":
|
| 144 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 145 |
+
graph = build_graph(provider="huggingface")
|
| 146 |
messages = [HumanMessage(content=question)]
|
| 147 |
messages = graph.invoke({"messages": messages})
|
| 148 |
for m in messages["messages"]:
|