openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Must provide an 'engine' when using ChatCompletion api

Open vincilee2 opened this issue 1 year ago • 1 comments

Describe the bug

I followed the instructions from https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb but got an error: openai.error.InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a <class 'openai.api_resources.chat_completion.ChatCompletion'>

To Reproduce

using a Azure openai endpoint execute the following code snippets with your own api_key

import openai
openai.api_type = "azure"
# Example OpenAI Python library request
MODEL = "gpt-3.5-turbo"
response = openai.ChatCompletion.create(
    model=MODEL,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
)

response

Code snippets

import openai
openai.api_type = "azure"
# Example OpenAI Python library request
MODEL = "gpt-3.5-turbo"
response = openai.ChatCompletion.create(
    model=MODEL,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
)

response

OS

macOS

Python version

Python 3.9.13

Library version

openai-python-0.27.2

vincilee2 avatar Mar 19 '23 02:03 vincilee2

According to the documentation, Azure endpoints does not work with chatpcompletion yet. It is only working for completion, embedding, and fine-tuning operations for now.

https://github.com/openai/openai-python "Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, embedding, and fine-tuning operations. "

onemonstera avatar Mar 20 '23 06:03 onemonstera

I believe you need access to the Azure Chat Completions API preview. More here.

buswedg avatar Mar 26 '23 19:03 buswedg

I hit same issue and eventually I found that I need to first deploy a model on Azure portal. Then use parameter "engine" rather than "model" and pass the deployed engine name. It works for me.😄

iaalm avatar Mar 30 '23 14:03 iaalm

What worked for me was replacing

llm = AzureOpenAI(
    deployment_name="td2",
    model_name="text-davinci-002", 
)

For

llm = AzureOpenAI(
    engine="td2",
    model_name="text-davinci-002", 
)

joeyDIGs avatar Jun 13 '23 14:06 joeyDIGs