mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-08 21:02:12 +02:00
Add type annotations to 23 modules covering: - Metrics classes (ConsumerMetrics, ProducerMetrics, etc.) - Spec classes (ConsumerSpec, ProducerSpec, SubscriberSpec, etc.) - Service classes with add_args() and run() methods - Utility functions (logging, pubsub, clients) - AsyncProcessor methods All 93 public functions now fully typed. Refs #785
104 lines
2.8 KiB
Python
104 lines
2.8 KiB
Python
from __future__ import annotations
|
|
from argparse import ArgumentParser
|
|
|
|
"""
|
|
Document embeddings query service. Input is vectors. Output is list of
|
|
embeddings.
|
|
"""
|
|
|
|
import logging
|
|
from argparse import ArgumentParser
|
|
|
|
from .. schema import DocumentEmbeddingsRequest, DocumentEmbeddingsResponse
|
|
from .. schema import Error, Term
|
|
from argparse import ArgumentParser
|
|
|
|
from . flow_processor import FlowProcessor
|
|
from . consumer_spec import ConsumerSpec
|
|
from . producer_spec import ProducerSpec
|
|
from argparse import ArgumentParser
|
|
|
|
# Module logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
default_ident = "doc-embeddings-query"
|
|
default_concurrency = 10
|
|
|
|
class DocumentEmbeddingsQueryService(FlowProcessor):
|
|
|
|
def __init__(self, **params):
|
|
|
|
id = params.get("id")
|
|
concurrency = params.get("concurrency", default_concurrency)
|
|
|
|
super(DocumentEmbeddingsQueryService, self).__init__(
|
|
**params | { "id": id }
|
|
)
|
|
|
|
self.register_specification(
|
|
ConsumerSpec(
|
|
name = "request",
|
|
schema = DocumentEmbeddingsRequest,
|
|
handler = self.on_message,
|
|
concurrency = concurrency,
|
|
)
|
|
)
|
|
|
|
self.register_specification(
|
|
ProducerSpec(
|
|
name = "response",
|
|
schema = DocumentEmbeddingsResponse,
|
|
)
|
|
)
|
|
|
|
async def on_message(self, msg, consumer, flow):
|
|
|
|
try:
|
|
|
|
request = msg.value()
|
|
|
|
# Sender-produced ID
|
|
id = msg.properties()["id"]
|
|
|
|
logger.debug(f"Handling document embeddings query request {id}...")
|
|
|
|
docs = await self.query_document_embeddings(request)
|
|
|
|
logger.debug("Sending document embeddings query response...")
|
|
r = DocumentEmbeddingsResponse(chunks=docs, error=None)
|
|
await flow("response").send(r, properties={"id": id})
|
|
|
|
logger.debug("Document embeddings query request completed")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Exception in document embeddings query service: {e}", exc_info=True)
|
|
|
|
logger.info("Sending error response...")
|
|
|
|
r = DocumentEmbeddingsResponse(
|
|
error=Error(
|
|
type = "document-embeddings-query-error",
|
|
message = str(e),
|
|
),
|
|
chunks=[],
|
|
)
|
|
|
|
await flow("response").send(r, properties={"id": id})
|
|
|
|
@staticmethod
|
|
def add_args(parser: ArgumentParser) -> None:
|
|
|
|
FlowProcessor.add_args(parser)
|
|
|
|
parser.add_argument(
|
|
'-c', '--concurrency',
|
|
type=int,
|
|
default=default_concurrency,
|
|
help=f'Number of concurrent requests (default: {default_concurrency})'
|
|
)
|
|
|
|
def run() -> None:
|
|
|
|
Processor.launch(default_ident, __doc__)
|
|
|