Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,132 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi, I am working on Azure OpenAI Studio Assistants, and I tried in playground and use API to get response. Trying to get same response content like the robot answered in playground.
{
"id": "msg_YGEQUbQhhBhmjS7O6rH4eAa6",
"object": "thread.message",
"created_at": 1718422913,
"assistant_id": null,
"thread_id": "thread_VU07MIEq2NvCT9h572V1Zr7L",
"run_id": null,
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "我想學烹飪課程",
"annotations": []
}
}
],
"file_ids": [],
"metadata": {}
}
], "first_id": "msg_YGEQUbQhhBhmjS7O6rH4eAa6", "last_id": "msg_YGEQUbQhhBhmjS7O6rH4eAa6", "has_more": false }from openai import AzureOpenAI
from dotenv import load_dotenv
import os
import time
load_dotenv('.env')
api_key = os.getenv("AZURE_OPENAI_API_KEY")
api_version = "2024-02-15-preview"
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
class AzureOpenAiAssistant:
client = AzureOpenAI(
api_key=api_key,
api_version=api_version,
azure_endpoint=azure_endpoint
)
def assistant_setup(self, instructions: str):
model = "wenshin-gpt-4o"
tools = [{"type": "code_interpreter"}]
assistant = self.client.beta.assistants.create(
instructions=instructions,
model=model,
tools=tools
)
return assistant
def assistant_run(self, assistant_id, instructions, userContent):
# Assistant setup
# assistant = self.assistant_setup(instructions)
# Create a thread
thread = self.client.beta.threads.create()
# Add a user question to the thread
message = self.client.beta.threads.messages.create(
thread_id=thread.id,
role='user',
content=userContent
)
# Run the thread
run = self.client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id
)
# Looping until the run completes or fails
while run.status in ['queued', 'in_progress', 'cancelling']:
time.sleep(1)
run = self.client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run.status == 'completed':
messages = self.client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages.data[0].content[0].text.value)
elif run.status == 'requires_action':
# the assistant requires calling some functions
# and submit the tool outputs back to the run
pass
else:
print(run.status)
ass = AzureOpenAiAssistant()
assistant_id = "asst_F0CqDxJ2464gjiwFspfwFhxZ"
instructions = "請根據使用者的訊息,從training_courses.csv,找出相關的課程,介紹並提供連結"
userContent = "我想學烹飪課程"
ass.assistant_run(assistant_id=assistant_id, instructions=instructions, userContent=userContent)
function create_thread() {
$.ajax({
url: `https://${endpoint}/openai/threads?api-version=2024-02-15-preview`,
crossDomain: true,
method: 'post',
headers: {
'api-key': API_KEY
},
contentType: 'application/json',
data: ''
}).done(function(response) {
context = JSON.stringify(response, null, 2);
console.log(context);
thread_id = response.id;
add_question_thread(thread_id);
});
}
function add_question_thread(thread_id) {
$.ajax({
url: `https://${endpoint}/openai/threads/${thread_id}/messages?api-version=2024-02-15-preview`,
crossDomain: true,
method: 'post',
headers: {
'api-key': API_KEY
},
contentType: 'application/json',
// data: '{\n "role": "user",\n "content": "hi"\n}',
data: JSON.stringify({
'role': 'user',
'content': '我想學烹飪課程'
})
}).done(function(response) {
context = JSON.stringify(response, null, 2);
console.log(context);
run_thread(thread_id);
});
}
function run_thread(thread_id) {
$.ajax({
url: `https://${endpoint}/openai/threads/${thread_id}/runs?api-version=2024-02-15-preview`,
crossDomain: true,
method: 'post',
headers: {
'api-key': API_KEY
},
contentType: 'application/json',
data: JSON.stringify({
'assistant_id': 'asst_Dv7ETnYdYW15wDOVjEVSHaxv'
})
}).done(function(response) {
context = JSON.stringify(response, null, 2);
console.log(context);
run_id = response.id;
get_run_status(thread_id, run_id);
});
}
function get_run_status(thread_id, run_id) {
$.ajax({
url: `https://${endpoint}/openai/threads/${thread_id}/runs/${run_id}`,
crossDomain: true,
headers: {
'api-key': API_KEY
},
data: {
'api-version': '2024-02-15-preview'
}
}).done(function(response) {
context = JSON.stringify(response, null, 2);
console.log(context);
get_assistant_response(thread_id);
});
}
function get_assistant_response(thread_id) {
$.ajax({
url: `https://${endpoint}/openai/threads/${thread_id}/messages`,
crossDomain: true,
headers: {
'api-key': API_KEY
},
contentType: 'application/json',
data: {
'api-version': '2024-02-15-preview'
}
}).done(function(response) {
// print response in json format
context = JSON.stringify(response, null, 2);
console.log(context);
});
}