HTTP

更新时间:

Shell

工具调用请求示例

curl --location 'https://nlp.aliyuncs.com/v2/api/completions' \
--header 'Content-Type: application/json' \
--header 'X-AcA-DataInspection: enable' \
--header 'x-fag-servicename: aca-completion' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
            "model": "xingchen-plus-v2",
            "user": "tester01",
            "max_tokens": 100,
            "stream": true,
            "messages":
            [
                {
                    "name": "小义",
                    "role": "system",
                    "content": "你是一个对话Agent,你可以访问以下API,请自动规划当前最必要的API调用,尽你所能为用户提供帮助。"
                },
                {
                    "role": "user",
                    "content": "今天杭州天气怎么样"
                }
            ],
            "tools":
            [
                {
                    "id": "001",
                    "type": "function",
                    "function":
                    {
                        "name": "weather",
                        "description": "通过调用天气预报API获取当地天气预报信息通过调用天气预报API获取当地天气预报信息",
                        "parameters":
                        {
                            "type": "object",
                            "properties":
                            {
                                "location":
                                {
                                    "type": "string",
                                    "description": "地点"
                                },
                                "format":
                                {
                                    "type": "string",
                                    "enum":
                                    [
                                        "celsius",
                                        "fahrenheit"
                                    ],
                                    "description": "温度单位"
                                }
                            },
                            "required":
                            [
                                "location",
                                "format"
                            ]
                        }
                    }
                }
            ]
        }'

工具调用响应示例

{
    "id": "414add7c-8c15-42e8-a53c-8bdfe80e2bd6",
    "created": 1715160819,
    "model": "xingchen-plus-v2",
    "choices":
    [
        {
            "index": 1,
            "finish_reason": "stop",
            "message":
            {
                "role": "assistant",
                "content": "",
                "tool_calls":
                [
                    {
                        "id": "001",
                        "type": "function",
                        "function":
                        {
                            "name": "weather",
                            "arguments": "{\"format\":\"celsius\",\"location\":\"杭州\"}"
                        }
                    }
                ]
            }
        }
    ],
    "usage":
    {
        "prompt_tokens": 0,
        "completion_tokens": 304,
        "total_tokens": 304
    },
    "stop": true
}

工具生成结果对话请求示例

curl --location 'https://nlp.aliyuncs.com/v2/api/completions' \
--header 'Content-Type: application/json' \
--header 'X-AcA-DataInspection: enable' \
--header 'x-fag-servicename: aca-completion' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
            "model": "xingchen-plus-v2",
            "user": "tester01",
            "max_tokens": 100,
            "stream": true,
            "messages":
            [
                {
                    "name": "小义",
                    "role": "system",
                    "content": "你是一个对话Agent,你可以访问以下API,请自动规划当前最必要的API调用,尽你所能为用户提供帮助。"
                },
                {
                    "role": "user",
                    "content": "今天杭州天气怎么样"
                },
                {
                    "role": "assistant",
                    "content": "",
                    "tool_calls":
                    [
                        {
                            "id": "weather",
                            "type": "function",
                            "function":
                            {
                                "name": "weather",
                                "arguments": "{\"format\":\"celsius\",\"location\":\"杭州\"}"
                            }
                        }
                    ]
                },
                {
                    "tool_call_id":"001",
                    "role": "tool",
                    "content": "{\"location\": \"杭州\", \"temperature\": \"25\", \"unit\": \"celsius\", \"forecast\": \"sunny\"}"
                }
            ],
            "tools":
            [
                {
                    "id": "001",
                    "type": "function",
                    "function":
                    {
                        "name": "weather",
                        "description": "通过调用天气预报API获取当地天气预报信息通过调用天气预报API获取当地天气预报信息",
                        "parameters":
                        {
                            "type": "object",
                            "properties":
                            {
                                "location":
                                {
                                    "type": "string",
                                    "description": "地点"
                                },
                                "format":
                                {
                                    "type": "string",
                                    "enum":
                                    [
                                        "celsius",
                                        "fahrenheit"
                                    ],
                                    "description": "温度单位"
                                }
                            },
                            "required":
                            [
                                "location",
                                "format"
                            ]
                        }
                    }
                }
            ]
        }'

工具调用结果对话回复

{
    "id": "4feaf99f-61b1-4824-85bf-f9a4265c6e13",
    "created": 1715166710,
    "model": "xingchen-plus-v2",
    "choices":
    [
        {
            "index": 1,
            "finish_reason": "stop",
            "message":
            {
                "role": "assistant",
                "content": "今天的杭州天气格外晴朗,温度是25摄氏度,真是个出门游玩的好日子呢!",
                "tool_calls":
                []
            }
        }
    ],
    "usage":
    {
        "prompt_tokens": 222,
        "completion_tokens": 23,
        "total_tokens": 275
    },
    "stop": true
}

Python

工具调用请求示例

import json

import requests


def handle_stream(response: requests.Response):
    is_error = False
    for line in response.iter_lines():
        if line:
            line = line.decode('utf8').rstrip('\n').rstrip('\r')
            if line.startswith('event:error'):
                is_error = True
            elif line.startswith('data:'):
                line = line[len('data:'):]
                yield is_error, line
                if is_error:
                    break


api_key = "{YOUR_API_KEY}"
service_name = "aca-completion"

