Skip to content

The agent engineering platform.

Harrison/load tools kwargs (#681)

Harrison Chase3d41af0abad1
diff --git a/docs/modules/agents/tools.md b/docs/modules/agents/tools.md
index 212528a1ce..0aedd9612d 100644
--- a/docs/modules/agents/tools.md
+++ b/docs/modules/agents/tools.md
@@ -22,6 +22,7 @@ tools = load_tools(tool_names, llm=llm)
```
Below is a list of all supported tools and relevant information:
+
- Tool Name: The name the LLM refers to the tool by.
- Tool Description: The description of the tool that is passed to the LLM.
- Notes: Notes about the tool that are NOT passed to the LLM.
@@ -31,61 +32,71 @@ Below is a list of all supported tools and relevant information:
## List of Tools
**python_repl**
+
- Tool Name: Python REPL
- Tool Description: A Python shell. Use this to execute python commands. Input should be a valid python command. If you expect output it should be printed out.
- Notes: Maintains state.
- Requires LLM: No
-
**serpapi**
+
- Tool Name: Search
- Tool Description: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.
- Notes: Calls the Serp API and then parses results.
- Requires LLM: No
**wolfram-alpha**
+
- Tool Name: Wolfram Alpha
- Tool Description: A wolfram alpha search engine. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query.
- Notes: Calls the Wolfram Alpha API and then parses results.
- Requires LLM: No
+- Extra Parameters: `wolfram_alpha_appid`: The Wolfram Alpha app id.
**requests**
+
- Tool Name: Requests
- Tool Description: A portal to the internet. Use this when you need to get specific content from a site. Input should be a specific url, and the output will be all the text on that page.
- Notes: Uses the Python requests module.
- Requires LLM: No
**terminal**
+
- Tool Name: Terminal
- Tool Description: Executes commands in a terminal. Input should be valid commands, and the output will be any output from running that command.
- Notes: Executes commands with subprocess.
- Requires LLM: No
**pal-math**
+
- Tool Name: PAL-MATH
- Tool Description: A language model that is excellent at solving complex word math problems. Input should be a fully worded hard word math problem.
- Notes: Based on [this paper](https://arxiv.org/pdf/2211.10435.pdf).
- Requires LLM: Yes
**pal-colored-objects**
+
- Tool Name: PAL-COLOR-OBJ
- Tool Description: A language model that is wonderful at reasoning about position and the color attributes of objects. Input should be a fully worded hard reasoning problem. Make sure to include all information about the objects AND the final question you want to answer.
- Notes: Based on [this paper](https://arxiv.org/pdf/2211.10435.pdf).
- Requires LLM: Yes
**llm-math**
+
- Tool Name: Calculator
- Tool Description: Useful for when you need to answer questions about math.
- Notes: An instance of the `LLMMath` chain.
- Requires LLM: Yes
**open-meteo-api**
+
- Tool Name: Open Meteo API
- Tool Description: Useful for when you want to get weather information from the OpenMeteo API. The input should be a question in natural language that this API can answer.
- Notes: A natural language connection to the Open Meteo API (`https://api.open-meteo.com/`), specifically the `/v1/forecast` endpoint.
- Requires LLM: Yes
**news-api**
+
- Tool Name: News API
- Tool Description: Use this when you want to get information about the top headlines of current news stories. The input should be a question in natural language that this API can answer.
- Notes: A natural language connection to the News API (`https://newsapi.org`), specifically the `/v2/top-headlines` endpoint.
@@ -93,8 +104,18 @@ Below is a list of all supported tools and relevant information:
- Extra Parameters: `news_api_key` (your API key to access this endpoint)
**tmdb-api**
+
- Tool Name: TMDB API
- Tool Description: Useful for when you want to get information from The Movie Database. The input should be a question in natural language that this API can answer.
- Notes: A natural language connection to the TMDB API (`https://api.themoviedb.org/3`), specifically the `/search/movie` endpoint.
- Requires LLM: Yes
- Extra Parameters: `tmdb_bearer_token` (your Bearer Token to access this endpoint - note that this is different from the API key)
+
+**google-search**
+
+- Tool Name: Search
+- Tool Description: A wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query.
+- Notes: Uses the Google Custom Search API
+- Requires LLM: No
+- Extra Parameters: `google_api_key`, `google_cse_id`
+- For more information on this, see [this page](../../ecosystem/google_search.md)
diff --git a/langchain/agents/load_tools.py b/langchain/agents/load_tools.py
index c5e8b43de5..f9476f316d 100644
--- a/langchain/agents/load_tools.py
+++ b/langchain/agents/load_tools.py
@@ -32,22 +32,6 @@ def _get_serpapi() -> Tool:
)
-def _get_google_search() -> Tool:
- return Tool(
- "Google Search",
- GoogleSearchAPIWrapper().run,
- "A wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query.",
- )
-
-
-def _get_wolfram_alpha() -> Tool:
- return Tool(
- "Wolfram Alpha",
- WolframAlphaAPIWrapper().run,
- "A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query.",
- )
-
-
def _get_requests() -> Tool:
return Tool(
"Requests",
@@ -69,8 +53,6 @@ _BASE_TOOLS = {
"serpapi": _get_serpapi,
"requests": _get_requests,
"terminal": _get_terminal,
- "google-search": _get_google_search,
- "wolfram-alpha": _get_wolfram_alpha,
}
@@ -141,10 +123,30 @@ def _get_tmdb_api(llm: BaseLLM, **kwargs: Any) -> Tool:
)
-_EXTRA_TOOLS = {
+def _get_wolfram_alpha(**kwargs: Any) -> Tool:
+ return Tool(
+ "Wolfram Alpha",
+ WolframAlphaAPIWrapper(**kwargs).run,
+ "A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query.",
+ )
+
+
+def _get_google_search(**kwargs: Any) -> Tool:
+ return Tool(
+ "Google Search",
+ GoogleSearchAPIWrapper(**kwargs).run,
+ "A wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query.",
+ )
+
+
+_EXTRA_LLM_TOOLS = {
"news-api": (_get_news_api, ["news_api_key"]),
"tmdb-api": (_get_tmdb_api, ["tmdb_bearer_token"]),
}
+_EXTRA_TOOLS = {
+ "wolfram-alpha": (_get_wolfram_alpha, ["wolfram_alpha_appid"]),
+ "google-search": (_get_google_search, ["google_api_key", "google_cse_id"]),
+}
def load_tools(
@@ -167,7 +169,7 @@ def load_tools(
if llm is None:
raise ValueError(f"Tool {name} requires an LLM to be provided")
tools.append(_LLM_TOOLS[name](llm))
- elif name in _EXTRA_TOOLS:
+ elif name in _EXTRA_LLM_TOOLS:
if llm is None:
raise ValueError(f"Tool {name} requires an LLM to be provided")
_get_tool_func, extra_keys = _EXTRA_TOOLS[name]
@@ -179,6 +181,17 @@ def load_tools(
)
sub_kwargs = {k: kwargs[k] for k in extra_keys}
tools.append(_get_tool_func(llm=llm, **sub_kwargs))
+ elif name in _EXTRA_TOOLS:
+ _get_tool_func, extra_keys = _EXTRA_TOOLS[name]
+ missing_keys = set(extra_keys).difference(kwargs)
+ if missing_keys:
+ raise ValueError(
+ f"Tool {name} requires some parameters that were not "
+ f"provided: {missing_keys}"
+ )
+ sub_kwargs = {k: kwargs[k] for k in extra_keys}
+ tools.append(_get_tool_func(**sub_kwargs))
+
else:
raise ValueError(f"Got unknown tool {name}")
return tools