Skip to content

Commit 4187950

Browse files
add back deprecated api [v0.9.48] (run-llama#10581)
1 parent 9793117 commit 4187950

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## [0.9.48] - 2024-02-12
4+
5+
### Bug Fixes / Nits
6+
7+
- Add back deprecated API for BedrockEmbdding (#10581)
8+
39
## [0.9.47] - 2024-02-11
410

511
Last patch before v0.10!

llama_index/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.47
1+
0.9.48

llama_index/embeddings/bedrock.py

+152
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import json
2+
import os
3+
import warnings
24
from enum import Enum
35
from typing import Any, Callable, Dict, List, Literal, Optional, Sequence
46

7+
from deprecated import deprecated
8+
59
from llama_index.bridge.pydantic import Field, PrivateAttr
610
from llama_index.callbacks.base import CallbackManager
11+
from llama_index.constants import DEFAULT_EMBED_BATCH_SIZE
712
from llama_index.core.embeddings.base import BaseEmbedding, Embedding
813
from llama_index.core.llms.types import ChatMessage
914
from llama_index.types import BaseOutputParser, PydanticProgramMode
@@ -166,6 +171,153 @@ def list_supported_models() -> Dict[str, List[str]]:
166171
def class_name(self) -> str:
167172
return "BedrockEmbedding"
168173

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+
169321
def _get_embedding(self, payload: str, type: Literal["text", "query"]) -> Embedding:
170322
if self._client is None:
171323
self.set_credentials()

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ name = "llama-index"
4242
packages = [{include = "llama_index"}]
4343
readme = "README.md"
4444
repository = "https://github.com/run-llama/llama_index"
45-
version = "0.9.47"
45+
version = "0.9.48"
4646

4747
[tool.poetry.dependencies]
4848
SQLAlchemy = {extras = ["asyncio"], version = ">=1.4.49"}

0 commit comments

Comments
 (0)