Skip to content
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 missing ts examples for multi-target vector search #2418

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 117 additions & 28 deletions _includes/code/howto/search.multi-target-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,139 @@
// ===== INSTANTIATION-COMMON =====
// ================================

import weaviate from 'weaviate-client';


import weaviate from 'weaviate-client'

const client = await weaviate.connectToLocal(
{
headers: { 'X-Openai-Api-Key': process.env.OPENAI_API_KEY || '', }
})
const client = await weaviate.connectToLocal({
headers: { 'X-Openai-Api-Key': process.env.OPENAI_API_KEY || '' },
});

// ========================
// ===== Basic search =====
// ========================

// START-ANY
var jeopardy, result;

// END-ANY

// START MultiBasic
const jeopardy = client.collections.get('Jeopardy_Tiny_Dataset');
jeopardy = client.collections.get('JeopardyTiny');

const result = await jeopardy.query.nearText('animals in movies', {
result = await jeopardy.query.nearText('a wild animal', {
limit: 2,
targetVector: jeopardy.multiTargetVector.sum(['jeopardy_questions_vector', 'jeopardy_answers_vector']),
})
// highlight-start
targetVector: ['jeopardy_questions_vector', 'jeopardy_answers_vector'],
// highlight-end
returnMetadata: ['distance'],
});

result.objects.forEach(item => {
console.log(JSON.stringify(item.properties, null, 2))
})
result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
console.log(JSON.stringify(item.metadata.distance, null, 2));
});
// END MultiBasic

// ========================
// ===== Specify query vectors =====
// ========================

var resp = await jeopardy.query.fetchObjects({
limit: 2,
includeVector: true,
});

var v1 = resp.objects[0].vectors.jeopardy_questions_vector;
var v2 = resp.objects[0].vectors.jeopardy_answers_vector;

// START MultiTargetNearVector
jeopardy = client.collections.get('JeopardyTiny');

result = await jeopardy.query.nearVector({
// highlight-start
'jeopardy_questions_vector': v1,
'jeopardy_answers_vector': v2
// highlight-end
}, {
limit: 2,
// highlight-start
targetVector: ['jeopardy_questions_vector', 'jeopardy_answers_vector'],
// highlight-end
returnMetadata: ['distance'],
});

result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
console.log(JSON.stringify(item.metadata.distance, null, 2));
});
// END MultiTargetNearVector

// ========================
// ===== Simple join strategy =====
// ========================

// START MultiTargetWithSimpleJoin
jeopardy = client.collections.get('JeopardyTiny');

result = await jeopardy.query.nearText('a wild animal', {
limit: 2,
// highlight-start
targetVector: jeopardy.multiTargetVector.average(['jeopardy_questions_vector', 'jeopardy_answers_vector']),
// highlight-end
returnMetadata: ['distance'],
});

result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
console.log(JSON.stringify(item.metadata.distance, null, 2));
});
// END MultiTargetWithSimpleJoin

// ========================
// ===== Set Weights =====
// ===== Set Manual Weights =====
// ========================

// START MultiWeights
const jeopardy = client.collections.get('Jeopardy_Tiny_Dataset');
// START MultiTargetManualWeights
jeopardy = client.collections.get('JeopardyTiny');

const result = await jeopardy.query.nearText('animals in movies', {
limit: 2,
targetVector: jeopardy.multiTargetVector.manualWeights(
{
'jeopardy_questions_vector': .5,
'jeopardy_answers_vector': 10
result = await jeopardy.query.nearText('a wild animal', {
limit: 2,
// highlight-start
targetVector: jeopardy.multiTargetVector.manualWeights({
jeopardy_questions_vector: 10,
jeopardy_answers_vector: 50,
}),
})
// highlight-end
returnMetadata: ['distance'],
});

result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
console.log(JSON.stringify(item.metadata.distance, null, 2));
});
// END MultiTargetManualWeights

// ========================
// ===== Relative Score =====
// ========================

// START MultiTargetRelativeScore
jeopardy = client.collections.get('JeopardyTiny');

result = await jeopardy.query.nearText('a wild animal', {
limit: 2,
// highlight-start
targetVector: jeopardy.multiTargetVector.relativeScore({
jeopardy_questions_vector: 10,
jeopardy_answers_vector: 10,
}),
// highlight-end
returnMetadata: ['distance'],
});

result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
console.log(JSON.stringify(item.metadata.distance, null, 2));
});
// END MultiTargetRelativeScore

result.objects.forEach(item => {
console.log(JSON.stringify(item.properties, null, 2))
})
// END MultiWeights
client.close();
34 changes: 29 additions & 5 deletions developers/weaviate/search/multi-vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ Specify target vectors as a list/array of named vectors. The default join strate
text={GoCode}
startMarker="// START MultiBasic"
endMarker="// END MultiBasic"
language="python"
language="go"
/>
<details>
<summary>Complete code</summary>
<FilteredTextBlock
text={GoCode}
startMarker="// START BasicFull"
endMarker="// END BasicFull"
language="python"
language="go"
/>
</details>
</TabItem>
Expand All @@ -89,9 +89,9 @@ Specify query vectors as a dictionary/map of names and vectors.
<TabItem value="ts" label="JS/TS Client v3">
<FilteredTextBlock
text={TSCodeV3}
startMarker="// START MultiWeights"
endMarker="// END MultiWeights"
language="python"
startMarker="// START MultiTargetNearVector"
endMarker="// END MultiTargetNearVector"
language="ts"
/>
</TabItem>
</Tabs>
Expand All @@ -113,6 +113,14 @@ The `sum`, `average`, `minimum` join strategies only require the name of the str
language="python"
/>
</TabItem>
<TabItem value="ts" label="JS/TS Client v3">
<FilteredTextBlock
text={TSCodeV3}
startMarker="// START MultiTargetWithSimpleJoin"
endMarker="// END MultiTargetWithSimpleJoin"
language="ts"
/>
</TabItem>
</Tabs>

## Weight raw vector distances
Expand All @@ -137,6 +145,14 @@ For a more detailed explanation of how scores are normalized, see the blog post
language="python"
/>
</TabItem>
<TabItem value="ts" label="JS/TS Client v3">
<FilteredTextBlock
text={TSCodeV3}
startMarker="// START MultiTargetManualWeights"
endMarker="// END MultiTargetManualWeights"
language="ts"
/>
</TabItem>
</Tabs>

## Weight normalized vector distances
Expand All @@ -159,6 +175,14 @@ Each distance is normalized against other results for that target vector. Each n
language="python"
/>
</TabItem>
<TabItem value="ts" label="JS/TS Client v3">
<FilteredTextBlock
text={TSCodeV3}
startMarker="// START MultiTargetRelativeScore"
endMarker="// END MultiTargetRelativeScore"
language="ts"
/>
</TabItem>
</Tabs>

## Related pages
Expand Down