question_Paper / code_sample.py
abhishek2406's picture
app_file_correct1
84e5038
Raw History Blame Contribute Delete
1.76 kB
import os
import base64
from openai import AzureOpenAI
#from azure.identity import DefaultAzureCredential, get_bearer_token_provider
endpoint = os.getenv("ENDPOINT_URL", "https://learningabhishekquestionmakerapp.openai.azure.com/")
deployment = os.getenv("DEPLOYMENT_NAME", "gpt-4.1")
# Initialize Azure OpenAI client with Entra ID authentication
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
azure_endpoint=endpoint,
azure_ad_token_provider=token_provider,
api_version="2025-01-01-preview",
)
# IMAGE_PATH = "YOUR_IMAGE_PATH"
# encoded_image = base64.b64encode(open(IMAGE_PATH, 'rb').read()).decode('ascii')
chat_prompt = [
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an AI assistant that helps people find information."
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Hello How are you"
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! I'm just a computer program, so I don't have feelings, but I'm here and ready to help you. How can I assist you today?"
}
]
}
]
# Include speech result if speech is enabled
messages = chat_prompt
completion = client.chat.completions.create(
model=deployment,
messages=messages,
max_tokens=800,
temperature=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None,
stream=False
)
print(completion.to_json())