Skip to content

Commit

Permalink
matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgill committed May 28, 2024
1 parent b6278c5 commit f919604
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 8 deletions.
7 changes: 0 additions & 7 deletions packages/csv/src/dummy.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/csv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// export { csvBlockExample } from "./csvBlockExample";
// export { csvBlockPrompt } from "./csvBlockPrompt";
// export { csvBlockLookBack } from "./lookback";
// export { findCompleteCsvBlock, findPartialCsvBlock } from "./matchers";
export { findCompleteCsvBlock, findPartialCsvBlock } from "./matchers";
export type { CsvBlockOptions } from "./options";
139 changes: 139 additions & 0 deletions packages/csv/src/matchers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { MaybeLLMOutputMatch } from "@llm-ui/react";
import { describe, expect, it } from "vitest";
import { findCompleteCsvBlock, findPartialCsvBlock } from "./matchers";
import { CsvBlockOptions } from "./options";

type TestCase = {
name: string;
input: string;
expected: MaybeLLMOutputMatch;
options?: CsvBlockOptions;
};
describe("findCompleteCsvBlock", () => {
const testCases: TestCase[] = [
{
name: "single char",
input: "⦅a⦆",
expected: {
startIndex: 0,
endIndex: 3,
outputRaw: "⦅a⦆",
},
},
{
name: "multiple chars",
input: "⦅abc⦆",
expected: {
startIndex: 0,
endIndex: 5,
outputRaw: "⦅abc⦆",
},
},
{
name: "delimited",
input: "⦅a,b,c⦆",
expected: {
startIndex: 0,
endIndex: 7,
outputRaw: "⦅a,b,c⦆",
},
},
{
name: "text before",
input: "abc⦅a,b,c⦆",
expected: {
startIndex: 3,
endIndex: 10,
outputRaw: "⦅a,b,c⦆",
},
},
{
name: "text after",
input: "⦅a,b,c⦆ def",
expected: {
startIndex: 0,
endIndex: 7,
outputRaw: "⦅a,b,c⦆",
},
},
{
name: "text before and after",
input: "abc⦅a,b,c⦆ def",
expected: {
startIndex: 3,
endIndex: 10,
outputRaw: "⦅a,b,c⦆",
},
},
{
name: "not a block",
input: "```\nhello\n```",
expected: undefined,
},
{
name: "unfinished block",
input: "⦅a,b,c",
expected: undefined,
},
];

testCases.forEach(({ name, input, expected, options }) => {
it(name, () => {
const result = findCompleteCsvBlock(options)(input);
expect(result).toEqual(expected);
});
});
});

describe("findPartialCsvBlock", () => {
const testCases: TestCase[] = [
{
name: "single char",
input: "⦅a",
expected: {
startIndex: 0,
endIndex: 2,
outputRaw: "⦅a",
},
},
{
name: "multiple chars",
input: "⦅abc",
expected: {
startIndex: 0,
endIndex: 4,
outputRaw: "⦅abc",
},
},
{
name: "delimited",
input: "⦅a,b,c",
expected: {
startIndex: 0,
endIndex: 6,
outputRaw: "⦅a,b,c",
},
},
{
name: "text before",
input: "abc⦅a,b,c",
expected: {
startIndex: 3,
endIndex: 9,
outputRaw: "⦅a,b,c",
},
},
{
name: "not a block",
input: "```\nhello\n```",
expected: undefined,
},
];

testCases.forEach(({ name, input, expected, options }) => {
it(name, () => {
const result = findPartialCsvBlock(options)(input);
expect(result).toEqual(expected);
});
});
});
22 changes: 22 additions & 0 deletions packages/csv/src/matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LLMOutputMatcher } from "@llm-ui/react";
import { CsvBlockOptions, getOptions } from "./options";

import { regexMatcher } from "@llm-ui/shared";

export const findCompleteCsvBlock = (
userOptions?: Partial<CsvBlockOptions>,
): LLMOutputMatcher => {
const { startChar, endChar } = getOptions(userOptions);

const regex = new RegExp(`${startChar}([\\s\\S]*?)${endChar}`);
return regexMatcher(regex);
};

export const findPartialCsvBlock = (
userOptions?: Partial<CsvBlockOptions>,
): LLMOutputMatcher => {
const { startChar } = getOptions(userOptions);

const regex = new RegExp(`${startChar}([\\s\\S]*)`);
return regexMatcher(regex);
};

0 comments on commit f919604

Please sign in to comment.