Closed9

ChromaDBを試す。

🤨🤔😑😨😱🤨🤔😑😨😱

chromaの良さそうなところ

お金払わないと使えない有料機能がない。

PythonとJSから使える。

LangChainから使える。
https://python.langchain.com/docs/integrations/vectorstores/chroma

ChatGPT Retrieval Pluginから使える。
https://github.com/openai/chatgpt-retrieval-plugin#chroma

LlamaIndexから使える。
https://gpt-index.readthedocs.io/en/latest/examples/vector_stores/ChromaIndexDemo.html

🤨🤔😑😨😱🤨🤔😑😨😱

他に調べたDB

FAISS

ChatGPT Retrieval Pluginから使えない。
公式でJSから使う機能がない。(非公式のJS bindingはよくメンテナンスされてそう。)

MongoDB

ChatGPT Retrieval Pluginから使えない。
オンプレだとVector検索ができない。512MB以上保存するなら課金が必要。

🤨🤔😑😨😱🤨🤔😑😨😱

Getting startedをJSでやる。

https://docs.trychroma.com/getting-started?lang=js

🤨🤔😑😨😱🤨🤔😑😨😱

いきなりここで止まる。

const {OpenAIEmbeddingFunction} = require('chromadb');
const embedder = new OpenAIEmbeddingFunction({openai_api_key: "your_api_key"})
const collection = await client.createCollection({name: "my_collection", embeddingFunction: embedder})

Chromaのjs clientはAzure OpenAIに対応してないみたい。

以下ソース。Azure OpenAIに対応させること自体はそんなに難しくなさそう。

https://github.com/chroma-core/chroma/blob/main/clients/js/src/embeddings/OpenAIEmbeddingFunction.ts

とりあえずPythonでやるか。

🤨🤔😑😨😱🤨🤔😑😨😱

pythonだと簡単に登録できた。

main.py
import chromadb

chroma_client = chromadb.Client()

collection = chroma_client.create_collection("mycol")
collection.add(
    documents = ["Hello, World!","This is a test."],
    metadatas = [{"source": "mysrc",},{"source": "mysrc",}],
    ids = ["id1","id2"]
)
results = collection.query(query_texts = ["This is a query."],n_results = 2)

print(results)
$ python main.py
{'ids': [['id2', 'id1']], 'distances': [[1.0197579860687256, 1.7822961807250977]], 'metadatas': [[{'source': 'mysrc'}, {'source': 'mysrc'}]], 'embeddings': None, 'documents': [['This is a test.', 'Hello, World!']]}

Embeddingを何も指定しない場合、SentenceTransformersall-MiniLM-L6-v2でEmbeddingされる。

🤨🤔😑😨😱🤨🤔😑😨😱

ローカルに保存する。

chroma_client = chromadb.PersistentClient("mydb")
🤨🤔😑😨😱🤨🤔😑😨😱

こんな感じで保存された。

$ ls -hlR mydb   
mydb:
total 124K
drwxr-xr-x 6 user staff  192 Sep 25 22:30 2a81dca8-8f47-4d6f-b583-0f335964b5bc
-rw-r--r-- 1 user staff 124K Sep 25 22:30 chroma.sqlite3

mydb/2a81dca8-8f47-4d6f-b583-0f335964b5bc:
total 1.7M
-rw-r--r-- 1 user staff 1.6M Sep 25 22:30 data_level0.bin
-rw-r--r-- 1 user staff  100 Sep 25 22:30 header.bin
-rw-r--r-- 1 user staff 4.0K Sep 25 22:30 length.bin
-rw-r--r-- 1 user staff    0 Sep 25 22:30 link_lists.bin
🤨🤔😑😨😱🤨🤔😑😨😱

まとめ

機能は非常にシンプル。手元のパソコンで簡単に実験する用途で使える。
JSのクライアントはまだ整備されてない。現状だとPythonから使うことになる。
DBの使いやすさとしてはMongoDBには遠く及ばない。
ChromaDB独自のアルゴリズムとかがあるならいいんだけど。

このスクラップは2023/09/25にクローズされました