openai / openai-assistants-quickstart

OpenAI Assistants API quickstart with Next.js.
https://platform.openai.com/docs/assistants
MIT License
1.19k stars 210 forks source link

How do you handle files generated by the assistant? #25

Open kemeny opened 2 weeks ago

kemeny commented 2 weeks ago

Could you please share some insights on how files generated are handled?

athrael-soju commented 2 weeks ago

The example below is on the official doc: https://platform.openai.com/docs/assistants/tools/file-search/quickstart?lang=node.js

const stream = openai.beta.threads.runs
  .stream(thread.id, {
    assistant_id: assistant.id,
  })
  .on("textCreated", () => console.log("assistant >"))
  .on("toolCallCreated", (event) => console.log("assistant " + event.type))
  .on("messageDone", async (event) => {
    if (event.content[0].type === "text") {
      const { text } = event.content[0];
      const { annotations } = text;
      const citations: string[] = [];

      let index = 0;
      for (let annotation of annotations) {
        text.value = text.value.replace(annotation.text, "[" + index + "]");
        const { file_citation } = annotation;
        if (file_citation) {
          const citedFile = await openai.files.retrieve(file_citation.file_id);
          citations.push("[" + index + "]" + citedFile.filename);
        }
        index++;
      }

      console.log(text.value);
      console.log(citations.join("\n"));
    }

to retrieve generated images you can add this in the code snipped above

    .on('imageFileDone', async (image: { file_id: string}) => {
      const imageUrl = `\n![${image.file_id}](/api/files/${image.file_id})\n`;
    // And so on...
    })

Practical example can be found here: https://github.com/openai/openai-assistants-quickstart/blob/main/app/components/chat.tsx#L197