mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 01:19:38 +02:00
Replace the three-prompt LLM scoring pipeline (kg-edge-scoring, kg-edge-reasoning, kg-edge-selection) with a cross-encoder reranker service backed by FlashRank. The new hop_and_filter() method performs iterative graph traversal with semantic scoring at each hop, replacing the previous follow_edges/get_subgraph approach. - Add reranker service (trustgraph-base client/service, FlashRank processor) - Add gateway dispatch for reranker via API and WebSocket - Rewrite GraphRAG pipeline: hop_and_filter() with per-hop cross-encoder scoring - Remove kg_prompt() and edge_score_limit from prompt client - Update provenance: add tg:EdgeSelection type, tg:concept, tg:score predicates - Update CLIs (tg-invoke-graph-rag, tg-show-explain-trace) for new metadata - Add tg-invoke-reranker CLI tool - Add tech spec and UX developer guidance - Update all unit and integration tests
109 lines
2.7 KiB
Python
109 lines
2.7 KiB
Python
|
|
from __future__ import annotations
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import logging
|
|
|
|
from .. schema import (
|
|
RerankerRequest, RerankerResponse, RerankerResult, Error,
|
|
)
|
|
from .. exceptions import TooManyRequests
|
|
from .. base import FlowProcessor, ConsumerSpec, ProducerSpec, ParameterSpec
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
default_ident = "reranker"
|
|
default_concurrency = 1
|
|
|
|
class RerankerService(FlowProcessor):
|
|
|
|
def __init__(self, **params):
|
|
|
|
id = params.get("id")
|
|
concurrency = params.get("concurrency", 1)
|
|
|
|
super(RerankerService, self).__init__(**params | {
|
|
"id": id,
|
|
"concurrency": concurrency,
|
|
})
|
|
|
|
self.register_specification(
|
|
ConsumerSpec(
|
|
name = "request",
|
|
schema = RerankerRequest,
|
|
handler = self.on_request,
|
|
concurrency = concurrency,
|
|
)
|
|
)
|
|
|
|
self.register_specification(
|
|
ProducerSpec(
|
|
name = "response",
|
|
schema = RerankerResponse
|
|
)
|
|
)
|
|
|
|
self.register_specification(
|
|
ParameterSpec(
|
|
name = "model",
|
|
)
|
|
)
|
|
|
|
async def on_request(self, msg, consumer, flow):
|
|
|
|
try:
|
|
|
|
request = msg.value()
|
|
|
|
id = msg.properties()["id"]
|
|
|
|
logger.debug(f"Handling reranker request {id}...")
|
|
|
|
model = flow("model")
|
|
results = await self.on_rerank(
|
|
request.queries, request.documents,
|
|
request.limit, model=model,
|
|
)
|
|
|
|
await flow("response").send(
|
|
RerankerResponse(
|
|
error = None,
|
|
results = results,
|
|
),
|
|
properties={"id": id}
|
|
)
|
|
|
|
logger.debug("Reranker request handled successfully")
|
|
|
|
except TooManyRequests as e:
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Exception in reranker service: {e}", exc_info=True)
|
|
|
|
logger.info("Sending error response...")
|
|
|
|
await flow.producer["response"].send(
|
|
RerankerResponse(
|
|
error=Error(
|
|
type = "reranker-error",
|
|
message = str(e),
|
|
),
|
|
results=[],
|
|
),
|
|
properties={"id": id}
|
|
)
|
|
|
|
@staticmethod
|
|
def add_args(parser: ArgumentParser) -> None:
|
|
|
|
parser.add_argument(
|
|
'-c', '--concurrency',
|
|
type=int,
|
|
default=default_concurrency,
|
|
help=f'Concurrent processing threads (default: {default_concurrency})'
|
|
)
|
|
|
|
FlowProcessor.add_args(parser)
|