Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
20240425_nakamura.pdf
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Kazutaka Nakamura
April 26, 2024
Technology
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
20240425_nakamura.pdf
Kazutaka Nakamura
April 26, 2024
More Decks by Kazutaka Nakamura
See All by Kazutaka Nakamura
htmx for backend engineer
nakamurakzz
0
180
Other Decks in Technology
See All in Technology
事業価値を⽣み出すSREへ SREが担うべき意思決定の5層
kenta_hi
0
150
型は壁、Rustでもバグを直すな、表現できなくせよ
nwiizo
7
630
なぜ人は自分のプロジェクトを 「なんちゃってアジャイル」と 自嘲するのか
kozotaira
0
250
Keeping applications secure by evolving OAuth 2.0 and OpenID Connect
ahus1
PRO
1
140
飲食店もAIで。レジ締めやハンディシステムをつくってる話 / Using AI for restaurant management
vtryo
0
240
打造你的 AI 工作流:Agent Skill + MCP 實戰工作坊
appleboy
0
530
『AIに負けない』より『AIと遊ぶ』」〜ワクワクが最強のテスト・QA学習戦略_公開用
odan611
1
370
Hatena Engineer Seminar 37 jj1uzh
jj1uzh
0
420
AIに「使われる」時代のSaaS戦略 〜既存WebAPIのMCPサーバー化における開発ノウハウ〜
ekispert_api
0
290
次世代ランサムウェア対策の考察 / 20260704 Mitsutoshi Matsuo
shift_evolve
PRO
5
1.7k
#エンジニアBooks 30分でわかる 「技術記事を書く技術」 / engineer-books 2026-06-30
jnchito
1
180
プロンプト_きのこカンファレンス2026_LT
yurufuwahealer
0
130
Featured
See All Featured
So, you think you're a good person
axbom
PRO
2
2.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
170
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
620
The agentic SEO stack - context over prompts
schlessera
0
840
Site-Speed That Sticks
csswizardry
13
1.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
360
Utilizing Notion as your number one productivity tool
mfonobong
4
340
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
72
40k
Documentation Writing (for coders)
carmenintech
77
5.4k
The Cult of Friendly URLs
andyhume
79
6.9k
Transcript
Firestore の ベクトル検索で衣料品レコメンド機能を作ってみる 2024/04/25 Lazuli 株式会社 中村 和貴
自己紹介 所属: Lazuli 株式会社 ロール: Software Engineer 言語: Go, TypeScript
普段触っているサービス: Cloud Run, Pub/Sub 興味: システムアーキテクチャ・ソフトウェアアーキテクチャ ラスベガス歴: re:Invent 2022 に参加して以来 2 回目
本日のテーマ Firestore の Vector Search で 衣料品レコメンド機能を作ってみる
Opening Keynote でのデモ
Opening Keynote でのデモ
Opening Keynote でのデモ 誰でも簡単にベクトル検索を実装できるようになった
Vector Search
Vector Search https://cloud.google.com/blog/products/ai-machine-learning/grounding-gen-ai-in-enterprise-truth?hl=en
What's Vector Search ? データをベクトル(数値の配列)で表現し、類似度に基づいて検索する技術 従来のキーワード検索とは異なり、意味的な類似性を捉えることができる https://www.youtube.com/watch?v=7XI45ll8fqQ https://www.youtube.com/watch?v=MyIsz2BLTYc
Firestore の Vector Search を使用して作ったもの
まとめ 公開されているデータセットを使用して、商品の説明文を Gemini Pro 1.0 で生成 説明文のベクトルを生成し、プロダクトデータといっしょに Firestore に登録 ユーザーの入力によるベクトル検索を行い、複数の関連する商品を提示できた
ベクトル検索を使ってみましょう!
以下、ベクトル検索を実装してみた、の話
使用したデータセット BigQuery の公開プロダクトデータセットを使用 デモでは 500 件のデータを使用
プロダクトの説明文の生成 model: gemini-1.0-pro で商品の説明文を生成 Gemini models: https://ai.google.dev/gemini-api/docs/models/gemini?hl=ja アクセスレート(無料使用分) Gemini Pro
1.0 Gemini Pro 1.5 (Preview) - 15 req / 分 - 32,000 トークン / 分 - 1,500 req / 分 - 2 req / 分 - 32,000 トークン / 分 - 50 req / 分 https://ai.google.dev/pricing?hl=ja
プロダクトの説明文の生成 プロダクトデータを元に説明文を生成 func generateProductInfo(ctx context.Context, products string) (string, error) {
prompt := fmt.Sprintf(` あなたはオンラインショップのダミーデータを生成するAI アシスタントです。商品データは以下の通りです。 '''csv id,cost,category,name,brand,retail_price,department,sku,distribution_center_id %s ''' 商品の説明文を各商品の特長や素材、シーズン、対象、サイズ等をふまえ、100~150 文字程度で1 行の英語で記述してください。`, products) resp, _ := model.GenerateContent(ctx, genai.Text(prompt)) content := resp.Candidates[0].Content.Parts[0].(genai.Text) return string(content), nil } Go SDK for Google Generative AI: https://github.com/google/generative-ai-go
生成された説明文例 プロダクト名: Calvin Klein Men's 3 Pack No Show Liner
Socks 生成された説明文: " カルバン・クラインの男性用ノーショーライナーソックス3 足組で、快適な履き心地 をキープしよう。コットンとスパンデックス製でソフトで通気性に優れたフィット 感。普段履きに最適なこのソックスは、便利な3 足組で滑り止めグリップ付き"
(memo) 生成エラー! Input: 10576,3218,Intimates,Calvin Klein Women's Sexy Signature Demi Bra,Calvin
Klein,41.900001525878906,Women,433F3EB6C5B7BD3FB4A2D0E8756C6836, 3 Error: blocked: candidate: FinishReasonSafety
説明文のベクトル化 Embedding (ベクトル化)モデル: embedding-001 を使用 func generateEmbbedingValue(ctx context.Context, text string)
([]float32, error) { vertexAIClient, _ := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY"))) embedder := vertexAIClient.EmbeddingModel("embedding-001") resp, _ := embedder.EmbedContent(ctx, genai.Text(text)) return resp.Embedding.Values, nil } Go SDK for Google Generative AI: https://github.com/google/generative-ai-go embedding-001: https://ai.google.dev/gemini- api/tutorials/document_search#api_changes_to_embeddings_with_model_embedding-001
プロダクトデータと説明文のベクトルを Firestore に登録 import { Firestore, FieldValue } from "@google-cloud/firestore";
import fs from "fs"; const db = new Firestore({ projectId: process.env.PJ_ID, databaseId: process.env.DB_ID,}); const addDocuments = async () => { const records = parseToJson(fs.readFileSync("products.csv")); for (const record of records) { const vector = parseVector(rowVector); // 文字の行列データを整形 const doc = { ...record, embedding_field: FieldValue.vector(vector) }; await db.collection(process.env.COLLECTION_NAME).add(doc); } }; Google AI JavaScript SDK: https://github.com/google-gemini/generative-ai-js
ベクトルを含むプロダクトデータと Firestore に登録
Firestore のベクトル検索 ベクトルの類似度の計算方法を選択可能 EUCLIDEAN 距離 コサイン類似度 ドット積 制限事項 ベクトル検索では、リアルタイムデータの取得は出来ない 不等式フィルタを使用してデータを事前にフィルタリングが出来ない
Firestore のベクトル検索がサポートされているのは、Node.js と Python のみ https://cloud.google.com/firestore/docs/vector-search?hl=ja
Firestore のベクトル検索 import { Firestore, FieldValue, VectorQuery, VectorQuerySnapshot } from
"@google-cloud/firestore"; import { GoogleGenerativeAI } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY ?? ""); const model = genAI.getGenerativeModel({model: "embedding-001"}); const searchDoc = async (message: string) => { const result = await model.embedContent(message); const vectorQuery: VectorQuery = db.collection("products").findNearest( "embedding_field", FieldValue.vector(result.embedding.values), { limit: 5, distanceMeasure: "COSINE" } ); const vectorQuerySnapshot: VectorQuerySnapshot = await vectorQuery.get(); const res: any[] = []; vectorQuerySnapshot.forEach((doc) => res.push(doc.data())); return res; };
Demo "I'm going to the beach this summer, so I'd
like to get a new swimsuit."
まとめ 公開されているデータセットを使用して、商品の説明文を Gemini Pro 1.0 で生成 説明文のベクトルを生成し、プロダクトデータといっしょに Firestore に登録 ユーザーの入力によるベクトル検索を行い、複数の関連する商品を提示できた
ベクトル検索を使ってみましょう!