-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nearest neighbors support #1182
base: main
Are you sure you want to change the base?
Changes from all commits
b4061d6
5d0f194
83fa5f0
ac166e4
42d75fb
85d5ff6
3695874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
"@osdk/foundry-sdk-generator": minor | ||
"@osdk/generator-converters": minor | ||
"@osdk/cli.cmd.typescript": minor | ||
"@osdk/shared.test": minor | ||
"@osdk/generator": minor | ||
"@osdk/client": minor | ||
"@osdk/maker": minor | ||
"@osdk/api": minor | ||
--- | ||
|
||
Support nearest neighbors search |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import type { InterfaceDefinition } from "../ontology/InterfaceDefinition.js"; | |
import type { | ||
ObjectOrInterfaceDefinition, | ||
PropertyKeys, | ||
VectorPropertyKeys, | ||
} from "../ontology/ObjectOrInterface.js"; | ||
import type { | ||
CompileTimeMetadata, | ||
|
@@ -254,4 +255,23 @@ export interface ObjectSet< | |
listener: ObjectSetListener<Q, P>, | ||
opts?: ObjectSetListenerOptions<Q, P>, | ||
) => { unsubscribe: () => void }; | ||
|
||
/** | ||
* Finds the nearest neighbors for a given text or vector within the object set. | ||
* | ||
* @param query - Queries support either a vector matching the embedding model defined on the property, or text that is | ||
automatically embedded. | ||
* @param numNeighbors - The number of objects to return. If the number of documents in the objectType is less than the provided | ||
value, all objects will be returned. This value is limited to 1 ≤ numNeighbors ≥ 500. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'm not actually sure how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, when I used
|
||
* @param property - The property key with a defined embedding model to search over. | ||
* | ||
* @returns An object set containing the `numNeighbors` nearest neighbors. To return the objects ordered by relevance and each | ||
* objects associated score, specify "relevance" in the orderBy. | ||
*/ | ||
|
||
readonly nearestNeighbors: ( | ||
query: string | number[], | ||
numNeighbors: number, | ||
property: VectorPropertyKeys<Q>, | ||
) => this; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,65 @@ describe("ObjectSet", () => { | |
expect(iter).toEqual(2); | ||
}); | ||
|
||
it("nearest neighbors object set", async () => { | ||
const numNeighbors = 3; | ||
const nearestNeighborsObjectSet = client(Employee).nearestNeighbors( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some type tests in here to make sure the type of the call signature is what we expect? |
||
"python3", | ||
numNeighbors, | ||
"skillSetEmbedding", | ||
); | ||
const { data: employees } = await nearestNeighborsObjectSet.fetchPage(); | ||
expect(employees).toHaveLength(numNeighbors); | ||
// Check that no score is returned when not ordered by relevance | ||
employees.forEach(e => expect(e.$score).toBeUndefined()); | ||
}); | ||
|
||
it("nearest neighbors object set ordered by relevance", async () => { | ||
const objectSet = client(Employee); | ||
const { data: employees } = await objectSet.nearestNeighbors( | ||
"python3", | ||
3, | ||
"skillSetEmbedding", | ||
).fetchPage({ | ||
$orderBy: "relevance", | ||
}); | ||
|
||
expect(employees).toHaveLength(3); | ||
// Check that returned objects have scores | ||
employees.forEach(e => expect(e.$score).toBeGreaterThanOrEqual(0)); | ||
}); | ||
|
||
it("nearest neighbors object set ordered by relevance fetchPageWithErrors", async () => { | ||
const objectSet = client(Employee); | ||
const result = await objectSet.nearestNeighbors( | ||
"python3", | ||
3, | ||
"skillSetEmbedding", | ||
).fetchPageWithErrors({ | ||
$orderBy: "relevance", | ||
}); | ||
|
||
if (isOk(result)) { | ||
const employees = result.value.data; | ||
expect(employees).toHaveLength(3); | ||
// Check that returned objects have scores | ||
employees.forEach(e => expect(e.$score).toBeGreaterThanOrEqual(0)); | ||
} | ||
}); | ||
|
||
it("nearest neighbors object set vector query", async () => { | ||
const numNeighbors = 3; | ||
const nearestNeighborsObjectSet = client(Employee).nearestNeighbors( | ||
Array.from({ length: 1536 }, () => 0.3), | ||
numNeighbors, | ||
"skillSetEmbedding", | ||
); | ||
const { data: employees } = await nearestNeighborsObjectSet.fetchPage(); | ||
expect(employees).toHaveLength(numNeighbors); | ||
// Check that no score is returned when not ordered by relevance | ||
employees.forEach(e => expect(e.$score).toBeUndefined()); | ||
}); | ||
|
||
it("objects set subtract", async () => { | ||
const objectSet = client(Employee); | ||
const objectSet2 = client(Employee).where({ | ||
|
@@ -179,6 +238,7 @@ describe("ObjectSet", () => { | |
.fetchPage({ | ||
$orderBy: { "employeeId": "asc" }, | ||
}); | ||
|
||
expect(employees).toMatchObject([ | ||
{ | ||
$apiName: "Employee", | ||
|
@@ -651,6 +711,8 @@ describe("ObjectSet", () => { | |
| "startDate" | ||
| "employeeLocation" | ||
| "employeeSensor" | ||
| "skillSet" | ||
| "skillSetEmbedding" | ||
>(); | ||
|
||
expectTypeOf< | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not sort by ascending or descending relevance as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope.
By default, nearest neighbors returns objects with the highest scores first, so sorting by relevance is equivalent to sorting by score in descending order.