Skip to main content

An integration package connecting VeDB and LangChain

Project description

VeDB

This notebook covers how to get started with the VeDB.

Setup

To access VeDB you'll need to create a/an ByteDance account, get an API key, and install the langchain-vedb-link integration package.

%pip install -qU "langchain-vedb-link>=MINIMUM_VERSION"

Initialization

import EmbeddingTabs from "@theme/EmbeddingTabs";

extra_column = {"source": "VARCHAR(255)"}   # Define the field information to be added to metadata

client_config = {"host": 'xxx.xxx.xxx.xxx',  # e.g., '192.168.1.88'
                 "port": 12345,
                 "user": 'vector_user',
                 "password": '123456',  # Leave empty if no password was set during authorization
                 "database": 'vectorStoreTest',  # Use your actual database name
                 "table_name": 'langchain_vector_test',  # Set an existing table name or initialize a new one
                 "charset": 'utf8mb4',
                 "metadata_columns": extra_column}     # Additional metadata columns
vector_store = ByteDanceVectorStore(embeddings, client_config)

Manage vector store

Add items to vector store

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])

You can also use the from_text function to directly instantiate a vector store from a document.

docs = ["foo","bar","bar"]

metadatas = [{"source": "https://example.com"},{"source": "https://example.com"},{"source": "https://example.com"}]

vector_store = ByteDanceVectorStore.from_texts(texts=docs, embedding=embeddings, metadatas=metadatas, client_config=client_config)

Delete items from vector store

vector_store.delete(ids=["3"])

Update items in vector store

updated_document_1 = Document(
    id = "1", page_content="qux", metadata={"source": "https://another-example.com"}
)

vector_store.update_document(document=updated_document_1)

Get documents by ids

You can directly get a specific document by its ID.

results = vector_store.get_by_ids(["1", "2", "3"])
for doc in results:
     print(f"* {doc.id} {doc.page_content} [{doc.metadata}]")

Query vector store

Once your vector store has been created and the relevant documents have been added, you will most likely wish to query it during the running of your chain or agent.

Filtering Support

The vectorstore supports a set of filters that can be applied against the metadata fields of the documents.

Operator Meaning/Category
$eq Equality (==)
$ne Inequality (!=)
$lt Less than (<)
$lte Less than or equal (<=)
$gt Greater than (>)
$gte Greater than or equal (>=)
$in Special Case (in)
$nin Special Case (not in)
$between Special Case (between)
$exists Exists (IS [NOT] NULL)
$like Text (like)
$ilike Text (case-insensitive like)
$and Logical (and)
$or Logical (or)

Query directly

A simple similarity search can be performed in the following way.

results = vector_store.similarity_search(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")

You can run the following to execute a similarity search and get the corresponding scores

results = vector_store.similarity_search_with_score(
    query="thud", k=1, filter={"source": "https://example.com"}    
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

If you provide a dict with multiple fields, but no operators, the top level will be interpreted as a logical AND filter

results = vector_store.similarity_search_with_score(
    query="thud", 
    k=10, 
    filter={"year": {"$in": [2004, 2022]}, "topic": {"$eq": "magic"}},
)
results = vector_store.similarity_search_with_score(
    query="thud", 
    k=10, 
    filter={
         "$and":[
         {"year": {"$in": [2004, 2025]}}, 
         {"topic": {"$eq": "magic"}},
         ]
     },
)

Limitations

After adding documents, the system builds the ANN index asynchronously in the background. Therefore, you need to wait a short period between adding documents and performing similarity searches. You can use sleep to pause for a while. To prevent potential crashes, you can run the following between adding documents and performing a similarity search.

vector_store.close()

❗️Needs to be modified

API reference

For detailed documentation of all ByteDanceVectorStore features and configurations head to the API reference: https://api.python.langchain.com/en/latest/vectorstores/langchain_vedb_link.vectorstores.VeDB.html

Related

  • Vector store conceptual guide
  • Vector store how-to guides

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

langchain_vedb-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

langchain_vedb-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_vedb-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_vedb-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.9.6 Darwin/24.6.0

File hashes

Hashes for langchain_vedb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 388c1008fed7a52d44854cbb242fe5a97ab66fa77577fced8ae3b5da95f78326
MD5 7bd7c419811fb9a4a5213704a6527c49
BLAKE2b-256 ed233baf3d4cf6e9eb37d12257e609562611a51aaa960a456a4c30f1773b7ed2

See more details on using hashes here.

File details

Details for the file langchain_vedb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_vedb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.9.6 Darwin/24.6.0

File hashes

Hashes for langchain_vedb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e3891e844cf34f0ad974f48969d6dc55bf4c415dcd1b64b42cd5cd7b2c46ec9
MD5 4914a3db0a746b68912cc82f3c5a0c46
BLAKE2b-256 5e8d711a2853159d8b4063a09d209a68048343eb19f6e91ee6625fca68996566

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