Call functions using Azure OpenAI

to call openai.ChatCompletion.create() with a set of messages and a function, but got an InvalidRequestError.

response = openai.ChatCompletion.create(
    engine="XXX", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
    messages=[
        {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
        {"role": "user", "content": "Calculate the circumference of circle with radius 5?"}
    ],
    functions=[{'name': 'circumference_calc', 'description': 'Calculate the circumference of circle given the radius', 'parameters': {'type': 'object', 'properties': {'radius': {'description': 'The radius of the circle', 'type': 'number'}}}, 'required': ['radius']}]
)

I got an InvalidRequestError when calling openai.ChatCompletion.create() with the above code that included messages and a function.

The issue you are facing is due to a syntax error in your code. The functions parameter should be a list of dictionaries, but you have a closing square bracket ] at the end of the parameter value. To fix this issue, remove the closing square bracket at the end of the functions parameter value. Here’s the corrected code:

response = openai.ChatCompletion.create(
    engine="XXX", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
    messages=[
        {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
        {"role": "user", "content": "Calculate the circumference of circle with radius 5?"}
    ],
    functions=[{'name': 'circumference_calc', 'description': 'Calculate the circumference of circle given the radius', 'parameters': {'type': 'object', 'properties': {'radius': {'description': 'The radius of the circle', 'type': 'number'}}}, 'required': ['radius']} # Remove the closing square bracket at the end of this line
)

By making this correction, the InvalidRequestError should no longer occur.