2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Graph embeddings query service. Input is vector, output is list of
|
|
|
|
|
entities. Pinecone implementation.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from pinecone import Pinecone, ServerlessSpec
|
|
|
|
|
from pinecone.grpc import PineconeGRPC, GRPCClientConfig
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
import os
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
from .... schema import GraphEmbeddingsResponse
|
2024-11-22 23:48:21 +00:00
|
|
|
from .... schema import Error, Value
|
2025-07-15 09:33:35 +01:00
|
|
|
from .... base import GraphEmbeddingsQueryService
|
2024-11-22 23:48:21 +00:00
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
default_ident = "ge-query"
|
2024-11-22 23:48:21 +00:00
|
|
|
default_api_key = os.getenv("PINECONE_API_KEY", "not-specified")
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
class Processor(GraphEmbeddingsQueryService):
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
def __init__(self, **params):
|
|
|
|
|
|
|
|
|
|
self.url = params.get("url", None)
|
|
|
|
|
self.api_key = params.get("api_key", default_api_key)
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
if self.api_key is None or self.api_key == "not-specified":
|
|
|
|
|
raise RuntimeError("Pinecone API key must be specified")
|
|
|
|
|
|
2024-11-22 23:48:21 +00:00
|
|
|
if self.url:
|
|
|
|
|
|
|
|
|
|
self.pinecone = PineconeGRPC(
|
|
|
|
|
api_key = self.api_key,
|
|
|
|
|
host = self.url
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
self.pinecone = Pinecone(api_key = self.api_key)
|
|
|
|
|
|
|
|
|
|
super(Processor, self).__init__(
|
|
|
|
|
**params | {
|
|
|
|
|
"url": self.url,
|
2025-07-15 09:33:35 +01:00
|
|
|
"api_key": self.api_key,
|
2024-11-22 23:48:21 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def create_value(self, ent):
|
|
|
|
|
if ent.startswith("http://") or ent.startswith("https://"):
|
|
|
|
|
return Value(value=ent, is_uri=True)
|
|
|
|
|
else:
|
|
|
|
|
return Value(value=ent, is_uri=False)
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
async def query_graph_embeddings(self, msg):
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
# Handle zero limit case
|
|
|
|
|
if msg.limit <= 0:
|
|
|
|
|
return []
|
2024-11-22 23:48:21 +00:00
|
|
|
|
2024-12-10 22:37:54 +00:00
|
|
|
entity_set = set()
|
|
|
|
|
entities = []
|
2024-11-22 23:48:21 +00:00
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
for vec in msg.vectors:
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
dim = len(vec)
|
|
|
|
|
|
|
|
|
|
index_name = (
|
2025-07-15 09:33:35 +01:00
|
|
|
"t-" + msg.user + "-" + msg.collection + "-" + str(dim)
|
2024-11-22 23:48:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
index = self.pinecone.Index(index_name)
|
|
|
|
|
|
2024-12-10 22:37:54 +00:00
|
|
|
# Heuristic hack, get (2*limit), so that we have more chance
|
|
|
|
|
# of getting (limit) entities
|
2024-11-22 23:48:21 +00:00
|
|
|
results = index.query(
|
|
|
|
|
vector=vec,
|
2025-07-15 09:33:35 +01:00
|
|
|
top_k=msg.limit * 2,
|
2024-11-22 23:48:21 +00:00
|
|
|
include_values=False,
|
|
|
|
|
include_metadata=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for r in results.matches:
|
2024-12-10 22:37:54 +00:00
|
|
|
|
2024-11-22 23:48:21 +00:00
|
|
|
ent = r.metadata["entity"]
|
|
|
|
|
|
2024-12-10 22:37:54 +00:00
|
|
|
# De-dupe entities
|
|
|
|
|
if ent not in entity_set:
|
|
|
|
|
entity_set.add(ent)
|
|
|
|
|
entities.append(ent)
|
|
|
|
|
|
|
|
|
|
# Keep adding entities until limit
|
2025-07-15 09:33:35 +01:00
|
|
|
if len(entity_set) >= msg.limit: break
|
2024-12-10 22:37:54 +00:00
|
|
|
|
|
|
|
|
# Keep adding entities until limit
|
2025-07-15 09:33:35 +01:00
|
|
|
if len(entity_set) >= msg.limit: break
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
ents2 = []
|
|
|
|
|
|
|
|
|
|
for ent in entities:
|
|
|
|
|
ents2.append(self.create_value(ent))
|
|
|
|
|
|
|
|
|
|
entities = ents2
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
return entities
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print(f"Exception: {e}")
|
2025-07-15 09:33:35 +01:00
|
|
|
raise e
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def add_args(parser):
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
GraphEmbeddingsQueryService.add_args(parser)
|
2024-11-22 23:48:21 +00:00
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-a', '--api-key',
|
|
|
|
|
default=default_api_key,
|
|
|
|
|
help='Pinecone API key. (default from PINECONE_API_KEY)'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-u', '--url',
|
|
|
|
|
help='Pinecone URL. If unspecified, serverless is used'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
Processor.launch(default_ident, __doc__)
|
2024-11-22 23:48:21 +00:00
|
|
|
|