mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-24 20:09:01 +00:00
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:
@@ -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))
|
||||
|
Reference in New Issue
Block a user