Azure GPT3.5-16k 的 function calling 在下面这个请求时一定会报错

2023-08-14 18:42:54 +08:00
 itskingname

下面这段代码在请求 Azure OpenAI GPT 接口时,一定会报错:

def calculator(num1, num2, operator):
    if operator == '+':
        value = round(num1 + num2, 2)
    elif operator == '-':
        value = round(num1 - num2, 2)
    elif operator == '*':
        value =  round(num1 * num2, 2)
    elif operator == '/':
        value = round(num1 / num2, 2)
    elif operator == '**':
        value =  round(num1 ** num2, 2)
    elif operator == 'sqrt':
        value = round(math.sqrt(num1), 2)
    else:
        return "Invalid operator"
    return str(value)


test_function = [
    {
            "name": "calculator",
            "description": "计算器函数,用来做基本的四则运算和乘方、根式运算。",
            "parameters": {
                "type": "object",
                "properties": {
                    "num1": {"type": "number", 'description': '数字'},
                    "num2": {"type": "number", 'description': '数字'},
                    "operator": {"type": "string", "enum": ["+", "-", "*", "/", "**", "sqrt"], 'description': '数学运算符'},
                },
                "required": ["num1", "num2", "operator"],
            },
    }
]

function_map = {
    'calculator': calculator
}



def test(messages):
    while True:
        print('=====================')
        print('请求参数:', messages)
        resp = query(messages, functions)  # 这个 query 函数就是去请求 gpt
        if not resp:
            print('接口暂时不稳定,稍后再次尝试')
            return
        print('%%%%%%', resp, '%%%%%%%%')
        if 'function_call' in resp:
            name = resp['function_call']['name']
            arguments = resp['function_call']['arguments']
            print(f'>>>>>>>>>>需要调用函数:{name},参数为:{arguments}')
            messages.append({
                'role': resp['role'],
                'name': name,
                'content': arguments
            })
            if name == 'python':
                ans = 'no python available, call other function'
            elif name in function_map:
                ans = function_map[name](**json.loads(arguments))
            else:
                ans = f'没有{name}函数,请重新分析'
            messages.append({
                'role': 'function',
                'name': name,
                'content': ans
            })
        else:
            print(resp)
            break

messages = [
    {'role': 'user', 'content': '1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 想加等于多少'}
]
test(messages)

日志为:

请求参数: [{'role': 'user', 'content': '1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 想加等于多少'}]
%%%%%% {'role': 'assistant', 'function_call': {'name': 'calculator', 'arguments': '{\n  "num1": 1,\n  "num2": 2,\n  "operator": "+"\n}'}} %%%%%%%%
>>>>>>>>>>需要调用函数:calculator ,参数为:{
  "num1": 1,
  "num2": 2,
  "operator": "+"
}
=====================
请求参数: [{'role': 'user', 'content': '1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 想加等于多少'}, {'role': 'assistant', 'name': 'calculator', 'content': '{\n  "num1": 1,\n  "num2": 2,\n  "operator": "+"\n}'}, {'role': 'function', 'name': 'calculator', 'content': '3'}]
%%%%%% {'role': 'assistant', 'function_call': {'name': 'calculator', 'arguments': '{\n  "num1": 3,\n  "num2": 3,\n  "operator": "+"\n}'}} %%%%%%%%
>>>>>>>>>>需要调用函数:calculator ,参数为:{
  "num1": 3,
  "num2": 3,
  "operator": "+"
}
=====================
请求参数: [{'role': 'user', 'content': '1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 想加等于多少'}, {'role': 'assistant', 'name': 'calculator', 'content': '{\n  "num1": 1,\n  "num2": 2,\n  "operator": "+"\n}'}, {'role': 'function', 'name': 'calculator', 'content': '3'}, {'role': 'assistant', 'name': 'calculator', 'content': '{\n  "num1": 3,\n  "num2": 3,\n  "operator": "+"\n}'}, {'role': 'function', 'name': 'calculator', 'content': '6'}]

触发报错

具体的报错信息如下,没有写任何原因:

{'error': {'message': 'The server had an error processing your request. Sorry about that! You can retry your request, or contact us through an Azure support request at: https://go.microsoft.com/fwlink/?linkid=2213926 if you keep seeing this error. (Please include the request ID 8d3afa4e-b91b-4a6c-bf2d-5f06febe952a in your email.)', 'type': 'server_error', 'param': None, 'code': None}} {'Content-Length': '412', 'Content-Type': 'application/json', 'access-control-allow-origin': '*', 'apim-request-id': '5284df6f-8575-4cdb-88fe-8b4a0cc9edb8', 'openai-model': 'gpt-35-turbo-16k', 'x-content-type-options': 'nosniff', 'openai-processing-ms': '2475.8569', 'x-ms-region': 'East US', 'x-request-id': '8d3afa4e-b91b-4a6c-bf2d-5f06febe952a', 'ms-azureml-model-error-reason': 'model_error', 'ms-azureml-model-error-statuscode': '500', 'x-ms-client-request-id': '5284df6f-8575-4cdb-88fe-8b4a0cc9edb8', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'azureml-model-session': 'turbo-669058-3', 'azureml-model-group': 'online', 'Date': 'Mon, 14 Aug 2023 09:59:18 GMT'}

有 V 友能看的出来是怎么回事吗?求帮忙。

653 次点击
所在节点    OpenAI
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/965231

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX