Source code for langchain.vectorstores.dashvector
from __future__ import annotations
import logging
import uuid
from typing import (
    Any,
    Iterable,
    List,
    Optional,
    Tuple,
)
import numpy as np
from langchain.docstore.document import Document
from langchain.schema.embeddings import Embeddings
from langchain.schema.vectorstore import VectorStore
from langchain.utils import get_from_env
from langchain.vectorstores.utils import maximal_marginal_relevance
logger = logging.getLogger(__name__)
[docs]class DashVector(VectorStore):
    """`DashVector` vector store.
    To use, you should have the ``dashvector`` python package installed.
    Example:
        .. code-block:: python
            from langchain.vectorstores import dashvector
            from langchain.embeddings.openai import OpenAIEmbeddings
            import dashvector
            client = dashvector.Client.init(api_key="***")
            client.create("langchain")
            collection = client.get("langchain")
            embeddings = OpenAIEmbeddings()
            vectorstore = Dashvector(collection, embeddings.embed_query, "text")
    """
[docs]    def __init__(
        self,
        collection: Any,
        embedding: Embeddings,
        text_field: str,
    ):
        """Initialize with DashVector collection."""
        try:
            import dashvector
        except ImportError:
            raise ValueError(
                "Could not import dashvector python package. "
                "Please install it with `pip install dashvector`."
            )
        if not isinstance(collection, dashvector.Collection):
            raise ValueError(
                f"collection should be an instance of dashvector.Collection, "
                f"bug got {type(collection)}"
            )
        self._collection = collection