Skip to content

Commit

Permalink
fix: supported reporting of image tokens from amazon image embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik committed Jul 31, 2024
1 parent 7c2a1f6 commit f5d734f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ __pycache__
.nox
dist
.vscode/launch.json
~*
leftovers
~*
20 changes: 15 additions & 5 deletions aidial_adapter_bedrock/embedding/amazon/titan_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class AmazonRequest(BaseModel):
inputText: str | None = None
inputImage: str | None = None

def get_image_tokens(self) -> int:
# According to https://aws.amazon.com/bedrock/pricing/:
# Price per 1000 input (text) tokens = $0.0008
# Price per input image = $0.00006
# Therefore, cost of input image = $0.00006 / ($0.0008 / 1000) = 75 tokens
return 0 if self.inputImage is None else 75


def create_titan_request(
request: AmazonRequest, dimensions: int | None
Expand Down Expand Up @@ -146,20 +153,23 @@ async def embeddings(
token_count = 0

# NOTE: Amazon Titan doesn't support batched inputs
async for text_input in get_requests(request, self.storage):
sub_request = create_titan_request(text_input, request.dimensions)
embedding, tokens = await call_embedding_model(
self.client, self.model, sub_request
async for sub_request in get_requests(request, self.storage):
embedding, text_tokens = await call_embedding_model(
self.client,
self.model,
create_titan_request(sub_request, request.dimensions),
)

image_tokens = sub_request.get_image_tokens()

vector = (
vector_to_base64(embedding)
if request.encoding_format == "base64"
else embedding
)

vectors.append(vector)
token_count += tokens
token_count += text_tokens + image_tokens

return make_embeddings_response(
model=self.model,
Expand Down

0 comments on commit f5d734f

Please sign in to comment.