Last active
December 16, 2025 11:11
-
-
Save kuc-arc-f/9ecb5dea98200065a9db4f80e46a96b6 to your computer and use it in GitHub Desktop.
node.js Chromadb , example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ChromaDB + Ollama を使用したベクトル検索のサンプルコード | |
| // 必要なパッケージ: | |
| // npm install chromadb ollama | |
| import { ChromaClient } from 'chromadb'; | |
| import ollama from 'ollama'; | |
| // ChromaDBクライアントの初期化 | |
| const chromaClient = new ChromaClient({ | |
| path: 'http://localhost:8000' // ChromaDBサーバーのURL | |
| }); | |
| // コレクション名 | |
| const COLLECTION_NAME = 'documents'; | |
| // Ollamaでテキストから埋め込みベクトルを生成 | |
| async function generateEmbedding(text) { | |
| const response = await ollama.embeddings({ | |
| model: "qwen3-embedding:0.6b", | |
| prompt: text | |
| }); | |
| return response.embedding; | |
| } | |
| // ドキュメントをChromaDBに追加 | |
| async function addDocuments() { | |
| try { | |
| // コレクションを作成または取得 | |
| const collection = await chromaClient.getOrCreateCollection({ | |
| name: COLLECTION_NAME, | |
| metadata: { description: 'サンプルドキュメント集' } | |
| }); | |
| // サンプルドキュメント | |
| const documents = [ | |
| { id: 'doc1', text: 'ChromaDBはベクトルデータベースです。', metadata: { category: 'database' } }, | |
| { id: 'doc2', text: 'Ollamaはローカルで大規模言語モデルを実行できます。', metadata: { category: 'llm' } }, | |
| { id: 'doc3', text: 'Node.jsはサーバーサイドJavaScriptの実行環境です。', metadata: { category: 'runtime' } } | |
| ]; | |
| // 各ドキュメントの埋め込みベクトルを生成 | |
| const embeddings = await Promise.all( | |
| documents.map(doc => generateEmbedding(doc.text)) | |
| ); | |
| // ドキュメントをコレクションに追加 | |
| await collection.add({ | |
| ids: documents.map(d => d.id), | |
| embeddings: embeddings, | |
| documents: documents.map(d => d.text), | |
| metadatas: documents.map(d => d.metadata) | |
| }); | |
| console.log('✅ ドキュメントを追加しました'); | |
| } catch (error) { | |
| console.error('❌ エラー:', error); | |
| } | |
| } | |
| // メイン実行関数 | |
| async function main() { | |
| console.log('🚀 ChromaDB + Ollama サンプルを開始します\n'); | |
| // ドキュメントを追加 | |
| await addDocuments(); | |
| } | |
| // 実行 | |
| main().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ChromaClient } from "chromadb"; | |
| // ChromaDBサーバーのアドレス | |
| const CHROMA_URL = "http://localhost:8000"; | |
| // 削除したいコレクションの名前 | |
| const COLLECTION_NAME = 'documents'; | |
| async function deleteSpecificCollection() { | |
| // 1. ChromaClient の初期化 | |
| const client = new ChromaClient({ path: CHROMA_URL }); | |
| try { | |
| // 2. コレクションの削除 | |
| // 成功した場合、このコレクションは完全に削除され、その中のすべてのデータ(埋め込みベクトル、メタデータなど)が失われます。 | |
| await client.deleteCollection({ name: COLLECTION_NAME }); | |
| console.log(`✅ コレクション '${COLLECTION_NAME}' は正常に削除されました。`); | |
| } catch (error) { | |
| console.error(`❌ コレクションの削除中にエラーが発生しました:`, error); | |
| // コレクションが存在しない場合などもここでエラーとして処理されます。 | |
| // エラーが "Collection not found" のような特定のエラーであれば、その旨を通知することもできます。 | |
| if (error.message && error.message.includes("does not exist")) { | |
| console.log(`💡 コレクション '${COLLECTION_NAME}' は存在しませんでした。`); | |
| } | |
| } | |
| } | |
| deleteSpecificCollection(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "chroma_1", | |
| "version": "1.0.0", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "description": "", | |
| "dependencies": { | |
| "chromadb": "^3.1.8", | |
| "ollama": "^0.6.3" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ChromaDB + Ollama を使用したベクトル検索のサンプルコード | |
| // 必要なパッケージ: | |
| // npm install chromadb ollama | |
| import { ChromaClient } from 'chromadb'; | |
| import ollama from 'ollama'; | |
| // ChromaDBクライアントの初期化 | |
| const chromaClient = new ChromaClient({ | |
| path: 'http://localhost:8000' // ChromaDBサーバーのURL | |
| }); | |
| // コレクション名 | |
| const COLLECTION_NAME = 'documents'; | |
| // Ollamaでテキストから埋め込みベクトルを生成 | |
| async function generateEmbedding(text) { | |
| const response = await ollama.embeddings({ | |
| model: "qwen3-embedding:0.6b", | |
| prompt: text | |
| }); | |
| return response.embedding; | |
| } | |
| // 類似ドキュメントを検索 | |
| async function searchDocuments(query) { | |
| try { | |
| const collection = await chromaClient.getCollection({ | |
| name: COLLECTION_NAME | |
| }); | |
| // クエリの埋め込みベクトルを生成 | |
| const queryEmbedding = await generateEmbedding(query); | |
| // 類似検索を実行 | |
| const results = await collection.query({ | |
| queryEmbeddings: [queryEmbedding], | |
| nResults: 3 // 上位3件を取得 | |
| }); | |
| console.log(`\n🔍 検索クエリ: "${query}"\n`); | |
| console.log('検索結果:'); | |
| results.documents[0].forEach((doc, i) => { | |
| console.log(`\n${i + 1}. ${doc}`); | |
| console.log(` 距離: ${results.distances[0][i].toFixed(4)}`); | |
| console.log(` メタデータ:`, results.metadatas[0][i]); | |
| }); | |
| return results; | |
| } catch (error) { | |
| console.error('❌ 検索エラー:', error); | |
| } | |
| } | |
| // メイン実行関数 | |
| async function main() { | |
| console.log('🚀 ChromaDB + Ollama サンプルを開始します\n'); | |
| // 類似検索を実行 | |
| await searchDocuments('データベース'); | |
| } | |
| // 実行 | |
| main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment