Overview

When building a chat interface using RAG (Retrieval-augmented generation) with OpenAI’s Assistants API, I used the following repository.

https://github.com/openai/openai-assistants-quickstart

A modification was needed regarding the handling of citation, so I am documenting it here as a memo.

Background

I used the above repository to try RAG with OpenAI’s Assistants API.

With the default settings, citation markers like “4:13†” were displayed as-is, as shown below.

Solution

I modified annotateLastMessage as follows. By changing file_path to file_citation, the citation markers could be replaced.

As an example, the following replaces them with links to the File API.

const annotateLastMessage = (annotations) => {
    // Get the current messages
    setMessages((prevMessages) => {
      const lastMessage = prevMessages[prevMessages.length - 1];
      const updatedLastMessage = {
        ...lastMessage,
      };

      annotations.forEach((annotation) => {
        if (annotation.type === "file_citation") {
          updatedLastMessage.text = updatedLastMessage.text.replaceAll(
            annotation.text,
            `[リンク](/api/files/${annotation.file_citation.file_id})`
          );
        }
      });
      return [...prevMessages.slice(0, -1), updatedLastMessage];
    });
  };

As a result, links are now displayed as shown below.

Summary

The replacement content can be changed depending on your purpose. I hope this serves as a helpful reference.