-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_aws.py
126 lines (100 loc) · 4.14 KB
/
utils_aws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import json
import os
from enum import Enum
from pprint import pp
import boto3
from dotenv import load_dotenv
load_dotenv()
class BedrockModelEnum(str, Enum):
TITAN_TEXT_EXPRESS_V1 = "amazon.titan-text-express-v1"
CLAUDE_V2_1 = "anthropic.claude-v2:1"
class BedrockEmbeddingEnum(str, Enum):
TITAN_EMBED_TEXT_V1 = "amazon.titan-embed-text-v1"
COHERE_EMBED_ENGLISH_V3 = "cohere.embed-english-v3"
COHERE_EMBED_MULTILINGUAL_V3 = "cohere.embed-multilingual-v3"
class TextException(Exception):
def __init__(self, message):
self.message = message
class BedrockTextModelBotoUtils:
def __init__(self, model_id=BedrockModelEnum.CLAUDE_V2_1):
self.model_id = model_id
self.bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
region_name=os.environ["REGION"],
aws_access_key_id=os.environ["AWS_ACCESS_KEY"],
aws_secret_access_key=os.environ["AWS_SECRET_KEY"]
)
def generate_text(self, body: dict):
response = self.bedrock_runtime.invoke_model(
body=json.dumps(body),
modelId=self.model_id,
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get("body").read())
finish_reason = response_body.get("error")
if finish_reason is not None:
raise TextException(f"Text generation error. Error is {finish_reason}")
return response_body
def test_call(self, input_text: str = "Human: Who are you\nAssistant:"):
if self.model_id == BedrockModelEnum.CLAUDE_V2_1:
body = {
"prompt": input_text,
"max_tokens_to_sample": 200,
"temperature": 0.5,
"stop_sequences": ["\n\nHuman:"],
}
response_body = self.generate_text(body)
completion = response_body["completion"]
return completion
elif self.model_id == BedrockModelEnum.TITAN_TEXT_EXPRESS_V1:
body = {
"inputText": input_text,
"textGenerationConfig": {
"maxTokenCount": 3072,
"stopSequences": [],
"temperature": 0.9,
"topP": 0.9
}
}
response_body = self.generate_text(body)
print(f"Input token count: {response_body['inputTextTokenCount']}")
for result in response_body['results']:
print(f"Token count: {result['tokenCount']}")
print(f"Output text: {result['outputText']}")
print(f"Completion reason: {result['completionReason']}")
return response_body['results'][0]['outputText']
def get_aws_foundation_model_list():
bedrock = boto3.client(
service_name="bedrock",
region_name=os.environ["REGION"],
aws_access_key_id=os.environ["AWS_ACCESS_KEY"],
aws_secret_access_key=os.environ["AWS_SECRET_KEY"]
)
pp(bedrock.list_foundation_models())
class BedrockEmbedModelBotoUtils:
def __init__(self, model_id=BedrockEmbeddingEnum.TITAN_EMBED_TEXT_V1.value):
self.model_id = model_id
self.bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
region_name=os.environ["REGION"],
aws_access_key_id=os.environ["AWS_ACCESS_KEY"],
aws_secret_access_key=os.environ["AWS_SECRET_KEY"]
)
def generate_embedding(self, body):
response = self.bedrock_runtime.invoke_model(
body=body,
modelId=self.model_id,
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get('body').read())
return response_body
def test_call(self, input_text="Retrieve random embeddings"):
# TODO: branching cohere embeddings body params
body = json.dumps({
"inputText": input_text,
})
response = self.generate_embedding(body)
print(f"Generated embeddings: {response['embedding']}")
print(f"Input Token count: {response['inputTextTokenCount']}")