-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anush008 <[email protected]> Co-authored-by: Michał Pstrąg <[email protected]>
- Loading branch information
Showing
23 changed files
with
706 additions
and
110 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,5 +95,6 @@ dist/ | |
|
||
# examples | ||
chroma/ | ||
qdrant/ | ||
|
||
.aider* |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
""" | ||
Ragbits Document Search Example: Qdrant | ||
This example demonstrates how to use the `DocumentSearch` class to search for documents with a more advanced setup. | ||
We will use the `LiteLLMEmbeddings` class to embed the documents and the query, the `QdrantVectorStore` class to store | ||
the embeddings. | ||
The script performs the following steps: | ||
1. Create a list of documents. | ||
2. Initialize the `LiteLLMEmbeddings` class with the OpenAI `text-embedding-3-small` embedding model. | ||
3. Initialize the `QdrantVectorStore` class with a `AsyncQdrantClient` in-memory instance and an index name. | ||
4. Initialize the `DocumentSearch` class with the embedder and the vector store. | ||
5. Ingest the documents into the `DocumentSearch` instance. | ||
6. List all documents in the vector store. | ||
7. Search for documents using a query. | ||
8. Print the list of all documents and the search results. | ||
To run the script, execute the following command: | ||
```bash | ||
uv run examples/document-search/qdrant.py | ||
``` | ||
""" | ||
|
||
# /// script | ||
# requires-python = ">=3.10" | ||
# dependencies = [ | ||
# "ragbits-document-search", | ||
# "ragbits-core[litellm,qdrant]", | ||
# ] | ||
# /// | ||
|
||
import asyncio | ||
|
||
from qdrant_client import AsyncQdrantClient | ||
|
||
from ragbits.core.embeddings.litellm import LiteLLMEmbeddings | ||
from ragbits.core.vector_stores.qdrant import QdrantVectorStore | ||
from ragbits.document_search import DocumentSearch, SearchConfig | ||
from ragbits.document_search.documents.document import DocumentMeta | ||
|
||
documents = [ | ||
DocumentMeta.create_text_document_from_literal( | ||
""" | ||
RIP boiled water. You will be mist. | ||
""" | ||
), | ||
DocumentMeta.create_text_document_from_literal( | ||
""" | ||
Why doesn't James Bond fart in bed? Because it would blow his cover. | ||
""" | ||
), | ||
DocumentMeta.create_text_document_from_literal( | ||
""" | ||
Why programmers don't like to swim? Because they're scared of the floating points. | ||
""" | ||
), | ||
DocumentMeta.create_text_document_from_literal( | ||
""" | ||
This one is completely unrelated. | ||
""" | ||
), | ||
] | ||
|
||
|
||
async def main() -> None: | ||
""" | ||
Run the example. | ||
""" | ||
embedder = LiteLLMEmbeddings( | ||
model="text-embedding-3-small", | ||
) | ||
vector_store = QdrantVectorStore( | ||
client=AsyncQdrantClient(":memory:"), | ||
index_name="jokes", | ||
) | ||
document_search = DocumentSearch( | ||
embedder=embedder, | ||
vector_store=vector_store, | ||
) | ||
|
||
await document_search.ingest(documents) | ||
|
||
all_documents = await vector_store.list() | ||
|
||
print() | ||
print("All documents:") | ||
print([doc.metadata["content"] for doc in all_documents]) | ||
|
||
query = "I'm boiling my water and I need a joke" | ||
vector_store_kwargs = { | ||
"k": 2, | ||
"max_distance": 0.6, | ||
} | ||
results = await document_search.search( | ||
query, | ||
config=SearchConfig(vector_store_kwargs=vector_store_kwargs), | ||
) | ||
|
||
print() | ||
print(f"Documents similar to: {query}") | ||
print([element.text_representation for element in results]) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains 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
This file contains 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
33 changes: 13 additions & 20 deletions
33
packages/ragbits-core/src/ragbits/core/vector_stores/__init__.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,27 @@ | ||
import sys | ||
|
||
from ..metadata_stores import get_metadata_store | ||
from ..utils.config_handling import get_cls_from_config | ||
from .base import VectorStore, VectorStoreEntry, VectorStoreOptions, WhereQuery | ||
from .in_memory import InMemoryVectorStore | ||
from ragbits.core.utils.config_handling import get_cls_from_config | ||
from ragbits.core.vector_stores.base import VectorStore, VectorStoreEntry, VectorStoreOptions, WhereQuery | ||
from ragbits.core.vector_stores.in_memory import InMemoryVectorStore | ||
|
||
__all__ = ["InMemoryVectorStore", "VectorStore", "VectorStoreEntry", "WhereQuery"] | ||
__all__ = ["InMemoryVectorStore", "VectorStore", "VectorStoreEntry", "VectorStoreOptions", "WhereQuery"] | ||
|
||
module = sys.modules[__name__] | ||
|
||
|
||
def get_vector_store(vector_store_config: dict) -> VectorStore: | ||
def get_vector_store(config: dict) -> VectorStore: | ||
""" | ||
Initializes and returns a VectorStore object based on the provided configuration. | ||
Args: | ||
vector_store_config: A dictionary containing configuration details for the VectorStore. | ||
config: A dictionary containing configuration details for the VectorStore. | ||
Returns: | ||
An instance of the specified VectorStore class, initialized with the provided config | ||
(if any) or default arguments. | ||
""" | ||
vector_store_cls = get_cls_from_config(vector_store_config["type"], module) | ||
config = vector_store_config.get("config", {}) | ||
if vector_store_config["type"].endswith("ChromaVectorStore"): | ||
return vector_store_cls.from_config(config) | ||
|
||
metadata_store_config = vector_store_config.get("metadata_store_config") | ||
return vector_store_cls( | ||
default_options=VectorStoreOptions(**config.get("default_options", {})), | ||
metadata_store=get_metadata_store(metadata_store_config), | ||
) | ||
Raises: | ||
KeyError: If the provided configuration does not contain a valid "type" key. | ||
InvalidConfigurationError: If the provided configuration is invalid. | ||
NotImplementedError: If the specified VectorStore class cannot be created from the provided configuration. | ||
""" | ||
vector_store_cls = get_cls_from_config(config["type"], sys.modules[__name__]) | ||
return vector_store_cls.from_config(config.get("config", {})) |
This file contains 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
Oops, something went wrong.