mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
feat(chroma): Add Search API (#34273)
This commit is contained in:
@@ -18,7 +18,7 @@ from typing import (
|
|||||||
import chromadb
|
import chromadb
|
||||||
import chromadb.config
|
import chromadb.config
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from chromadb import Settings
|
from chromadb import Search, Settings
|
||||||
from chromadb.api import CreateCollectionConfiguration
|
from chromadb.api import CreateCollectionConfiguration
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.embeddings import Embeddings
|
from langchain_core.embeddings import Embeddings
|
||||||
@@ -681,6 +681,52 @@ class Chroma(VectorStore):
|
|||||||
)
|
)
|
||||||
return ids
|
return ids
|
||||||
|
|
||||||
|
def hybrid_search(self, search: Search) -> list[Document]:
|
||||||
|
"""Run hybrid search with Chroma.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
search: The Search configuration for hybrid search.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A list of documents resulting from the search operation.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
from chromadb import Search, K, Knn, Rrf
|
||||||
|
|
||||||
|
# Create RRF ranking with text query
|
||||||
|
hybrid_rank = Rrf(
|
||||||
|
ranks=[
|
||||||
|
Knn(query="query", return_rank=True, limit=300),
|
||||||
|
Knn(query="query learning applications", key="sparse_embedding")
|
||||||
|
],
|
||||||
|
weights=[2.0, 1.0], # Dense 2x more important
|
||||||
|
k=60
|
||||||
|
)
|
||||||
|
|
||||||
|
# Build complete the search strategy
|
||||||
|
search = (Search()
|
||||||
|
.where(
|
||||||
|
(K("language") == "en") &
|
||||||
|
(K("year") >= 2020)
|
||||||
|
)
|
||||||
|
.rank(hybrid_rank)
|
||||||
|
.limit(10)
|
||||||
|
.select(K.DOCUMENT, K.SCORE, "title", "year")
|
||||||
|
)
|
||||||
|
|
||||||
|
results = vector_store.hybrid_search(search)
|
||||||
|
"""
|
||||||
|
results = self._collection.search(search)
|
||||||
|
return [
|
||||||
|
Document(
|
||||||
|
page_content=record["document"],
|
||||||
|
metadata=record["metadata"],
|
||||||
|
id=record["id"],
|
||||||
|
)
|
||||||
|
for record in results.rows()[0]
|
||||||
|
if record["document"] is not None
|
||||||
|
]
|
||||||
|
|
||||||
def similarity_search(
|
def similarity_search(
|
||||||
self,
|
self,
|
||||||
query: str,
|
query: str,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ dependencies = [
|
|||||||
"langchain-core>=1.0.0,<2.0.0",
|
"langchain-core>=1.0.0,<2.0.0",
|
||||||
"numpy>=1.26.0; python_version < '3.13'",
|
"numpy>=1.26.0; python_version < '3.13'",
|
||||||
"numpy>=2.1.0; python_version >= '3.13'",
|
"numpy>=2.1.0; python_version >= '3.13'",
|
||||||
"chromadb>=1.0.20,<2.0.0",
|
"chromadb>=1.3.5,<2.0.0",
|
||||||
]
|
]
|
||||||
name = "langchain-chroma"
|
name = "langchain-chroma"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
|||||||
18
libs/partners/chroma/uv.lock
generated
18
libs/partners/chroma/uv.lock
generated
@@ -304,7 +304,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chromadb"
|
name = "chromadb"
|
||||||
version = "1.1.0"
|
version = "1.3.6"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "bcrypt" },
|
{ name = "bcrypt" },
|
||||||
@@ -336,13 +336,13 @@ dependencies = [
|
|||||||
{ name = "typing-extensions" },
|
{ name = "typing-extensions" },
|
||||||
{ name = "uvicorn", extra = ["standard"] },
|
{ name = "uvicorn", extra = ["standard"] },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/c4/da/29ecec2b5609a8e4f6e93af01a95b716b3448fc94ab002efe421abef8e8e/chromadb-1.1.0.tar.gz", hash = "sha256:50be29e2ad45f1ac0b15f57e04f48766cf1e61de0fcc5e8d31dd738a5a773b48", size = 1311824, upload-time = "2025-09-16T21:23:08.273Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/c0/5c/c8d751b327f863c11cc51e4cd01750696bacdc65b291beda8b008917910e/chromadb-1.3.6.tar.gz", hash = "sha256:834d7d154471b36bed10ddb53fcc96dfa912d18f0d57418490d829f7aad59895", size = 1959127, upload-time = "2025-12-10T05:25:22.644Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/9f/63/7b02737d537aba017e464271fc0a94659b90862a9f8f6648942c00eb0541/chromadb-1.1.0-cp39-abi3-macosx_10_12_x86_64.whl", hash = "sha256:edfd17f5e04f762622d19969daffc255ae06cc3a63d8f9e5b04f291177f4bd5f", size = 19132671, upload-time = "2025-09-16T21:23:05.679Z" },
|
{ url = "https://files.pythonhosted.org/packages/3b/6d/2a09221575a4fd7b6356c1416160d491fccd38ab6b24eee7df030552a7ac/chromadb-1.3.6-cp39-abi3-macosx_10_12_x86_64.whl", hash = "sha256:d80b9621af6dfdd23a4aced8e33f667ab41e8252c2a1c4f46eb27acbbb67fe48", size = 20782782, upload-time = "2025-12-10T05:25:20.205Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/52/8a/33ff83d0eaaa83875aedbfa220f651ae0ad6f6c1d997515fd47e8ee4c4b9/chromadb-1.1.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:e847329f1e93252ae416478db54021cf7e86fe50bffc87e1429ead22d1ad0789", size = 18214077, upload-time = "2025-09-16T21:23:02.958Z" },
|
{ url = "https://files.pythonhosted.org/packages/bf/8e/528f64a8ec1e32b9d079c77572e0e8301e1fb461474fefca6bce7ce90f8e/chromadb-1.3.6-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:1dee1987755d1dc920d9608f76eedb0f70ae06ade020d462f3265b290ca05b65", size = 20078289, upload-time = "2025-12-10T05:25:17.621Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/e2/f0/a31bddc426b03a80286cc23480ace5e174c7b39f070b99967cd7bedb9a18/chromadb-1.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b97dd7282fda79ef94ce20ee83b7cb62795231ecc657da5781bd2be4c55d9046", size = 18818050, upload-time = "2025-09-16T21:22:57.008Z" },
|
{ url = "https://files.pythonhosted.org/packages/33/1c/07bb66f9d8f243ae232fa9f73bcb63ae1386dc327cfa085feca7365c34ad/chromadb-1.3.6-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f1a59a2ef5121b0321ce1ffcdf84dfa1f11fba7c6734fe6b3b22a22c9c531314", size = 20703285, upload-time = "2025-12-10T05:25:11.594Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/00/39/5969bec252d6b174eeb68a5b23c88cbe4913a1e20d6b313ec628e5079c74/chromadb-1.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:472d44f634e27b7b0ee42c17771c42af19a786f003500eb540add6f475791363", size = 19841393, upload-time = "2025-09-16T21:23:00.108Z" },
|
{ url = "https://files.pythonhosted.org/packages/4b/5d/210639c32d3f6f49b265c84990be5da1bb6e1b2fd31f9845abec4580e3ba/chromadb-1.3.6-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2f98ee1e180c11aaf33f0aed11a54f385def8361931fcab8c876e56e94f02699", size = 21633675, upload-time = "2025-12-10T05:25:14.759Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/90/64/595af82790623f72ee8301fcbfca55192e8e1f2d65562a14bc549e596b06/chromadb-1.1.0-cp39-abi3-win_amd64.whl", hash = "sha256:4f3eaad5817b81d9f90ba2727a8b956b5428db64c0350252b5d919f1fd74632e", size = 19789778, upload-time = "2025-09-16T21:23:10.657Z" },
|
{ url = "https://files.pythonhosted.org/packages/59/91/bf5dfa3bd5b457a9195439e077128c274202b1a231bd3a9d4d7f3c2259a3/chromadb-1.3.6-cp39-abi3-win_amd64.whl", hash = "sha256:cde42db6c3b31b7edf75bbf60447cd83e9693fe08d2005496ae418c05cae9b3f", size = 21870402, upload-time = "2025-12-10T05:25:24.475Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -803,7 +803,7 @@ typing = [
|
|||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "chromadb", specifier = ">=1.0.20,<2.0.0" },
|
{ name = "chromadb", specifier = ">=1.3.5,<2.0.0" },
|
||||||
{ name = "langchain-core", editable = "../../core" },
|
{ name = "langchain-core", editable = "../../core" },
|
||||||
{ name = "numpy", marker = "python_full_version < '3.13'", specifier = ">=1.26.0" },
|
{ name = "numpy", marker = "python_full_version < '3.13'", specifier = ">=1.26.0" },
|
||||||
{ name = "numpy", marker = "python_full_version >= '3.13'", specifier = ">=2.1.0" },
|
{ name = "numpy", marker = "python_full_version >= '3.13'", specifier = ">=2.1.0" },
|
||||||
@@ -839,7 +839,7 @@ typing = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "langchain-core"
|
name = "langchain-core"
|
||||||
version = "1.1.1"
|
version = "1.1.3"
|
||||||
source = { editable = "../../core" }
|
source = { editable = "../../core" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "jsonpatch" },
|
{ name = "jsonpatch" },
|
||||||
|
|||||||
Reference in New Issue
Block a user