2026-04-05 22:44:45 -05:00
|
|
|
/**
|
|
|
|
|
* Qdrant graph embeddings query service.
|
|
|
|
|
*
|
|
|
|
|
* Input: vector, user, collection, limit
|
|
|
|
|
* Output: list of Term entities with scores, deduplicated by entity value
|
|
|
|
|
*
|
|
|
|
|
* Queries limit*2 points and deduplicates by entity value to ensure
|
|
|
|
|
* we return up to `limit` unique entities.
|
|
|
|
|
*
|
|
|
|
|
* Python reference: trustgraph-flow/trustgraph/query/graph_embeddings/qdrant/service.py
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { QdrantClient } from "@qdrant/js-client-rest";
|
2026-06-01 16:22:25 -05:00
|
|
|
import { errorMessage, type Term } from "@trustgraph/base";
|
|
|
|
|
import { Context, Effect, Layer } from "effect";
|
|
|
|
|
import * as S from "effect/Schema";
|
2026-04-05 22:44:45 -05:00
|
|
|
|
|
|
|
|
export interface QdrantGraphQueryConfig {
|
|
|
|
|
url?: string;
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EntityMatch {
|
|
|
|
|
entity: Term;
|
|
|
|
|
score: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface GraphEmbeddingsQueryRequest {
|
|
|
|
|
vector: number[];
|
|
|
|
|
user: string;
|
|
|
|
|
collection: string;
|
|
|
|
|
limit: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createTerm(value: string): Term {
|
|
|
|
|
if (value.startsWith("http://") || value.startsWith("https://")) {
|
|
|
|
|
return { type: "IRI", iri: value };
|
|
|
|
|
}
|
|
|
|
|
return { type: "LITERAL", value };
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export interface QdrantGraphEmbeddingsQuery {
|
|
|
|
|
readonly query: (request: GraphEmbeddingsQueryRequest) => Promise<EntityMatch[]>;
|
|
|
|
|
}
|
2026-04-05 22:44:45 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export function makeQdrantGraphEmbeddingsQuery(
|
|
|
|
|
config: QdrantGraphQueryConfig = {},
|
|
|
|
|
): QdrantGraphEmbeddingsQuery {
|
|
|
|
|
const url = config.url ?? process.env.QDRANT_URL ?? "http://localhost:6333";
|
|
|
|
|
const apiKey = config.apiKey ?? process.env.QDRANT_API_KEY;
|
2026-04-05 22:44:45 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const client = new QdrantClient({
|
|
|
|
|
url,
|
|
|
|
|
...(apiKey !== undefined && apiKey.length > 0 ? { apiKey } : {}),
|
|
|
|
|
});
|
2026-04-05 22:44:45 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
console.log("[QdrantGraphQuery] Query service initialized");
|
2026-04-05 22:44:45 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const query = async (request: GraphEmbeddingsQueryRequest): Promise<EntityMatch[]> => {
|
2026-04-05 22:44:45 -05:00
|
|
|
const { vector, user, collection, limit } = request;
|
|
|
|
|
|
2026-05-12 08:06:58 -05:00
|
|
|
if (vector.length === 0) {
|
2026-04-05 22:44:45 -05:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dim = vector.length;
|
|
|
|
|
const collectionName = `t_${user}_${collection}_${dim}`;
|
|
|
|
|
|
|
|
|
|
// Check if collection exists -- return empty if not
|
2026-06-01 20:26:47 -05:00
|
|
|
const exists = await client.collectionExists(collectionName);
|
2026-04-05 22:44:45 -05:00
|
|
|
if (!exists.exists) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[QdrantGraphQuery] Collection ${collectionName} does not exist, returning empty results`,
|
|
|
|
|
);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query 2x the limit so we have a better chance of getting `limit`
|
|
|
|
|
// unique entities after deduplication (same heuristic as Python impl)
|
2026-06-01 20:26:47 -05:00
|
|
|
const searchResult = await client.search(collectionName, {
|
2026-04-05 22:44:45 -05:00
|
|
|
vector,
|
|
|
|
|
limit: limit * 2,
|
|
|
|
|
with_payload: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const entitySet = new Set<string>();
|
|
|
|
|
const entities: EntityMatch[] = [];
|
|
|
|
|
|
|
|
|
|
for (const point of searchResult) {
|
|
|
|
|
const payload = point.payload as Record<string, unknown> | undefined;
|
|
|
|
|
const entityValue = payload?.entity as string | undefined;
|
2026-05-12 08:06:58 -05:00
|
|
|
if (entityValue === undefined || entityValue.length === 0) continue;
|
2026-04-05 22:44:45 -05:00
|
|
|
|
|
|
|
|
// Deduplicate by entity value, keeping the highest score (results are
|
|
|
|
|
// already sorted by score descending from Qdrant)
|
|
|
|
|
if (!entitySet.has(entityValue)) {
|
|
|
|
|
entitySet.add(entityValue);
|
|
|
|
|
entities.push({
|
|
|
|
|
entity: createTerm(entityValue),
|
|
|
|
|
score: point.score,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop once we have enough unique entities
|
|
|
|
|
if (entities.length >= limit) break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entities;
|
2026-06-01 20:26:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { query };
|
2026-04-05 22:44:45 -05:00
|
|
|
}
|
2026-06-01 16:22:25 -05:00
|
|
|
|
|
|
|
|
export class QdrantGraphEmbeddingsQueryError extends S.TaggedErrorClass<QdrantGraphEmbeddingsQueryError>()(
|
|
|
|
|
"QdrantGraphEmbeddingsQueryError",
|
|
|
|
|
{
|
|
|
|
|
message: S.String,
|
|
|
|
|
operation: S.String,
|
|
|
|
|
cause: S.DefectWithStack,
|
|
|
|
|
},
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
export interface QdrantGraphEmbeddingsQueryServiceShape {
|
|
|
|
|
readonly query: (
|
|
|
|
|
request: GraphEmbeddingsQueryRequest,
|
|
|
|
|
) => Effect.Effect<ReadonlyArray<EntityMatch>, QdrantGraphEmbeddingsQueryError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class QdrantGraphEmbeddingsQueryService extends Context.Service<
|
|
|
|
|
QdrantGraphEmbeddingsQueryService,
|
|
|
|
|
QdrantGraphEmbeddingsQueryServiceShape
|
|
|
|
|
>()(
|
|
|
|
|
"@trustgraph/flow/query/embeddings/qdrant-graph/QdrantGraphEmbeddingsQueryService",
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
const qdrantGraphEmbeddingsQueryError = (operation: string, cause: unknown) =>
|
|
|
|
|
new QdrantGraphEmbeddingsQueryError({
|
|
|
|
|
operation,
|
|
|
|
|
message: errorMessage(cause),
|
|
|
|
|
cause,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const makeQdrantGraphEmbeddingsQueryService = (
|
|
|
|
|
config: QdrantGraphQueryConfig = {},
|
|
|
|
|
): QdrantGraphEmbeddingsQueryServiceShape => {
|
2026-06-01 20:26:47 -05:00
|
|
|
const query = makeQdrantGraphEmbeddingsQuery(config);
|
2026-06-01 16:22:25 -05:00
|
|
|
return {
|
|
|
|
|
query: Effect.fn("QdrantGraphEmbeddingsQuery.query")(function* (request) {
|
|
|
|
|
return yield* Effect.tryPromise({
|
|
|
|
|
try: () => query.query(request),
|
|
|
|
|
catch: (cause) => qdrantGraphEmbeddingsQueryError("query", cause),
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const QdrantGraphEmbeddingsQueryLive = (
|
|
|
|
|
config: QdrantGraphQueryConfig = {},
|
|
|
|
|
): Layer.Layer<QdrantGraphEmbeddingsQueryService> =>
|
|
|
|
|
Layer.succeed(
|
|
|
|
|
QdrantGraphEmbeddingsQueryService,
|
|
|
|
|
QdrantGraphEmbeddingsQueryService.of(makeQdrantGraphEmbeddingsQueryService(config)),
|
|
|
|
|
);
|