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

community: Added splitting the Input to the allowed length of the Jina API. #27136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions libs/community/langchain_community/embeddings/jina.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ def validate_environment(cls, values: Dict) -> Any:

def _embed(self, input: Any) -> List[List[float]]:
# Call Jina AI Embedding API
resp = self.session.post( # type: ignore
JINA_API_URL, json={"input": input, "model": self.model_name}
).json()
if "data" not in resp:
raise RuntimeError(resp["detail"])

embeddings = resp["data"]
chunks = [input[i : i + 2047] for i in range(0, len(input), 2047)]
embeddings = []
for chunk in chunks:
resp = self.session.post( # type: ignore
JINA_API_URL, json={"input": chunk, "model": self.model_name}
).json()
if "data" not in resp:
raise RuntimeError(resp["detail"])
embeddings.extend(resp["data"])

# Sort resulting embeddings by index
sorted_embeddings = sorted(embeddings, key=lambda e: e["index"]) # type: ignore
Expand Down