Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions haystack/nodes/retriever/_embedding_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,30 +391,30 @@ def save(self, save_dir: Union[Path, str]):
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)
self.url = "https://api.openai.com/v1/embeddings"
self.api_key = retriever.api_key
self.batch_size = min(64, retriever.batch_size)
self.progress_bar = retriever.progress_bar
model_class: str = next(
(m for m in ["ada", "babbage", "davinci", "curie"] if m in retriever.embedding_model), "babbage"
)
self._setup_encoding_models(model_class, retriever.embedding_model)
self._setup_encoding_models(model_class, retriever.embedding_model, retriever.max_seq_len)

self.tokenizer = AutoTokenizer.from_pretrained("gpt2")

def _setup_encoding_models(self, model_class: str, model_name: str):
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"

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

self.max_seq_len = min(2046, max_seq_len)

def _ensure_text_limit(self, text: str) -> str:
"""
Expand Down