Skip to content

fix: adjust max token size for openai ADA-v2 embeddings - #3793

Merged
julian-risch merged 2 commits into
deepset-ai:mainfrom
LeoGitGuy:patch-2
Jan 4, 2023
Merged

fix: adjust max token size for openai ADA-v2 embeddings#3793
julian-risch merged 2 commits into
deepset-ai:mainfrom
LeoGitGuy:patch-2

Conversation

@LeoGitGuy

@LeoGitGuy LeoGitGuy commented Jan 2, 2023

Copy link
Copy Markdown
Contributor

Related Issues

  • fixes number of input tokens being restricted to 2048 even when using the new "text-embedding-ada-002" which can take 8191 tokens.

Proposed Changes:

The following code shows that the max_seq_len is set to 2048 or lower:

class _OpenAIEmbeddingEncoder(_BaseEmbeddingEncoder):
    def __init__(self, retriever: "EmbeddingRetriever"):
        # See https://beta.openai.com/docs/guides/embeddings for more details
        # OpenAI has a max seq length of 2048 tokens and unknown max batch size
        self.max_seq_len = min(2048, retriever.max_seq_len)

To fix this, I added the user specified parameter retriever.max_seq_len to the function self._setup_encoding_models() and change the max_seq_len when the if clause for the new text-embedding is called, so the function definition will look like this:

def _setup_encoding_models(self, model_class: str, model_name: str, max_seq_len: int):
    """
    Setup the encoding models for the retriever.
    """
    # new generation of embedding models (December 2022), we need to specify the full name
    if "text-embedding" in model_name:
        self.query_encoder_model = model_name
        self.doc_encoder_model = model_name
        self.max_seq_len = min(8191, max_seq_len)
    else:
        self.query_encoder_model = f"text-search-{model_class}-query-001"
        self.doc_encoder_model = f"text-search-{model_class}-doc-001"

How did you test it?

manual tests with text-embedding set to text-embedding-ada-002 and number of tokens set to 8191

Notes for the reviewer

n/A

Checklist

this is my first pull request so I'm sorry if I did something wrong, tried my best

@LeoGitGuy
LeoGitGuy requested a review from a team as a code owner January 2, 2023 10:33
@LeoGitGuy
LeoGitGuy requested review from julian-risch and removed request for a team January 2, 2023 10:33

@julian-risch julian-risch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@LeoGitGuy Thank you for opening this PR. The added functionality looks very good. 👍 I just have a small change request about moving two lines of code so that we use

# OpenAI has a max seq length of 2048 tokens and unknown max batch size
self.max_seq_len = min(2048, max_seq_len)

in the _setup_encoding_models method instead of the init method. My comment below describes that in more detail. Once you add this change, I will merge your PR.

self.max_seq_len = min(8191, max_seq_len)
else:
self.query_encoder_model = f"text-search-{model_class}-query-001"
self.doc_encoder_model = f"text-search-{model_class}-doc-001"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add self.max_seq_len = min(2048, max_seq_len) after this line of code instead of having it in the init method. If we add it here, we can remove line 394 and 395 (the self.max_seq_len=... assignment and the comment # OpenAI has a max seq length of 2048 tokens and unknown max batch size) from the init.

That makes it easier to read the code and see where the max_seq_len is set based on the inferred generation of embedding models (Dec 2022 or earlier).

@LeoGitGuy LeoGitGuy Jan 4, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot for your reply! I added your proposed change and also fixed a small error since for the earlier embedding models the max_seq_len is 2046 instead of 2048 (see https://beta.openai.com/docs/guides/embeddings/what-are-embeddings). I tested it and hope everything is fine now

Apparently the limit for the older models is 2046 and not 2048, I included this change directly. 
See (https://beta.openai.com/docs/guides/embeddings/what-are-embeddings) to check.

@julian-risch julian-risch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks very good to me and I will merge it now. 👍 @LeoGitGuy thank you so much for your contribution.

@julian-risch
julian-risch merged commit 35e9ff2 into deepset-ai:main Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants