fix arxiv tool
Browse files- orchestrator/tools.py +45 -4
orchestrator/tools.py
CHANGED
|
@@ -6,7 +6,7 @@ from typing import List, Optional
|
|
| 6 |
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 9 |
-
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
| 10 |
|
| 11 |
# --- Calculator tool (safe arithmetic) ---
|
| 12 |
import ast
|
|
@@ -101,15 +101,56 @@ def _make_wikipedia_tool(*, wiki_k: int, wiki_chars: int):
|
|
| 101 |
|
| 102 |
return wikipedia_search
|
| 103 |
|
| 104 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"""Return tool objects compatible with LangGraph ToolNode."""
|
| 106 |
|
| 107 |
web = DuckDuckGoSearchRun()
|
| 108 |
|
| 109 |
wiki = _make_wikipedia_tool(wiki_k=wiki_k, wiki_chars=wiki_chars)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
arxiv =
|
| 113 |
|
| 114 |
return [web, wiki, arxiv, calculator]
|
| 115 |
|
|
|
|
| 6 |
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 9 |
+
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
| 10 |
|
| 11 |
# --- Calculator tool (safe arithmetic) ---
|
| 12 |
import ast
|
|
|
|
| 101 |
|
| 102 |
return wikipedia_search
|
| 103 |
|
| 104 |
+
def _make_arxiv_tool(*, arxiv_k: int = 3, doc_chars: int = 4000):
|
| 105 |
+
"""arXiv tool driven through arxiv.Client rather than langchain's wrapper.
|
| 106 |
+
|
| 107 |
+
langchain_community still calls Search(...).results(), which arxiv 4.0
|
| 108 |
+
removed -- it surfaces as "'Search' object has no attribute 'results'".
|
| 109 |
+
Client.results() has existed since arxiv 2.0, so this works on every version
|
| 110 |
+
requirements.txt allows. Output format matches ArxivQueryRun, so nothing
|
| 111 |
+
downstream has to change.
|
| 112 |
+
"""
|
| 113 |
+
import arxiv
|
| 114 |
+
|
| 115 |
+
client = arxiv.Client()
|
| 116 |
+
|
| 117 |
+
@tool("arxiv")
|
| 118 |
+
def arxiv_search(query: str) -> str:
|
| 119 |
+
"""Search arXiv for research papers; returns title, authors, and abstract.
|
| 120 |
+
|
| 121 |
+
Pass a paper title or a few topic keywords, not the user's full question.
|
| 122 |
+
arXiv supports field prefixes -- when you know the exact title use
|
| 123 |
+
ti:"Attention Is All You Need", because bare keywords rank the real paper
|
| 124 |
+
far down. au:"Author Name" filters by author.
|
| 125 |
+
Use this when the question is about a paper, its authors, or affiliations.
|
| 126 |
+
"""
|
| 127 |
+
try:
|
| 128 |
+
search = arxiv.Search(query=query[:300], max_results=arxiv_k)
|
| 129 |
+
docs = [
|
| 130 |
+
f"Published: {r.updated.date()}\n"
|
| 131 |
+
f"Title: {r.title}\n"
|
| 132 |
+
f"Authors: {', '.join(a.name for a in r.authors)}\n"
|
| 133 |
+
f"Summary: {r.summary}"
|
| 134 |
+
for r in client.results(search)
|
| 135 |
+
]
|
| 136 |
+
except Exception as e: # noqa: BLE001 - surface a readable message to the agent
|
| 137 |
+
return f"[arxiv error] {type(e).__name__}: {e}"
|
| 138 |
+
|
| 139 |
+
if not docs:
|
| 140 |
+
return "No good Arxiv Result was found"
|
| 141 |
+
return "\n\n".join(docs)[:doc_chars]
|
| 142 |
+
|
| 143 |
+
return arxiv_search
|
| 144 |
+
|
| 145 |
+
def make_web_wiki_arxiv_tools(*, wiki_k: int = 3, wiki_chars: int = 2000, arxiv_k: int = 3):
|
| 146 |
"""Return tool objects compatible with LangGraph ToolNode."""
|
| 147 |
|
| 148 |
web = DuckDuckGoSearchRun()
|
| 149 |
|
| 150 |
wiki = _make_wikipedia_tool(wiki_k=wiki_k, wiki_chars=wiki_chars)
|
| 151 |
|
| 152 |
+
# Its underlying API doesn't require keys.
|
| 153 |
+
arxiv = _make_arxiv_tool(arxiv_k=arxiv_k)
|
| 154 |
|
| 155 |
return [web, wiki, arxiv, calculator]
|
| 156 |
|