|
1 | 1 | import json
|
| 2 | +import os |
| 3 | +import warnings |
2 | 4 | from enum import Enum
|
3 | 5 | from typing import Any, Callable, Dict, List, Literal, Optional, Sequence
|
4 | 6 |
|
| 7 | +from deprecated import deprecated |
| 8 | + |
5 | 9 | from llama_index.bridge.pydantic import Field, PrivateAttr
|
6 | 10 | from llama_index.callbacks.base import CallbackManager
|
| 11 | +from llama_index.constants import DEFAULT_EMBED_BATCH_SIZE |
7 | 12 | from llama_index.core.embeddings.base import BaseEmbedding, Embedding
|
8 | 13 | from llama_index.core.llms.types import ChatMessage
|
9 | 14 | from llama_index.types import BaseOutputParser, PydanticProgramMode
|
@@ -166,6 +171,153 @@ def list_supported_models() -> Dict[str, List[str]]:
|
166 | 171 | def class_name(self) -> str:
|
167 | 172 | return "BedrockEmbedding"
|
168 | 173 |
|
| 174 | + @deprecated( |
| 175 | + version="0.9.48", |
| 176 | + reason=( |
| 177 | + "Use the provided kwargs in the constructor, " |
| 178 | + "set_credentials will be removed in future releases." |
| 179 | + ), |
| 180 | + action="once", |
| 181 | + ) |
| 182 | + def set_credentials( |
| 183 | + self, |
| 184 | + aws_region: Optional[str] = None, |
| 185 | + aws_access_key_id: Optional[str] = None, |
| 186 | + aws_secret_access_key: Optional[str] = None, |
| 187 | + aws_session_token: Optional[str] = None, |
| 188 | + aws_profile: Optional[str] = None, |
| 189 | + ) -> None: |
| 190 | + aws_region = aws_region or os.getenv("AWS_REGION") |
| 191 | + aws_access_key_id = aws_access_key_id or os.getenv("AWS_ACCESS_KEY_ID") |
| 192 | + aws_secret_access_key = aws_secret_access_key or os.getenv( |
| 193 | + "AWS_SECRET_ACCESS_KEY" |
| 194 | + ) |
| 195 | + aws_session_token = aws_session_token or os.getenv("AWS_SESSION_TOKEN") |
| 196 | + |
| 197 | + if aws_region is None: |
| 198 | + warnings.warn( |
| 199 | + "AWS_REGION not found. Set environment variable AWS_REGION or set aws_region" |
| 200 | + ) |
| 201 | + |
| 202 | + if aws_access_key_id is None: |
| 203 | + warnings.warn( |
| 204 | + "AWS_ACCESS_KEY_ID not found. Set environment variable AWS_ACCESS_KEY_ID or set aws_access_key_id" |
| 205 | + ) |
| 206 | + assert aws_access_key_id is not None |
| 207 | + |
| 208 | + if aws_secret_access_key is None: |
| 209 | + warnings.warn( |
| 210 | + "AWS_SECRET_ACCESS_KEY not found. Set environment variable AWS_SECRET_ACCESS_KEY or set aws_secret_access_key" |
| 211 | + ) |
| 212 | + assert aws_secret_access_key is not None |
| 213 | + |
| 214 | + if aws_session_token is None: |
| 215 | + warnings.warn( |
| 216 | + "AWS_SESSION_TOKEN not found. Set environment variable AWS_SESSION_TOKEN or set aws_session_token" |
| 217 | + ) |
| 218 | + assert aws_session_token is not None |
| 219 | + |
| 220 | + session_kwargs = { |
| 221 | + "profile_name": aws_profile, |
| 222 | + "region_name": aws_region, |
| 223 | + "aws_access_key_id": aws_access_key_id, |
| 224 | + "aws_secret_access_key": aws_secret_access_key, |
| 225 | + "aws_session_token": aws_session_token, |
| 226 | + } |
| 227 | + |
| 228 | + try: |
| 229 | + import boto3 |
| 230 | + |
| 231 | + session = boto3.Session(**session_kwargs) |
| 232 | + except ImportError: |
| 233 | + raise ImportError( |
| 234 | + "boto3 package not found, install with" "'pip install boto3'" |
| 235 | + ) |
| 236 | + |
| 237 | + if "bedrock-runtime" in session.get_available_services(): |
| 238 | + self._client = session.client("bedrock-runtime") |
| 239 | + else: |
| 240 | + self._client = session.client("bedrock") |
| 241 | + |
| 242 | + @classmethod |
| 243 | + @deprecated( |
| 244 | + version="0.9.48", |
| 245 | + reason=( |
| 246 | + "Use the provided kwargs in the constructor, " |
| 247 | + "set_credentials will be removed in future releases." |
| 248 | + ), |
| 249 | + action="once", |
| 250 | + ) |
| 251 | + def from_credentials( |
| 252 | + cls, |
| 253 | + model_name: str = Models.TITAN_EMBEDDING, |
| 254 | + aws_region: Optional[str] = None, |
| 255 | + aws_access_key_id: Optional[str] = None, |
| 256 | + aws_secret_access_key: Optional[str] = None, |
| 257 | + aws_session_token: Optional[str] = None, |
| 258 | + aws_profile: Optional[str] = None, |
| 259 | + embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE, |
| 260 | + callback_manager: Optional[CallbackManager] = None, |
| 261 | + verbose: bool = False, |
| 262 | + ) -> "BedrockEmbedding": |
| 263 | + """ |
| 264 | + Instantiate using AWS credentials. |
| 265 | +
|
| 266 | + Args: |
| 267 | + model_name (str) : Name of the model |
| 268 | + aws_access_key_id (str): AWS access key ID |
| 269 | + aws_secret_access_key (str): AWS secret access key |
| 270 | + aws_session_token (str): AWS session token |
| 271 | + aws_region (str): AWS region where the service is located |
| 272 | + aws_profile (str): AWS profile, when None, default profile is chosen automatically |
| 273 | +
|
| 274 | + Example: |
| 275 | + .. code-block:: python |
| 276 | +
|
| 277 | + from llama_index.embeddings import BedrockEmbedding |
| 278 | +
|
| 279 | + # Define the model name |
| 280 | + model_name = "your_model_name" |
| 281 | +
|
| 282 | + embeddings = BedrockEmbedding.from_credentials( |
| 283 | + model_name, |
| 284 | + aws_access_key_id, |
| 285 | + aws_secret_access_key, |
| 286 | + aws_session_token, |
| 287 | + aws_region, |
| 288 | + aws_profile, |
| 289 | + ) |
| 290 | +
|
| 291 | + """ |
| 292 | + session_kwargs = { |
| 293 | + "profile_name": aws_profile, |
| 294 | + "region_name": aws_region, |
| 295 | + "aws_access_key_id": aws_access_key_id, |
| 296 | + "aws_secret_access_key": aws_secret_access_key, |
| 297 | + "aws_session_token": aws_session_token, |
| 298 | + } |
| 299 | + |
| 300 | + try: |
| 301 | + import boto3 |
| 302 | + |
| 303 | + session = boto3.Session(**session_kwargs) |
| 304 | + except ImportError: |
| 305 | + raise ImportError( |
| 306 | + "boto3 package not found, install with" "'pip install boto3'" |
| 307 | + ) |
| 308 | + |
| 309 | + if "bedrock-runtime" in session.get_available_services(): |
| 310 | + client = session.client("bedrock-runtime") |
| 311 | + else: |
| 312 | + client = session.client("bedrock") |
| 313 | + return cls( |
| 314 | + client=client, |
| 315 | + model_name=model_name, |
| 316 | + embed_batch_size=embed_batch_size, |
| 317 | + callback_manager=callback_manager, |
| 318 | + verbose=verbose, |
| 319 | + ) |
| 320 | + |
169 | 321 | def _get_embedding(self, payload: str, type: Literal["text", "query"]) -> Embedding:
|
170 | 322 | if self._client is None:
|
171 | 323 | self.set_credentials()
|
|
0 commit comments