Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshma committed Jan 31, 2025
1 parent 35a8ea4 commit 7b90406
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 200 deletions.
29 changes: 29 additions & 0 deletions ts/examples/chat/src/memory/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ export function argChunkSize(defaultValue?: number | undefined): ArgDef {
};
}

export function recordFromArgs(
args: NamedArgs,
metadata?: CommandMetadata,
): Record<string, string> {
const record: Record<string, string> = {};
const keys = Object.keys(args);
for (const key of keys) {
const value = args[key];
if (typeof value !== "function") {
record[key] = value;
}
}
if (metadata !== undefined) {
if (metadata.args) {
removeKeysFromRecord(record, Object.keys(metadata.args));
}
if (metadata.options) {
removeKeysFromRecord(record, Object.keys(metadata.options));
}
}
return record;
}

function removeKeysFromRecord(record: Record<string, string>, keys: string[]) {
for (const key of keys) {
delete record[key];
}
}

export function argToDate(value: string | undefined): Date | undefined {
return value ? dateTime.stringToDate(value) : undefined;
}
Expand Down
211 changes: 11 additions & 200 deletions ts/examples/chat/src/memory/knowproMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import {
import { ChatContext } from "./chatMemory.js";
import { ChatModel } from "aiclient";
import fs from "fs";
import { ChatPrinter } from "../chatPrinter.js";
import {
addFileNameSuffixToPath,
argDestFile,
argSourceFile,
parseFreeAndNamedArguments,
recordFromArgs,
} from "./common.js";
import { ensureDir, readJsonFile, writeJsonFile } from "typeagent";
import path from "path";
import chalk from "chalk";
import { KnowProPrinter } from "./knowproPrinter.js";

type KnowProContext = {
knowledgeModel: ChatModel;
Expand Down Expand Up @@ -178,9 +179,10 @@ export async function createKnowproCommands(
if (!conversation) {
return;
}
const commandDef = searchTermsDef();
let [termArgs, namedArgs] = parseFreeAndNamedArguments(
args,
searchTermsDef(),
commandDef,
);
const terms = parseQueryTerms(termArgs); // Todo: De dupe
if (conversation.semanticRefIndex && conversation.semanticRefs) {
Expand All @@ -192,7 +194,7 @@ export async function createKnowproCommands(
const matches = await kp.searchConversation(
conversation,
terms,
filterFromArgs(namedArgs),
filterFromArgs(namedArgs, commandDef),
);
if (matches === undefined || matches.size === 0) {
context.printer.writeLine("No matches");
Expand All @@ -210,23 +212,12 @@ export async function createKnowproCommands(
}
}

function filterFromArgs(namedArgs: NamedArgs) {
let filter: kp.SearchFilter = { type: namedArgs.ktype };
let argCopy = { ...namedArgs };
delete argCopy.maxToDisplay;
delete argCopy.ktype;
let keys = Object.keys(argCopy);
if (keys.length > 0) {
for (const key of keys) {
const value = argCopy[key];
if (typeof value === "function") {
delete argCopy[key];
}
}
if (Object.keys(argCopy).length > 0) {
filter.propertiesToMatch = argCopy;
}
}
function filterFromArgs(namedArgs: NamedArgs, metadata: CommandMetadata) {
let filter: kp.SearchFilter = {
type: namedArgs.ktype,
propertiesToMatch: recordFromArgs(namedArgs, metadata),
};
return filter;
return filter;
}

Expand Down Expand Up @@ -349,174 +340,6 @@ export async function createKnowproCommands(
}
}

class KnowProPrinter extends ChatPrinter {
constructor() {
super();
}

public writeEntity(
entity: knowLib.conversation.ConcreteEntity | undefined,
) {
if (entity !== undefined) {
this.writeLine(entity.name.toUpperCase());
this.writeList(entity.type, { type: "csv" });
if (entity.facets) {
const facetList = entity.facets.map((f) =>
knowLib.conversation.facetToString(f),
);
this.writeList(facetList, { type: "ul" });
}
}
return this;
}

public writeAction(action: knowLib.conversation.Action | undefined) {
if (action !== undefined) {
this.writeLine(knowLib.conversation.actionToString(action));
}
}

public writeTopic(topic: kp.ITopic | undefined) {
if (topic !== undefined) {
this.writeLine(topic.text);
}
}

public writeSemanticRef(semanticRef: kp.SemanticRef) {
switch (semanticRef.knowledgeType) {
default:
this.writeLine(semanticRef.knowledgeType);
break;
case "entity":
this.writeEntity(
semanticRef.knowledge as knowLib.conversation.ConcreteEntity,
);
break;
case "action":
this.writeAction(
semanticRef.knowledge as knowLib.conversation.Action,
);
break;
case "topic":
this.writeTopic(semanticRef.knowledge as kp.ITopic);
break;
}
return this;
}

public writeSemanticRefs(refs: kp.SemanticRef[] | undefined) {
if (refs && refs.length > 0) {
for (const ref of refs) {
this.writeSemanticRef(ref);
this.writeLine();
}
}
return this;
}

public writeScoredSemanticRefs(
semanticRefMatches: kp.ScoredSemanticRef[],
semanticRefs: kp.SemanticRef[],
maxToDisplay: number,
) {
this.writeLine(
`Displaying ${maxToDisplay} matches of total ${semanticRefMatches.length}`,
);
for (let i = 0; i < maxToDisplay; ++i) {
const match = semanticRefMatches[i];
const semanticRef = semanticRefs[match.semanticRefIndex];

this.writeInColor(
chalk.green,
`#${i + 1}: ${semanticRef.knowledgeType} [${match.score}]`,
);
this.writeSemanticRef(semanticRef);
this.writeLine();
}
}

public writeSearchResult(
conversation: kp.IConversation,
result: kp.SearchResult | undefined,
maxToDisplay: number,
) {
if (result) {
this.writeListInColor(chalk.cyanBright, result.termMatches, {
title: "Matched terms",
type: "ol",
});
maxToDisplay = Math.min(
result.semanticRefMatches.length,
maxToDisplay,
);
this.writeScoredSemanticRefs(
result.semanticRefMatches,
conversation.semanticRefs!,
maxToDisplay,
);
}
}

public writeSearchResults(
conversation: kp.IConversation,
results: Map<kp.KnowledgeType, kp.SearchResult>,
maxToDisplay: number,
) {
// Do entities before actions...
this.writeResult(conversation, "entity", results, maxToDisplay);
this.writeResult(conversation, "action", results, maxToDisplay);
this.writeResult(conversation, "topic", results, maxToDisplay);
this.writeResult(conversation, "tag", results, maxToDisplay);
}

private writeResult(
conversation: kp.IConversation,
type: kp.KnowledgeType,
results: Map<kp.KnowledgeType, kp.SearchResult>,
maxToDisplay: number,
) {
const result = results.get(type);
if (result !== undefined) {
this.writeTitle(type.toUpperCase());
this.writeSearchResult(conversation, result, maxToDisplay);
}
}

public writeConversationInfo(conversation: kp.IConversation) {
this.writeTitle(conversation.nameTag);
this.writeLine(`${conversation.messages.length} messages`);
return this;
}

public writePodcastInfo(podcast: kp.Podcast) {
this.writeConversationInfo(podcast);
this.writeList(getPodcastParticipants(podcast), {
type: "csv",
title: "Participants",
});
}

public writeIndexingResults(
results: kp.ConversationIndexingResult,
verbose = false,
) {
if (results.failedMessages.length > 0) {
this.writeError(
`Errors for ${results.failedMessages.length} messages`,
);
if (verbose) {
for (const failedMessage of results.failedMessages) {
this.writeInColor(
chalk.cyan,
failedMessage.message.textChunks[0],
);
this.writeError(failedMessage.error);
}
}
}
}
}

export function filterSemanticRefsByType(
semanticRefs: kp.SemanticRef[] | undefined,
type: string,
Expand All @@ -532,18 +355,6 @@ export function filterSemanticRefsByType(
return matches;
}

export function getPodcastParticipants(podcast: kp.Podcast) {
const participants = new Set<string>();
for (let message of podcast.messages) {
const meta = message.metadata;
if (meta.speaker) {
participants.add(meta.speaker);
}
meta.listeners.forEach((l) => participants.add(l));
}
return [...participants.values()];
}

export function parseQueryTerms(args: string[]): kp.QueryTerm[] {
const queryTerms: kp.QueryTerm[] = [];
for (const arg of args) {
Expand Down
Loading

0 comments on commit 7b90406

Please sign in to comment.