Update awsLib.py
Browse files
awsLib.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import json, asyncio
|
| 2 |
import requests
|
|
|
|
| 3 |
from requests_auth_aws_sigv4 import AWSSigV4
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# lib by drago
|
| 6 |
async def bedrock_model_available(session, key_id="", secret_key="", region="us-east-1", model="anthropic.claude-3-sonnet-20240229-v1%3A"):
|
| 7 |
try:
|
| 8 |
-
model = model.replace(":","%3A")
|
| 9 |
except Exception as e:
|
| 10 |
pass
|
| 11 |
endpoint = f'https://bedrock.{region}.amazonaws.com/foundation-model-availability/'+model
|
|
@@ -81,4 +85,98 @@ async def bedrock_send_fake_form(session, key_id="", secret_key="", region="us-e
|
|
| 81 |
response_data = await response.text()
|
| 82 |
return json.loads(response_data)
|
| 83 |
|
| 84 |
-
return await response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json, asyncio
|
| 2 |
import requests
|
| 3 |
+
from requests.models import PreparedRequest
|
| 4 |
from requests_auth_aws_sigv4 import AWSSigV4
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from datetime import UTC
|
| 7 |
+
from dateutil.relativedelta import relativedelta
|
| 8 |
|
| 9 |
# lib by drago
|
| 10 |
async def bedrock_model_available(session, key_id="", secret_key="", region="us-east-1", model="anthropic.claude-3-sonnet-20240229-v1%3A"):
|
| 11 |
try:
|
| 12 |
+
model = model.replace(":","%3A")
|
| 13 |
except Exception as e:
|
| 14 |
pass
|
| 15 |
endpoint = f'https://bedrock.{region}.amazonaws.com/foundation-model-availability/'+model
|
|
|
|
| 85 |
response_data = await response.text()
|
| 86 |
return json.loads(response_data)
|
| 87 |
|
| 88 |
+
return await response.json()
|
| 89 |
+
|
| 90 |
+
async def send_signed_request_bedrock(session, payload, model="", key_id="", secret_key="", region="us-east-1"):
|
| 91 |
+
try:
|
| 92 |
+
model = model.replace(":","%3A")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
pass
|
| 95 |
+
|
| 96 |
+
endpoint = f'https://bedrock-runtime.{region}.amazonaws.com/model/{model}/invoke'
|
| 97 |
+
|
| 98 |
+
headers = {
|
| 99 |
+
"Content-Type": "application/json"
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
aws_auth = AWSSigV4("bedrock",
|
| 103 |
+
aws_access_key_id=key_id,
|
| 104 |
+
aws_secret_access_key=secret_key,
|
| 105 |
+
region=region
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
loop = asyncio.get_event_loop()
|
| 109 |
+
req = requests.Request('POST', endpoint, data=payload, headers=headers)
|
| 110 |
+
prepared_req = req.prepare()
|
| 111 |
+
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
|
| 112 |
+
|
| 113 |
+
async with session.post(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
|
| 114 |
+
if response.status != 200:
|
| 115 |
+
response_data = await response.text()
|
| 116 |
+
return json.loads(response_data)
|
| 117 |
+
|
| 118 |
+
return await response.json()
|
| 119 |
+
|
| 120 |
+
async def send_signed_request_ce(session, service, payload, key_id="", secret_key=""):
|
| 121 |
+
ce_endpoint = 'https://ce.us-east-1.amazonaws.com'
|
| 122 |
+
headers = {
|
| 123 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 124 |
+
"Accept": "application/json",
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
aws_auth = AWSSigV4(service,
|
| 128 |
+
aws_access_key_id=key_id,
|
| 129 |
+
aws_secret_access_key=secret_key,
|
| 130 |
+
region="us-east-1"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Creating the request with required headers for AWS Cost Explorer
|
| 134 |
+
headers = {
|
| 135 |
+
"Content-Type": "application/x-amz-json-1.1",
|
| 136 |
+
"Accept": "application/json",
|
| 137 |
+
"X-Amz-Target": f"AWSInsightsIndexService.GetCostAndUsage" # This header is specific for the GetCostAndUsage operation
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
loop = asyncio.get_event_loop()
|
| 141 |
+
req = PreparedRequest()
|
| 142 |
+
req.prepare_url(ce_endpoint, {})
|
| 143 |
+
req.prepare_method('POST')
|
| 144 |
+
req.prepare_headers(headers)
|
| 145 |
+
req.prepare_body(data=payload, files=None)
|
| 146 |
+
|
| 147 |
+
signed_req = await loop.run_in_executor(None, aws_auth, req)
|
| 148 |
+
|
| 149 |
+
async with session.post(ce_endpoint, headers=signed_req.headers, data=signed_req.body) as response:
|
| 150 |
+
response_text = await response.text()
|
| 151 |
+
|
| 152 |
+
if response.status != 200:
|
| 153 |
+
try:
|
| 154 |
+
return json.loads(response_text)
|
| 155 |
+
except:
|
| 156 |
+
return {}
|
| 157 |
+
return json.loads(response_text)
|
| 158 |
+
|
| 159 |
+
async def get_service_cost_and_usage(session, key, secret, service="Claude 3 Sonnet (Amazon Bedrock Edition)"):
|
| 160 |
+
now = datetime.now(UTC) # Use UTC for AWS requests
|
| 161 |
+
six_months_ago = now - relativedelta(months=6)
|
| 162 |
+
start_date = six_months_ago.replace(day=1).strftime('%Y-%m-%d')
|
| 163 |
+
end_date = now.replace(day=30).strftime('%Y-%m-%d')
|
| 164 |
+
|
| 165 |
+
payload = {
|
| 166 |
+
"TimePeriod": {
|
| 167 |
+
"Start": start_date,
|
| 168 |
+
"End": end_date
|
| 169 |
+
},
|
| 170 |
+
"Granularity": "MONTHLY",
|
| 171 |
+
"Metrics": ["BlendedCost"],
|
| 172 |
+
"Filter": {
|
| 173 |
+
"Dimensions": {
|
| 174 |
+
"Key": "SERVICE",
|
| 175 |
+
"Values": [service]
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
json_payload = json.dumps(payload)
|
| 181 |
+
|
| 182 |
+
return await send_signed_request_ce(session, "ce", json_payload, key_id=key, secret_key=secret), service
|