Last active
December 15, 2025 05:17
-
-
Save kuc-arc-f/2b2e8258b581c0e739f5ef525385b7df to your computer and use it in GitHub Desktop.
chromadb ollama, python 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
| import chromadb | |
| import ollama | |
| import uuid | |
| # | |
| # | |
| # | |
| def test(): | |
| # クライアントの初期化 (ローカルにデータベースファイルを作成) | |
| # インメモリで実行する場合は chromadb.Client() を使用します | |
| client = chromadb.PersistentClient(path="./my_chroma_db") | |
| print("ChromaDB クライアントを初期化しました。") | |
| # コレクション名の指定 | |
| collection_name = "my_document_collection" | |
| # コレクションの作成または取得 | |
| # get_or_create を使うと、既に存在すればそれを取得し、なければ新しく作成します | |
| #collection = client.get_or_create_collection(name=collection_name) | |
| #print(f"コレクション '{collection_name}' を作成/取得しました。") | |
| # 'docs'コレクションが存在する場合は削除 | |
| if collection_name in [col.name for col in client.list_collections()]: | |
| client.delete_collection(name=collection_name) | |
| # | |
| # | |
| # | |
| if __name__ == "__main__": | |
| test() | |
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 chromadb | |
| import ollama | |
| import uuid | |
| # | |
| # | |
| # | |
| def add_embed(): | |
| # クライアントの初期化 (ローカルにデータベースファイルを作成) | |
| # インメモリで実行する場合は chromadb.Client() を使用します | |
| client = chromadb.PersistentClient(path="./my_chroma_db") | |
| print("ChromaDB クライアントを初期化しました。") | |
| # コレクション名の指定 | |
| collection_name = "my_document_collection" | |
| # コレクションの作成または取得 | |
| # get_or_create を使うと、既に存在すればそれを取得し、なければ新しく作成します | |
| collection = client.get_or_create_collection(name=collection_name) | |
| print(f"コレクション '{collection_name}' を作成/取得しました。") | |
| # 追加するデータ | |
| documents_to_add = [ | |
| "私の名前は山田太郎で、関西地域に住んでいます。", | |
| "美味しいラーメンを探していて、特に味噌味が好きです。", | |
| "今日は晴れていて、散歩に最適な天気です。", | |
| "私はエンジニアで、PythonとJavaScriptが得意です。", | |
| "私は公園が好きで、歩いて公園まで行きます。", | |
| ] | |
| # 各ドキュメントに対応する一意の ID (uuid などを使うと便利) | |
| ids_to_add = [str(uuid.uuid4()) for _ in documents_to_add] | |
| for i, d in enumerate(documents_to_add): | |
| response = ollama.embeddings(model="qwen3-embedding:0.6b", prompt=d) | |
| embedding = response["embedding"] | |
| collection.add( | |
| #ids=[str(i)], | |
| ids=[str(uuid.uuid4())], | |
| embeddings=[embedding], | |
| documents=[d] | |
| ) | |
| return | |
| # | |
| # | |
| # | |
| if __name__ == "__main__": | |
| add_embed() | |
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 chromadb | |
| import ollama | |
| import uuid | |
| # | |
| # | |
| # | |
| def test(): | |
| # クライアントの初期化 (ローカルにデータベースファイルを作成) | |
| # インメモリで実行する場合は chromadb.Client() を使用します | |
| client = chromadb.PersistentClient(path="./my_chroma_db") | |
| print("ChromaDB クライアントを初期化しました。") | |
| # コレクション名の指定 | |
| collection_name = "my_document_collection" | |
| # コレクションの作成または取得 | |
| # get_or_create を使うと、既に存在すればそれを取得し、なければ新しく作成します | |
| collection = client.get_or_create_collection(name=collection_name) | |
| print(f"コレクション '{collection_name}' を作成/取得しました。") | |
| # 検索クエリ | |
| #query_text = "今日 天気" | |
| query_text = "公園 好き" | |
| # クエリーテキストに対して埋め込みを生成 | |
| response = ollama.embeddings(prompt=query_text, model="qwen3-embedding:0.6b") | |
| #最も関連性の高いドキュメントを取得 | |
| results = collection.query( | |
| query_embeddings=[response["embedding"]], | |
| n_results=3 | |
| ) | |
| # 結果をdistancesでソートして表示 | |
| documents = results['documents'][0] | |
| distances = results['distances'][0] | |
| # (distance, document)のペアを作成してソート | |
| sorted_results = sorted(zip(distances, documents)) | |
| print("検索結果 (ベクトル距離が近い順):") | |
| for distance, doc in sorted_results: | |
| print(f"\nベクトル距離: {distance:.2f}") | |
| print(f"ドキュメント: {doc}") | |
| return | |
| # | |
| # | |
| # | |
| if __name__ == "__main__": | |
| test() | |
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 chromadb | |
| import uuid | |
| # クライアントの初期化 (ローカルにデータベースファイルを作成) | |
| # インメモリで実行する場合は chromadb.Client() を使用します | |
| client = chromadb.PersistentClient(path="./my_chroma_db") | |
| print("ChromaDB クライアントを初期化しました。") |
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
| annotated-types==0.7.0 | |
| anyio==4.12.0 | |
| attrs==25.4.0 | |
| backoff==2.2.1 | |
| bcrypt==5.0.0 | |
| build==1.3.0 | |
| cachetools==6.2.3 | |
| certifi==2025.11.12 | |
| charset-normalizer==3.4.4 | |
| chromadb==1.3.7 | |
| click==8.3.1 | |
| colorama==0.4.6 | |
| coloredlogs==15.0.1 | |
| distro==1.9.0 | |
| durationpy==0.10 | |
| filelock==3.20.0 | |
| flatbuffers==25.9.23 | |
| fsspec==2025.12.0 | |
| google-auth==2.43.0 | |
| googleapis-common-protos==1.72.0 | |
| grpcio==1.76.0 | |
| h11==0.16.0 | |
| hf-xet==1.2.0 | |
| httpcore==1.0.9 | |
| httptools==0.7.1 | |
| httpx==0.28.1 | |
| huggingface_hub==1.2.3 | |
| humanfriendly==10.0 | |
| idna==3.11 | |
| importlib_metadata==8.7.0 | |
| importlib_resources==6.5.2 | |
| jsonschema==4.25.1 | |
| jsonschema-specifications==2025.9.1 | |
| kubernetes==34.1.0 | |
| markdown-it-py==4.0.0 | |
| mdurl==0.1.2 | |
| mmh3==5.2.0 | |
| mpmath==1.3.0 | |
| numpy==2.3.5 | |
| oauthlib==3.3.1 | |
| ollama==0.6.1 | |
| onnxruntime==1.23.2 | |
| opentelemetry-api==1.39.1 | |
| opentelemetry-exporter-otlp-proto-common==1.39.1 | |
| opentelemetry-exporter-otlp-proto-grpc==1.39.1 | |
| opentelemetry-proto==1.39.1 | |
| opentelemetry-sdk==1.39.1 | |
| opentelemetry-semantic-conventions==0.60b1 | |
| orjson==3.11.5 | |
| overrides==7.7.0 | |
| packaging==25.0 | |
| posthog==5.4.0 | |
| protobuf==6.33.2 | |
| pyasn1==0.6.1 | |
| pyasn1_modules==0.4.2 | |
| pybase64==1.4.3 | |
| pydantic==2.12.5 | |
| pydantic_core==2.41.5 | |
| Pygments==2.19.2 | |
| PyPika==0.48.9 | |
| pyproject_hooks==1.2.0 | |
| pyreadline3==3.5.4 | |
| python-dateutil==2.9.0.post0 | |
| python-dotenv==1.2.1 | |
| PyYAML==6.0.3 | |
| referencing==0.37.0 | |
| requests==2.32.5 | |
| requests-oauthlib==2.0.0 | |
| rich==14.2.0 | |
| rpds-py==0.30.0 | |
| rsa==4.9.1 | |
| shellingham==1.5.4 | |
| six==1.17.0 | |
| sympy==1.14.0 | |
| tenacity==9.1.2 | |
| tokenizers==0.22.1 | |
| tqdm==4.67.1 | |
| typer==0.20.0 | |
| typer-slim==0.20.0 | |
| typing-inspection==0.4.2 | |
| typing_extensions==4.15.0 | |
| urllib3==2.3.0 | |
| uvicorn==0.38.0 | |
| watchfiles==1.1.1 | |
| websocket-client==1.9.0 | |
| websockets==15.0.1 | |
| zipp==3.23.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment