|
| 1 | +""" |
| 2 | +Ragbits Document Search Example: Weaviate |
| 3 | +
|
| 4 | +This example demonstrates how to use the `DocumentSearch` class to search for documents with a more advanced setup. |
| 5 | +We will use the `LiteLLMEmbedder` class to embed the documents and the query, the `WeaviateVectorStore` class to store |
| 6 | +the embeddings. |
| 7 | +
|
| 8 | +The script performs the following steps: |
| 9 | +
|
| 10 | + 1. Create a list of documents. |
| 11 | + 2. Initialize the `LiteLLMEmbedder` class with the OpenAI `text-embedding-3-small` embedding model. |
| 12 | + 3. Initialize the `WeaviateVectorStore` class with a `WeaviateAsyncClient` local instance and an index name. |
| 13 | + 4. Initialize the `DocumentSearch` class with the embedder and the vector store. |
| 14 | + 5. Ingest the documents into the `DocumentSearch` instance. |
| 15 | + 6. List all documents in the vector store. |
| 16 | + 7. Search for documents using a query. |
| 17 | + 8. Print the list of all documents and the search results. |
| 18 | +
|
| 19 | +To run the script, execute the following command: |
| 20 | +
|
| 21 | + ```bash |
| 22 | + uv run examples/document-search/weaviate_db.py |
| 23 | + ``` |
| 24 | +
|
| 25 | +Requires local Weaviate instance to be running, instructions how to set it up can be found here: https://weaviate.io/developers/weaviate/quickstart/local |
| 26 | +""" |
| 27 | + |
| 28 | +# /// script |
| 29 | +# requires-python = ">=3.10" |
| 30 | +# dependencies = [ |
| 31 | +# "ragbits-document-search", |
| 32 | +# "ragbits-core[weaviate]", |
| 33 | +# ] |
| 34 | +# /// |
| 35 | + |
| 36 | +import asyncio |
| 37 | + |
| 38 | +import weaviate |
| 39 | + |
| 40 | +from ragbits.core.audit import set_trace_handlers |
| 41 | +from ragbits.core.embeddings.dense import LiteLLMEmbedder |
| 42 | +from ragbits.core.vector_stores.base import VectorStoreOptions |
| 43 | +from ragbits.core.vector_stores.weaviate import WeaviateVectorStore |
| 44 | +from ragbits.document_search import DocumentSearch, DocumentSearchOptions |
| 45 | +from ragbits.document_search.documents.document import DocumentMeta |
| 46 | + |
| 47 | +set_trace_handlers("cli") |
| 48 | + |
| 49 | +documents = [ |
| 50 | + DocumentMeta.from_literal( |
| 51 | + """ |
| 52 | + RIP boiled water. You will be mist. |
| 53 | + """ |
| 54 | + ), |
| 55 | + DocumentMeta.from_literal( |
| 56 | + """ |
| 57 | + Why doesn't James Bond fart in bed? Because it would blow his cover. |
| 58 | + """ |
| 59 | + ), |
| 60 | + DocumentMeta.from_literal( |
| 61 | + """ |
| 62 | + Why programmers don't like to swim? Because they're scared of the floating points. |
| 63 | + """ |
| 64 | + ), |
| 65 | + DocumentMeta.from_literal( |
| 66 | + """ |
| 67 | + This one is completely unrelated. |
| 68 | + """ |
| 69 | + ), |
| 70 | +] |
| 71 | + |
| 72 | + |
| 73 | +async def main() -> None: |
| 74 | + """ |
| 75 | + Run the example. |
| 76 | + """ |
| 77 | + client = weaviate.use_async_with_local() |
| 78 | + embedder = LiteLLMEmbedder(model_name="text-embedding-3-small") |
| 79 | + vector_store = WeaviateVectorStore( |
| 80 | + client=client, |
| 81 | + index_name="jokes", |
| 82 | + embedder=embedder, |
| 83 | + ) |
| 84 | + document_search = DocumentSearch( |
| 85 | + vector_store=vector_store, |
| 86 | + ) |
| 87 | + |
| 88 | + await document_search.ingest(documents) |
| 89 | + |
| 90 | + all_documents = await vector_store.list() |
| 91 | + |
| 92 | + print() |
| 93 | + print("All documents:") |
| 94 | + print([doc.metadata["content"] for doc in all_documents]) |
| 95 | + |
| 96 | + query = "I'm boiling my water and I need a joke" |
| 97 | + vector_store_options = VectorStoreOptions( |
| 98 | + k=2, |
| 99 | + score_threshold=0.6, |
| 100 | + ) |
| 101 | + options = DocumentSearchOptions(vector_store_options=vector_store_options) |
| 102 | + results = await document_search.search(query, options=options) |
| 103 | + |
| 104 | + print() |
| 105 | + print(f"Documents similar to: {query}") |
| 106 | + print([element.text_representation for element in results]) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + asyncio.run(main()) |
0 commit comments