Spaces:
Sleeping
Sleeping
update
Browse files- agent.py +2 -1
- app_playground.ipynb +157 -1
agent.py
CHANGED
|
@@ -127,6 +127,7 @@ def get_graph(llm):
|
|
| 127 |
|
| 128 |
Args:
|
| 129 |
query: The search query."""
|
|
|
|
| 130 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 131 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 132 |
[
|
|
@@ -156,7 +157,7 @@ def get_graph(llm):
|
|
| 156 |
|
| 157 |
Args:
|
| 158 |
query: The search query."""
|
| 159 |
-
search_docs = TavilySearchResults(max_results=3).invoke(query
|
| 160 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 161 |
[
|
| 162 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
|
|
|
| 127 |
|
| 128 |
Args:
|
| 129 |
query: The search query."""
|
| 130 |
+
print("\n-------------------- Tool (Wikipedia) has been called --------------------\n")
|
| 131 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 132 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 133 |
[
|
|
|
|
| 157 |
|
| 158 |
Args:
|
| 159 |
query: The search query."""
|
| 160 |
+
search_docs = TavilySearchResults(max_results=3).invoke({'query': query})
|
| 161 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 162 |
[
|
| 163 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
app_playground.ipynb
CHANGED
|
@@ -20,7 +20,9 @@
|
|
| 20 |
"\n",
|
| 21 |
"llm = get_llm()\n",
|
| 22 |
"\n",
|
| 23 |
-
"graph = get_graph(llm)\n"
|
|
|
|
|
|
|
| 24 |
],
|
| 25 |
"outputs": [],
|
| 26 |
"execution_count": 1
|
|
@@ -1221,6 +1223,160 @@
|
|
| 1221 |
"execution_count": null,
|
| 1222 |
"source": "",
|
| 1223 |
"id": "84872aedbe970456"
|
|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ph = get_graph(llm)\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"# graph.invoke({\"messages\": [HumanMessage(content=question),]})"
|
| 26 |
],
|
| 27 |
"outputs": [],
|
| 28 |
"execution_count": 1
|
|
|
|
| 1223 |
"execution_count": null,
|
| 1224 |
"source": "",
|
| 1225 |
"id": "84872aedbe970456"
|
| 1226 |
+
},
|
| 1227 |
+
{
|
| 1228 |
+
"metadata": {},
|
| 1229 |
+
"cell_type": "markdown",
|
| 1230 |
+
"source": "# Graph Test with web search",
|
| 1231 |
+
"id": "f53a68dafbed7643"
|
| 1232 |
+
},
|
| 1233 |
+
{
|
| 1234 |
+
"metadata": {
|
| 1235 |
+
"ExecuteTime": {
|
| 1236 |
+
"end_time": "2025-04-30T09:44:01.940985Z",
|
| 1237 |
+
"start_time": "2025-04-30T09:43:58.712696Z"
|
| 1238 |
+
}
|
| 1239 |
+
},
|
| 1240 |
+
"cell_type": "code",
|
| 1241 |
+
"source": [
|
| 1242 |
+
"from dotenv import load_dotenv\n",
|
| 1243 |
+
"\n",
|
| 1244 |
+
"from agent import *\n",
|
| 1245 |
+
"\n",
|
| 1246 |
+
"load_dotenv()\n",
|
| 1247 |
+
"\n",
|
| 1248 |
+
"llm = get_llm()\n",
|
| 1249 |
+
"\n",
|
| 1250 |
+
"graph = get_graph(llm)\n"
|
| 1251 |
+
],
|
| 1252 |
+
"id": "106b84a960377aad",
|
| 1253 |
+
"outputs": [],
|
| 1254 |
+
"execution_count": 4
|
| 1255 |
+
},
|
| 1256 |
+
{
|
| 1257 |
+
"metadata": {
|
| 1258 |
+
"ExecuteTime": {
|
| 1259 |
+
"end_time": "2025-04-30T09:46:23.580205Z",
|
| 1260 |
+
"start_time": "2025-04-30T09:45:52.434420Z"
|
| 1261 |
+
}
|
| 1262 |
+
},
|
| 1263 |
+
"cell_type": "code",
|
| 1264 |
+
"source": "graph.invoke({\"messages\": [HumanMessage(content=\"Search the web for the brith day of Friedrich Merz\"),]})",
|
| 1265 |
+
"id": "8ca9db24590884fa",
|
| 1266 |
+
"outputs": [
|
| 1267 |
+
{
|
| 1268 |
+
"name": "stdout",
|
| 1269 |
+
"output_type": "stream",
|
| 1270 |
+
"text": [
|
| 1271 |
+
"\n",
|
| 1272 |
+
"-------------------- Starting to create a plan --------------------\n",
|
| 1273 |
+
"\n",
|
| 1274 |
+
"The plan is: 1. Search the web for \"Friedrich Merz birthday\".\n",
|
| 1275 |
+
"2. Extract the date from the search results.\n",
|
| 1276 |
+
"3. Provide the extracted date as the answer.\n",
|
| 1277 |
+
"\n",
|
| 1278 |
+
"-------------------- Agent has been called -----------------------------------\n",
|
| 1279 |
+
"\n",
|
| 1280 |
+
"Agent has made a decision:\n",
|
| 1281 |
+
" [{'name': 'web_search', 'args': {'query': 'Friedrich Merz birthday'}, 'id': '5105ab7c-041e-4cef-b3c4-256092d9cc5c', 'type': 'tool_call'}]\n",
|
| 1282 |
+
"Waiting for 4 seconds...\n",
|
| 1283 |
+
"\n",
|
| 1284 |
+
"-------------------- Decision of forwarding has been made --------------------\n",
|
| 1285 |
+
"\n",
|
| 1286 |
+
"This is round: 2\n",
|
| 1287 |
+
"The last message is: content='' additional_kwargs={'function_call': {'name': 'web_search', 'arguments': '{\"query\": \"Friedrich Merz birthday\"}'}} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []} id='run-3b477f1f-5d26-4a58-ae29-581bd4ffcd4e-0' tool_calls=[{'name': 'web_search', 'args': {'query': 'Friedrich Merz birthday'}, 'id': '5105ab7c-041e-4cef-b3c4-256092d9cc5c', 'type': 'tool_call'}] usage_metadata={'input_tokens': 493, 'output_tokens': 7, 'total_tokens': 500, 'input_token_details': {'cache_read': 0}}\n",
|
| 1288 |
+
"\n",
|
| 1289 |
+
"-------------------- Agent has been called -----------------------------------\n",
|
| 1290 |
+
"\n",
|
| 1291 |
+
"Agent has made a decision:\n",
|
| 1292 |
+
" I am sorry, I cannot fulfill this request. There was an error when calling the web_search tool. Please try again. []\n",
|
| 1293 |
+
"Waiting for 4 seconds...\n",
|
| 1294 |
+
"\n",
|
| 1295 |
+
"-------------------- Decision of forwarding has been made --------------------\n",
|
| 1296 |
+
"\n",
|
| 1297 |
+
"This is round: 3\n",
|
| 1298 |
+
"The last message is: content='I am sorry, I cannot fulfill this request. There was an error when calling the web_search tool. Please try again.' additional_kwargs={} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []} id='run-70f8b036-ea1d-42e9-8d4b-f647ea13b31d-0' usage_metadata={'input_tokens': 540, 'output_tokens': 27, 'total_tokens': 567, 'input_token_details': {'cache_read': 0}}\n",
|
| 1299 |
+
"The final answer is: February 11, 1955\n"
|
| 1300 |
+
]
|
| 1301 |
+
},
|
| 1302 |
+
{
|
| 1303 |
+
"data": {
|
| 1304 |
+
"text/plain": [
|
| 1305 |
+
"{'messages': [HumanMessage(content='Search the web for the brith day of Friedrich Merz', additional_kwargs={}, response_metadata={}, id='1b56f817-9fc8-475c-b6c0-162dfeba5b83'),\n",
|
| 1306 |
+
" HumanMessage(content='Write a plan how to solve this qustion?', additional_kwargs={}, response_metadata={}, id='d60f2d18-0c51-431b-90b1-851edf83e439'),\n",
|
| 1307 |
+
" AIMessage(content='1. Search the web for \"Friedrich Merz birthday\".\\n2. Extract the date from the search results.\\n3. Provide the extracted date as the answer.', additional_kwargs={}, response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []}, id='run-bb9ba6a8-82db-48f5-919b-f05f3ea4b67f-0', usage_metadata={'input_tokens': 231, 'output_tokens': 34, 'total_tokens': 265, 'input_token_details': {'cache_read': 0}}),\n",
|
| 1308 |
+
" HumanMessage(content='Please provide me the answer to the question in detail.', additional_kwargs={}, response_metadata={}, id='70a1709b-1067-40e9-bed6-6d081ce6efe5'),\n",
|
| 1309 |
+
" AIMessage(content='', additional_kwargs={'function_call': {'name': 'web_search', 'arguments': '{\"query\": \"Friedrich Merz birthday\"}'}}, response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []}, id='run-3b477f1f-5d26-4a58-ae29-581bd4ffcd4e-0', tool_calls=[{'name': 'web_search', 'args': {'query': 'Friedrich Merz birthday'}, 'id': '5105ab7c-041e-4cef-b3c4-256092d9cc5c', 'type': 'tool_call'}], usage_metadata={'input_tokens': 493, 'output_tokens': 7, 'total_tokens': 500, 'input_token_details': {'cache_read': 0}}),\n",
|
| 1310 |
+
" ToolMessage(content='Error: TypeError(\"BaseTool.invoke() missing 1 required positional argument: \\'input\\'\")\\n Please fix your mistakes.', name='web_search', id='0068624d-ff9e-4cb6-9415-273e489ae94e', tool_call_id='5105ab7c-041e-4cef-b3c4-256092d9cc5c', status='error'),\n",
|
| 1311 |
+
" HumanMessage(content='Please provide me the answer to the question in detail.', additional_kwargs={}, response_metadata={}, id='024947cb-abd5-4f5a-9aab-63722a483271'),\n",
|
| 1312 |
+
" AIMessage(content='I am sorry, I cannot fulfill this request. There was an error when calling the web_search tool. Please try again.', additional_kwargs={}, response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []}, id='run-70f8b036-ea1d-42e9-8d4b-f647ea13b31d-0', usage_metadata={'input_tokens': 540, 'output_tokens': 27, 'total_tokens': 567, 'input_token_details': {'cache_read': 0}}),\n",
|
| 1313 |
+
" HumanMessage(content='Please provide me just the plain answer to the question', additional_kwargs={}, response_metadata={}, id='480e9cd8-0b33-4385-8450-7454fd9f0873'),\n",
|
| 1314 |
+
" AIMessage(content='February 11, 1955', additional_kwargs={}, response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []}, id='run-3697ccf4-c694-4492-a07d-594497a381a9-0', usage_metadata={'input_tokens': 358, 'output_tokens': 10, 'total_tokens': 368, 'input_token_details': {'cache_read': 0}})],\n",
|
| 1315 |
+
" 'aggregate': ['Plan', 'Agent', 'Agent', 'Answer']}"
|
| 1316 |
+
]
|
| 1317 |
+
},
|
| 1318 |
+
"execution_count": 5,
|
| 1319 |
+
"metadata": {},
|
| 1320 |
+
"output_type": "execute_result"
|
| 1321 |
+
}
|
| 1322 |
+
],
|
| 1323 |
+
"execution_count": 5
|
| 1324 |
+
},
|
| 1325 |
+
{
|
| 1326 |
+
"metadata": {
|
| 1327 |
+
"ExecuteTime": {
|
| 1328 |
+
"end_time": "2025-04-30T09:50:08.495390Z",
|
| 1329 |
+
"start_time": "2025-04-30T09:50:05.663936Z"
|
| 1330 |
+
}
|
| 1331 |
+
},
|
| 1332 |
+
"cell_type": "code",
|
| 1333 |
+
"source": "search_docs = TavilySearchResults(max_results=3).invoke({'query': 'who won the last french open'})",
|
| 1334 |
+
"id": "bf74512499009afd",
|
| 1335 |
+
"outputs": [],
|
| 1336 |
+
"execution_count": 7
|
| 1337 |
+
},
|
| 1338 |
+
{
|
| 1339 |
+
"metadata": {
|
| 1340 |
+
"ExecuteTime": {
|
| 1341 |
+
"end_time": "2025-04-30T09:50:11.782505Z",
|
| 1342 |
+
"start_time": "2025-04-30T09:50:11.773627Z"
|
| 1343 |
+
}
|
| 1344 |
+
},
|
| 1345 |
+
"cell_type": "code",
|
| 1346 |
+
"source": "search_docs",
|
| 1347 |
+
"id": "a0c1339790a66002",
|
| 1348 |
+
"outputs": [
|
| 1349 |
+
{
|
| 1350 |
+
"data": {
|
| 1351 |
+
"text/plain": [
|
| 1352 |
+
"[{'title': \"List of French Open men's singles champions - Wikipedia\",\n",
|
| 1353 |
+
" 'url': 'https://en.wikipedia.org/wiki/List_of_French_Open_men%27s_singles_champions',\n",
|
| 1354 |
+
" 'content': \"Country | Amateur Era | Open Era | All-time | First title | Last title\\nFrance(FRA) | 37 | 1 | 38 | 1892 | 1983\\nSpain(ESP) | 2 | 21 | 23 | 1961 | 2024\\nAustralia(AUS) | 9 | 2 | 11 | 1933 | 1969\\nUnited States(USA) | 7 | 4 | 11 | 1938 | 1999\\nSweden(SWE) | 1 | 9 | 10 | 1957 | 1988\\nCzechoslovakia(TCH)[h] | 0 | 5 | 5 | 1970 | 1987\\nGermany(GER) | 3 | 0 | 3 | 1934 | 1937\\nItaly(ITA) | 2 | 1 | 3 | 1959 | 1976\\nBrazil(BRA) | 0 | 3 | 3 | 1997 | 2001\\nSerbia(SRB) | 0 | 3 | 3 | 2016 | 2023 [...] Location | Paris[a]France\\nVenue | Stade Roland Garros\\nGoverning body | Fédération Française de Tennis(FFT)\\nCreated | 1891 (established)1925 (Grand Slamevent)\\nEditions | 128 events (2024)94Grand Slamevents (since1925)57 events (Open Era)\\nSurface | Red clay(since 1908)Sand / Clay (1892–1907)Grass (1891)\\nTrophy | Coupe des Mousquetaires\\nWebsite | French Open champions\\nMost titles\\n14:Rafael Nadal\\nCurrent champion\\nCarlos Alcaraz(First title) [...] Open Era, who won the 2023 French Open at 36 years, twenty days.[8] French players have won the most French Open men's singles titles, with 38 victories, followed by players from Spain (20) and Australia (11). The current champion is Carlos Alcaraz, who beat Alexander Zverev in the 2024 final.[9]\",\n",
|
| 1355 |
+
" 'score': 0.8190992},\n",
|
| 1356 |
+
" {'title': 'French Open | History, Winners, & Facts | Britannica',\n",
|
| 1357 |
+
" 'url': 'https://www.britannica.com/sports/French-Open',\n",
|
| 1358 |
+
" 'content': '2014 | Julien Benneteau, Edouard Roger-Vasselin | Su-Wei Hsieh, Shuai Peng\\n2015 | Ivan Dodig, Marcelo Melo | Bethanie Mattek-Sands, Lucie Safarova\\n2016 | Feliciano Lopez, Marc Lopez | Caroline Garcia, Kristina Mladenovic\\n2017 | Ryan Harrison, Michael Venus | Bethanie Mattek-Sands, Lucie Safarova\\n2018 | Pierre-Hugues Herbert, Nicolas Mahut | Barbora Krejcikova, Katerina Siniakova\\n2019 | Kevin Krawietz, Andreas Mies | Timea Babos, Kristina Mladenovic [...] 2017 | Rafael Nadal (Spain) | Jelena Ostapenko (Latvia)\\n2018 | Rafael Nadal (Spain) | Simona Halep (Rom.)\\n2019 | Rafael Nadal (Spain) | Ashleigh Barty (Austl.)\\n2020 | Rafael Nadal (Spain) | Iga Świątek (Pol.)\\n2021 | Novak Djokovic (Serbia) | Barbora Krejčíková (Czech.)\\n2022 | Rafael Nadal (Spain) | Iga Świątek (Pol.)\\n2023 | Novak Djokovic (Serbia) | Iga Świątek (Pol.)\\n2024 | Carlos Alcaraz (Spain) | Iga Świątek (Pol.)\\nWinners of the French Open doubles championship are provided in the table. [...] 2007 | Mark Knowles, Daniel Nestor | Alicia Molik, Mara Santangelo\\n2008 | Pablo Cuevas, Luis Horna | Anabel Medina Garrigues, Virginia Ruano Pascual\\n2009 | Lukas Dlouhy, Leander Paes | Anabel Medina Garrigues, Virginia Ruano Pascual\\n2010 | Daniel Nestor, Nenad Zimonjic | Serena Williams, Venus Williams\\n2011 | Max Mirnyi, Daniel Nestor | Andrea Hlavackova, Lucie Hradecka\\n2012 | Max Mirnyi, Daniel Nestor | Sara Errani, Roberta Vinci\\n2013 | Bob Bryan, Mike Bryan | Ekaterina Makarova, Elena Vesnina',\n",
|
| 1359 |
+
" 'score': 0.783542},\n",
|
| 1360 |
+
" {'title': \"Winners of the Men's French Open - Tennis - Topend Sports\",\n",
|
| 1361 |
+
" 'url': 'https://www.topendsports.com/events/tennis-grand-slam/french-open/winners-men.htm',\n",
|
| 1362 |
+
" 'content': \"Home > Events > Tennis > Grand Slam > French Open > Winners > Men\\n\\nWinners of the Men's French Open\\n\\nHere are all the winners of the French Tennis Open men's title since the first tournament in 1925. The recent tournaments have been dominated by Spanish player Rafael Nadal. He now has 14 French Open men's singles titles (the last in 2022). See also the Women's Singles Results.\",\n",
|
| 1363 |
+
" 'score': 0.77446437}]"
|
| 1364 |
+
]
|
| 1365 |
+
},
|
| 1366 |
+
"execution_count": 8,
|
| 1367 |
+
"metadata": {},
|
| 1368 |
+
"output_type": "execute_result"
|
| 1369 |
+
}
|
| 1370 |
+
],
|
| 1371 |
+
"execution_count": 8
|
| 1372 |
+
},
|
| 1373 |
+
{
|
| 1374 |
+
"metadata": {},
|
| 1375 |
+
"cell_type": "code",
|
| 1376 |
+
"outputs": [],
|
| 1377 |
+
"execution_count": null,
|
| 1378 |
+
"source": "",
|
| 1379 |
+
"id": "bdab71ec26eaadc0"
|
| 1380 |
}
|
| 1381 |
],
|
| 1382 |
"metadata": {
|