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

Add pagination for Vertex AI embeddings #5325

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions langchain/embeddings/vertexai.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
Returns:
List of embeddings, one for each text.
"""
embeddings = self.client.get_embeddings(texts)
return [el.values for el in embeddings]
batch_size = 5
embeddings = []
for batch in range(0, len(texts), batch_size):
text_batch = texts[batch : batch + batch_size]
embeddings_batch = self.client.get_embeddings(text_batch)
embeddings.extend([el.values for el in embeddings_batch])
return embeddings

def embed_query(self, text: str) -> List[float]:
"""Embed a text.
Expand Down
19 changes: 19 additions & 0 deletions tests/integration_tests/embeddings/test_vertexai.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ def test_embedding_query() -> None:
model = VertexAIEmbeddings()
output = model.embed_query(document)
assert len(output) == 768


def test_paginated_texts() -> None:
documents = [
"foo bar",
"foo baz",
"bar foo",
"baz foo",
"bar bar",
"foo foo",
"baz baz",
"baz bar",
]
model = VertexAIEmbeddings()
output = model.embed_documents(documents)
assert len(output) == 8
assert len(output[0]) == 768
assert model._llm_type == "vertexai"
assert model.model_name == model.client._model_id