質問でベクトル検索して見つけた文書とともにLLMに聞くTypeScript
https://www.kaggle.com/code/markishere/day-2-document-q-a-with-rag を真似て作ってみるtakker.icon
ChromaDBというのを使っている
Vector Databaseらしい
Chroma
TypeScriptから動かせるかな
https://www.npmjs.com/package/chromadb
https://doc.deno.land/https://esm.sh/chromadb@1.10.4
npm:chromadbを使うようだ
内部でnpm:@google/generative-aiの https://ai.google.dev/api/embeddings?hl=ja#method:-models.batchembedcontents を使っている
実行方法
1. pip install choromadb
2. chroma run
3. 同じdirectoryに.envを作る
code:.env
GOOGLE_API_KEY=<your secret key>
4. 2.と別のterminalで↓を実行
$ deno run --allow-env=GOOGLE_API_KEY --allow-read --allow-net=i.gyazo.com,generativelanguage.googleapis.com,localhost https://scrapbox.io/api/code/work4ai/質問でベクトル検索して見つけた文書とともにLLMに聞くTypeScript/rag.ts
仕組み
localで立ち上げたChromaのserverでVector Databaseを作る
text-embedding-004でText embeddingを作り、serverに格納する
db.queryでqueryに似ているdocumentを見つける
queryと見つかったdocumentをpromptに埋め込んで、Gemini 2.0 Flashで聞く
実行したdirectoryに変なの生えてた
https://gyazo.com/e076982356437641f1ea3cc24be2f6cf
embeddingデータがsqlite3で保存されているのか
やりたいこと
これはserverが必要なタイプだけど、serverlessでembeddingと検索を出来ないだろうか?takker.icon
Gemini APIをOpenAIライブラリで動かすしてみたいtakker.icon
code:rag.ts
import "jsr:@std/dotenv@0.225/load";
import { GoogleGenerativeAI } from "npm:@google/generative-ai@0.21.0";
import {
ChromaClient,
GoogleGenerativeAiEmbeddingFunction,
} from "npm:chromadb@1";
const DOCUMENT1 =
"Operating the Climate Control System Your Googlecar has a climate control system that allows you to adjust the temperature and airflow in the car. To operate the climate control system, use the buttons and knobs located on the center console. Temperature: The temperature knob controls the temperature inside the car. Turn the knob clockwise to increase the temperature or counterclockwise to decrease the temperature. Airflow: The airflow knob controls the amount of airflow inside the car. Turn the knob clockwise to increase the airflow or counterclockwise to decrease the airflow. Fan speed: The fan speed knob controls the speed of the fan. Turn the knob clockwise to increase the fan speed or counterclockwise to decrease the fan speed. Mode: The mode button allows you to select the desired mode. The available modes are: Auto: The car will automatically adjust the temperature and airflow to maintain a comfortable level. Cool: The car will blow cool air into the car. Heat: The car will blow warm air into the car. Defrost: The car will blow warm air onto the windshield to defrost it.";
const DOCUMENT2 =
'Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.';
const DOCUMENT3 =
"Shifting Gears Your Googlecar has an automatic transmission. To shift gears, simply move the shift lever to the desired position. Park: This position is used when you are parked. The wheels are locked and the car cannot move. Reverse: This position is used to back up. Neutral: This position is used when you are stopped at a light or in traffic. The car is not in gear and will not move unless you press the gas pedal. Drive: This position is used to drive forward. Low: This position is used for driving in snow or other slippery conditions.";
const documents = DOCUMENT1, DOCUMENT2, DOCUMENT3;
const DB_NAME = "googlecardb";
const client = new ChromaClient();
const embeddingFunction = new GoogleGenerativeAiEmbeddingFunction({
googleApiKey: Deno.env.get("GOOGLE_API_KEY")!,
model: "models/text-embedding-004",
taskType: "RETRIEVAL_DOCUMENT",
taskTypeを後から変える方法がわからないtakker.icon
既成のGoogleGenerativeAiEmbeddingFunctionを使うのではなく、自前でEmbeddingFunctionを作成すべきか
taskTypeはprivateで宣言されているだけだから、@ts-ignoreを使って無理やり変えられはする
そうしよtakker.icon
code:rag.ts
});
ここまでは動くのだが、これ以降はlocalhostが立ち上がるだけで、全然動かない
ChromaClient.getOrCreateCollectionが何をしているのかがよくわからない
おそらくlocalhostでpythonのserverと通信しようとして、失敗している?
https://docs.trychroma.com/docs/overview/getting-started?lang=typescript
chromadbのpython packageが必要みたい?
$ pip install chromadb
やっぱりそうだった。chromaをlocal serverで動かす必要があった
code:rag.ts
const db = await client.getOrCreateCollection({
name: "googlecardb",
embeddingFunction,
});
await db.upsert({
documents,
ids: documents.map((_, i) => ${i}),
});
// Search the Chroma DB using the specified query.
// @ts-ignore workaround
embeddingFunction.taskType = "RETRIEVAL_QUERY";
const query = "How do you use the touchscreen to play music?";
const result = await db.query({
queryTexts: query,
nResults: 1,
});
const passage = result.documents;
console.log(passage);
const passageOneline = passage!.replace("\n", " ");
const queryOneline = query.replace("\n", " ");
const prompt =
`You are a helpful and informative bot that answers questions using text from the reference passage included below.
Be sure to respond in a complete sentence, being comprehensive, including all relevant background information.
However, you are talking to a non-technical audience, so be sure to break down complicated concepts and
strike a friendly and conversational tone. If the passage is irrelevant to the answer, you may ignore it.
QUESTION: ${queryOneline}
PASSAGE: ${passageOneline}`;
const model = new GoogleGenerativeAI(Deno.env.get("GOOGLE_API_KEY")!)
.getGenerativeModel({
model: "gemini-2.0-flash",
generationConfig: {
temperature: 1.0,
topK: 64,
topP: 0.95,
},
});
const answer = await model.generateContent(prompt);
console.log(answer.response.text());