litellm-mirror/litellm/llms/azure_ai/embed/cohere_transformation.py
Krish Dholakia ed5635e9a2 LiteLLM Minor Fixes & Improvements (09/24/2024) (#5880)
* LiteLLM Minor Fixes & Improvements (09/23/2024)  (#5842)

* feat(auth_utils.py): enable admin to allow client-side credentials to be passed

Makes it easier for devs to experiment with finetuned fireworks ai models

* feat(router.py): allow setting configurable_clientside_auth_params for a model

Closes https://github.com/BerriAI/litellm/issues/5843

* build(model_prices_and_context_window.json): fix anthropic claude-3-5-sonnet max output token limit

Fixes https://github.com/BerriAI/litellm/issues/5850

* fix(azure_ai/): support content list for azure ai

Fixes https://github.com/BerriAI/litellm/issues/4237

* fix(litellm_logging.py): always set saved_cache_cost

Set to 0 by default

* fix(fireworks_ai/cost_calculator.py): add fireworks ai default pricing

handles calling 405b+ size models

* fix(slack_alerting.py): fix error alerting for failed spend tracking

Fixes regression with slack alerting error monitoring

* fix(vertex_and_google_ai_studio_gemini.py): handle gemini no candidates in streaming chunk error

* docs(bedrock.md): add llama3-1 models

* test: fix tests

* fix(azure_ai/chat): fix transformation for azure ai calls

* feat(azure_ai/embed): Add azure ai embeddings support

Closes https://github.com/BerriAI/litellm/issues/5861

* fix(azure_ai/embed): enable async embedding

* feat(azure_ai/embed): support azure ai multimodal embeddings

* fix(azure_ai/embed): support async multi modal embeddings

* feat(together_ai/embed): support together ai embedding calls

* feat(rerank/main.py): log source documents for rerank endpoints to langfuse

improves rerank endpoint logging

* fix(langfuse.py): support logging `/audio/speech` input to langfuse

* test(test_embedding.py): fix test

* test(test_completion_cost.py): fix helper util
2024-09-25 22:11:57 -07:00

98 lines
3.5 KiB
Python

"""
Transformation logic from OpenAI /v1/embeddings format to Azure AI Cohere's /v1/embed.
Why separate file? Make it easy to see how transformation works
Convers
- Cohere request format
Docs - https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-embed-text.html
"""
from typing import List, Optional, Tuple, Union
from litellm.types.llms.azure_ai import ImageEmbeddingInput, ImageEmbeddingRequest
from litellm.types.llms.openai import EmbeddingCreateParams
from litellm.types.utils import Embedding, EmbeddingResponse, Usage
from litellm.utils import is_base64_encoded
class AzureAICohereConfig:
def __init__(self) -> None:
pass
def _map_azure_model_group(self, model: str) -> str:
if "model=offer-cohere-embed-multili-paygo":
return "Cohere-embed-v3-multilingual"
elif "model=offer-cohere-embed-english-paygo":
return "Cohere-embed-v3-english"
return model
def _transform_request_image_embeddings(
self, input: List[str], optional_params: dict
) -> ImageEmbeddingRequest:
"""
Assume all str in list is base64 encoded string
"""
image_input: List[ImageEmbeddingInput] = []
for i in input:
embedding_input = ImageEmbeddingInput(image=i)
image_input.append(embedding_input)
return ImageEmbeddingRequest(input=image_input, **optional_params)
def _transform_request(
self, input: List[str], optional_params: dict, model: str
) -> Tuple[ImageEmbeddingRequest, EmbeddingCreateParams, List[int]]:
"""
Return the list of input to `/image/embeddings`, `/v1/embeddings`, list of image_embedding_idx for recombination
"""
image_embeddings: List[str] = []
image_embedding_idx: List[int] = []
for idx, i in enumerate(input):
"""
- is base64 -> route to image embeddings
- is ImageEmbeddingInput -> route to image embeddings
- else -> route to `/v1/embeddings`
"""
if is_base64_encoded(i):
image_embeddings.append(i)
image_embedding_idx.append(idx)
## REMOVE IMAGE EMBEDDINGS FROM input list
filtered_input = [
item for idx, item in enumerate(input) if idx not in image_embedding_idx
]
v1_embeddings_request = EmbeddingCreateParams(
input=filtered_input, model=model, **optional_params
)
image_embeddings_request = self._transform_request_image_embeddings(
input=image_embeddings, optional_params=optional_params
)
return image_embeddings_request, v1_embeddings_request, image_embedding_idx
def _transform_response(self, response: EmbeddingResponse) -> EmbeddingResponse:
additional_headers: Optional[dict] = response._hidden_params.get(
"additional_headers"
)
if additional_headers:
# CALCULATE USAGE
input_tokens: Optional[str] = additional_headers.get(
"llm_provider-num_tokens"
)
if input_tokens:
if response.usage:
response.usage.prompt_tokens = int(input_tokens)
else:
response.usage = Usage(prompt_tokens=int(input_tokens))
# SET MODEL
base_model: Optional[str] = additional_headers.get(
"llm_provider-azureml-model-group"
)
if base_model:
response.model = self._map_azure_model_group(base_model)
return response