-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dia-1033): infiniteDiscovery queries and temporal direct integra…
…tion with OpenSearch (#6306) * feat(dia-1033): infiniteDiscovery queries and temporal direct integration with OpenSearch * delete tasteProfileVector from the schema * remove tasteProfileVector parameter * change type name * fix description * move opensearch related code into old type * remove deprecation warning * random curators picks for inital batch of artworks
- Loading branch information
1 parent
3194328
commit 7569b27
Showing
9 changed files
with
260 additions
and
56 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
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,26 @@ | ||
import urljoin from "url-join" | ||
import { assign } from "lodash" | ||
import config from "config" | ||
import fetch from "node-fetch" | ||
|
||
const { OPENSEARCH_API_BASE } = config | ||
|
||
export const opensearch = async ( | ||
path, | ||
_accessToken, | ||
fetchOptions: any = {} | ||
) => { | ||
const headers = { | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
} | ||
|
||
const response = await ( | ||
await fetch( | ||
urljoin(OPENSEARCH_API_BASE, path), | ||
assign({}, fetchOptions, { headers }) | ||
) | ||
).json() | ||
|
||
return response | ||
} |
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,30 @@ | ||
import config from "config" | ||
import { opensearch } from "lib/apis/opensearch" | ||
import { mean } from "mathjs" | ||
|
||
export const calculateMeanArtworksVector = async (artworkIds) => { | ||
const getVectorsQuery = { | ||
size: artworkIds.length, | ||
_source: ["_id", "vector_embedding"], | ||
query: { | ||
ids: { | ||
values: artworkIds, | ||
}, | ||
}, | ||
} | ||
|
||
const artworksResponse = await opensearch( | ||
`/${config.OPENSEARCH_ARTWORKS_INFINITE_DISCOVERY_INDEX}/_search`, | ||
undefined, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify(getVectorsQuery), | ||
} | ||
) | ||
|
||
const vectorEmbeddings = artworksResponse.hits?.hits?.map( | ||
(hit) => hit._source.vector_embedding | ||
) | ||
|
||
return mean(vectorEmbeddings, 0) | ||
} |
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,55 @@ | ||
import config from "config" | ||
import { opensearch } from "lib/apis/opensearch" | ||
|
||
/** | ||
* Perform kNN operation to find artworks similiar to vectorEmbedding | ||
* and then return the artworks loaded by artworksLoader | ||
* | ||
* @param vectorEmbedding - vector embedding of the artwork | ||
* @param size - number of similar artworks to return | ||
* @param excludeArtworkIds - list of artwork ids to exclude from the response | ||
* @param artworksLoader - artworks loader | ||
*/ | ||
export const findSimilarArtworks = async ( | ||
vectorEmbedding: number[], | ||
size = 10, | ||
excludeArtworkIds: string[] = [], | ||
artworksLoader | ||
) => { | ||
const knnQuery = { | ||
size: size, | ||
_source: ["_id"], | ||
query: { | ||
bool: { | ||
must_not: { | ||
terms: { | ||
_id: excludeArtworkIds, | ||
}, | ||
}, | ||
should: [ | ||
{ | ||
knn: { | ||
vector_embedding: { | ||
vector: vectorEmbedding, | ||
k: size, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
const knnResponse = await opensearch( | ||
`/${config.OPENSEARCH_ARTWORKS_INFINITE_DISCOVERY_INDEX}/_search`, | ||
undefined, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify(knnQuery), | ||
} | ||
) | ||
|
||
const artworkIds = knnResponse.hits?.hits?.map((hit) => hit._id) || [] | ||
|
||
return await artworksLoader({ ids: artworkIds }) | ||
} |
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,27 @@ | ||
import { opensearch } from "lib/apis/opensearch" | ||
|
||
export const getInitialArtworksSample = async (limit, artworksLoader) => { | ||
// initial artworks sample comes from indexed curators picks, but | ||
// in future we plan to come up with a more sophisticated approach | ||
const curatorsPicks = await opensearch(`/curators_picks/_search`, undefined, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
size: limit, | ||
query: { | ||
function_score: { | ||
functions: [ | ||
{ | ||
random_score: { | ||
seed: Math.floor(Math.random() * 1000), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
}) | ||
|
||
const artworkIds = curatorsPicks.hits?.hits?.map((hit) => hit._id) || [] | ||
|
||
return await artworksLoader({ ids: artworkIds }) | ||
} |
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.