pythongoogle-cloud-functionsopenai-apiazure-openai

AzureOpenAI: Missing credentials of `api_key`, `azure_ad_token`, `azure_ad_token_provider`


I'm trying to use the Azure OpenAI model to generate comments based on data from my BigQuery table in GCP using Cloud Functions. Here's the Python script I've been working on:

from azure_openai import AzureOpenAI
def generate_comment(month, year, country, column_name, current_value, previous_value):
        prompt_ = ("")
    
        client = AzureOpenAI(
            api_key=os.getenv("AZURE_OPENAI_API_KEY"), ## tried also api_key="AZURE_OPENAI_API_KEY"
            api_version="2023-09-15-preview",
            azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
        )
    
        response = client.completions.create(model="MODEL_NAME", prompt=prompt_, max_tokens=50, temperature=0.35)
        return response.choices[0].text

I tried the old version before, but got openai.lib._old_api.APIRemovedInV1 error:

openai.api_type = "azure"
openai.api_base = "https://xxx.openai.azure.com/"
openai.api_version = "2023-09-15-preview"
openai.api_key = "xxx"

response = openai.Completion.create(
engine="xxx",
prompt=prompt_,
temperature=0.35)

return response['choices'][0]['message']['content']

However, I'm encountering a 500 Internal Server Error with the message:

ValueError: Must provide one of the `base_url` or `azure_endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable

I've checked my Azure OpenAI configuration and ensured that the API key and endpoint are correct. Could someone please help me identify what might be causing this error?


Solution

  • It worked like the following:

    import openai
    # Set up the Azure OpenAI configuration
    openai.api_type = "azure"
    openai.api_base = "https://XXXX.openai.azure.com/"
    openai.api_key = "XXXX"
    openai.api_version = "XXXX"
    
    def generate_comment():
    prompt_ = ""
        messages = [
            {"role": "system", "content": "You will generate comments based on the given data."},
            {"role": "user", "content": prompt_}
        ]
        # Send a completion call to Azure OpenAI to generate a comment
        response = openai.ChatCompletion.create(
            engine="XXXX", # engine = "deployment_name"
            messages=[
                {"role": "system", "content": "You will generate comments based on the given data."},
                {"role": "user", "content": prompt_}
            ],
            max_tokens=50,
            temperature=0.35
        )
        return response['choices'][0]['message']['content']