V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
itskingname
V2EX  ›  OpenAI

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

  •  
  •   itskingname · 2023-08-14 18:42:54 +08:00 · 641 次点击
    这是一个创建于 471 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面这段代码在请求 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 友能看的出来是怎么回事吗?求帮忙。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1204 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:18 · PVG 02:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.