community[minor]: add JsonRequestsWrapper tool (#15374)

**Description:** This new feature enhances the flexibility of pipeline
integration, particularly when working with RESTful APIs.
``JsonRequestsWrapper`` allows for the decoding of JSON output, instead
of the only option for text output.

---------

Co-authored-by: Zhichao HAN <hanzhichao2000@hotmail.com>
This commit is contained in:
Zhichao HAN
2024-01-16 04:27:19 +08:00
committed by GitHub
parent d334efc848
commit 5cf06db3b3
4 changed files with 238 additions and 42 deletions

View File

@@ -1,7 +1,7 @@
# flake8: noqa
"""Tools for making requests to an API endpoint."""
import json
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Union
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.callbacks import (
@@ -9,7 +9,7 @@ from langchain_core.callbacks import (
CallbackManagerForToolRun,
)
from langchain_community.utilities.requests import TextRequestsWrapper
from langchain_community.utilities.requests import GenericRequestsWrapper
from langchain_core.tools import BaseTool
@@ -26,7 +26,7 @@ def _clean_url(url: str) -> str:
class BaseRequestsTool(BaseModel):
"""Base class for requests tools."""
requests_wrapper: TextRequestsWrapper
requests_wrapper: GenericRequestsWrapper
class RequestsGetTool(BaseRequestsTool, BaseTool):
@@ -37,7 +37,7 @@ class RequestsGetTool(BaseRequestsTool, BaseTool):
def _run(
self, url: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool."""
return self.requests_wrapper.get(_clean_url(url))
@@ -45,7 +45,7 @@ class RequestsGetTool(BaseRequestsTool, BaseTool):
self,
url: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool asynchronously."""
return await self.requests_wrapper.aget(_clean_url(url))
@@ -64,7 +64,7 @@ class RequestsPostTool(BaseRequestsTool, BaseTool):
def _run(
self, text: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool."""
try:
data = _parse_input(text)
@@ -76,7 +76,7 @@ class RequestsPostTool(BaseRequestsTool, BaseTool):
self,
text: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool asynchronously."""
try:
data = _parse_input(text)
@@ -101,7 +101,7 @@ class RequestsPatchTool(BaseRequestsTool, BaseTool):
def _run(
self, text: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool."""
try:
data = _parse_input(text)
@@ -113,7 +113,7 @@ class RequestsPatchTool(BaseRequestsTool, BaseTool):
self,
text: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool asynchronously."""
try:
data = _parse_input(text)
@@ -138,7 +138,7 @@ class RequestsPutTool(BaseRequestsTool, BaseTool):
def _run(
self, text: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool."""
try:
data = _parse_input(text)
@@ -150,7 +150,7 @@ class RequestsPutTool(BaseRequestsTool, BaseTool):
self,
text: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool asynchronously."""
try:
data = _parse_input(text)
@@ -171,7 +171,7 @@ class RequestsDeleteTool(BaseRequestsTool, BaseTool):
self,
url: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool."""
return self.requests_wrapper.delete(_clean_url(url))
@@ -179,6 +179,6 @@ class RequestsDeleteTool(BaseRequestsTool, BaseTool):
self,
url: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
) -> Union[str, Dict[str, Any]]:
"""Run the tool asynchronously."""
return await self.requests_wrapper.adelete(_clean_url(url))