Skip to content

Commit

Permalink
Query
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshma committed Jan 30, 2025
1 parent f2e7d49 commit 6afc4eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
10 changes: 5 additions & 5 deletions ts/examples/chat/src/memory/knowproMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export async function createKnowproCommands(
options: {
maxToDisplay: argNum("Maximum matches to display", 25),
type: arg("Knowledge type"),
speaker: arg("Speaker"),
},
};
}
Expand All @@ -189,11 +190,10 @@ export async function createKnowproCommands(
`Searching ${conversation.nameTag}...`,
);

const matches = await kp.searchConversation(
conversation,
terms,
namedArgs.type,
);
const matches = await kp.searchConversation(conversation, terms, {
type: namedArgs.type,
speaker: namedArgs.speaker,
});
if (matches === undefined || matches.size === 0) {
context.printer.writeLine("No matches");
return;
Expand Down
5 changes: 1 addition & 4 deletions ts/packages/knowPro/src/accumulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ export class QueryTermAccumulator {
return false;
}

if (
this.termMatches.has(testText) &&
collections.stringEquals(testText, expectedText, false)
) {
if (collections.stringEquals(testText, expectedText, false)) {
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions ts/packages/knowPro/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ export class WhereSemanticRefExpr
) {
for (let i = 0; i < predicates.length; ++i) {
const semanticRef = context.getSemanticRef(match.value);
if (predicates[i].eval(context, queryTermMatches, semanticRef)) {
return true;
if (!predicates[i].eval(context, queryTermMatches, semanticRef)) {
return false;
}
}
return false;
return true;
}
}

Expand Down
22 changes: 18 additions & 4 deletions ts/packages/knowPro/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export type SearchResult = {
semanticRefMatches: ScoredSemanticRef[];
};

export type SearchFilter = {
type?: KnowledgeType;
speaker?: string;
};
/**
* Searches conversation for terms
* @param conversation
Expand All @@ -26,7 +30,7 @@ export type SearchResult = {
export async function searchConversation(
conversation: IConversation,
terms: QueryTerm[],
type?: KnowledgeType,
filter?: SearchFilter,
maxMatches?: number,
): Promise<Map<KnowledgeType, SearchResult> | undefined> {
if (!q.isConversationSearchable(conversation)) {
Expand All @@ -37,7 +41,7 @@ export async function searchConversation(
const query = createTermSearchQuery(
conversation,
terms,
type ? [new q.KnowledgeTypePredicate(type)] : undefined,
filter,
maxMatches,
);
return toGroupedSearchResults(await query.eval(context));
Expand Down Expand Up @@ -69,13 +73,23 @@ export async function searchConversationExact(
function createTermSearchQuery(
conversation: IConversation,
terms: QueryTerm[],
wherePredicates?: q.IQuerySemanticRefPredicate[] | undefined,
filter?: SearchFilter,
maxMatches?: number,
minHitCount?: number,
) {
let where: q.IQuerySemanticRefPredicate[] | undefined;
if (filter !== undefined) {
where = [];
if (filter.type) {
where.push(new q.KnowledgeTypePredicate(filter.type));
}
if (filter.speaker) {
where.push(new q.ActionPredicate(filter.speaker));
}
}
const query = new q.SelectTopNKnowledgeGroupExpr(
new q.GroupByKnowledgeTypeExpr(
createTermsMatch(conversation, terms, wherePredicates),
createTermsMatch(conversation, terms, where),
),
maxMatches,
minHitCount,
Expand Down

0 comments on commit 6afc4eb

Please sign in to comment.