From 6eafdc8f0537bdc1091d0f60b68983bf661b7a62 Mon Sep 17 00:00:00 2001 From: JP Hwang Date: Thu, 1 Aug 2024 11:50:09 +0100 Subject: [PATCH] Add missing ts examples for multi-target vector search --- .../code/howto/search.multi-target-v3.ts | 145 ++++++++++++++---- developers/weaviate/search/multi-vector.md | 34 +++- 2 files changed, 146 insertions(+), 33 deletions(-) diff --git a/_includes/code/howto/search.multi-target-v3.ts b/_includes/code/howto/search.multi-target-v3.ts index 0f778b40f8..54f78e0d06 100644 --- a/_includes/code/howto/search.multi-target-v3.ts +++ b/_includes/code/howto/search.multi-target-v3.ts @@ -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(); diff --git a/developers/weaviate/search/multi-vector.md b/developers/weaviate/search/multi-vector.md index 7012fb7cf7..4a5ffe0045 100644 --- a/developers/weaviate/search/multi-vector.md +++ b/developers/weaviate/search/multi-vector.md @@ -59,7 +59,7 @@ 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" />
Complete code @@ -67,7 +67,7 @@ Specify target vectors as a list/array of named vectors. The default join strate text={GoCode} startMarker="// START BasicFull" endMarker="// END BasicFull" - language="python" + language="go" />
@@ -89,9 +89,9 @@ Specify query vectors as a dictionary/map of names and vectors. @@ -113,6 +113,14 @@ The `sum`, `average`, `minimum` join strategies only require the name of the str language="python" /> + + + ## Weight raw vector distances @@ -137,6 +145,14 @@ For a more detailed explanation of how scores are normalized, see the blog post language="python" /> + + + ## Weight normalized vector distances @@ -159,6 +175,14 @@ Each distance is normalized against other results for that target vector. Each n language="python" /> + + + ## Related pages