Skip to content

Commit 6afc4eb

Browse files
committed
Query
1 parent f2e7d49 commit 6afc4eb

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

ts/examples/chat/src/memory/knowproMemory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export async function createKnowproCommands(
166166
options: {
167167
maxToDisplay: argNum("Maximum matches to display", 25),
168168
type: arg("Knowledge type"),
169+
speaker: arg("Speaker"),
169170
},
170171
};
171172
}
@@ -189,11 +190,10 @@ export async function createKnowproCommands(
189190
`Searching ${conversation.nameTag}...`,
190191
);
191192

192-
const matches = await kp.searchConversation(
193-
conversation,
194-
terms,
195-
namedArgs.type,
196-
);
193+
const matches = await kp.searchConversation(conversation, terms, {
194+
type: namedArgs.type,
195+
speaker: namedArgs.speaker,
196+
});
197197
if (matches === undefined || matches.size === 0) {
198198
context.printer.writeLine("No matches");
199199
return;

ts/packages/knowPro/src/accumulators.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,7 @@ export class QueryTermAccumulator {
327327
return false;
328328
}
329329

330-
if (
331-
this.termMatches.has(testText) &&
332-
collections.stringEquals(testText, expectedText, false)
333-
) {
330+
if (collections.stringEquals(testText, expectedText, false)) {
334331
return true;
335332
}
336333

ts/packages/knowPro/src/query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ export class WhereSemanticRefExpr
333333
) {
334334
for (let i = 0; i < predicates.length; ++i) {
335335
const semanticRef = context.getSemanticRef(match.value);
336-
if (predicates[i].eval(context, queryTermMatches, semanticRef)) {
337-
return true;
336+
if (!predicates[i].eval(context, queryTermMatches, semanticRef)) {
337+
return false;
338338
}
339339
}
340-
return false;
340+
return true;
341341
}
342342
}
343343

ts/packages/knowPro/src/search.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export type SearchResult = {
1515
semanticRefMatches: ScoredSemanticRef[];
1616
};
1717

18+
export type SearchFilter = {
19+
type?: KnowledgeType;
20+
speaker?: string;
21+
};
1822
/**
1923
* Searches conversation for terms
2024
* @param conversation
@@ -26,7 +30,7 @@ export type SearchResult = {
2630
export async function searchConversation(
2731
conversation: IConversation,
2832
terms: QueryTerm[],
29-
type?: KnowledgeType,
33+
filter?: SearchFilter,
3034
maxMatches?: number,
3135
): Promise<Map<KnowledgeType, SearchResult> | undefined> {
3236
if (!q.isConversationSearchable(conversation)) {
@@ -37,7 +41,7 @@ export async function searchConversation(
3741
const query = createTermSearchQuery(
3842
conversation,
3943
terms,
40-
type ? [new q.KnowledgeTypePredicate(type)] : undefined,
44+
filter,
4145
maxMatches,
4246
);
4347
return toGroupedSearchResults(await query.eval(context));
@@ -69,13 +73,23 @@ export async function searchConversationExact(
6973
function createTermSearchQuery(
7074
conversation: IConversation,
7175
terms: QueryTerm[],
72-
wherePredicates?: q.IQuerySemanticRefPredicate[] | undefined,
76+
filter?: SearchFilter,
7377
maxMatches?: number,
7478
minHitCount?: number,
7579
) {
80+
let where: q.IQuerySemanticRefPredicate[] | undefined;
81+
if (filter !== undefined) {
82+
where = [];
83+
if (filter.type) {
84+
where.push(new q.KnowledgeTypePredicate(filter.type));
85+
}
86+
if (filter.speaker) {
87+
where.push(new q.ActionPredicate(filter.speaker));
88+
}
89+
}
7690
const query = new q.SelectTopNKnowledgeGroupExpr(
7791
new q.GroupByKnowledgeTypeExpr(
78-
createTermsMatch(conversation, terms, wherePredicates),
92+
createTermsMatch(conversation, terms, where),
7993
),
8094
maxMatches,
8195
minHitCount,

0 commit comments

Comments
 (0)