| 1 | # Llama-Index |
| 2 |  |
| 3 | |
| 4 | ## Quick start |
| 5 | You would need to install the integration via `pip install llama-index-vector-stores-lancedb` in order to use it. |
| 6 | You can run the below script to try it out : |
| 7 | ```python |
| 8 | import logging |
| 9 | import sys |
| 10 | |
| 11 | # Uncomment to see debug logs |
| 12 | # logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 13 | # logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) |
| 14 | |
| 15 | from llama_index.core import SimpleDirectoryReader, Document, StorageContext |
| 16 | from llama_index.core import VectorStoreIndex |
| 17 | from llama_index.vector_stores.lancedb import LanceDBVectorStore |
| 18 | import textwrap |
| 19 | import openai |
| 20 | |
| 21 | openai.api_key = "sk-..." |
| 22 | |
| 23 | documents = SimpleDirectoryReader("./data/your-data-dir/").load_data() |
| 24 | print("Document ID:", documents[0].doc_id, "Document Hash:", documents[0].hash) |
| 25 | |
| 26 | ## For LanceDB cloud : |
| 27 | # vector_store = LanceDBVectorStore( |
| 28 | # uri="db://db_name", # your remote DB URI |
| 29 | # api_key="sk_..", # lancedb cloud api key |
| 30 | # region="your-region" # the region you configured |
| 31 | # ... |
| 32 | # ) |
| 33 | |
| 34 | vector_store = LanceDBVectorStore( |
| 35 | uri="./lancedb", mode="overwrite", query_type="vector" |
| 36 | ) |
| 37 | storage_context = StorageContext.from_defaults(vector_store=vector_store) |
| 38 | |
| 39 | index = VectorStoreIndex.from_documents( |
| 40 | documents, storage_context=storage_context |
| 41 | ) |
| 42 | lance_filter = "metadata.file_name = 'paul_graham_essay.txt' " |
| 43 | retriever = index.as_retriever(vector_store_kwargs={"where": lance_filter}) |
| 44 | response = retriever.retrieve("What did the author do growing up?") |
| 45 | ``` |
| 46 | |
| 47 | Checkout Complete example here - [LlamaIndex demo](../notebooks/LlamaIndex_example.ipynb) |
| 48 | |
| 49 | ### Filtering |
| 50 | For metadata filtering, you can use a Lance SQL-like string filter as demonstrated in the example above. Additionally, you can also filter using the `MetadataFilters` class from LlamaIndex: |
| 51 | ```python |
| 52 | from llama_index.core.vector_stores import ( |
| 53 | MetadataFilters, |
| 54 | FilterOperator, |
| 55 | FilterCondition, |
| 56 | MetadataFilter, |
| 57 | ) |
| 58 | |
| 59 | query_filters = MetadataFilters( |
| 60 | filters=[ |
| 61 | MetadataFilter( |
| 62 | key="creation_date", operator=FilterOperator.EQ, value="2024-05-23" |
| 63 | ), |
| 64 | MetadataFilter( |
| 65 | key="file_size", value=75040, operator=FilterOperator.GT |
| 66 | ), |
| 67 | ], |
| 68 | condition=FilterCondition.AND, |
| 69 | ) |
| 70 | ``` |
| 71 | |
| 72 | ### Hybrid Search |
| 73 | For complete documentation, refer [here](https://lancedb.github.io/lancedb/hybrid_search/hybrid_search/). This example uses the `colbert` reranker. Make sure to install necessary dependencies for the reranker you choose. |
| 74 | ```python |
| 75 | from lancedb.rerankers import ColbertReranker |
| 76 | |
| 77 | reranker = ColbertReranker() |
| 78 | vector_store._add_reranker(reranker) |
| 79 | |
| 80 | query_engine = index.as_query_engine( |
| 81 | filters=query_filters, |
| 82 | vector_store_kwargs={ |
| 83 | "query_type": "hybrid", |
| 84 | } |
| 85 | ) |
| 86 | |
| 87 | response = query_engine.query("How much did Viaweb charge per month?") |
| 88 | ``` |
| 89 | |
| 90 | In the above snippet, you can change/specify query_type again when creating the engine/retriever. |
| 91 | |
| 92 | ## API reference |
| 93 | The exhaustive list of parameters for `LanceDBVectorStore` vector store are : |
| 94 | - `connection`: Optional, `lancedb.db.LanceDBConnection` connection object to use. If not provided, a new connection will be created. |
| 95 | - `uri`: Optional[str], the uri of your database. Defaults to `"/tmp/lancedb"`. |
| 96 | - `table_name` : Optional[str], Name of your table in the database. Defaults to `"vectors"`. |
| 97 | - `table`: Optional[Any], `lancedb.db.LanceTable` object to be passed. Defaults to `None`. |
| 98 | - `vector_column_name`: Optional[Any], Column name to use for vector's in the table. Defaults to `'vector'`. |
| 99 | - `doc_id_key`: Optional[str], Column name to use for document id's in the table. Defaults to `'doc_id'`. |
| 100 | - `text_key`: Optional[str], Column name to use for text in the table. Defaults to `'text'`. |
| 101 | - `api_key`: Optional[str], API key to use for LanceDB cloud database. Defaults to `None`. |
| 102 | - `region`: Optional[str], Region to use for LanceDB cloud database. Only for LanceDB Cloud, defaults to `None`. |
| 103 | - `nprobes` : Optional[int], Set the number of probes to use. Only applicable if ANN index is created on the table else its ignored. Defaults to `20`. |
| 104 | - `refine_factor` : Optional[int], Refine the results by reading extra elements and re-ranking them in memory. Defaults to `None`. |
| 105 | - `reranker`: Optional[Any], The reranker to use for LanceDB. |
| 106 | Defaults to `None`. |
| 107 | - `overfetch_factor`: Optional[int], The factor by which to fetch more results. |
| 108 | Defaults to `1`. |
| 109 | - `mode`: Optional[str], The mode to use for LanceDB. |
| 110 | Defaults to `"overwrite"`. |
| 111 | - `query_type`:Optional[str], The type of query to use for LanceDB. |
| 112 | Defaults to `"vector"`. |
| 113 | |
| 114 | |
| 115 | ### Methods |
| 116 | |
| 117 | - __from_table(cls, table: lancedb.db.LanceTable) -> `LanceDBVectorStore`__ : (class method) Creates instance from lancedb table. |
| 118 | |
| 119 | - **_add_reranker(self, reranker: lancedb.rerankers.Reranker) -> `None`** : Add a reranker to an existing vector store. |
| 120 | - Usage : |
| 121 | ```python |
| 122 | from lancedb.rerankers import ColbertReranker |
| 123 | reranker = ColbertReranker() |
| 124 | vector_store._add_reranker(reranker) |
| 125 | ``` |
| 126 | - **_table_exists(self, tbl_name: `Optional[str]` = `None`) -> `bool`** : Returns `True` if `tbl_name` exists in database. |
| 127 | - __create_index( |
| 128 | self, scalar: `Optional[bool]` = False, col_name: `Optional[str]` = None, num_partitions: `Optional[int]` = 256, num_sub_vectors: `Optional[int]` = 96, index_cache_size: `Optional[int]` = None, metric: `Optional[str]` = "L2", |
| 129 | ) -> `None`__ : Creates a scalar(for non-vector cols) or a vector index on a table. |
| 130 | Make sure your vector column has enough data before creating an index on it. |
| 131 | |
| 132 | - __add(self, nodes: `List[BaseNode]`, **add_kwargs: `Any`, ) -> `List[str]`__ : |
| 133 | adds Nodes to the table |
| 134 | |
| 135 | - **delete(self, ref_doc_id: `str`) -> `None`**: Delete nodes using with node_ids. |
| 136 | - **delete_nodes(self, node_ids: `List[str]`) -> `None`** : Delete nodes using with node_ids. |
| 137 | - __query( |
| 138 | self, |
| 139 | query: `VectorStoreQuery`, |
| 140 | **kwargs: `Any`, |
| 141 | ) -> `VectorStoreQueryResult`__: |
| 142 | Query index(`VectorStoreIndex`) for top k most similar nodes. Accepts llamaIndex `VectorStoreQuery` object. |