How can I create a vector embedding for a statement/sentence using the Titan embedding model on Bedrock?

0

Hi, I have been using the langchain library for LLM coding. Could someone share a simple code snippet that can be used to generate the vector embedding for a sentence stored in a string? I have no experience/ background using my AWS authentication in a Jupyter notebook and need guidance there. Thanks.

1 Answer
0

Hi Vishal! Thanks for reaching out.

Refer to the below snippet to generate a vector embedding for a sentence stored in a string:

from langchain.embeddings import BedrockEmbeddings

#create an Amazon Titan Text Embeddings client
embeddings_client = BedrockEmbeddings() 

#Define the text from which to create embeddings
text = "Can you please tell me how to get to the bakery?"

#Invoke the model
embedding = embeddings_client.embed_query(text)

#Print response
print(embedding)

This snippet was pulled from the "Getting started with Amazon Titan Text Embeddings in Amazon Bedrock" blog - https://aws.amazon.com/blogs/machine-learning/getting-started-with-amazon-titan-text-embeddings/

You'll be able to get the full example in the above link so please feel free to review this resource for additional, insightful information. This example uses the boto3 library which you can also use for authentication. That said, credentials can be configured in multiple ways so I'll point you to another relevant resource. You can choose which method is most appropriate given your specific use-case and/or needs which is helpful if you're working within a local Jupyter notebook - https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

Cheers!

AWS
SUPPORT ENGINEER
answered 2 months ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions