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,4 +1,5 @@
import asyncio
import json
from typing import Any, Dict
import pytest
@@ -11,7 +12,10 @@ from langchain_community.tools.requests.tool import (
RequestsPutTool,
_parse_input,
)
from langchain_community.utilities.requests import TextRequestsWrapper
from langchain_community.utilities.requests import (
JsonRequestsWrapper,
TextRequestsWrapper,
)
class _MockTextRequestsWrapper(TextRequestsWrapper):
@@ -98,3 +102,97 @@ def test_requests_delete_tool(mock_requests_wrapper: TextRequestsWrapper) -> Non
tool = RequestsDeleteTool(requests_wrapper=mock_requests_wrapper)
assert tool.run("https://example.com") == "delete_response"
assert asyncio.run(tool.arun("https://example.com")) == "adelete_response"
class _MockJsonRequestsWrapper(JsonRequestsWrapper):
@staticmethod
def get(url: str, **kwargs: Any) -> Dict[str, Any]:
return {"response": "get_response"}
@staticmethod
async def aget(url: str, **kwargs: Any) -> Dict[str, Any]:
return {"response": "aget_response"}
@staticmethod
def post(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"post {json.dumps(data)}"}
@staticmethod
async def apost(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"apost {json.dumps(data)}"}
@staticmethod
def patch(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"patch {json.dumps(data)}"}
@staticmethod
async def apatch(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"apatch {json.dumps(data)}"}
@staticmethod
def put(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"put {json.dumps(data)}"}
@staticmethod
async def aput(url: str, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
return {"response": f"aput {json.dumps(data)}"}
@staticmethod
def delete(url: str, **kwargs: Any) -> Dict[str, Any]:
return {"response": "delete_response"}
@staticmethod
async def adelete(url: str, **kwargs: Any) -> Dict[str, Any]:
return {"response": "adelete_response"}
@pytest.fixture
def mock_json_requests_wrapper() -> JsonRequestsWrapper:
return _MockJsonRequestsWrapper()
def test_requests_get_tool_json(
mock_json_requests_wrapper: JsonRequestsWrapper,
) -> None:
tool = RequestsGetTool(requests_wrapper=mock_json_requests_wrapper)
assert tool.run("https://example.com") == {"response": "get_response"}
assert asyncio.run(tool.arun("https://example.com")) == {
"response": "aget_response"
}
def test_requests_post_tool_json(
mock_json_requests_wrapper: JsonRequestsWrapper,
) -> None:
tool = RequestsPostTool(requests_wrapper=mock_json_requests_wrapper)
input_text = '{"url": "https://example.com", "data": {"key": "value"}}'
assert tool.run(input_text) == {"response": 'post {"key": "value"}'}
assert asyncio.run(tool.arun(input_text)) == {"response": 'apost {"key": "value"}'}
def test_requests_patch_tool_json(
mock_json_requests_wrapper: JsonRequestsWrapper,
) -> None:
tool = RequestsPatchTool(requests_wrapper=mock_json_requests_wrapper)
input_text = '{"url": "https://example.com", "data": {"key": "value"}}'
assert tool.run(input_text) == {"response": 'patch {"key": "value"}'}
assert asyncio.run(tool.arun(input_text)) == {"response": 'apatch {"key": "value"}'}
def test_requests_put_tool_json(
mock_json_requests_wrapper: JsonRequestsWrapper,
) -> None:
tool = RequestsPutTool(requests_wrapper=mock_json_requests_wrapper)
input_text = '{"url": "https://example.com", "data": {"key": "value"}}'
assert tool.run(input_text) == {"response": 'put {"key": "value"}'}
assert asyncio.run(tool.arun(input_text)) == {"response": 'aput {"key": "value"}'}
def test_requests_delete_tool_json(
mock_json_requests_wrapper: JsonRequestsWrapper,
) -> None:
tool = RequestsDeleteTool(requests_wrapper=mock_json_requests_wrapper)
assert tool.run("https://example.com") == {"response": "delete_response"}
assert asyncio.run(tool.arun("https://example.com")) == {
"response": "adelete_response"
}