Add Persona semantic memory recall
This commit is contained in:
parent
a055cc286e
commit
c779f4915f
16 changed files with 396 additions and 12 deletions
|
|
@ -111,6 +111,10 @@ function matchesFilters(
|
|||
return false;
|
||||
}
|
||||
|
||||
if (filters.identityId && metadata.identityId !== filters.identityId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.corpusKind && metadata.corpusKind !== filters.corpusKind) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ export interface StoredQdrantChunk extends EmbeddingChunk {
|
|||
export interface QdrantVectorStoreOptions {
|
||||
url: string;
|
||||
collectionName: string;
|
||||
corpusKind: "discord_history" | "repository_source";
|
||||
corpusKind: "discord_history" | "repository_source" | "persona_memory";
|
||||
apiKey?: string;
|
||||
timeoutMs?: number;
|
||||
embedder?: TextEmbedder;
|
||||
|
|
@ -296,6 +296,10 @@ function buildPayload(chunk: StoredQdrantChunk): QdrantPayload {
|
|||
copyOptionalMetadataField(payload, chunk.metadata, "guildId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "channelId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "authorId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "identityId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "memoryKind");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "targetKind");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "targetId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "threadId");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "repoName");
|
||||
copyOptionalMetadataField(payload, chunk.metadata, "language");
|
||||
|
|
@ -346,8 +350,14 @@ function buildPathPrefixes(path: string): string[] {
|
|||
|
||||
function inferCorpusKind(
|
||||
sourceKind: StoredQdrantChunk["sourceKind"],
|
||||
): "discord_history" | "repository_source" {
|
||||
return sourceKind === "source_document" ? "repository_source" : "discord_history";
|
||||
): "discord_history" | "repository_source" | "persona_memory" {
|
||||
if (sourceKind === "source_document") {
|
||||
return "repository_source";
|
||||
}
|
||||
if (sourceKind === "persona_memory") {
|
||||
return "persona_memory";
|
||||
}
|
||||
return "discord_history";
|
||||
}
|
||||
|
||||
function fromQdrantPoint(point: QdrantScoredPoint): RetrievalResult | undefined {
|
||||
|
|
@ -407,8 +417,10 @@ function readString(value: unknown): string | undefined {
|
|||
return typeof value === "string" && value.length > 0 ? value : undefined;
|
||||
}
|
||||
|
||||
function readSourceKind(value: unknown): "discord_message" | "source_document" | undefined {
|
||||
return value === "discord_message" || value === "source_document" ? value : undefined;
|
||||
function readSourceKind(value: unknown): "discord_message" | "source_document" | "persona_memory" | undefined {
|
||||
return value === "discord_message" || value === "source_document" || value === "persona_memory"
|
||||
? value
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function validateEmbeddingBatch(vectors: number[][], expectedCount: number): number {
|
||||
|
|
@ -433,7 +445,7 @@ function validateCollectionCompatibility(
|
|||
collection: Schemas["CollectionInfo"],
|
||||
expected: {
|
||||
collectionName: string;
|
||||
corpusKind: "discord_history" | "repository_source";
|
||||
corpusKind: "discord_history" | "repository_source" | "persona_memory";
|
||||
embedderId: string;
|
||||
vectorLength: number;
|
||||
},
|
||||
|
|
@ -484,7 +496,7 @@ function readCollectionVectorLength(
|
|||
}
|
||||
|
||||
function buildPayloadIndexDefinitions(
|
||||
corpusKind: "discord_history" | "repository_source",
|
||||
corpusKind: "discord_history" | "repository_source" | "persona_memory",
|
||||
): Array<{
|
||||
field_name: string;
|
||||
field_schema:
|
||||
|
|
@ -509,6 +521,16 @@ function buildPayloadIndexDefinitions(
|
|||
];
|
||||
}
|
||||
|
||||
if (corpusKind === "persona_memory") {
|
||||
return [
|
||||
...shared,
|
||||
{ field_name: "identityId", field_schema: "keyword" as const },
|
||||
{ field_name: "memoryKind", field_schema: "keyword" as const },
|
||||
{ field_name: "targetKind", field_schema: "keyword" as const },
|
||||
{ field_name: "targetId", field_schema: "keyword" as const },
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
...shared,
|
||||
{ field_name: "repoName", field_schema: "keyword" as const },
|
||||
|
|
@ -528,6 +550,7 @@ function toQdrantFilter(filters?: RetrievalFilters): QdrantFilter | undefined {
|
|||
appendExactMatch(must, "guildId", filters.guildId);
|
||||
appendExactMatch(must, "channelId", filters.channelId);
|
||||
appendExactMatch(must, "authorId", filters.authorId);
|
||||
appendExactMatch(must, "identityId", filters.identityId);
|
||||
appendExactMatch(must, "repoName", filters.repoName);
|
||||
appendExactMatch(must, "language", filters.language);
|
||||
appendExactMatch(must, "sourceId", filters.sourceId);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ export class RetrievalService {
|
|||
public constructor(
|
||||
private readonly historyVectorStore: VectorStore,
|
||||
private readonly sourceVectorStore: VectorStore = historyVectorStore,
|
||||
private readonly personaMemoryVectorStore: VectorStore = historyVectorStore,
|
||||
) {}
|
||||
|
||||
public async search(
|
||||
|
|
@ -39,4 +40,15 @@ export class RetrievalService {
|
|||
corpusKind: "repository_source",
|
||||
});
|
||||
}
|
||||
|
||||
public async searchPersonaMemory(
|
||||
query: string,
|
||||
limit = 5,
|
||||
filters?: Omit<RetrievalFilters, "corpusKind" | "guildId" | "channelId" | "authorId" | "repoName" | "pathPrefix" | "language">,
|
||||
): Promise<RetrievalResult[]> {
|
||||
return this.personaMemoryVectorStore.query(query, limit, {
|
||||
...filters,
|
||||
corpusKind: "persona_memory",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import { ShardedSourceVectorStore } from "./source-vector-store";
|
|||
export interface VectorStoreFactoryOptions {
|
||||
kind: "local_json" | "qdrant";
|
||||
historyPath: string;
|
||||
personaMemoryPath: string;
|
||||
sourceRoot: string;
|
||||
qdrant: {
|
||||
url: string;
|
||||
|
|
@ -15,14 +16,17 @@ export interface VectorStoreFactoryOptions {
|
|||
timeoutMs: number;
|
||||
historyCollection: string;
|
||||
sourceCollection: string;
|
||||
personaMemoryCollection: string;
|
||||
};
|
||||
historyEmbedder: TextEmbedder;
|
||||
sourceEmbedder: TextEmbedder;
|
||||
personaMemoryEmbedder: TextEmbedder;
|
||||
}
|
||||
|
||||
export interface BuiltVectorStores {
|
||||
history: VectorStore;
|
||||
source: VectorStore;
|
||||
personaMemory: VectorStore;
|
||||
}
|
||||
|
||||
export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVectorStores {
|
||||
|
|
@ -44,11 +48,20 @@ export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVec
|
|||
corpusKind: "repository_source",
|
||||
embedder: options.sourceEmbedder,
|
||||
}),
|
||||
personaMemory: new QdrantVectorStore({
|
||||
url: options.qdrant.url,
|
||||
apiKey: options.qdrant.apiKey,
|
||||
timeoutMs: options.qdrant.timeoutMs,
|
||||
collectionName: options.qdrant.personaMemoryCollection,
|
||||
corpusKind: "persona_memory",
|
||||
embedder: options.personaMemoryEmbedder,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
history: new FileVectorStore(options.historyPath, options.historyEmbedder),
|
||||
source: new ShardedSourceVectorStore(options.sourceRoot, options.sourceEmbedder),
|
||||
personaMemory: new FileVectorStore(options.personaMemoryPath, options.personaMemoryEmbedder),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue