Uploading fiel to Azure OpenAi with "assistant" purpose using public SAS URL

Mohamed Hussein 45 Reputation points
2024-11-05T13:50:27.19+00:00

I'm trying to upload file to Azure OpenAi to be used at assistants using a public URL like blob SAS or so..

Appreciate your support

{
    "error": {
        "code": "invalidPayload",
        "message": "purpose contains an invalid purpose."
    }
}
POST /openai/files/import?api-version=2024-08-01-preview HTTP/1.1
Host: huswabagpt.openai.azure.com
api-key: xxxxxxxxxxxxxxxxxxxx
Content-Type: application/json
Content-Length: 166
{
  "content_url": "https://husazopenaipublic.blob.core.windows.net/public/966115219003/sampleai.pdf",
  "filename": "sampleai.pdf",
  "purpose": "assistants"
}

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,224 questions
{count} votes

Accepted answer
  1. santoshkc 9,240 Reputation points Microsoft Vendor
    2024-11-07T16:13:08.3333333+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.