Can’t consume the Azure OpenAI API

441 views Asked by At

I seem unable to consume my Azure OpenAI API. The model is deployed. I’m getting the following error message:

“openai.error.InvalidRequestError: Must provide an ‘engine’ or ‘deployment_id’ parameter to create a <class ‘openai.api_resources.chat_completion.ChatCompletion’>”

import openai
import os
import load_dotenv

'''
AZURE_OPENAI_RESOURCE = 'AI_Sandbox'
AZURE_OPENAI_MODEL = 'gpt-3.5-turbo'
AZURE_OPENAI_KEY = ''
AZURE_OPENAI_ENDPOINT = 'https://{resource}.openai.azure.com/'
AZURE_OPENAI_MODEL_NAME = 'gpt-35-turbo'
AZURE_DEPLOYMENT_NAME = 'GPT-35-TURBO'
'''

# AOAI Integration Settings
AZURE_OPENAI_RESOURCE = os.environ.get("AZURE_OPENAI_RESOURCE")
AZURE_OPENAI_MODEL = os.environ.get("AZURE_OPENAI_MODEL")
AZURE_OPENAI_MODEL_NAME = os.environ.get("AZURE_OPENAI_MODEL_NAME")
AZURE_OPENAI_ENDPOINT = os.environ.get("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_KEY = os.environ.get("AZURE_OPENAI_KEY")
AZURE_DEPLOYMENT_NAME = os.environ.get("AZURE_DEPLOYMENT_NAME")

base_url = AZURE_OPENAI_ENDPOINT if AZURE_OPENAI_ENDPOINT else f"https://{AZURE_OPENAI_RESOURCE}.openai.azure.com/"
        
class OpenAIService:
    def __init__(self):
        openai.api_key = AZURE_OPENAI_KEY
        openai.api_type = "azure"
        openai.api_base = base_url
        openai.api_version = "2023-03-15-preview"

    def list_models(self):
        return openai.Model.list()

    def prompt(self, prompt):
        return openai.ChatCompletion.create(
            engine=AZURE_OPENAI_MODEL_NAME,
            deployment_id=AZURE_DEPLOYMENT_NAME,
            messages=[{"role": "user", "content": prompt}],
            temperature=0,
            timeout=60,
        )
1

There are 1 answers

0
Rishabh Meshram On

Based on the provided information, it seems the issue is you are passing

engine=AZURE_OPENAI_MODEL_NAME

seems both engine and deployment_id take deployment name as input value (You can use either of these parameter).

Below is the update code snippet: enter image description here

class OpenAIService:
    def __init__(self):
        openai.api_key = AZURE_OPENAI_KEY
        openai.api_type = "azure"
        openai.api_base = base_url
        openai.api_version = "2023-03-15-preview"

    def list_models(self):
        return openai.Model.list()

    def prompt(self, prompt):
        return openai.ChatCompletion.create(
            #You can use either of these
            engine="deploymentname1",
            deployment_id="deploymentname1",
            messages=[{"role": "user", "content": prompt}],
            temperature=0,
            timeout=60,
        )

if __name__ == "__main__":
    openai_service = OpenAIService()
    prompt = "What is the capital of France?"
    response = openai_service.prompt(prompt)
    print(response.choices[0])

With this I was able to get the required results: enter image description here