4
4
import json
5
5
from typing import Any , Dict , Optional , Union
6
6
7
- from pydantic import BaseModel
7
+ from pydantic import BaseModel , model_validator
8
+
8
9
from langchain_core .callbacks import (
9
10
AsyncCallbackManagerForToolRun ,
10
11
CallbackManagerForToolRun ,
11
12
)
12
-
13
13
from langchain_community .utilities .requests import GenericRequestsWrapper
14
14
from langchain_core .tools import BaseTool
15
15
@@ -28,9 +28,20 @@ class BaseRequestsTool(BaseModel):
28
28
"""Base class for requests tools."""
29
29
30
30
requests_wrapper : GenericRequestsWrapper
31
-
32
31
allow_dangerous_requests : bool = False
33
32
33
+ @model_validator (mode = "before" )
34
+ @classmethod
35
+ def _validate_requests_wrapper (cls , values : Dict [str , Any ]) -> Dict [str , Any ]:
36
+ """Validate that requests_wrapper is present."""
37
+ if "requests_wrapper" not in values :
38
+ raise ValueError (
39
+ "`requests_wrapper` is a required argument for this tool. "
40
+ "Please pass in an instance of a RequestsWrapper, e.g. "
41
+ "`TextRequestsWrapper()`."
42
+ )
43
+ return values
44
+
34
45
def __init__ (self , ** kwargs : Any ):
35
46
"""Initialize the tool."""
36
47
if not kwargs .get ("allow_dangerous_requests" , False ):
@@ -48,14 +59,41 @@ def __init__(self, **kwargs: Any):
48
59
49
60
50
61
class RequestsGetTool (BaseRequestsTool , BaseTool ):
51
- """Tool for making a GET request to an API endpoint."""
62
+ """Tool for making a GET request to an API endpoint.
52
63
53
- name : str = "requests_get"
54
- description : str = """A portal to the internet. Use this when you need to get specific
55
- content from a website. Input should be a url (i.e. https://www.google.com).
56
- The output will be the text response of the GET request.
64
+ .. warning::
65
+ This tool can be used to make arbitrary GET requests.
66
+ By default, it is unsafe and requires ``allow_dangerous_requests=True``
67
+ to be passed to the constructor. Use with caution.
68
+
69
+ This tool requires that a ``requests_wrapper`` be provided.
70
+
71
+ Example:
72
+ .. code-block:: python
73
+
74
+ from langchain_community.tools import RequestsGetTool
75
+ from langchain_community.utilities import TextRequestsWrapper
76
+
77
+ requests_wrapper = TextRequestsWrapper()
78
+ tool = RequestsGetTool(
79
+ requests_wrapper=requests_wrapper,
80
+ allow_dangerous_requests=True
81
+ )
82
+ tool.run("https://www.google.com")
83
+
84
+ Args:
85
+ requests_wrapper: Required wrapper for making HTTP requests.
86
+ allow_dangerous_requests: If True, allows potentially dangerous requests.
87
+ Defaults to False.
57
88
"""
58
89
90
+ name : str = "requests_get"
91
+ description : str = (
92
+ "A portal to the internet. Use this when you need to get specific content "
93
+ "from a website. Input should be a url (i.e. https://www.google.com). "
94
+ "The output will be the text response of the GET request."
95
+ )
96
+
59
97
def _run (
60
98
self , url : str , run_manager : Optional [CallbackManagerForToolRun ] = None
61
99
) -> Union [str , Dict [str , Any ]]:
@@ -75,13 +113,14 @@ class RequestsPostTool(BaseRequestsTool, BaseTool):
75
113
"""Tool for making a POST request to an API endpoint."""
76
114
77
115
name : str = "requests_post"
78
- description : str = """Use this when you want to POST to a website.
79
- Input should be a json string with two keys: "url" and "data".
80
- The value of "url" should be a string, and the value of "data" should be a dictionary of
81
- key-value pairs you want to POST to the url.
82
- Be careful to always use double quotes for strings in the json string
83
- The output will be the text response of the POST request.
84
- """
116
+ description : str = (
117
+ "Use this when you want to POST to a website. "
118
+ 'Input should be a json string with two keys: "url" and "data". '
119
+ 'The value of "url" should be a string, and the value of "data" should be a '
120
+ "dictionary of key-value pairs you want to POST to the url. "
121
+ "Be careful to always use double quotes for strings in the json string. "
122
+ "The output will be the text response of the POST request."
123
+ )
85
124
86
125
def _run (
87
126
self , text : str , run_manager : Optional [CallbackManagerForToolRun ] = None
@@ -112,13 +151,14 @@ class RequestsPatchTool(BaseRequestsTool, BaseTool):
112
151
"""Tool for making a PATCH request to an API endpoint."""
113
152
114
153
name : str = "requests_patch"
115
- description : str = """Use this when you want to PATCH to a website.
116
- Input should be a json string with two keys: "url" and "data".
117
- The value of "url" should be a string, and the value of "data" should be a dictionary of
118
- key-value pairs you want to PATCH to the url.
119
- Be careful to always use double quotes for strings in the json string
120
- The output will be the text response of the PATCH request.
121
- """
154
+ description : str = (
155
+ "Use this when you want to PATCH to a website. "
156
+ 'Input should be a json string with two keys: "url" and "data". '
157
+ 'The value of "url" should be a string, and the value of "data" should be a '
158
+ "dictionary of key-value pairs you want to PATCH to the url. "
159
+ "Be careful to always use double quotes for strings in the json string. "
160
+ "The output will be the text response of the PATCH request."
161
+ )
122
162
123
163
def _run (
124
164
self , text : str , run_manager : Optional [CallbackManagerForToolRun ] = None
@@ -149,13 +189,14 @@ class RequestsPutTool(BaseRequestsTool, BaseTool):
149
189
"""Tool for making a PUT request to an API endpoint."""
150
190
151
191
name : str = "requests_put"
152
- description : str = """Use this when you want to PUT to a website.
153
- Input should be a json string with two keys: "url" and "data".
154
- The value of "url" should be a string, and the value of "data" should be a dictionary of
155
- key-value pairs you want to PUT to the url.
156
- Be careful to always use double quotes for strings in the json string.
157
- The output will be the text response of the PUT request.
158
- """
192
+ description : str = (
193
+ "Use this when you want to PUT to a website. "
194
+ 'Input should be a json string with two keys: "url" and "data". '
195
+ 'The value of "url" should be a string, and the value of "data" should be a '
196
+ "dictionary of key-value pairs you want to PUT to the url. "
197
+ "Be careful to always use double quotes for strings in the json string. "
198
+ "The output will be the text response of the PUT request."
199
+ )
159
200
160
201
def _run (
161
202
self , text : str , run_manager : Optional [CallbackManagerForToolRun ] = None
@@ -186,11 +227,12 @@ class RequestsDeleteTool(BaseRequestsTool, BaseTool):
186
227
"""Tool for making a DELETE request to an API endpoint."""
187
228
188
229
name : str = "requests_delete"
189
- description : str = """A portal to the internet.
190
- Use this when you need to make a DELETE request to a URL.
191
- Input should be a specific url, and the output will be the text
192
- response of the DELETE request.
193
- """
230
+ description : str = (
231
+ "A portal to the internet. "
232
+ "Use this when you need to make a DELETE request to a URL. "
233
+ "Input should be a specific url, and the output will be the text "
234
+ "response of the DELETE request."
235
+ )
194
236
195
237
def _run (
196
238
self ,
0 commit comments