Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kemeny opened this issue Jun 10, 2024 · 1 comment
Open

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

kemeny opened this issue Jun 10, 2024 · 1 comment

Comments

@kemeny
Copy link

kemeny commented Jun 10, 2024

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

@athrael-soju
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants