Skip to main content

llama-index vector_stores tablestore integration

Project description

LlamaIndex Vector_Stores Integration: Tablestore

Tablestore is a fully managed NoSQL cloud database service that enables storage of a massive amount of structured and semi-structured data.

This page shows how to use functionality related to the Tablestore vector database.

To use Tablestore, you must create an instance. Here are the creating instance instructions.

Example

pip install llama-index-vector-stores-tablestore
import os

import tablestore
from llama_index.core import MockEmbedding
from llama_index.core.schema import TextNode
from llama_index.core.vector_stores import (
    VectorStoreQuery,
    MetadataFilters,
    MetadataFilter,
    FilterCondition,
    FilterOperator,
)

from llama_index.vector_stores.tablestore import TablestoreVectorStore

# 1. create tablestore vector store
test_dimension_size = 4
store = TablestoreVectorStore(
    endpoint=os.getenv("end_point"),
    instance_name=os.getenv("instance_name"),
    access_key_id=os.getenv("access_key_id"),
    access_key_secret=os.getenv("access_key_secret"),
    vector_dimension=test_dimension_size,
    vector_metric_type=tablestore.VectorMetricType.VM_COSINE,
    # metadata mapping is used to filter non-vector fields.
    metadata_mappings=[
        tablestore.FieldSchema(
            "type",
            tablestore.FieldType.KEYWORD,
            index=True,
            enable_sort_and_agg=True,
        ),
        tablestore.FieldSchema(
            "time",
            tablestore.FieldType.LONG,
            index=True,
            enable_sort_and_agg=True,
        ),
    ],
)

# 2. create table and index
store.create_table_if_not_exist()
store.create_search_index_if_not_exist()

# 3. new a mock embedding for test
embedder = MockEmbedding(test_dimension_size)

# 4. prepare some docs
movies = [
    TextNode(
        id_="1",
        text="hello world",
        metadata={"type": "a", "time": 1995},
    ),
    TextNode(
        id_="2",
        text="a b c",
        metadata={"type": "a", "time": 1990},
    ),
    TextNode(
        id_="3",
        text="sky cloud table",
        metadata={"type": "a", "time": 2009},
    ),
    TextNode(
        id_="4",
        text="dog cat",
        metadata={"type": "a", "time": 2023},
    ),
    TextNode(
        id_="5",
        text="computer python java",
        metadata={"type": "b", "time": 2018},
    ),
    TextNode(
        id_="6",
        text="java python js nodejs",
        metadata={"type": "c", "time": 2010},
    ),
    TextNode(
        id_="7",
        text="sdk golang python",
        metadata={"type": "a", "time": 2023},
    ),
]
for movie in movies:
    movie.embedding = embedder.get_text_embedding(movie.text)

# 5. write some docs
ids = store.add(movies)
assert len(ids) == 7

# 6. delete docs
store.delete(ids[0])

# 7. query with filters
query_result = store.query(
    query=VectorStoreQuery(
        query_embedding=embedder.get_text_embedding("nature fight physical"),
        similarity_top_k=5,
        filters=MetadataFilters(
            filters=[
                MetadataFilter(
                    key="type", value="a", operator=FilterOperator.EQ
                ),
                MetadataFilter(
                    key="time", value=2020, operator=FilterOperator.LTE
                ),
            ],
            condition=FilterCondition.AND,
        ),
    ),
)
print(query_result)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_vector_stores_tablestore-0.2.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file llama_index_vector_stores_tablestore-0.2.2.tar.gz.

File metadata

File hashes

Hashes for llama_index_vector_stores_tablestore-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a8986b49dd343f4dd1076a28d258a8a503d7e90f5ed95bb75c52df0acaec50f2
MD5 32c721a0cbda149dec9576df75a9a6e9
BLAKE2b-256 5e44e7a3de4708793a959d7994bbb124f4c1c6c60a01d48398ce9c2be5fe0b4a

See more details on using hashes here.

File details

Details for the file llama_index_vector_stores_tablestore-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_vector_stores_tablestore-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f5b1647c0e6a8de246884fe51817947a075ab6bb9975cdb7b10d44d98a78a8b2
MD5 d96a696443dc245e4508877efe937f9a
BLAKE2b-256 7da52cc04e56066f760e5eb255e52cec71bec4f06e5ca1c6c189387a9f8aa3a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page