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?
Try replacing client.completions.create()
to client.chat.completions.create()
. Upgrade your opeani
version if you haven’t done so. Below code is for openai_1.19.0
, should work with concurrent versions.
from azure_openai import AzureOpenAI
def generate_comment():
messages = [{"role" : "system", "content" : "You are an assitant"}]
messages = [{"role" : "user", "content" : "YOUR_PROMPT_HERE"}]
client = AzureOpenAI(
api_key="YOUR_API_KEY",
api_version="2023-09-15-preview",
azure_endpoint="YOUR_AZURE_OPENAI_ENDPOINT"
)
response = client.chat.completions.create(model="YOUR_DEPLOYED_MODEL_NAME",
messages=messages,
max_tokens=50,
temperature=0.35)
return response.choices[0].message.content
As you’re using os.getenv()
for several parameters, make sure you have those configured, otherwise you can copy-paste the strings directly.