Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 19, 2024
1 parent f432c9b commit 824d090
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
30 changes: 24 additions & 6 deletions langchain-core/src/structured_query/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class FunctionalTranslator extends BaseTranslator {
formatFunction(): string {
throw new Error("Not implemented");
}

/**
* Returns the allowed comparators for a given data type.
* @param input The input value to get the allowed comparators for.
Expand All @@ -74,10 +74,24 @@ export class FunctionalTranslator extends BaseTranslator {
getAllowedComparatorsForType(inputType: string): Comparator[] {
switch (inputType) {
case "string": {
return [Comparators.eq, Comparators.ne, Comparators.gt, Comparators.gte, Comparators.lt, Comparators.lte];
return [
Comparators.eq,
Comparators.ne,
Comparators.gt,
Comparators.gte,
Comparators.lt,
Comparators.lte,
];
}
case "number": {
return [Comparators.eq, Comparators.ne, Comparators.gt, Comparators.gte, Comparators.lt, Comparators.lte];
return [
Comparators.eq,
Comparators.ne,
Comparators.gt,
Comparators.gte,
Comparators.lt,
Comparators.lte,
];
}
case "boolean": {
return [Comparators.eq, Comparators.ne];
Expand All @@ -87,7 +101,7 @@ export class FunctionalTranslator extends BaseTranslator {
}
}
}

/**
* Returns a function that performs a comparison based on the provided
* comparator.
Expand Down Expand Up @@ -181,8 +195,12 @@ export class FunctionalTranslator extends BaseTranslator {
const { comparator, attribute, value } = comparison;
const undefinedTrue = [Comparators.ne];
if (this.allowedComparators.includes(comparator)) {
if (!this.getAllowedComparatorsForType(typeof value).includes(comparator)) {
throw new Error(`'${comparator}' comparator not allowed to be used with ${typeof value}`);
if (
!this.getAllowedComparatorsForType(typeof value).includes(comparator)
) {
throw new Error(
`'${comparator}' comparator not allowed to be used with ${typeof value}`
);
}
const comparatorFunction = this.getComparatorFunction(comparator);
return (document: Document) => {
Expand Down
26 changes: 17 additions & 9 deletions langchain-core/src/structured_query/tests/functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ describe("FunctionalTranslator", () => {
number: "numberValue",
boolean: "booleanValue",
};

const inputValuesByAttribute: { [key in string]: string | number | boolean } = {

const inputValuesByAttribute: {
[key in string]: string | number | boolean;
} = {
stringValue: "value",
numberValue: 1,
booleanValue: true,
};

// documents that will match against the comparison
const validDocumentsByComparator: { [key in string]: Document<Record<string, unknown>>[] } = {
const validDocumentsByComparator: {
[key in string]: Document<Record<string, unknown>>[];
} = {
[Comparators.eq]: [
new Document({
pageContent: "",
Expand Down Expand Up @@ -137,9 +141,11 @@ describe("FunctionalTranslator", () => {
}),
],
};

// documents that will not match against the comparison
const invalidDocumentsByComparator: { [key in string]: Document<Record<string, unknown>>[] } = {
const invalidDocumentsByComparator: {
[key in string]: Document<Record<string, unknown>>[];
} = {
[Comparators.eq]: [
new Document({
pageContent: "",
Expand Down Expand Up @@ -201,8 +207,10 @@ describe("FunctionalTranslator", () => {
}),
],
};

function generateComparatorTestsForType(type: "string" | "number" | "boolean") {

function generateComparatorTestsForType(
type: "string" | "number" | "boolean"
) {
const comparators = translator.getAllowedComparatorsForType(type);
for (const comparator of comparators) {
const attribute = attributesByType[type];
Expand Down Expand Up @@ -237,7 +245,7 @@ describe("FunctionalTranslator", () => {
}
}
}

generateComparatorTestsForType("string");
generateComparatorTestsForType("number");
generateComparatorTestsForType("boolean");
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/structured_query/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test("Casting values correctly", () => {
expect(typeof value).toBe("number");
expect(isFloat(value)).toBe(true);
});

booleanBoolean.map(castValue).forEach((value) => {
expect(typeof value).toBe("boolean");
expect(isBoolean(value)).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/structured_query/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function isString(value: unknown): boolean {
* Checks if the provided value is a boolean.
*/
export function isBoolean(value: unknown): boolean {
return typeof value === "boolean";
return typeof value === "boolean";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ test("Query Chain Test", async () => {
type: "number",
},
{
name: "isReleased",
description: "Whether the movie has been released",
type: "boolean",
}
name: "isReleased",
description: "Whether the movie has been released",
type: "boolean",
},
];
const documentContents = "Brief summary of a movie";

Expand Down

0 comments on commit 824d090

Please sign in to comment.