llama-index

Форк
0
143 строки · 4.3 Кб
1
"""
2
Upstash vector store index.
3

4
An index that is built with Upstash Vector.
5

6
https://upstash.com/docs/vector/overall/getstarted
7
"""
8

9
import logging
10
from typing import Any, List
11

12
from llama_index.legacy.schema import BaseNode
13
from llama_index.legacy.utils import iter_batch
14
from llama_index.legacy.vector_stores.types import (
15
    VectorStore,
16
    VectorStoreQuery,
17
    VectorStoreQueryMode,
18
    VectorStoreQueryResult,
19
)
20
from llama_index.legacy.vector_stores.utils import (
21
    metadata_dict_to_node,
22
    node_to_metadata_dict,
23
)
24

25
logger = logging.getLogger(__name__)
26

27
DEFAULT_BATCH_SIZE = 128
28

29

30
class UpstashVectorStore(VectorStore):
31
    """
32
    Upstash Vector Store.
33
    """
34

35
    stores_text: bool = True
36
    flat_metadata: bool = False
37

38
    @classmethod
39
    def class_name(cls) -> str:
40
        return "UpstashVectorStore"
41

42
    @property
43
    def client(self) -> Any:
44
        """Return the Upstash client."""
45
        return self._index
46

47
    def __init__(
48
        self, url: str, token: str, batch_size: int = DEFAULT_BATCH_SIZE
49
    ) -> None:
50
        """
51
        Create a UpstashVectorStore. The index can be created using the Upstash console.
52

53
        Args:
54
            url (String): URL of the Upstash Vector instance, found in the Upstash console.
55
            token (String): Token for the Upstash Vector Index, found in the Upstash console.
56
            batch_size (Optional[int]): Batch size for adding nodes to the vector store.
57

58
        Raises:
59
            ImportError: If the upstash-vector python package is not installed.
60
        """
61
        self.batch_size = batch_size
62

63
        try:
64
            from upstash_vector import Index
65
        except ImportError:
66
            raise ImportError(
67
                "Could not import upstash_vector.Index, Please install it with `pip install upstash-vector`"
68
            )
69

70
        self._index = Index(url=url, token=token)
71

72
    def add(self, nodes: List[BaseNode], **add_kwargs: Any) -> List[str]:
73
        """
74
        Add nodes to the vector store.
75

76
        Args:
77
            nodes: List of nodes to add to the vector store.
78
            add_kwargs: Additional arguments to pass to the add method.
79

80
        Returns:
81
            List of ids of the added nodes.
82
        """
83
        ids = []
84
        vectors = []
85
        for node_batch in iter_batch(nodes, self.batch_size):
86
            for node in node_batch:
87
                metadata_dict = node_to_metadata_dict(node)
88
                ids.append(node.node_id)
89
                vectors.append((node.node_id, node.embedding, metadata_dict))
90

91
            self.client.upsert(vectors=vectors)
92

93
        return ids
94

95
    def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
96
        """
97
        Delete node from the vector store.
98

99
        Args:
100
            ref_doc_id: Reference doc id of the node to delete.
101
            delete_kwargs: Additional arguments to pass to the delete method.
102
        """
103
        raise NotImplementedError(
104
            "Delete is not currently supported, but will be in the future."
105
        )
106

107
    def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
108
        """
109
        Query the vector store.
110

111
        Args:
112
            query: Query to run against the vector store.
113
            kwargs: Additional arguments to pass to the query method.
114

115
        Returns:
116
            Query result.
117
        """
118
        if query.mode != VectorStoreQueryMode.DEFAULT:
119
            raise ValueError(f"Query mode {query.mode} not supported")
120

121
        if query.filters:
122
            raise ValueError("Metadata filtering not supported")
123

124
        res = self.client.query(
125
            vector=query.query_embedding,
126
            top_k=query.similarity_top_k,
127
            include_vectors=True,
128
            include_metadata=True,
129
        )
130

131
        top_k_nodes = []
132
        top_k_ids = []
133
        top_k_scores = []
134
        for vector in res:
135
            node = metadata_dict_to_node(vector.metadata)
136
            node.embedding = vector.vector
137
            top_k_nodes.append(node)
138
            top_k_ids.append(vector.id)
139
            top_k_scores.append(vector.score)
140

141
        return VectorStoreQueryResult(
142
            nodes=top_k_nodes, similarities=top_k_scores, ids=top_k_ids
143
        )
144

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.