Skip to content

Commit

Permalink
switch to content_and_artifact, add output_format
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarda committed Oct 22, 2024
1 parent e327ac7 commit 2a32152
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions libs/community/langchain_community/tools/ddg_search/tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Tool for the DuckDuckGo search API."""

import json
import warnings
from typing import Any, List, Optional, Type
from typing import Any, List, Literal, Optional, Type, Union

from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
Expand Down Expand Up @@ -74,14 +75,13 @@ def _run(


class DuckDuckGoSearchResults(BaseTool):
"""Tool that queries the DuckDuckGo search API and gets back List of Dictionary."""
"""Tool that queries the DuckDuckGo search API and returns the results in `output_format`."""

Check failure on line 78 in libs/community/langchain_community/tools/ddg_search/tool.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/tools/ddg_search/tool.py:78:89: E501 Line too long (97 > 88)

Check failure on line 78 in libs/community/langchain_community/tools/ddg_search/tool.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/tools/ddg_search/tool.py:78:89: E501 Line too long (97 > 88)

name: str = "duckduckgo_results_json"
description: str = (
"A wrapper around Duck Duck Go Search. "
"Useful for when you need to answer questions about current events. "
"Input should be a search query. Output is a List of Dictionaries of the "
"query results"
"Input should be a search query."
)
max_results: int = Field(alias="num_results", default=4)
api_wrapper: DuckDuckGoSearchAPIWrapper = Field(
Expand All @@ -93,25 +93,44 @@ class DuckDuckGoSearchResults(BaseTool):
"""Which keys from each result to include. If None all keys are included."""
results_separator: str = ", "
"""Character for separating results."""
output_format: Literal["string", "json", "list"] = "string"
"""Output format of the search results.
- 'string': Return a concatenated string of the search results.
- 'json': Return a JSON string of the search results.
- 'list': Return a list of dictionaries of the search results.
"""
response_format: Literal["content_and_artifact"] = "content_and_artifact"

def _run(
self,
query: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> list[dict]:
) -> tuple[Union[List[dict], str], List[dict]]:
"""Use the tool."""
res = self.api_wrapper.results(query, self.max_results, source=self.backend)

raw_results = self.api_wrapper.results(
query, self.max_results, source=self.backend
)
results = [
{
k: v
for k, v in d.items()
if not self.keys_to_include or k in self.keys_to_include
}
for d in res
for d in raw_results
]

return results
if self.output_format == "list":
return results, raw_results
elif self.output_format == "json":
return json.dumps(results), raw_results
elif self.output_format == "string":
res_strs = [", ".join([f"{k}: {v}" for k, v in d.items()]) for d in results]
return self.results_separator.join(res_strs), raw_results
else:
raise ValueError(
f"Invalid output_format: {self.output_format}. Needs to be one of 'string', 'json', 'list'."

Check failure on line 132 in libs/community/langchain_community/tools/ddg_search/tool.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/tools/ddg_search/tool.py:132:89: E501 Line too long (108 > 88)

Check failure on line 132 in libs/community/langchain_community/tools/ddg_search/tool.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/tools/ddg_search/tool.py:132:89: E501 Line too long (108 > 88)
)


def DuckDuckGoSearchTool(*args: Any, **kwargs: Any) -> DuckDuckGoSearchRun:
Expand Down

0 comments on commit 2a32152

Please sign in to comment.