openai / openai-node

The official Node.js / Typescript library for the OpenAI API
https://www.npmjs.com/package/openai
Apache License 2.0
7.3k stars 762 forks source link

batch error when upload file vector in file_search type tool assistant ia #805

Open raharinjatovo opened 2 months ago

raharinjatovo commented 2 months ago

Confirm this is a Node library issue and not an underlying OpenAI API issue

Describe the bug

when i upload file vector to file_seach type tool assistant, then I follow instruction from doc, i had that error when i process to finish data :`js file-batches.js:99 if (files === null || files.length == 0) { ^

TypeError: Cannot read properties of undefined (reading 'length') `

To Reproduce

1 install 2 create assistant ia tool type file_search 3 upload file for this tools

Code snippets

No response

OS

win 10

Node version

=20

Library version

=5.4.4

irakligeek commented 1 month ago

Same error

jfernandezrico commented 1 month ago

Same error with library version 4.46.1

wgrientidev commented 1 month ago

Hi.

Could you show some code? I had this error too while I was trying to implement an assistant with file_search.

I do:

 const fileData = await openai.files.create({
            file: fs.createReadStream("kit-idraulici.pdf"),
            purpose: "fine-tune",
        });

        let vectorStore = await openai.beta.vectorStores.create({
            name: "Test",
        });

        await openai.beta.vectorStores.files.createAndPoll(
            vectorStore.id,
            {
                file_id: fileData.id,
            }
        );

        await openai.beta.assistants.update(assistant.id, {
            tool_resources: { file_search: { vector_store_ids: [vectorStore.id] } },
        });

This works for me but I may have misunderstood your issue.

irakligeek commented 1 month ago

Ok this worked for me after changing these lines: Changed const fileStreams = fs.createReadStream(filePath); to

const fileData = await openai.files.create({
      file: fs.createReadStream(filePath),
      purpose: "fine-tune",
    });

And instead of

await openai.beta.vectorStores.fileBatches.uploadAndPoll(
      vectorStore.id,
      fileStreams
    );

I have added this code

await openai.beta.vectorStores.files.createAndPoll(vectorStore.id, {
      file_id: fileData.id,
    });

Essentially changing from fileBatches (call like it's shown in the official docs) to files call and passing the fileData.id resolved my error.

jfernandezrico commented 1 month ago

But what if the purpose of my assistant is "file_search". Following the Openai API documentation, I need to create a vector store with the new version of the library. https://platform.openai.com/docs/assistants/tools/file-search/quickstart

// Step 1: Create an Assistant with File Search Enabled
const myAssistant = await openai.beta.assistants.create({
       model: "gpt-3.5-turbo-1106",
       instructions:
        "You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
      name: "Customer Support Chatbot",
       tools: [{ type: "file_search" }],
 });

// Step 2: Upload files and add them to a Vector Store
const fileStreams = ["file1.txt"].map((path) =>
    fs.createReadStream(path)
  );

  // Create a vector store including our files.
  let vectorStore = await openai.beta.vectorStores.create({
    name: "Customer queries Vector Store",
  });

  await openai.beta.vectorStores.fileBatches.uploadAndPoll(
    vectorStore.id,
    fileStreams
  );

and here is where the error appears

rattrayalex commented 1 month ago

Thanks for reporting, we'll look into this soon.

Shalupanwar06 commented 1 month ago

Still getting the same error. Any update on the issue?

jfernandezrico commented 1 month ago

If you attach directly the file to the new thread, it works!

const knowledgeFile = await openai.files.create({
    file: fs.createReadStream("knowledge.txt"),
    purpose: "assistants",
  });

  const thread = await openai.beta.threads.create({
    messages: [
      {
        role: "user",
        content: "What can I buy in your online store?",
        // Attach the new file to the message.
        attachments: [
          { file_id: knowledgeFile.id, tools: [{ type: "file_search" }] },
        ],
      },
    ],
  });
hnputech commented 2 weeks ago

Hi.

Could you show some code? I had this error too while I was trying to implement an assistant with file_search.

I do:

  • Create files with openai.files.create()
  • Create vector store
  • CreateAndPoll vector store files.
  • Update assistant
 const fileData = await openai.files.create({
            file: fs.createReadStream("kit-idraulici.pdf"),
            purpose: "fine-tune",
        });

        let vectorStore = await openai.beta.vectorStores.create({
            name: "Test",
        });

        await openai.beta.vectorStores.files.createAndPoll(
            vectorStore.id,
            {
                file_id: fileData.id,
            }
        );

        await openai.beta.assistants.update(assistant.id, {
            tool_resources: { file_search: { vector_store_ids: [vectorStore.id] } },
        });

This works for me but I may have misunderstood your issue.

it is working fine with create and pull but when we use the updateAandPull it's throws the error can anyone able to resolve the issue ?

dgellow commented 2 weeks ago

Hi, thank you all for reporting that issue, we greatly appreciate the discussion and helpful details you already provided.

We will need a bit of time to investigate, it's not directly clear to me if the problem is from our SDK documentation, or OpenAI quick start guide being incorrect / outdated, or if the cause is an actual bug in the SDK or OpenAI backend (or a combination of all of this). I will do some testing on my side and review the thread internally with my colleagues. We will let you know once we have more details, but in the meantime feel free to keep this thread alive.

coloz commented 1 week ago

Same error

dews commented 1 week ago

This works for me, because uploadAndPoll's parameter is { files, fileIds = [] }

 const fileStreams = [
    "a.docx",
    "b.docx",
  ].map((path) => fs.createReadStream(path));

  // Create a vector store including our two files.
  let vectorStore = await openai.beta.vectorStores.create({
    name: "IDM docs",
  });

  await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, {
    files: fileStreams,
  });
Hussein-Ali-6 commented 1 week ago

As mentioned in the comment above, the issue is that the second parameter in uploadAndPoll accepts { files: Uploadable[]; fileIds?: string[] | undefined; }. I believe there are no issues with the SDK itself, but rather with the documentation. When I copied the code from the documentation into my TypeScript project, it showed a type error.

so instead of this (from documentation): await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, fileStreams)

add this code: await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, { files: fileStreams, });