Update cool_agent.py
Browse files- cool_agent.py +42 -25
cool_agent.py
CHANGED
|
@@ -17,23 +17,31 @@ from smolagents import (
|
|
| 17 |
CodeAgent,
|
| 18 |
LiteLLMModel,
|
| 19 |
ToolCallingAgent,
|
|
|
|
| 20 |
)
|
| 21 |
import threading
|
| 22 |
-
from langchain_core.tools import tool
|
| 23 |
|
| 24 |
-
@tool
|
| 25 |
-
def wiki_search(query: str) -> str:
|
| 26 |
-
"""Search Wikipedia for a query and return maximum 2 results.
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
AUTHORIZED_IMPORTS = [
|
|
@@ -68,10 +76,10 @@ AUTHORIZED_IMPORTS = [
|
|
| 68 |
|
| 69 |
append_answer_lock = threading.Lock()
|
| 70 |
|
| 71 |
-
|
| 72 |
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 73 |
|
| 74 |
-
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
|
|
|
|
| 75 |
|
| 76 |
BROWSER_CONFIG = {
|
| 77 |
"viewport_size": 1024 * 5,
|
|
@@ -107,7 +115,7 @@ def create_agent(model_id="gpt-4o-mini"):
|
|
| 107 |
FindNextTool(browser),
|
| 108 |
ArchiveSearchTool(browser),
|
| 109 |
TextInspectorTool(model, text_limit),
|
| 110 |
-
wiki_search,
|
| 111 |
]
|
| 112 |
text_webbrowser_agent = ToolCallingAgent(
|
| 113 |
model=model,
|
|
@@ -118,17 +126,26 @@ def create_agent(model_id="gpt-4o-mini"):
|
|
| 118 |
name="search_agent",
|
| 119 |
description="""A team member that will search the internet to answer your question.
|
| 120 |
Ask him for all your questions that require browsing the web.
|
| 121 |
-
Note that this agent is using a powerful language model and it can do the search and analyse
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
""",
|
| 127 |
provide_run_summary=True,
|
| 128 |
)
|
| 129 |
-
text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
manager_agent = CodeAgent(
|
| 134 |
model=model,
|
|
@@ -140,4 +157,4 @@ def create_agent(model_id="gpt-4o-mini"):
|
|
| 140 |
managed_agents=[text_webbrowser_agent],
|
| 141 |
)
|
| 142 |
|
| 143 |
-
return manager_agent
|
|
|
|
| 17 |
CodeAgent,
|
| 18 |
LiteLLMModel,
|
| 19 |
ToolCallingAgent,
|
| 20 |
+
Tool,
|
| 21 |
)
|
| 22 |
import threading
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
class WikiSearchTool(Tool):
|
| 26 |
+
name = "wiki_search"
|
| 27 |
+
description = "Search Wikipedia for a query and return maximum 2 results."
|
| 28 |
+
inputs = {
|
| 29 |
+
"query": {"type": "string", "description": "The search query to perform."},
|
| 30 |
+
}
|
| 31 |
+
output_type = "dict"
|
| 32 |
+
|
| 33 |
+
def __init__(self):
|
| 34 |
+
super().__init__(self)
|
| 35 |
+
|
| 36 |
+
def forward(self, query: str) -> dict:
|
| 37 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 38 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 39 |
+
[
|
| 40 |
+
f'<Document source="{doc.metadata["source"]}" page="'
|
| 41 |
+
f'{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 42 |
+
for doc in search_docs
|
| 43 |
+
])
|
| 44 |
+
return {"wiki_results": formatted_search_docs}
|
| 45 |
|
| 46 |
|
| 47 |
AUTHORIZED_IMPORTS = [
|
|
|
|
| 76 |
|
| 77 |
append_answer_lock = threading.Lock()
|
| 78 |
|
|
|
|
| 79 |
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 80 |
|
| 81 |
+
user_agent = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 82 |
+
"Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0")
|
| 83 |
|
| 84 |
BROWSER_CONFIG = {
|
| 85 |
"viewport_size": 1024 * 5,
|
|
|
|
| 115 |
FindNextTool(browser),
|
| 116 |
ArchiveSearchTool(browser),
|
| 117 |
TextInspectorTool(model, text_limit),
|
| 118 |
+
wiki_search(),
|
| 119 |
]
|
| 120 |
text_webbrowser_agent = ToolCallingAgent(
|
| 121 |
model=model,
|
|
|
|
| 126 |
name="search_agent",
|
| 127 |
description="""A team member that will search the internet to answer your question.
|
| 128 |
Ask him for all your questions that require browsing the web.
|
| 129 |
+
Note that this agent is using a powerful language model and it can do the search and analyse
|
| 130 |
+
the results.
|
| 131 |
+
You should ask question in a way to let the language model to perform the best, i.e. provide
|
| 132 |
+
as much context as possible and ask in a clear way.
|
| 133 |
+
Provide him as much context as possible, in particular if you need to search on a specific
|
| 134 |
+
timeframe!
|
| 135 |
+
And don't hesitate to provide him with a complex search task, like finding a difference
|
| 136 |
+
between two webpages.
|
| 137 |
+
Your request must be a real sentence, not a google search! Like "Find me this information (
|
| 138 |
+
...)" rather than a few keywords.
|
| 139 |
""",
|
| 140 |
provide_run_summary=True,
|
| 141 |
)
|
| 142 |
+
text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to
|
| 143 |
+
.txt online files.
|
| 144 |
+
If a non-html page is in another format, especially .pdf or a Youtube video, use tool
|
| 145 |
+
'inspect_file_as_text' to inspect it.
|
| 146 |
+
Additionally, if after some searching you find out that you need more information to answer
|
| 147 |
+
the question, you can use `final_answer` with your request for clarification as argument to
|
| 148 |
+
request for more information."""
|
| 149 |
|
| 150 |
manager_agent = CodeAgent(
|
| 151 |
model=model,
|
|
|
|
| 157 |
managed_agents=[text_webbrowser_agent],
|
| 158 |
)
|
| 159 |
|
| 160 |
+
return manager_agent
|