0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【RAGにも使える】苦労せずにFirestoreで類似度検索をする

Posted at

まとめ

  • FirestoreはKNNによる最近傍検索ができる
  • コレクショングループを対象にできる
  • インデックスが必要だが、作成のためのコマンドはエラーメッセージに出るので安心
  • ドキュメント埋め込みはVertex AIのtext-embedding-00X系が便利
    • Task Typeを選んでさらに性能を上げよう

Firestoreの類似検索

レコメンドシステムの要、類似検索がFirebaseで簡単にできるようになっています。
キーワード検索不要、ベクトル検索のみ必要であればalgoliaなどの外部サービスなしに実装できます。
2048次元まで対応しているので、機械学習モデルが出力する埋め込みベクトルでも余裕をもって対応できるはずです。

コレクショングループを対象にできる

Firebaseのコレクションはリソース階層を持たせることができ、セキュリティルール上重要です。
例えばユーザーのドキュメントを/users/:uid/*配下に格納し、uidが一致していない場合は拒否するルールを作ることが多いでしょう。
通常のクエリでは/users/:uid/someitemといった単一のコレクションしかクエリできませんが、コレクショングループを使用すると/users/*/someitemを対象として、横断的にクエリできます。

インデックス作成はエラーメッセージから行うのが簡単

公式ドキュメントにコマンドがありますが、地味にパラメータが多いので間違えやすいです。
作成に時間がかかるので、開発環境でエラーメッセージを確認してコピーペーストするのが簡単です。
ベクトル検索以外でも使用できるテクニックですが、ベクトルインデックスはfirestore.rulesファイルに反映されないので注意が必要です。

gcloud firestore indexes composite create \
--collection-group=collection-group \
--query-scope=COLLECTION \
--field-config field-path=vector-field,vector-config='vector-configuration' \
--database=database-id
`Missing vector index configuration. Please create the required index with the following gcloud command: gcloud firestore indexes composite create --project=broorec-7b512 --collection-group=posts --query-scope=COLLECTION_GROUP --field-config=order=ASCENDING,field-path=userId --field-config=vector-config='{"dimension":"768","flat": "{}"}',field-path=contentEmbeddings`,

おまけ

vertex aiの埋め込みモデルが面白い
https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/task-types?hl=ja

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?