|
5 | 5 | "id": "3a0b2a41-bebe-4f3a-b803-eadc275a916f", |
6 | 6 | "metadata": {}, |
7 | 7 | "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", |
9 | 9 | "Work with TwelveLabs Marengo Embed 2.7 Model and Amazon S3 Vectores\n", |
10 | 10 | "" |
11 | 11 | ] |
|
82 | 82 | "aws_account_id = '<YOUR_AWS_ACCOUNT_ID>'\n", |
83 | 83 | "\n", |
84 | 84 | "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" |
86 | 87 | ] |
87 | 88 | }, |
88 | 89 | { |
|
235 | 236 | "outputs": [], |
236 | 237 | "source": [ |
237 | 238 | "# 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)" |
240 | 244 | ] |
241 | 245 | }, |
242 | 246 | { |
|
247 | 251 | "outputs": [], |
248 | 252 | "source": [ |
249 | 253 | "# 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)" |
261 | 267 | ] |
262 | 268 | }, |
263 | 269 | { |
|
312 | 318 | { |
313 | 319 | "cell_type": "code", |
314 | 320 | "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 | + }, |
317 | 325 | "outputs": [], |
318 | 326 | "source": [ |
319 | 327 | "# Read image\n", |
|
322 | 330 | "with open('./images/meridian-car.png', \"rb\") as image_file:\n", |
323 | 331 | " base64_string = base64.b64encode(image_file.read()).decode(\"utf-8\")\n", |
324 | 332 | "\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", |
342 | 344 | ")\n", |
343 | 345 | "\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))" |
351 | 350 | ] |
352 | 351 | }, |
353 | 352 | { |
|
368 | 367 | }, |
369 | 368 | "outputs": [], |
370 | 369 | "source": [ |
371 | | - "embedding = query[0][\"embedding\"]\n", |
372 | | - "\n", |
373 | 370 | "# Query vector index.\n", |
374 | 371 | "response = s3vectors.query_vectors(\n", |
375 | 372 | " vectorBucketName=s3vector_bucket,\n", |
|
0 commit comments