Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
人工無能たいたん
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
hmatsu47
PRO
November 19, 2023
Technology
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
人工無能たいたん
JAWS-UG 名古屋 Amazon Bedrock ハンズオン 2023/11/20 LT
hmatsu47
PRO
November 19, 2023
More Decks by hmatsu47
See All by hmatsu47
中小企業に就職して転職せず30年。結果どうなった?
hmatsu47
PRO
0
97
JAWS ミート 2026 の「出し物」のゲームを Kiro と AI-DLC v2 で作った話
hmatsu47
PRO
0
31
ゲームで挑戦!VPC の気持ちになって IPv4/v6 パケットをルーティングしよう!
hmatsu47
PRO
0
35
続・名古屋城とデータセンター
hmatsu47
PRO
0
33
【再演】IPv6 VPC の実装パターンをいくつか
hmatsu47
PRO
0
38
名古屋城とデータセンター
hmatsu47
PRO
0
48
IPv6 に関する話
hmatsu47
PRO
0
38
さいきんの光ファイバーの話
hmatsu47
PRO
0
82
低いほうのレイヤを見てみる話
hmatsu47
PRO
0
40
Other Decks in Technology
See All in Technology
E2Eってつらくないですか?E2Eの管理コストを下げる手段とその実践
akaboshinit
6
2.7k
契約で守るコンパウンドプロダクトのデザインシステム
yug1224
1
1.6k
正解のない研修に放り出されて / Being thrown into a training program with no right answers
ohnoeight
1
170
データマネジメント × 法務で作りあげる、 freeeの新しいデータガバナンスへの軌跡
freee
PRO
2
560
デザインレビューは自動化できるのか?─ Flutter × Figma で UI 品質保証を考える
techiro
2
570
クラウドエージェントの先駆け Devin。今も最先端を走る、他との違い
kinopeee
1
510
AWSセキュリティで活用するAI機能
cmusudakeisuke
1
360
How we choose session proposals
kawaguti
PRO
1
190
AI駆動開発におけるハーネスエンジニアリングの勘所
tsukuboshi
1
860
Draft AIエージェントの トークン消費を最小化する 8つの原則
kinopeee
6
1.9k
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
1
160
キヨさん、67歳。AIコーディングエージェントに挑む
happysamurai294
0
120
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
Embracing the Ebb and Flow
colly
88
5.2k
Crafting Experiences
bethany
1
390
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
310
Building the Perfect Custom Keyboard
takai
2
930
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
230
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
290
Into the Great Unknown - MozCon
thekraken
41
2.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
590
GraphQLとの向き合い方2022年版
quramy
50
15k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
900
Transcript
人工無能たいたん JAWS-UG 名古屋 Amazon Bedrock ハンズオン 2023/11/20 まつひさ(hmatsu47)
自己紹介…は(また)書く時間がなかったのでスキップ 松久裕保(@hmatsu47) • https://qiita.com/hmatsu47 2
本日のネタ着想元 • 10 月に「吉祥寺.pm 34」で話した内容 3
パソコン通信むかしばなし 吉祥寺.pm34【オンライン】 2023/10/31 まつひさ(hmatsu47)
BBS ソフトウェアをリニューアル • 自作に切り替え ◦ 骨格は BASIC ◦ 通信制御とその周辺部分のみアセンブリ言語で書き換え ◦
日本語対応強化 ◦ ファイル転送対応(XMODEM) ◦ ついでにゲームを実装 ◦ おまけとしてチャットに人工無能を追加 5 ←これで思いついた
本日のネタ • Bedrock のモデルを使って文章をベクトル化して ◦ Titan Embeddings G1 - Text
• Vector store に突っ込んで ◦ pgvector(PostgreSQL) • 入力した文章に近い意味の文章を返すチャットを作る ◦ いわゆる人工無能(無脳)の一種 ▪ おうむ返し・唐突なリアクション 6
本日のネタ • こちらを参考に(LangChain を使って) ◦ あえて質問を Claude に投げないスタイルで実装 ▪ https://gihyo.jp/book/2023/978-4-297-13839-4
7
GitHub リポジトリはこちら • https://github.com/hmatsu47/munou-chat 8
Titan Embeddings G1 - Text • 文章ベクトル化のためのモデル ◦ 1536 次元のベクトルを出力
▪ OpenAI の text-embedding-ada-002 と同じ ◦ 日本語にも対応 ▪ Titan Text G1 - Express / Lite(プレビュー)は日本語未対応 9
他の文書ベクトル化方法(従来からあったもの) • TF-IDF(例:scikit-learn(sklearn)の TfidfVectorizer) ◦ 文章中に出てくる単語の頻度とレア度を使ってベクトル化 ▪ 事前に形態素解析して単語に分解した状態の文章を使う ▪ TfidfVectorizer
では単語の頻度・レア度を無視する使い方も可能 ▪ FAQ サイトの関連質問へのリンク生成などに使用 ▪ https://github.com/hmatsu47/vaccinecert-qa-similarity-test (旧ワクチン証明書アプリ FAQ) • Word2vec(2013 年) ◦ 機械学習のモデル(群)by Google の研究チーム 10
文章をベクトル化すると? • ベクトルを比較することで近い意味の文章が検索可能 ◦ ベクトル間のユークリッド距離、コサイン類似度などで比較 ▪ 近いものから順に文章を抽出 ◦ 多言語対応モデルを使うと違う言語間の検索(比較)が可能 ▪
例:「これは猫です」と「This is a cat」が近いものとして抽出 11
pgvector • PostgreSQL 用の拡張機能(Extension) ◦ ベクトルデータの保存・検索が可能に ▪ 16,000 次元までのベクトルに対応 ▪
ユークリッド距離(L2 距離)、コサイン類似度などを計算し検索 ▪ INDEX の作成も可能(検索・比較を高速化) ▪ https://github.com/pgvector/pgvector ◦ 今回は Docker イメージを利用 ▪ https://hub.docker.com/r/ankane/pgvector 12
LangChain から使う • ストア(pgvector)から入力に類似する文章を取得 13 # ベクターストアから応答を取得 bedrock_client = boto3.client('bedrock-runtime',
region_name="ap-northeast-1") embeddings = BedrockEmbeddings( client=bedrock_client, model_id="amazon.titan-embed-text-v1" ) store = PGVector( collection_name=COLLECTION_NAME, connection_string=CONNECTION_STRING, embedding_function=embeddings, ) docs = store.similarity_search_with_score(trimed_prompt) (中略) # 近い文章を返す response = docs[0][0].page_content 1つ目([0])の要素=入力文書に最も近い文章を返す
LangChain から使う • ストア(pgvector)に文章を追加 14 # ベクターストアに入力を追加 store.add_documents([Document(page_content=trimed_prompt)]) 文章を渡すとベクトル値と一緒に保存される
一方、本編(ハンズオン)では • RAG の Retriever として Kendra を使用 • Kendra
はセマンティック検索が可能 ◦ 人が入力(質問)した文の意味に沿った検索 15
デモ • 最初はおうむ返し → 言葉を覚えるとズレた会話に 16
正しく RAG として使うには? • ストアから取得した文章はプロンプトの文脈として渡す ◦ LangChain では RetrievalQA でストアの
retriever を渡す ▪ そうすれば Claude がちゃんとした答えを返してくれるはず • 会話履歴もあわせて渡す ◦ ただし一問一答式の QA の場合は除く 17
まとめ • Titan Embeddings G1 - Text は日本語文章のベクトル化 に使える •
機能は正しく使うべし ◦ 今回はちょっと変な(間違った)使い方をしてみた 18