跳到内容

请求

RequestsToolSpec #

基础:BaseToolSpec

请求工具。

源代码位于 llama-index-integrations/tools/llama-index-tools-requests/llama_index/tools/requests/base.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class RequestsToolSpec(BaseToolSpec):
    """Requests Tool."""

    spec_functions = ["get_request", "post_request", "patch_request"]

    def __init__(self, domain_headers: Optional[dict] = {}):
        self.domain_headers = domain_headers

    def get_request(self, url: str, params: Optional[dict] = {}):
        """
        Use this to GET content from a website.

        Args:
            url ([str]): The url to make the get request against
            params (Optional[dict]): the parameters to provide with the get request

        """
        if not self._valid_url(url):
            return INVALID_URL_PROMPT

        res = requests.get(url, headers=self._get_headers_for_url(url), params=params)
        return res.json()

    def post_request(self, url: str, data: Optional[dict] = {}):
        """
        Use this to POST content to a website.

        Args:
            url ([str]): The url to make the get request against
            data (Optional[dict]): the key-value pairs to provide with the get request

        """
        if not self._valid_url(url):
            return INVALID_URL_PROMPT

        res = requests.post(url, headers=self._get_headers_for_url(url), json=data)
        return res.json()

    def patch_request(self, url: str, data: Optional[dict] = {}):
        """
        Use this to PATCH content to a website.

        Args:
            url ([str]): The url to make the get request against
            data (Optional[dict]): the key-value pairs to provide with the get request

        """
        if not self._valid_url(url):
            return INVALID_URL_PROMPT

        requests.patch(url, headers=self._get_headers_for_url(url), json=data)
        return None

    def _valid_url(self, url: str) -> bool:
        parsed = urlparse(url)
        return parsed.scheme and parsed.hostname

    def _get_domain(self, url: str) -> str:
        return urlparse(url).hostname

    def _get_headers_for_url(self, url: str) -> dict:
        return self.domain_headers[self._get_domain(url)]

get_request #

get_request(url: str, params: Optional[dict] = {})

用于从网站获取(GET)内容。

参数

名称 类型 描述 默认
url [str]

发起GET请求的URL

必需
params 可选[dict]

GET请求附带的参数

{}
源代码位于 llama-index-integrations/tools/llama-index-tools-requests/llama_index/tools/requests/base.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_request(self, url: str, params: Optional[dict] = {}):
    """
    Use this to GET content from a website.

    Args:
        url ([str]): The url to make the get request against
        params (Optional[dict]): the parameters to provide with the get request

    """
    if not self._valid_url(url):
        return INVALID_URL_PROMPT

    res = requests.get(url, headers=self._get_headers_for_url(url), params=params)
    return res.json()

post_request #

post_request(url: str, data: Optional[dict] = {})

用于向网站发布(POST)内容。

参数

名称 类型 描述 默认
url [str]

发起GET请求的URL

必需
data 可选[dict]

GET请求附带的键值对

{}
源代码位于 llama-index-integrations/tools/llama-index-tools-requests/llama_index/tools/requests/base.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def post_request(self, url: str, data: Optional[dict] = {}):
    """
    Use this to POST content to a website.

    Args:
        url ([str]): The url to make the get request against
        data (Optional[dict]): the key-value pairs to provide with the get request

    """
    if not self._valid_url(url):
        return INVALID_URL_PROMPT

    res = requests.post(url, headers=self._get_headers_for_url(url), json=data)
    return res.json()

patch_request #

patch_request(url: str, data: Optional[dict] = {})

用于向网站修补(PATCH)内容。

参数

名称 类型 描述 默认
url [str]

发起GET请求的URL

必需
data 可选[dict]

GET请求附带的键值对

{}
源代码位于 llama-index-integrations/tools/llama-index-tools-requests/llama_index/tools/requests/base.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def patch_request(self, url: str, data: Optional[dict] = {}):
    """
    Use this to PATCH content to a website.

    Args:
        url ([str]): The url to make the get request against
        data (Optional[dict]): the key-value pairs to provide with the get request

    """
    if not self._valid_url(url):
        return INVALID_URL_PROMPT

    requests.patch(url, headers=self._get_headers_for_url(url), json=data)
    return None