Skip to content

Commit 2a888d7

Browse files
Merge pull request #656 from aws-samples/twelvelabs-sample
twelvelabs video embedding search sample update - add 3.0 sample, upd…
2 parents 173c415 + 5c40b1f commit 2a888d7

File tree

4 files changed

+254
-137
lines changed

4 files changed

+254
-137
lines changed

multi-modal/TwelveLabs/bedrock-twelvelabs-embedding-s3vectors.ipynb renamed to multi-modal/TwelveLabs/bedrock-twelvelabs-embedding-s3vectors-marengo-2.7.ipynb

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "3a0b2a41-bebe-4f3a-b803-eadc275a916f",
66
"metadata": {},
77
"source": [
8-
"# TwelveLabs Multimodal Embedding Search using Amazon Bedrock and Amazon S3 Vectors\n",
8+
"# TwelveLabs Multimodal Embedding (Marengo Embed 2.7) Search using Amazon Bedrock and Amazon S3 Vectors\n",
99
"Work with TwelveLabs Marengo Embed 2.7 Model and Amazon S3 Vectores\n",
1010
"![TwelveLabs Embedding](./images/12labs-embed-s3vectors.png)"
1111
]
@@ -82,7 +82,8 @@
8282
"aws_account_id = '<YOUR_AWS_ACCOUNT_ID>'\n",
8383
"\n",
8484
"s3vector_bucket = \"<S3_VECTOR_BUCKET_NAME_TO_CREATE>\"\n",
85-
"s3vector_index = \"<S3_VECTOR_INDEX_NAME_TO_CREATE>\""
85+
"s3vector_index = \"<S3_VECTOR_INDEX_NAME_TO_CREATE>\"\n",
86+
"dimension = 1024"
8687
]
8788
},
8889
{
@@ -235,8 +236,11 @@
235236
"outputs": [],
236237
"source": [
237238
"# Create a S3 vector bucket\n",
238-
"s3vectors.create_vector_bucket(vectorBucketName=s3vector_bucket)\n",
239-
"print(f\"Vector bucket '{s3vector_bucket}' created successfully.\")"
239+
"try:\n",
240+
" s3vectors.create_vector_bucket(vectorBucketName=s3vector_bucket)\n",
241+
" print(f\"Vector bucket '{s3vector_bucket}' created successfully.\")\n",
242+
"except Exception as ex:\n",
243+
" print(ex)"
240244
]
241245
},
242246
{
@@ -247,17 +251,19 @@
247251
"outputs": [],
248252
"source": [
249253
"# Create an index in the vector store\n",
250-
"vector_dimension = 1024\n",
251-
"distance_metric = 'cosine' # or 'euclidean'\n",
252-
"\n",
253-
"s3vectors.create_index(\n",
254-
" vectorBucketName=s3vector_bucket,\n",
255-
" indexName=s3vector_index,\n",
256-
" dataType='float32', # Common data type for vector embeddings\n",
257-
" dimension=vector_dimension,\n",
258-
" distanceMetric=distance_metric\n",
259-
")\n",
260-
"print(f\"Vector index '{s3vector_index}' created successfully in bucket '{s3vector_bucket}'.\")\n"
254+
"try:\n",
255+
" distance_metric = 'cosine' # or 'euclidean'\n",
256+
" \n",
257+
" s3vectors.create_index(\n",
258+
" vectorBucketName=s3vector_bucket,\n",
259+
" indexName=s3vector_index,\n",
260+
" dataType='float32', # Common data type for vector embeddings\n",
261+
" dimension=dimension,\n",
262+
" distanceMetric=distance_metric\n",
263+
" )\n",
264+
" print(f\"Vector index '{s3vector_index}' created successfully in bucket '{s3vector_bucket}'.\")\n",
265+
"except Exception as ex:\n",
266+
" print(ex)"
261267
]
262268
},
263269
{
@@ -312,8 +318,10 @@
312318
{
313319
"cell_type": "code",
314320
"execution_count": null,
315-
"id": "795ad337-ab73-4676-a2fc-93d504f6dda0",
316-
"metadata": {},
321+
"id": "c2d15aa6-6924-47d7-abc6-d78d6c252068",
322+
"metadata": {
323+
"scrolled": true
324+
},
317325
"outputs": [],
318326
"source": [
319327
"# Read image\n",
@@ -322,32 +330,23 @@
322330
"with open('./images/meridian-car.png', \"rb\") as image_file:\n",
323331
" base64_string = base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
324332
"\n",
325-
"import uuid\n",
326-
"query_prefix = f'{s3_prefix}/input/{uuid.uuid4()}'\n",
327-
"\n",
328-
"# Create an input embedding\n",
329-
"response = bedrock.start_async_invoke(\n",
330-
" modelId=model_id,\n",
331-
" modelInput = {\n",
332-
" \"inputType\": \"image\",\n",
333-
" \"mediaSource\": {\n",
334-
" \"base64String\": base64_string\n",
335-
" }\n",
336-
" },\n",
337-
" outputDataConfig={\n",
338-
" \"s3OutputDataConfig\": {\n",
339-
" \"s3Uri\": f's3://{s3_bucket}/{query_prefix}'\n",
340-
" }\n",
341-
" }\n",
333+
"request = {\n",
334+
" \"inputType\": \"image\",\n",
335+
" \"mediaSource\": {\n",
336+
" \"base64String\": base64_string\n",
337+
" }\n",
338+
"}\n",
339+
"\n",
340+
"# Make the request\n",
341+
"response = bedrock.invoke_model(\n",
342+
" modelId=f'us.{model_id}',\n",
343+
" body=json.dumps(request)\n",
342344
")\n",
343345
"\n",
344-
"# Print Job ID\n",
345-
"invocation_arn = response[\"invocationArn\"]\n",
346-
"print(\"Async Job Started\")\n",
347-
"print(\"Invocation Arn:\", invocation_arn)\n",
348-
"\n",
349-
"query = wait_for_output_file(s3_bucket, query_prefix, invocation_arn)\n",
350-
"display(JSON(query))"
346+
"# Print the response body\n",
347+
"response_body = json.loads(response['body'].read().decode('utf-8'))\n",
348+
"embedding = response_body[\"data\"][0][\"embedding\"]\n",
349+
"display(JSON(embedding))"
351350
]
352351
},
353352
{
@@ -368,8 +367,6 @@
368367
},
369368
"outputs": [],
370369
"source": [
371-
"embedding = query[0][\"embedding\"]\n",
372-
"\n",
373370
"# Query vector index.\n",
374371
"response = s3vectors.query_vectors(\n",
375372
" vectorBucketName=s3vector_bucket,\n",

0 commit comments

Comments
 (0)