Skip to content

Commit be32382

Browse files
authored
feat(chroma): Add Search API (#34273)
1 parent 16c984e commit be32382

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

libs/partners/chroma/langchain_chroma/vectorstores.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import chromadb
1919
import chromadb.config
2020
import numpy as np
21-
from chromadb import Settings
21+
from chromadb import Search, Settings
2222
from chromadb.api import CreateCollectionConfiguration
2323
from langchain_core.documents import Document
2424
from langchain_core.embeddings import Embeddings
@@ -681,6 +681,52 @@ def add_texts(
681681
)
682682
return ids
683683

684+
def hybrid_search(self, search: Search) -> list[Document]:
685+
"""Run hybrid search with Chroma.
686+
687+
Args:
688+
search: The Search configuration for hybrid search.
689+
690+
Returns:
691+
A list of documents resulting from the search operation.
692+
693+
Example:
694+
from chromadb import Search, K, Knn, Rrf
695+
696+
# Create RRF ranking with text query
697+
hybrid_rank = Rrf(
698+
ranks=[
699+
Knn(query="query", return_rank=True, limit=300),
700+
Knn(query="query learning applications", key="sparse_embedding")
701+
],
702+
weights=[2.0, 1.0], # Dense 2x more important
703+
k=60
704+
)
705+
706+
# Build complete the search strategy
707+
search = (Search()
708+
.where(
709+
(K("language") == "en") &
710+
(K("year") >= 2020)
711+
)
712+
.rank(hybrid_rank)
713+
.limit(10)
714+
.select(K.DOCUMENT, K.SCORE, "title", "year")
715+
)
716+
717+
results = vector_store.hybrid_search(search)
718+
"""
719+
results = self._collection.search(search)
720+
return [
721+
Document(
722+
page_content=record["document"],
723+
metadata=record["metadata"],
724+
id=record["id"],
725+
)
726+
for record in results.rows()[0]
727+
if record["document"] is not None
728+
]
729+
684730
def similarity_search(
685731
self,
686732
query: str,

libs/partners/chroma/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies = [
1010
"langchain-core>=1.0.0,<2.0.0",
1111
"numpy>=1.26.0; python_version < '3.13'",
1212
"numpy>=2.1.0; python_version >= '3.13'",
13-
"chromadb>=1.0.20,<2.0.0",
13+
"chromadb>=1.3.5,<2.0.0",
1414
]
1515
name = "langchain-chroma"
1616
version = "1.0.0"

libs/partners/chroma/uv.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)