Hi @Mohamed Hussein,
I'm glad to hear that my response was helpful to you. And thanks for sharing the feedback, which might be beneficial to other community members reading this thread as solution. Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", so I'll reiterate the previous response to an answer in case you'd like to accept the answer. This will help other users who may have a similar query find the solution more easily. If you have any further questions or concerns, please don't hesitate to ask. We're always here to help.
Query: Uploading fiel to Azure OpenAi with "assistant" purpose using public SAS URL
Solution:
import requests
from openai import AzureOpenAI
import os
import time
# Initialize the Azure OpenAI client
client = AzureOpenAI(
api_key="XXXXXXXXXXXXXXXXXXXXX", # Your API key
api_version="2024-05-01-preview",
azure_endpoint="https://XXXXXXXXXX.openai.azure.com/" # Your Azure endpoint
)
# URL of the file you want to upload (e.g., a blob SAS URL)
file_url = "BLOB_SAS_URL"
# Download the file to a local path
response = requests.get(file_url)
file_path = "sampleai.pdf"
with open(file_path, "wb") as file:
file.write(response.content)
# Use the downloaded file to upload for the assistant's purpose
message_file = client.files.create(
file=open(file_path, "rb"), purpose="assistants"
)
# Create a thread and attach the file to the message
thread = client.beta.threads.create(
messages=[{
"role": "user",
"content": "How many company..............",
# Attach the new file to the message
"attachments": [
{"file_id": message_file.id, "tools": [{"type": "file_search"}, {"type": "code_interpreter"}]}
],
}]
)
print(thread.id)
print(assistant.id)
# Run the thread
run = client.beta.threads.runs.create_and_poll(
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 = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
if run.status == 'completed':
messages = client.beta.threads.messages.list(thread_id=thread.id)
for message in messages:
if message.role == 'assistant':
print(message.content) # This will print the assistant's response, including the generated code
elif run.status == 'requires_action':
pass
else:
print(run.status)
# The thread now has a vector store with that file in its tool resources
print(thread.tool_resources.file_search)
Thank you.
Do click Accept Answer
and Yes
for was this answer helpful.