无法使用Azure OpenAI API

ukxgm1gy  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(168)

我似乎无法使用我的Azure OpenAI API。模型已部署。我收到以下错误消息:
“openai.error.InvalidRequestError:必须提供resources.chat”

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,
        )
wa7juj8i

wa7juj8i1#

根据提供的信息,似乎问题是你正在通过

engine=AZURE_OPENAI_MODEL_NAME

似乎enginedeployment_id都将deployment name作为输入值(您可以使用其中任何一个参数)。
下面是更新代码片段:

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])

这样,我就得到了所需的结果:

相关问题