openai-apigpt-4openai-assistants-api

OpenAI Assistants API v2: Should I attach files to the thread or to the assistant?


I am using the OpenAI Assistants API, and I have the option to add my PDF files as a knowledge base.

Should I attach files to the thread or to the assistant?

I am super confused here.

Or should I add them both times?

What would work best?


Solution

  • When using the OpenAI Assistants API v2, you attach files when you add a user message to the thread by using the attachments parameter.

    In the code examples below, I passed a file with the file_search tool.

    Python:

    my_thread_message = client.beta.threads.messages.create(
        thread_id=my_thread.id,
        role="user",
        content=user_input,
        attachments=[  # 👈 Add files by using the attachments parameter
            {"file_id": file_id, "tools": [{"type": "file_search"}]}
        ],
    )
    

    Node.js:

    const myThreadMessage = await openai.beta.threads.messages.create(
      (thread_id = myThread.id),
      {
        role: "user",
        content: userInput,
        attachments: [ // 👈 Add files by using the attachments parameter
          {
            file_id: fileID,
            tools: [{ type: "file_search" }],
          },
        ],
      }
    );