mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-25 08:03:39 +00:00
community[patch]: added option to change how duckduckgosearchresults tool converts api outputs into string (#22580)
Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
parent
78ff51ce83
commit
4833375200
@ -1,7 +1,7 @@
|
|||||||
"""Tool for the DuckDuckGo search API."""
|
"""Tool for the DuckDuckGo search API."""
|
||||||
|
|
||||||
import warnings
|
import warnings
|
||||||
from typing import Any, Optional, Type
|
from typing import Any, List, Optional, Type
|
||||||
|
|
||||||
from langchain_core.callbacks import CallbackManagerForToolRun
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||||
@ -89,6 +89,10 @@ class DuckDuckGoSearchResults(BaseTool):
|
|||||||
)
|
)
|
||||||
backend: str = "text"
|
backend: str = "text"
|
||||||
args_schema: Type[BaseModel] = DDGInput
|
args_schema: Type[BaseModel] = DDGInput
|
||||||
|
keys_to_include: Optional[List[str]] = None
|
||||||
|
"""Which keys from each result to include. If None all keys are included."""
|
||||||
|
results_separator: str = ", "
|
||||||
|
"""Character for separating results."""
|
||||||
|
|
||||||
def _run(
|
def _run(
|
||||||
self,
|
self,
|
||||||
@ -97,8 +101,17 @@ class DuckDuckGoSearchResults(BaseTool):
|
|||||||
) -> str:
|
) -> str:
|
||||||
"""Use the tool."""
|
"""Use the tool."""
|
||||||
res = self.api_wrapper.results(query, self.max_results, source=self.backend)
|
res = self.api_wrapper.results(query, self.max_results, source=self.backend)
|
||||||
res_strs = [", ".join([f"{k}: {v}" for k, v in d.items()]) for d in res]
|
res_strs = [
|
||||||
return ", ".join([f"[{rs}]" for rs in res_strs])
|
", ".join(
|
||||||
|
[
|
||||||
|
f"{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
|
||||||
|
]
|
||||||
|
return self.results_separator.join(res_strs)
|
||||||
|
|
||||||
|
|
||||||
def DuckDuckGoSearchTool(*args: Any, **kwargs: Any) -> DuckDuckGoSearchRun:
|
def DuckDuckGoSearchTool(*args: Any, **kwargs: Any) -> DuckDuckGoSearchRun:
|
||||||
|
Loading…
Reference in New Issue
Block a user