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
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
170
Other Decks in Technology
See All in Technology
10倍の生産性を実現するAI駆動並列エージェントのすべて
kumaiu
5
1.4k
200個のGitHubリポジトリを横断調査したかった
icck
0
120
Bedrock AgentCore RuntimeでAuth0 Changelog調査AIをアップグレードした話
t5u8a5a
1
110
データサイエンスを価値につなげるプロジェクト設計 〜 DS一年目が現場で得た気づき 〜
ysd113
1
210
連合学習と機密コンピューティング
lycorptech_jp
PRO
0
110
エラーバジェットのアラートのタイミングを考える.pdf
kairim0
0
140
【NRUG vol.18】なぜ多くのオブザーバビリティ導入は失敗するのか
nrug_member
0
100
FinOps × AIエージェントで実現する コストインシデントの自動調査
oasis1994liveforever
0
130
AIネイティブな開発のサプライチェーンリスク対策 〜激動の開発現場でリスクに立ち向かう〜【ZennFes】
cscengineer
PRO
2
100
SIer20年! 培ったスキルがスタートアップで輝く時
shucho0103
0
850
スキルと MCP ツール、責務をどう分けるか? AI が迷わないインターフェース設計の戦略
cdataj
1
990
AIのReact習熟度を測る
uhyo
2
240
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
58k
Site-Speed That Sticks
csswizardry
13
1.2k
Become a Pro
speakerdeck
PRO
31
6k
Statistics for Hackers
jakevdp
799
230k
Music & Morning Musume
bryan
47
7.2k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
410
How STYLIGHT went responsive
nonsquared
100
6.2k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
580
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
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 に登録 ユーザーの入力によるベクトル検索を行い、複数の関連する商品を提示できた
ベクトル検索を使ってみましょう!