Can't upload file through API

Zandarashvili, Levani 20 Reputation points
2024-11-21T20:13:43.6+00:00

I'm trying to upload files to Azure for fine-tuning, but I'm getting resource not available error.
I know that the client I creates is fine, because I tested it with the chat option. For example, if I run the following using my client:

client.chat.completions.create(
    messages=[{"content": "What is the weather like today?", "role": "user"}], model="gpt-4o"
)

It will work fine, but when I try to run a command to upload a file:

client.files.create(file=open("messages/train_messages.jsonl", "rb"), purpose="fine-tune")

It gives me an error: NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

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

Accepted answer
  1. navba-MSFT 27,550 Reputation points Microsoft Employee Moderator
    2024-11-22T05:21:51.6533333+00:00

    @Zandarashvili, Levani Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    .

    I was able to make this work at my end. I used the below sample code for the client.files.create:

    # Import necessary libraries
    from openai import AzureOpenAI
    import os
    
    # Initialize the Azure OpenAI client
    client = AzureOpenAI(
        api_key= "48bXXXXXXX3664",
        api_version="2024-08-01-preview",
        azure_endpoint = "https://XXXXX.openai.azure.com/"
    )
    
    # Create the assistant with the file_search tool
    assistant = client.beta.assistants.create(
        name="Financial Analyst Assistant",
        instructions="You are an expert financial analyst. Use your knowledge base to answer questions about audited financial statements.",
        model="gpt4o",
        tools=[{"type": "file_search"}]
    )
    
    # Create a vector store called "Financial Statements"
    vector_store = client.beta.vector_stores.create(name="Financial Statements")
    
    # Ready the files for upload to OpenAI
    file_paths = [r"C:\acs_paas\mydirectory\myfile1.pdf", r"C:\acs_paas\mydirectory\myfile2.pdf"]
    file_streams = [open(path, "rb") for path in file_paths]
    
    # Use the upload and poll SDK helper to upload the files, add them to the vector store,
    # and poll the status of the file batch for completion
    file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
        vector_store_id=vector_store.id, files=file_streams
    )
    
    # Print the status and the file counts of the batch to see the result of this operation
    print(file_batch.status)
    print(file_batch.file_counts)
    
    # Upload the user-provided file to OpenAI
    message_file = client.files.create(
        file=open(r"C:\acs_paas\mydirectory\myfile1.pdf", "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 shares were outstanding last quarter?",
                # Attach the new file to the message
                "attachments": [
                    {"file_id": message_file.id, "tools": [{"type": "file_search"}]}
                ],
            }
        ]
    )
    
    # The thread now has a vector store with that file in its tool resources
    print(thread.tool_resources.file_search)
    
    vector_store_ids = thread.tool_resources.file_search.vector_store_ids
    
    
    # Update the assistant with the correct vector_store_ids
    assistant123 = client.beta.assistants.update(
        assistant_id=assistant.id,
        description="test",
        instructions="You are an expert financial analyst. Use your knowledge base to answer questions about audited financial statements.",
        tools=[{
            "type": "file_search",
            "file_search": {
                "max_num_results": 5,
                "ranking_options": {
                    "score_threshold": 0.6
                }
            }
        }],
        tool_resources={"file_search": {"vector_store_ids": vector_store_ids}},
        temperature=0.00001,
        model="gpt4o"
    )
    

    .

    Action Plan:

    • Try with api-version: 2024-08-01-preview
    • Ensure that you have given the correct model name in your code.
    • Try with my above sample and check if that helps.

    .

    Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.

    2 people found this answer helpful.

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.