Michele De Stefano
commited on
Commit
·
6770007
1
Parent(s):
bc0013e
Now using Tavily for web searches. It's a lot more powerful than DuckDuckGo
Browse files- agent_factory.py +36 -17
- requirements.txt +1 -0
- tests/tools/test_web_search.py +1 -1
agent_factory.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
from typing import Any, Literal
|
| 3 |
|
|
@@ -6,6 +8,7 @@ from langchain_core.messages import SystemMessage, AnyMessage
|
|
| 6 |
from langchain_core.runnables import Runnable
|
| 7 |
from langchain_core.tools import BaseTool
|
| 8 |
from langchain_ollama import ChatOllama
|
|
|
|
| 9 |
from langgraph.constants import START, END
|
| 10 |
from langgraph.graph import MessagesState, StateGraph
|
| 11 |
from langgraph.graph.graph import CompiledGraph
|
|
@@ -23,6 +26,9 @@ from tools import (
|
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
class AgentFactory:
|
| 27 |
"""
|
| 28 |
A factory for the agent. It is assumed that an Ollama server is running
|
|
@@ -67,7 +73,9 @@ class AgentFactory:
|
|
| 67 |
"again the same tool, by calling it again.\n"
|
| 68 |
"You are advised to cycle between reasoning and tool calling also\n"
|
| 69 |
"multiple times. Provide an answer only when you are sure you don't\n"
|
| 70 |
-
"have to call any tool again
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
__llm: Runnable
|
|
@@ -90,31 +98,42 @@ class AgentFactory:
|
|
| 90 |
num_ctx: Size of the context window used to generate the
|
| 91 |
next token.
|
| 92 |
"""
|
| 93 |
-
search_tool = DuckDuckGoSearchResults(
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
-
search_tool.with_retry()
|
| 109 |
self.__tools = [
|
| 110 |
execute_python_script,
|
| 111 |
get_excel_table_content,
|
| 112 |
get_youtube_video_transcript,
|
| 113 |
reverse_string,
|
| 114 |
search_tool,
|
|
|
|
| 115 |
sum_list,
|
| 116 |
transcribe_audio_file,
|
| 117 |
-
web_page_info_retriever,
|
| 118 |
youtube_video_to_frame_captions
|
| 119 |
]
|
| 120 |
self.__llm = ChatOllama(
|
|
|
|
| 1 |
+
import datetime as dt
|
| 2 |
+
import dotenv
|
| 3 |
import re
|
| 4 |
from typing import Any, Literal
|
| 5 |
|
|
|
|
| 8 |
from langchain_core.runnables import Runnable
|
| 9 |
from langchain_core.tools import BaseTool
|
| 10 |
from langchain_ollama import ChatOllama
|
| 11 |
+
from langchain_tavily import TavilySearch, TavilyExtract
|
| 12 |
from langgraph.constants import START, END
|
| 13 |
from langgraph.graph import MessagesState, StateGraph
|
| 14 |
from langgraph.graph.graph import CompiledGraph
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
|
| 29 |
+
dotenv.load_dotenv()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
class AgentFactory:
|
| 33 |
"""
|
| 34 |
A factory for the agent. It is assumed that an Ollama server is running
|
|
|
|
| 73 |
"again the same tool, by calling it again.\n"
|
| 74 |
"You are advised to cycle between reasoning and tool calling also\n"
|
| 75 |
"multiple times. Provide an answer only when you are sure you don't\n"
|
| 76 |
+
"have to call any tool again.\n"
|
| 77 |
+
"\n"
|
| 78 |
+
f"If you need it, the date today is {dt.date.today()}."
|
| 79 |
)
|
| 80 |
|
| 81 |
__llm: Runnable
|
|
|
|
| 98 |
num_ctx: Size of the context window used to generate the
|
| 99 |
next token.
|
| 100 |
"""
|
| 101 |
+
# search_tool = DuckDuckGoSearchResults(
|
| 102 |
+
# description=(
|
| 103 |
+
# "A wrapper around Duck Duck Go Search. Useful for when you "
|
| 104 |
+
# "need to answer questions about information you can find on "
|
| 105 |
+
# "the web. Input should be a search query. It is advisable to "
|
| 106 |
+
# "use this tool to retrieve web page URLs and use another tool "
|
| 107 |
+
# "to analyze the pages. If the web source is suggested by the "
|
| 108 |
+
# "user query, prefer retrieving information from that source. "
|
| 109 |
+
# "For example, the query may suggest to search on Wikipedia or "
|
| 110 |
+
# "Medium. In those cases, prepend the query with "
|
| 111 |
+
# "'site: <name of the source>'. For example: "
|
| 112 |
+
# "'site: wikipedia.org'"
|
| 113 |
+
# ),
|
| 114 |
+
# output_format="list"
|
| 115 |
+
# )
|
| 116 |
+
|
| 117 |
+
search_tool = TavilySearch(
|
| 118 |
+
topic="general",
|
| 119 |
+
max_results=5,
|
| 120 |
+
include_answer="advanced",
|
| 121 |
+
)
|
| 122 |
+
# search_tool.with_retry()
|
| 123 |
+
extract_tool = TavilyExtract(
|
| 124 |
+
extract_depth="advanced",
|
| 125 |
+
include_images=False,
|
| 126 |
)
|
|
|
|
| 127 |
self.__tools = [
|
| 128 |
execute_python_script,
|
| 129 |
get_excel_table_content,
|
| 130 |
get_youtube_video_transcript,
|
| 131 |
reverse_string,
|
| 132 |
search_tool,
|
| 133 |
+
extract_tool,
|
| 134 |
sum_list,
|
| 135 |
transcribe_audio_file,
|
| 136 |
+
# web_page_info_retriever,
|
| 137 |
youtube_video_to_frame_captions
|
| 138 |
]
|
| 139 |
self.__llm = ChatOllama(
|
requirements.txt
CHANGED
|
@@ -8,6 +8,7 @@ langchain
|
|
| 8 |
langchain_community
|
| 9 |
langchain-core
|
| 10 |
langchain-ollama
|
|
|
|
| 11 |
langchain-unstructured[local]
|
| 12 |
langgraph
|
| 13 |
opencv-python
|
|
|
|
| 8 |
langchain_community
|
| 9 |
langchain-core
|
| 10 |
langchain-ollama
|
| 11 |
+
langchain-tavily
|
| 12 |
langchain-unstructured[local]
|
| 13 |
langgraph
|
| 14 |
opencv-python
|
tests/tools/test_web_search.py
CHANGED
|
@@ -25,4 +25,4 @@ def test_web_search() -> None:
|
|
| 25 |
answer = final_state["messages"][-1].content
|
| 26 |
|
| 27 |
# then
|
| 28 |
-
assert answer == "
|
|
|
|
| 25 |
answer = final_state["messages"][-1].content
|
| 26 |
|
| 27 |
# then
|
| 28 |
+
assert answer == "3"
|