|
1 | 1 | import os |
| 2 | +import copy |
| 3 | +import json |
| 4 | +import botocore |
| 5 | +import aioboto3 |
| 6 | +import botocore.errorfactory |
2 | 7 | import numpy as np |
3 | 8 | import ollama |
4 | 9 | from openai import AsyncOpenAI, APIConnectionError, RateLimitError, Timeout |
@@ -48,6 +53,81 @@ async def openai_complete_if_cache( |
48 | 53 | ) |
49 | 54 | return response.choices[0].message.content |
50 | 55 |
|
| 56 | + |
| 57 | +class BedrockError(Exception): |
| 58 | + """Generic error for issues related to Amazon Bedrock""" |
| 59 | + |
| 60 | + |
| 61 | +@retry( |
| 62 | + stop=stop_after_attempt(5), |
| 63 | + wait=wait_exponential(multiplier=1, max=60), |
| 64 | + retry=retry_if_exception_type((BedrockError)), |
| 65 | +) |
| 66 | +async def bedrock_complete_if_cache( |
| 67 | + model, prompt, system_prompt=None, history_messages=[], |
| 68 | + aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, **kwargs |
| 69 | +) -> str: |
| 70 | + os.environ['AWS_ACCESS_KEY_ID'] = os.environ.get('AWS_ACCESS_KEY_ID', aws_access_key_id) |
| 71 | + os.environ['AWS_SECRET_ACCESS_KEY'] = os.environ.get('AWS_SECRET_ACCESS_KEY', aws_secret_access_key) |
| 72 | + os.environ['AWS_SESSION_TOKEN'] = os.environ.get('AWS_SESSION_TOKEN', aws_session_token) |
| 73 | + |
| 74 | + # Fix message history format |
| 75 | + messages = [] |
| 76 | + for history_message in history_messages: |
| 77 | + message = copy.copy(history_message) |
| 78 | + message['content'] = [{'text': message['content']}] |
| 79 | + messages.append(message) |
| 80 | + |
| 81 | + # Add user prompt |
| 82 | + messages.append({'role': "user", 'content': [{'text': prompt}]}) |
| 83 | + |
| 84 | + # Initialize Converse API arguments |
| 85 | + args = { |
| 86 | + 'modelId': model, |
| 87 | + 'messages': messages |
| 88 | + } |
| 89 | + |
| 90 | + # Define system prompt |
| 91 | + if system_prompt: |
| 92 | + args['system'] = [{'text': system_prompt}] |
| 93 | + |
| 94 | + # Map and set up inference parameters |
| 95 | + inference_params_map = { |
| 96 | + 'max_tokens': "maxTokens", |
| 97 | + 'top_p': "topP", |
| 98 | + 'stop_sequences': "stopSequences" |
| 99 | + } |
| 100 | + if (inference_params := list(set(kwargs) & set(['max_tokens', 'temperature', 'top_p', 'stop_sequences']))): |
| 101 | + args['inferenceConfig'] = {} |
| 102 | + for param in inference_params: |
| 103 | + args['inferenceConfig'][inference_params_map.get(param, param)] = kwargs.pop(param) |
| 104 | + |
| 105 | + hashing_kv: BaseKVStorage = kwargs.pop("hashing_kv", None) |
| 106 | + if hashing_kv is not None: |
| 107 | + args_hash = compute_args_hash(model, messages) |
| 108 | + if_cache_return = await hashing_kv.get_by_id(args_hash) |
| 109 | + if if_cache_return is not None: |
| 110 | + return if_cache_return["return"] |
| 111 | + |
| 112 | + # Call model via Converse API |
| 113 | + session = aioboto3.Session() |
| 114 | + async with session.client("bedrock-runtime") as bedrock_async_client: |
| 115 | + |
| 116 | + try: |
| 117 | + response = await bedrock_async_client.converse(**args, **kwargs) |
| 118 | + except Exception as e: |
| 119 | + raise BedrockError(e) |
| 120 | + |
| 121 | + if hashing_kv is not None: |
| 122 | + await hashing_kv.upsert({ |
| 123 | + args_hash: { |
| 124 | + 'return': response['output']['message']['content'][0]['text'], |
| 125 | + 'model': model |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + return response['output']['message']['content'][0]['text'] |
| 130 | + |
51 | 131 | async def hf_model_if_cache( |
52 | 132 | model, prompt, system_prompt=None, history_messages=[], **kwargs |
53 | 133 | ) -> str: |
@@ -145,6 +225,19 @@ async def gpt_4o_mini_complete( |
145 | 225 | **kwargs, |
146 | 226 | ) |
147 | 227 |
|
| 228 | + |
| 229 | +async def bedrock_complete( |
| 230 | + prompt, system_prompt=None, history_messages=[], **kwargs |
| 231 | +) -> str: |
| 232 | + return await bedrock_complete_if_cache( |
| 233 | + "anthropic.claude-3-haiku-20240307-v1:0", |
| 234 | + prompt, |
| 235 | + system_prompt=system_prompt, |
| 236 | + history_messages=history_messages, |
| 237 | + **kwargs, |
| 238 | + ) |
| 239 | + |
| 240 | + |
148 | 241 | async def hf_model_complete( |
149 | 242 | prompt, system_prompt=None, history_messages=[], **kwargs |
150 | 243 | ) -> str: |
@@ -186,6 +279,71 @@ async def openai_embedding(texts: list[str], model: str = "text-embedding-3-smal |
186 | 279 | return np.array([dp.embedding for dp in response.data]) |
187 | 280 |
|
188 | 281 |
|
| 282 | +# @wrap_embedding_func_with_attrs(embedding_dim=1024, max_token_size=8192) |
| 283 | +# @retry( |
| 284 | +# stop=stop_after_attempt(3), |
| 285 | +# wait=wait_exponential(multiplier=1, min=4, max=10), |
| 286 | +# retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)), # TODO: fix exceptions |
| 287 | +# ) |
| 288 | +async def bedrock_embedding( |
| 289 | + texts: list[str], model: str = "amazon.titan-embed-text-v2:0", |
| 290 | + aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None) -> np.ndarray: |
| 291 | + os.environ['AWS_ACCESS_KEY_ID'] = os.environ.get('AWS_ACCESS_KEY_ID', aws_access_key_id) |
| 292 | + os.environ['AWS_SECRET_ACCESS_KEY'] = os.environ.get('AWS_SECRET_ACCESS_KEY', aws_secret_access_key) |
| 293 | + os.environ['AWS_SESSION_TOKEN'] = os.environ.get('AWS_SESSION_TOKEN', aws_session_token) |
| 294 | + |
| 295 | + session = aioboto3.Session() |
| 296 | + async with session.client("bedrock-runtime") as bedrock_async_client: |
| 297 | + |
| 298 | + if (model_provider := model.split(".")[0]) == "amazon": |
| 299 | + embed_texts = [] |
| 300 | + for text in texts: |
| 301 | + if "v2" in model: |
| 302 | + body = json.dumps({ |
| 303 | + 'inputText': text, |
| 304 | + # 'dimensions': embedding_dim, |
| 305 | + 'embeddingTypes': ["float"] |
| 306 | + }) |
| 307 | + elif "v1" in model: |
| 308 | + body = json.dumps({ |
| 309 | + 'inputText': text |
| 310 | + }) |
| 311 | + else: |
| 312 | + raise ValueError(f"Model {model} is not supported!") |
| 313 | + |
| 314 | + response = await bedrock_async_client.invoke_model( |
| 315 | + modelId=model, |
| 316 | + body=body, |
| 317 | + accept="application/json", |
| 318 | + contentType="application/json" |
| 319 | + ) |
| 320 | + |
| 321 | + response_body = await response.get('body').json() |
| 322 | + |
| 323 | + embed_texts.append(response_body['embedding']) |
| 324 | + elif model_provider == "cohere": |
| 325 | + body = json.dumps({ |
| 326 | + 'texts': texts, |
| 327 | + 'input_type': "search_document", |
| 328 | + 'truncate': "NONE" |
| 329 | + }) |
| 330 | + |
| 331 | + response = await bedrock_async_client.invoke_model( |
| 332 | + model=model, |
| 333 | + body=body, |
| 334 | + accept="application/json", |
| 335 | + contentType="application/json" |
| 336 | + ) |
| 337 | + |
| 338 | + response_body = json.loads(response.get('body').read()) |
| 339 | + |
| 340 | + embed_texts = response_body['embeddings'] |
| 341 | + else: |
| 342 | + raise ValueError(f"Model provider '{model_provider}' is not supported!") |
| 343 | + |
| 344 | + return np.array(embed_texts) |
| 345 | + |
| 346 | + |
189 | 347 | async def hf_embedding(texts: list[str], tokenizer, embed_model) -> np.ndarray: |
190 | 348 | input_ids = tokenizer(texts, return_tensors='pt', padding=True, truncation=True).input_ids |
191 | 349 | with torch.no_grad(): |
|
0 commit comments