Skip to content

Commit b3a35d2

Browse files
authored
knowproTest improvements (#1314)
Improved test result collection, logging
1 parent 2bf5121 commit b3a35d2

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export async function createKnowproCommands(
310310
}
311311
// Log any new queries
312312
if (!savedQuery) {
313-
context.log.writeFile("kpSearch", {
313+
context.log.writeCommandResult("kpSearch", {
314314
searchText: debugContext.searchText,
315315
searchQuery: debugContext.searchQuery,
316316
});
@@ -395,7 +395,7 @@ export async function createKnowproCommands(
395395
return;
396396
},
397397
);
398-
context.log.writeFile("kpAnswer", answerResponse);
398+
context.log.writeCommandResult("kpAnswer", answerResponse);
399399
context.printer.writeLine();
400400
}
401401

ts/examples/chat/src/memory/knowproTest.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ import * as kpTest from "knowpro-test";
1818
import * as cm from "conversation-memory";
1919
import {
2020
changeFileExt,
21-
ensureDir,
2221
getAbsolutePath,
2322
getFileName,
2423
htmlToMd,
2524
readAllText,
2625
simplifyHtml,
2726
simplifyText,
28-
writeJsonFile,
2927
} from "typeagent";
3028
import chalk from "chalk";
3129
import { openai } from "aiclient";
@@ -192,6 +190,7 @@ export async function createKnowproTestCommands(
192190
);
193191
if (!results.success) {
194192
context.printer.writeError(results.message);
193+
return;
195194
}
196195
} finally {
197196
endTestBatch();
@@ -219,6 +218,8 @@ export async function createKnowproTestCommands(
219218

220219
const namedArgs = parseNamedArguments(args, verifySearchBatchDef());
221220
const srcPath = namedArgs.srcPath;
221+
222+
const startTimestamp = new Date();
222223
const results = await kpTest.verifyLangSearchResultsBatch(
223224
context,
224225
srcPath,
@@ -235,7 +236,11 @@ export async function createKnowproTestCommands(
235236
context.printer.writeError(results.message);
236237
return;
237238
}
238-
await writeSearchValidationReport(results.data, srcPath);
239+
await writeSearchValidationReport(
240+
results.data,
241+
srcPath,
242+
startTimestamp,
243+
);
239244
} finally {
240245
endTestBatch();
241246
}
@@ -476,21 +481,18 @@ export async function createKnowproTestCommands(
476481

477482
async function writeSearchValidationReport(
478483
results: kpTest.Comparison<kpTest.LangSearchResults>[],
479-
srcPath?: string,
484+
srcPath: string,
485+
startDate: Date,
480486
) {
481-
const errorResults = results.filter(
482-
(c) => c.error !== undefined && c.error.length > 0,
483-
);
484-
if (errorResults.length === 0) {
485-
context.printer.writeLineInColor(chalk.green, "No errors");
486-
return;
487-
}
488-
489-
context.printer.writeLine(`${errorResults.length} errors`);
490-
context.printer.writeList(errorResults.map((e) => e.expected.cmd));
491-
if (srcPath) {
492-
await saveReport(srcPath, errorResults);
487+
const report = kpTest.createSearchTestReport(results, srcPath, {
488+
startDate,
489+
stopDate: new Date(),
490+
});
491+
context.printer.writeLine(`${report.errors.length} errors`);
492+
if (report.errors.length > 0) {
493+
context.printer.writeList(report.errors.map((e) => e.expected.cmd));
493494
}
495+
await context.log.writeTestReport(report, getFileName(srcPath));
494496
}
495497

496498
function writeAnswerScore(
@@ -514,17 +516,11 @@ export async function createKnowproTestCommands(
514516
}
515517
}
516518

517-
async function saveReport(srcPath: string, report: any) {
518-
const outputDir = path.join(context.basePath, "logs/testReports");
519-
await ensureDir(outputDir);
520-
const outputPath = path.join(outputDir, getFileName(srcPath) + ".json");
521-
await writeJsonFile(outputPath, report);
522-
}
523-
524-
function beginTestBatch() {
519+
function beginTestBatch(): void {
525520
context.retryNoAnswer = true;
526521
}
527-
function endTestBatch() {
522+
523+
function endTestBatch(): void {
528524
context.retryNoAnswer = false;
529525
}
530526
return;

ts/packages/knowProTest/src/common.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import fs from "fs";
55
import { ArgDef, NamedArgs, parseCommandLine } from "interactive-app";
66
import path from "path";
7-
import { getFileName } from "typeagent";
7+
import { dateTime, getFileName } from "typeagent";
88
import { error, Result, Error } from "typechat";
99

1010
export function ensureDirSync(folderPath: string): string {
@@ -34,6 +34,10 @@ export function writeObjectToUniqueFile(filePath: string, obj: any): void {
3434
fs.writeFileSync(filePath, stringifyReadable(obj));
3535
}
3636

37+
export function writeObjectToFile(filePath: string, obj: any): void {
38+
fs.writeFileSync(filePath, stringifyReadable(obj));
39+
}
40+
3741
export function getCommandArgs(line: string | undefined): string[] {
3842
if (line !== undefined && line.length > 0) {
3943
const args = parseCommandLine(line);
@@ -47,6 +51,15 @@ export function getCommandArgs(line: string | undefined): string[] {
4751
return [];
4852
}
4953

54+
export function dateRangeToTimeRange(
55+
range: dateTime.DateRange,
56+
): dateTime.TimestampRange {
57+
return {
58+
startTimestamp: range.startDate.toISOString(),
59+
endTimestamp: range.stopDate ? range.stopDate.toISOString() : undefined,
60+
};
61+
}
62+
5063
export async function execCommandLine<T>(
5164
line: string,
5265
cb: (args: string[], cmdLine: string) => Promise<Result<T>>,
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { ensureDirSync, writeObjectToUniqueFile } from "./common.js";
5-
import { dateTime } from "typeagent";
4+
import {
5+
ensureDirSync,
6+
writeObjectToFile,
7+
writeObjectToUniqueFile,
8+
} from "./common.js";
9+
import { changeFileExt, dateTime, ensureDir } from "typeagent";
610
import path from "path";
711

812
export class KnowproLog {
913
constructor(public baseLogDir: string) {
1014
ensureDirSync(baseLogDir);
1115
}
1216

13-
public writeFile(commandName: string, obj: any) {
17+
public writeCommandResult(commandName: string, obj: any) {
1418
try {
1519
const timestamp = new Date();
16-
const dirPath = this.ensureLogDir(timestamp, commandName);
20+
const dirPath = this.ensureCommandLogDir(timestamp, commandName);
1721
const fileName = `${dateTime.timestampString(timestamp)}.json`;
1822
let filePath = path.join(dirPath, fileName);
1923
writeObjectToUniqueFile(filePath, obj);
@@ -22,7 +26,27 @@ export class KnowproLog {
2226
}
2327
}
2428

25-
public ensureLogDir(timestamp: Date, commandName?: string): string {
29+
public async writeTestReport(
30+
report: TestRunReport,
31+
fileName: string,
32+
timestamp?: Date,
33+
) {
34+
const logDir = path.join(this.baseLogDir, "testReports");
35+
await ensureDir(logDir);
36+
37+
timestamp ??= new Date();
38+
const outputPath = path.join(
39+
logDir,
40+
changeFileExt(
41+
fileName,
42+
".json",
43+
dateTime.timestampString(timestamp),
44+
),
45+
);
46+
writeObjectToFile(outputPath, report);
47+
}
48+
49+
private ensureCommandLogDir(timestamp: Date, commandName?: string): string {
2650
let dirName: string;
2751
if (commandName) {
2852
dirName = `${commandName}_${dateTime.timestampStringShort(timestamp)}`;
@@ -34,3 +58,12 @@ export class KnowproLog {
3458
return dirPath;
3559
}
3660
}
61+
62+
export interface TestRunReport<T = any> {
63+
name: string;
64+
timeRange: dateTime.TimestampRange;
65+
srcData: string;
66+
countRun: number;
67+
errors: T[];
68+
rawResults?: T[] | undefined;
69+
}

ts/packages/knowProTest/src/searchTest.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22
// Licensed under the MIT License.
33

44
import { KnowproContext } from "./knowproContext.js";
5-
import { readJsonFile, writeJsonFile } from "typeagent";
5+
import { dateTime, readJsonFile, writeJsonFile } from "typeagent";
66
import { error, Result, success } from "typechat";
77
import { BatchCallback, Comparison } from "./types.js";
88
import {
99
compareArray,
1010
compareObject,
11+
dateRangeToTimeRange,
1112
getCommandArgs,
1213
queryError,
1314
} from "./common.js";
1415
import { getLangSearchResult } from "./knowproCommands.js";
1516
import { getBatchFileLines } from "interactive-app";
1617
import { execSearchRequest } from "./knowproCommands.js";
1718
import * as kp from "knowpro";
19+
import { TestRunReport } from "./logging.js";
1820

1921
export type LangSearchResults = {
2022
searchText: string;
2123
cmd?: string | undefined;
2224
searchQueryExpr: kp.querySchema.SearchQuery;
25+
compiledQueryExpr?: kp.SearchQueryExpr[] | undefined;
2326
results: LangSearchResult[];
2427
};
2528

@@ -65,7 +68,6 @@ export async function runSearchBatch(
6568
}
6669
return success(results);
6770
}
68-
6971
async function getSearchResults(
7072
context: KnowproContext,
7173
args: string[],
@@ -90,6 +92,7 @@ function collectLangSearchResults(
9092
return {
9193
searchText,
9294
searchQueryExpr: debugContext.searchQuery!,
95+
compiledQueryExpr: debugContext.searchQueryExpr,
9396
results: searchResults.map((cr) => {
9497
const lr: LangSearchResult = {
9598
messageMatches: cr.messageMatches.map((m) => m.messageOrdinal),
@@ -198,6 +201,23 @@ export async function* runLangSearchBatch(
198201
}
199202
}
200203

204+
export function createSearchTestReport(
205+
results: Comparison<LangSearchResults>[],
206+
srcDataPath: string,
207+
dateRange: dateTime.DateRange,
208+
): TestRunReport<Comparison<LangSearchResults>> {
209+
const errors = results.filter(
210+
(c) => c.error !== undefined && c.error.length > 0,
211+
);
212+
return {
213+
name: "search",
214+
timeRange: dateRangeToTimeRange(dateRange),
215+
srcData: srcDataPath,
216+
errors,
217+
countRun: results.length,
218+
};
219+
}
220+
201221
function getKnowledgeResults(
202222
cr: kp.ConversationSearchResult,
203223
lr: LangSearchResult,

0 commit comments

Comments
 (0)