url = "https://nlp.aliyuncs.com/v2/api/completions"
headers = {
    "Content-Type": "application/json",
    "X-AcA-DataInspection": "enable",
    "x-fag-servicename": service_name,
    "x-fag-appcode": "aca",
    "Authorization": "Bearer " + api_key
}
payload = {
    "model": "xingchen-plus-v2",
    "user": "tester01",
    "max_tokens": 100,
    "stream": True,
    "messages":
        [
            {
                "name": "小义",
                "role": "system",
                "content": "你是一个对话Agent,你可以访问以下API,请自动规划当前最必要的API调用,尽你所能为用户提供帮助。"
            },
            {
                "role": "user",
                "content": "今天杭州天气怎么样"
            }
        ],
    "tools":
        [
            {
                "id": "001",
                "type": "function",
                "function":
                    {
                        "name": "weather",
                        "description": "通过调用天气预报API获取当地天气预报信息通过调用天气预报API获取当地天气预报信息",
                        "parameters":
                            {
                                "type": "object",
                                "properties":
                                    {
                                        "location":
                                            {
                                                "type": "string",
                                                "description": "地点"
                                            },
                                        "format":
                                            {
                                                "type": "string",
                                                "enum":
                                                    [
                                                        "celsius",
                                                        "fahrenheit"
                                                    ],
                                                "description": "温度单位"
                                            }
                                    },
                                "required":
                                    [
                                        "location",
                                        "format"
                                    ]
                            }
                    }
            }
        ]
}

response = requests.post(url, headers=headers, json=payload, stream=True)

for is_error, resp in handle_stream(response):
    if is_error:
        break
    print(json.loads(resp))
    

工具调用结果示例

{'id': 'bde6e7facdc604cc29a6f0041f1a42a7', 'created': 1715237728, 'model': 'xingchen-plus', 'choices': [{'index': 1, 'finish_reason': 'stop', 'message': {'role': 'assistant', 'content': '', 'tool_calls': [{'id': '001', 'type': 'function', 'function': {'name': 'weather', 'arguments': '{"format":"celsius","location":"杭州"}'}}]}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 297, 'total_tokens': 297}, 'stop': True}

工具生成结果对话请求示例

import json

import requests


def handle_stream(response: requests.Response):
    is_error = False
    for line in response.iter_lines():
        if line:
            line = line.decode('utf8').rstrip('\n').rstrip('\r')
            if line.startswith('event:error'):
                is_error = True
            elif line.startswith('data:'):
                line = line[len('data:'):]
                yield is_error, line
                if is_error:
                    break


api_key = "{YOUR_API_KEY}"
service_name = "aca-completion"

url = "https://nlp.aliyuncs.com/v2/api/completions"
headers = {
    "Content-Type": "application/json",
    "X-AcA-DataInspection": "enable",
    "x-fag-servicename": service_name,
    "x-fag-appcode": "aca",
    "Authorization": "Bearer " + api_key
}
payload = {
    "model": "xingchen-plus-v2",
    "user": "tester01",
    "max_tokens": 100,
    "stream": True,
    "messages":
        [
            {
                "name": "小义",
                "role": "system",
                "content": "你是一个对话Agent,你可以访问以下API,请自动规划当前最必要的API调用,尽你所能为用户提供帮助。"
            },
            {
                "role": "user",
                "content": "今天杭州天气怎么样"
            },
            {
                "role": "assistant",
                "content": "",
                "tool_calls":
                    [
                        {
                            "id": "weather",
                            "type": "function",
                            "function":
                                {
                                    "name": "weather",
                                    "arguments": "{\"format\":\"celsius\",\"location\":\"杭州\"}"
                                }
                        }
                    ]
            },
            {
                "tool_call_id": "001",
                "role": "tool",
                "content": "{\"location\": \"杭州\", \"temperature\": \"25\", \"unit\": \"celsius\", \"forecast\": \"sunny\"}"
            }
        ],
    "tools":
        [
            {
                "id": "001",
                "type": "function",
                "function":
                    {
                        "name": "weather",
                        "description": "通过调用天气预报API获取当地天气预报信息通过调用天气预报API获取当地天气预报信息",
                        "parameters":
                            {
                                "type": "object",
                                "properties":
                                    {
                                        "location":
                                            {
                                                "type": "string",
                                                "description": "地点"
                                            },
                                        "format":
                                            {
                                                "type": "string",
                                                "enum":
                                                    [
                                                        "celsius",
                                                        "fahrenheit"
                                                    ],
                                                "description": "温度单位"
                                            }
                                    },
                                "required":
                                    [
                                        "location",
                                        "format"
                                    ]
                            }
                    }
            }
        ]
}

response = requests.post(url, headers=headers, json=payload,stream=True)

for is_error, resp in handle_stream(response):
    if is_error:
        break
    print(json.loads(resp))

工具调用结果对话回复

{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237801, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237801, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好啊', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237801, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好啊,', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237801, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好啊,今天杭州的天气很不错', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237801, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好啊,今天杭州的天气很不错呢,气温是25摄氏', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237802, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'null', 'message': {'role': 'assistant', 'content': '您好啊,今天杭州的天气很不错呢,气温是25摄氏度,晴朗的天气让人感觉', 'tool_calls': []}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}, 'stop': False}
{'id': 'ab391320db822b8756f745c5b35681a3', 'created': 1715237802, 'model': 'xingchen-plus-v2', 'choices': [{'index': 1, 'finish_reason': 'stop', 'message': {'role': 'assistant', 'content': '您好啊,今天杭州的天气很不错呢,气温是25摄氏度,晴朗的天气让人感觉很舒服。', 'tool_calls': []}}], 'usage': {'prompt_tokens': 222, 'completion_tokens': 27, 'total_tokens': 279}, 'stop': True}