|
| 1 | +""" |
| 2 | +Transforms OpenAI-style requests into TwelveLabs Pegasus 1.2 requests for Bedrock. |
| 3 | +
|
| 4 | +Reference: |
| 5 | +https://docs.twelvelabs.io/docs/models/pegasus |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Any, Dict, List, Optional |
| 9 | + |
| 10 | +from litellm.llms.base_llm.base_utils import type_to_response_format_param |
| 11 | +from litellm.llms.base_llm.chat.transformation import BaseConfig |
| 12 | +from litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformation import ( |
| 13 | + AmazonInvokeConfig, |
| 14 | +) |
| 15 | +from litellm.types.llms.openai import AllMessageValues |
| 16 | +from litellm.utils import get_base64_str |
| 17 | + |
| 18 | + |
| 19 | +class AmazonTwelveLabsPegasusConfig(AmazonInvokeConfig, BaseConfig): |
| 20 | + """ |
| 21 | + Handles transforming OpenAI-style requests into Bedrock InvokeModel requests for |
| 22 | + `twelvelabs.pegasus-1-2-v1:0`. |
| 23 | +
|
| 24 | + Pegasus 1.2 requires an `inputPrompt` and a `mediaSource` that either references |
| 25 | + an S3 object or a base64-encoded clip. Optional OpenAI params (temperature, |
| 26 | + response_format, max_tokens) are translated to the TwelveLabs schema. |
| 27 | + """ |
| 28 | + |
| 29 | + def get_supported_openai_params(self, model: str) -> List[str]: |
| 30 | + return [ |
| 31 | + "max_tokens", |
| 32 | + "max_completion_tokens", |
| 33 | + "temperature", |
| 34 | + "response_format", |
| 35 | + ] |
| 36 | + |
| 37 | + def map_openai_params( |
| 38 | + self, |
| 39 | + non_default_params: dict, |
| 40 | + optional_params: dict, |
| 41 | + model: str, |
| 42 | + drop_params: bool, |
| 43 | + ) -> dict: |
| 44 | + for param, value in non_default_params.items(): |
| 45 | + if param in {"max_tokens", "max_completion_tokens"}: |
| 46 | + optional_params["maxOutputTokens"] = value |
| 47 | + if param == "temperature": |
| 48 | + optional_params["temperature"] = value |
| 49 | + if param == "response_format": |
| 50 | + optional_params["responseFormat"] = self._normalize_response_format( |
| 51 | + value |
| 52 | + ) |
| 53 | + return optional_params |
| 54 | + |
| 55 | + def _normalize_response_format(self, value: Any) -> Any: |
| 56 | + if isinstance(value, dict): |
| 57 | + return value |
| 58 | + return type_to_response_format_param(response_format=value) or value |
| 59 | + |
| 60 | + def transform_request( |
| 61 | + self, |
| 62 | + model: str, |
| 63 | + messages: List[AllMessageValues], |
| 64 | + optional_params: dict, |
| 65 | + litellm_params: dict, |
| 66 | + headers: dict, |
| 67 | + ) -> dict: |
| 68 | + input_prompt = self._convert_messages_to_prompt(messages=messages) |
| 69 | + request_data: Dict[str, Any] = {"inputPrompt": input_prompt} |
| 70 | + |
| 71 | + media_source = self._build_media_source(optional_params) |
| 72 | + if media_source is not None: |
| 73 | + request_data["mediaSource"] = media_source |
| 74 | + |
| 75 | + for key in ("temperature", "maxOutputTokens", "responseFormat"): |
| 76 | + if key in optional_params: |
| 77 | + request_data[key] = optional_params.get(key) |
| 78 | + return request_data |
| 79 | + |
| 80 | + def _build_media_source(self, optional_params: dict) -> Optional[dict]: |
| 81 | + direct_source = optional_params.get("mediaSource") or optional_params.get( |
| 82 | + "media_source" |
| 83 | + ) |
| 84 | + if isinstance(direct_source, dict): |
| 85 | + return direct_source |
| 86 | + |
| 87 | + base64_input = optional_params.get("video_base64") or optional_params.get( |
| 88 | + "base64_string" |
| 89 | + ) |
| 90 | + if base64_input: |
| 91 | + return {"base64String": get_base64_str(base64_input)} |
| 92 | + |
| 93 | + s3_uri = ( |
| 94 | + optional_params.get("video_s3_uri") |
| 95 | + or optional_params.get("s3_uri") |
| 96 | + or optional_params.get("media_source_s3_uri") |
| 97 | + ) |
| 98 | + if s3_uri: |
| 99 | + s3_location = {"uri": s3_uri} |
| 100 | + bucket_owner = ( |
| 101 | + optional_params.get("video_s3_bucket_owner") |
| 102 | + or optional_params.get("s3_bucket_owner") |
| 103 | + or optional_params.get("media_source_bucket_owner") |
| 104 | + ) |
| 105 | + if bucket_owner: |
| 106 | + s3_location["bucketOwner"] = bucket_owner |
| 107 | + return {"s3Location": s3_location} |
| 108 | + return None |
| 109 | + |
| 110 | + def _convert_messages_to_prompt(self, messages: List[AllMessageValues]) -> str: |
| 111 | + prompt_parts: List[str] = [] |
| 112 | + for message in messages: |
| 113 | + role = message.get("role", "user") |
| 114 | + content = message.get("content", "") |
| 115 | + if isinstance(content, list): |
| 116 | + text_fragments = [] |
| 117 | + for item in content: |
| 118 | + if isinstance(item, dict): |
| 119 | + item_type = item.get("type") |
| 120 | + if item_type == "text": |
| 121 | + text_fragments.append(item.get("text", "")) |
| 122 | + elif item_type == "image_url": |
| 123 | + text_fragments.append("<image>") |
| 124 | + elif item_type == "video_url": |
| 125 | + text_fragments.append("<video>") |
| 126 | + elif item_type == "audio_url": |
| 127 | + text_fragments.append("<audio>") |
| 128 | + elif isinstance(item, str): |
| 129 | + text_fragments.append(item) |
| 130 | + content = " ".join(text_fragments) |
| 131 | + prompt_parts.append(f"{role}: {content}") |
| 132 | + return "\n".join(part for part in prompt_parts if part).strip() |
| 133 | + |
0 commit comments