
openai-assistants-quickstartの微修正
概要 OpenAIのAssistants APIを用いたRAG( Retrieval-augmented generation)を用いたチャット画面の構築にあたり、以下のリポジトリを使用しました。 https://github.com/openai/openai-assistants-quickstart この時、citationの扱いについて修正が必要であったため、備忘録としてメモします。 背景 上記のリポジトリを使い、OpenAIのAssistants APIを用いたRAGを試みました。 この時、デフォルトの設定では、以下のように、「4:13†」のように、引用箇所を示す記号がそのまま表示されてしまいました。 対策 annotateLastMessageを以下のように修正しました。file_pathをfile_citationに変更することで、引用箇所を置換することができた。 一例として、以下では、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]; }); }; 結果、以下のようにリンクが表示されました。 まとめ 目的に応じて、置換の内容を変更できるかと思います。参考になりましたら幸いです。

















