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

Provide CAII batch embedding for better performance #35

Merged
merged 1 commit into from
Dec 9, 2024
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
29 changes: 28 additions & 1 deletion llm-service/app/services/CaiiEmbeddingModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import http.client as http_client
import json
import os
from typing import Any, Dict
from typing import Any, Dict, List

from llama_index.core.base.embeddings.base import BaseEmbedding, Embedding
from pydantic import Field
Expand Down Expand Up @@ -86,6 +86,33 @@ def _get_embedding(self, query: str, input_type: str) -> Embedding:

return embedding

def _get_text_embeddings(self, texts: List[str]) -> List[Embedding]:
model = self.endpoint["endpointmetadata"]["model_name"]
domain = os.environ["CAII_DOMAIN"]

connection = http_client.HTTPSConnection(domain, 443)
headers = self.build_auth_headers()
headers["Content-Type"] = "application/json"
body = json.dumps(
{
"input": texts,
"input_type": "passage",
"truncate": "END",
"model": model,
}
)
connection.request("POST", self.endpoint["url"], body=body, headers=headers)
res = connection.getresponse()
data = res.read()
json_response = data.decode("utf-8")
structured_response = json.loads(json_response)
embeddings = structured_response["data"][0]["embedding"]
assert isinstance(embeddings, list)
assert all(isinstance(x, list) for x in embeddings)
assert all(all(isinstance(y, float) for y in x) for x in embeddings)

return embeddings

def build_auth_headers(self) -> Dict[str, str]:
with open("/tmp/jwt", "r") as file:
jwt_contents = json.load(file)
Expand Down
Loading