-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add support for
boolean
metadata attributes in `Functio…
…nalTranslator` (#7407) Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
21f3b2d
commit 53feade
Showing
9 changed files
with
346 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
langchain-core/src/structured_query/tests/functional.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
import { test, expect, describe } from "@jest/globals"; | ||
import { Document } from "../../documents/document.js"; | ||
import { FunctionalTranslator } from "../functional.js"; | ||
import { Comparators, Visitor } from "../ir.js"; | ||
|
||
describe("FunctionalTranslator", () => { | ||
const translator = new FunctionalTranslator(); | ||
|
||
describe("getAllowedComparatorsForType", () => { | ||
test("string", () => { | ||
expect(translator.getAllowedComparatorsForType("string")).toEqual([ | ||
Comparators.eq, | ||
Comparators.ne, | ||
Comparators.gt, | ||
Comparators.gte, | ||
Comparators.lt, | ||
Comparators.lte, | ||
]); | ||
}); | ||
test("number", () => { | ||
expect(translator.getAllowedComparatorsForType("number")).toEqual([ | ||
Comparators.eq, | ||
Comparators.ne, | ||
Comparators.gt, | ||
Comparators.gte, | ||
Comparators.lt, | ||
Comparators.lte, | ||
]); | ||
}); | ||
test("boolean", () => { | ||
expect(translator.getAllowedComparatorsForType("boolean")).toEqual([ | ||
Comparators.eq, | ||
Comparators.ne, | ||
]); | ||
}); | ||
test("unsupported", () => { | ||
expect(() => | ||
translator.getAllowedComparatorsForType("unsupported") | ||
).toThrow("Unsupported data type: unsupported"); | ||
}); | ||
}); | ||
|
||
describe("visitComparison", () => { | ||
describe("returns true or false for valid comparisons", () => { | ||
const attributesByType = { | ||
string: "stringValue", | ||
number: "numberValue", | ||
boolean: "booleanValue", | ||
}; | ||
|
||
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>>[]; | ||
} = { | ||
[Comparators.eq]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "value", | ||
numberValue: 1, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.ne]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "not-value", | ||
numberValue: 0, | ||
booleanValue: false, | ||
}, | ||
}), | ||
], | ||
[Comparators.gt]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "valueee", | ||
numberValue: 2, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.gte]: [ | ||
// test for greater than | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "valueee", | ||
numberValue: 2, | ||
booleanValue: true, | ||
}, | ||
}), | ||
// test for equal to | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "value", | ||
numberValue: 1, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.lt]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "val", | ||
numberValue: 0, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.lte]: [ | ||
// test for less than | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "val", | ||
numberValue: 0, | ||
booleanValue: true, | ||
}, | ||
}), | ||
// test for equal to | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "value", | ||
numberValue: 1, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
}; | ||
|
||
// documents that will not match against the comparison | ||
const invalidDocumentsByComparator: { | ||
[key in string]: Document<Record<string, unknown>>[]; | ||
} = { | ||
[Comparators.eq]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "not-value", | ||
numberValue: 0, | ||
booleanValue: false, | ||
}, | ||
}), | ||
], | ||
[Comparators.ne]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "value", | ||
numberValue: 1, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.gt]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "value", | ||
numberValue: 1, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.gte]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "val", | ||
numberValue: 0, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.lt]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "valueee", | ||
numberValue: 2, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
[Comparators.lte]: [ | ||
new Document({ | ||
pageContent: "", | ||
metadata: { | ||
stringValue: "valueee", | ||
numberValue: 2, | ||
booleanValue: true, | ||
}, | ||
}), | ||
], | ||
}; | ||
|
||
function generateComparatorTestsForType( | ||
type: "string" | "number" | "boolean" | ||
) { | ||
const comparators = translator.getAllowedComparatorsForType(type); | ||
for (const comparator of comparators) { | ||
const attribute = attributesByType[type]; | ||
const value = inputValuesByAttribute[attribute]; | ||
const validDocuments = validDocumentsByComparator[comparator]; | ||
for (const validDocument of validDocuments) { | ||
test(`${value} -> ${comparator} -> ${validDocument.metadata[attribute]}`, () => { | ||
const comparison = translator.visitComparison({ | ||
attribute, | ||
comparator, | ||
value, | ||
exprName: "Comparison", | ||
accept: (visitor: Visitor) => visitor, | ||
}); | ||
const result = comparison(validDocument); | ||
expect(result).toBeTruthy(); | ||
}); | ||
} | ||
const invalidDocuments = invalidDocumentsByComparator[comparator]; | ||
for (const invalidDocument of invalidDocuments) { | ||
test(`${value} -> ${comparator} -> ${invalidDocument.metadata[attribute]}`, () => { | ||
const comparison = translator.visitComparison({ | ||
attribute, | ||
comparator, | ||
value, | ||
exprName: "Comparison", | ||
accept: (visitor: Visitor) => visitor, | ||
}); | ||
const result = comparison(invalidDocument); | ||
expect(result).toBeFalsy(); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
generateComparatorTestsForType("string"); | ||
generateComparatorTestsForType("number"); | ||
generateComparatorTestsForType("boolean"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.