-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add elastic search wrapper #509
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import List | ||
|
||
import numpy as np | ||
|
||
from gptcache.manager.vector_data.base import VectorBase, VectorData | ||
from gptcache.utils.log import gptcache_log | ||
from elasticsearch import Elasticsearch | ||
from elasticsearch.exceptions import NotFoundError | ||
|
||
|
||
class ElasticSearchStore(VectorBase): | ||
def __init__( | ||
self, | ||
host: str = "localhost", | ||
port: str = "9200", | ||
username: str = "", | ||
password: str = "", | ||
dimension: int = 0, | ||
collection_name: str = "gptcache", | ||
top_k: int = 1, | ||
namespace: str = "", | ||
): | ||
self._client = Elasticsearch("http://localhost:9200") | ||
self.top_k = top_k | ||
self.dimension = dimension | ||
self.collection_name = collection_name | ||
self.namespace = namespace | ||
self.doc_prefix = f"{self.namespace}doc:" | ||
self.create_collection(collection_name) | ||
|
||
def _create_collection(self, collection_name): | ||
if self._check_index_exists(collection_name): | ||
gptcache_log.info( | ||
"The %s already exists, and it will be used directly", collection_name | ||
) | ||
else: | ||
gptcache_log.info("Index does not exist") | ||
mappings = { | ||
"properties": { | ||
"text": {"type": "text"}, | ||
"vector": {"type": "dense_vector", "dims": self.dimension}, | ||
} | ||
} | ||
self._client.indices.create(index=collection_name, mappings=mappings) | ||
|
||
def _check_index_exists(self, index_name): | ||
try: | ||
return self._client.exists(index=index_name) | ||
except NotFoundError: | ||
return False | ||
|
||
def mul_add(self, datas: List[VectorData]): | ||
for data in datas: | ||
id: int = data.id | ||
doc = { | ||
"_index": self.collection_name, | ||
"_id": id, | ||
"_source": {"vector": data.data.tolist()}, | ||
} | ||
|
||
def search(self, data: np.ndarray, top_k: int = -1): | ||
search_body: dict = { | ||
"query": { | ||
"script_score": { | ||
"query": {"match_all": {}}, | ||
"script": { | ||
"source": "cosineSimilarity(params.queryVector, 'vector') + 1.0", | ||
"params": {"queryVector": data}, | ||
}, | ||
} | ||
}, | ||
"size": top_k, | ||
"sort": [{"_score": {"order": "desc"}}], | ||
} | ||
|
||
results = self._client.search(index=self.collection_name, body=search_body) | ||
return [ | ||
(float(result["_score"]), int(result["_id"])) | ||
for result in results["hits"]["hits"] | ||
] | ||
|
||
def rebuild(self, ids=None) -> bool: | ||
pass | ||
|
||
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimFG @cxie @xiaofan-luan There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the index can be updated as the data changes, rebuild does not need to be implemented. If not, say faiss, you need to implement |
||
def delete(self, ids) -> None: | ||
for id in ids: | ||
self._client.delete(index=self.collection_name, id=id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimFG @cxie @xiaofan-luan
I am currently running a local docker image of elastic search. What should be the hostname for the production use cases here?
Also, a point to note, I have used a
http
URL since, TLS was required to use ahttps
with Elastic SearchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps through a variable, let the user pass the url parameter by himself. The default value of the parameter is: "http://localhost:9200"