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

Determine embedding size with Titan Embedding v2 model #39

Merged
merged 1 commit into from
May 17, 2024

Conversation

bigbernnn
Copy link
Contributor

Support for Titan Embedding v2 with changing embedding sizes based on this blog. This change lets the user specify dimension.

from langchain_aws.embeddings import BedrockEmbeddings

prompt_data = """Priority should be funding retirement through ROTH/IRA/401K over HSA extra.  
You need to fund your HSA for reasonable and expected medical expenses. """

bedrock_embedding_model_id = "amazon.titan-embed-text-v2:0"

embed_model = BedrockEmbeddings(model_id=bedrock_embedding_model_id)
response = embed_model.embed_documents([prompt_data], 256, True) 
response = embed_model.embed_query(prompt_data, 512, True)

Copy link
Collaborator

@3coins 3coins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@3coins 3coins merged commit 622756c into langchain-ai:main May 17, 2024
12 checks passed
@rsgrewal-aws
Copy link

Looks good

@trendafil-gechev
Copy link

@bigbernnn @3coins Hi all,
I think this change breaks the use of model_kwargs when passed to the BedrockEmbeddings instance. Consider this example:

from langchain_aws import BedrockEmbeddings

model_kwargs = {
    "dimensions": 256,
    "normalize": False
}

embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0",
                               region_name="eu-central-1", model_kwargs=model_kwargs)

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
embedded = embeddings.embed_documents(texts=[text])

print(embedded[0])
print(len(embedded[0]))

This would still result in a normalized vector of 1024 dimensions since the method embed_documents resolves to its default values for dim and norm.

This is also problematic when using the BedrockEmbeddings with a vector store e.g:

from langchain_aws import BedrockEmbeddings
from langchain_postgres.vectorstores import PGVector
import os

DB_USER = os.getenv('PGUSER')
DB_PASSWORD = os.getenv('PGPASSWORD')
DB_HOST = os.getenv('PGHOST')
DB_PORT = os.getenv('PGPORT')
DB_NAME = os.getenv('PGDATABASE')

model_kwargs = {
    "dimensions": 256,
    "normalize": False
}

connection = f"postgresql+psycopg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0",
                               region_name="eu-central-1", model_kwargs=model_kwargs)

pgvector_store = PGVector(
    connection=connection, embeddings=embeddings)

pgvector_store.add_texts(texts=[text])

The add_texts method calls the embed_documents underneath which again results in a vector with settings determined by the dim and norm parameters rather than the model_kwargs supplied to the BedrockEmbeddings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants