-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathvectorstore.py
179 lines (158 loc) · 5.63 KB
/
vectorstore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from abc import ABC, abstractmethod
from typing import Iterable, List, Optional
class VectorStore(ABC):
"""Interface for vector store."""
@abstractmethod
def add_question_answer(
self,
queries: Iterable[str],
codes: Iterable[str],
ids: Optional[Iterable[str]] = None,
metadatas: Optional[List[dict]] = None,
) -> List[str]:
"""
Add question and answer(code) to the training set
Args:
query: string of question
code: str
ids: Optional Iterable of ids associated with the texts.
metadatas: Optional list of metadatas associated with the texts.
kwargs: vectorstore specific parameters
Returns:
List of ids from adding the texts into the vectorstore.
"""
raise NotImplementedError(
"add_question_answer method must be implemented by subclass."
)
@abstractmethod
def add_docs(
self,
docs: Iterable[str],
ids: Optional[Iterable[str]] = None,
metadatas: Optional[List[dict]] = None,
) -> List[str]:
"""
Add docs to the training set
Args:
docs: Iterable of strings to add to the vectorstore.
ids: Optional Iterable of ids associated with the texts.
metadatas: Optional list of metadatas associated with the texts.
kwargs: vectorstore specific parameters
Returns:
List of ids from adding the texts into the vectorstore.
"""
raise NotImplementedError("add_docs method must be implemented by subclass.")
def update_question_answer(
self,
ids: Iterable[str],
queries: Iterable[str],
codes: Iterable[str],
metadatas: Optional[List[dict]] = None,
) -> List[str]:
"""
Update question and answer(code) to the training set
Args:
ids: Iterable of ids associated with the texts.
queries: string of question
codes: str
metadatas: Optional list of metadatas associated with the texts.
kwargs: vectorstore specific parameters
Returns:
List of ids from updating the texts into the vectorstore.
"""
pass
def update_docs(
self,
ids: Iterable[str],
docs: Iterable[str],
metadatas: Optional[List[dict]] = None,
) -> List[str]:
"""
Update docs to the training set
Args:
ids: Iterable of ids associated with the texts.
docs: Iterable of strings to update to the vectorstore.
metadatas: Optional list of metadatas associated with the texts.
kwargs: vectorstore specific parameters
Returns:
List of ids from adding the texts into the vectorstore.
"""
pass
def delete_question_and_answers(
self, ids: Optional[List[str]] = None
) -> Optional[bool]:
"""
Delete by vector ID or other criteria.
Args:
ids: List of ids to delete
Returns:
Optional[bool]: True if deletion is successful,
False otherwise
"""
raise NotImplementedError(
"delete_question_and_answers method must be implemented by subclass."
)
def delete_docs(self, ids: Optional[List[str]] = None) -> Optional[bool]:
"""
Delete by vector ID or other criteria.
Args:
ids: List of ids to delete
Returns:
Optional[bool]: True if deletion is successful,
False otherwise
"""
raise NotImplementedError("delete_docs method must be implemented by subclass.")
def delete_collection(self, collection_name: str) -> Optional[bool]:
"""
Delete the collection
Args:
collection_name (str): name of the collection
Returns:
Optional[bool]: _description_
"""
def get_relevant_question_answers(self, question: str, k: int = 1) -> List[dict]:
"""
Returns relevant question answers based on search
"""
raise NotImplementedError(
"get_relevant_question_answers method must be implemented by subclass."
)
def get_relevant_docs(self, question: str, k: int = 1) -> List[dict]:
"""
Returns relevant documents based search
"""
raise NotImplementedError(
"get_relevant_docs method must be implemented by subclass."
)
def get_relevant_question_answers_by_id(self, ids: Iterable[str]) -> List[dict]:
"""
Returns relevant question answers based on ids
"""
pass
def get_relevant_docs_by_id(self, ids: Iterable[str]) -> List[dict]:
"""
Returns relevant documents based on ids
"""
pass
@abstractmethod
def get_relevant_qa_documents(self, question: str, k: int = 1) -> List[str]:
"""
Returns relevant question answers documents only
Args:
question (_type_): list of documents
"""
raise NotImplementedError(
"get_relevant_qa_documents method must be implemented by subclass."
)
@abstractmethod
def get_relevant_docs_documents(self, question: str, k: int = 1) -> List[str]:
"""
Returns relevant question answers documents only
Args:
question (_type_): list of documents
"""
raise NotImplementedError(
"get_relevant_docs_documents method must be implemented by subclass."
)
def _format_qa(self, query: str, code: str) -> str:
return f"Q: {query}\n A: {code}"