请求工具包#
- class langchain_community.agent_toolkits.openapi.toolkit.RequestsToolkit[来源]#
基础类:
BaseToolkit
用于制作REST请求的工具包。
- Security Note: This toolkit contains tools to make GET, POST, PATCH, PUT,
向API发送DELETE请求。
在使用此工具包时,请谨慎考虑允许使用的人员。如果向最终用户公开,请考虑到用户将能够代表托管代码的服务器发出任意请求。例如,用户可以要求服务器向仅可从服务器访问的私有API发出请求。
控制谁可以使用此工具包提交问题请求以及它拥有哪些网络访问权限。
查看 https://python.langchain.com/docs/security 获取更多信息。
- Setup:
安装
langchain-community
。pip install -U langchain-community
- Key init args:
- requests_wrapper: langchain_community.utilities.requests.GenericRequestsWrapper
用于执行请求的包装器。
- allow_dangerous_requests: bool
默认为 False。必须通过设置为 True 来“选择加入”使用危险的请求。
- Instantiate:
from langchain_community.agent_toolkits.openapi.toolkit import RequestsToolkit from langchain_community.utilities.requests import TextRequestsWrapper toolkit = RequestsToolkit( requests_wrapper=TextRequestsWrapper(headers={}), allow_dangerous_requests=ALLOW_DANGEROUS_REQUEST, )
- Tools:
tools = toolkit.get_tools() tools
[RequestsGetTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True), RequestsPostTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True), RequestsPatchTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True), RequestsPutTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True), RequestsDeleteTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True)]
- Use within an agent:
from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent api_spec = """ openapi: 3.0.0 info: title: JSONPlaceholder API version: 1.0.0 servers: - url: https://jsonplaceholder.typicode.com paths: /posts: get: summary: Get posts parameters: &id001 - name: _limit in: query required: false schema: type: integer example: 2 description: Limit the number of results """ system_message = """ You have access to an API to help answer user queries. Here is documentation on the API: {api_spec} """.format(api_spec=api_spec) llm = ChatOpenAI(model="gpt-4o-mini") agent_executor = create_react_agent(llm, tools, state_modifier=system_message) example_query = "Fetch the top two posts. What are their titles?" events = agent_executor.stream( {"messages": [("user", example_query)]}, stream_mode="values", ) for event in events: event["messages"][-1].pretty_print()
================================[1m Human Message [0m================================= Fetch the top two posts. What are their titles? ==================================[1m Ai Message [0m================================== Tool Calls: requests_get (call_RV2SOyzCnV5h2sm4WPgG8fND) Call ID: call_RV2SOyzCnV5h2sm4WPgG8fND Args: url: https://jsonplaceholder.typicode.com/posts?_limit=2 =================================[1m Tool Message [0m================================= Name: requests_get [ { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit..." }, { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum tempore vitae..." } ] ==================================[1m Ai Message [0m================================== The titles of the top two posts are: 1. "sunt aut facere repellat provident occaecati excepturi optio reprehenderit" 2. "qui est esse"
通过解析和验证来自关键字参数的输入数据来创建一个新模型。
如果输入数据无法验证以形成有效模型,则引发 [ValidationError][pydantic_core.ValidationError]。
self 被显式地设为仅位置参数,以允许 self 作为字段名称。
- param allow_dangerous_requests: bool = False#
允许危险请求。详情请参阅文档。
- param requests_wrapper: TextRequestsWrapper [Required]#
请求包装器。
使用 RequestsToolkit 的示例