File size: 6,796 Bytes
339fa5d 832ac59 339fa5d 832ac59 339fa5d 832ac59 339fa5d 832ac59 |
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import json, asyncio
import requests
from requests.models import PreparedRequest
from requests_auth_aws_sigv4 import AWSSigV4
from datetime import datetime
from datetime import UTC
from dateutil.relativedelta import relativedelta
# lib by drago
async def bedrock_model_available(session, key_id="", secret_key="", region="us-east-1", model="anthropic.claude-3-sonnet-20240229-v1%3A"):
try:
model = model.replace(":","%3A")
except Exception as e:
pass
endpoint = f'https://bedrock.{region}.amazonaws.com/foundation-model-availability/'+model
headers = {
'authority': 'bedrock.us-east-1.amazonaws.com',
'accept': '*/*',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7',
'amz-sdk-invocation-id': '17a6e1df-4d2a-41c8-87fd-991da34b2a92',
'amz-sdk-request': 'attempt=1; max=3',
'origin': 'https://us-east-1.console.aws.amazon.com',
'referer': 'https://us-east-1.console.aws.amazon.com/',
'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
}
aws_auth = AWSSigV4("bedrock",
aws_access_key_id=key_id,
aws_secret_access_key=secret_key,
region=region
)
loop = asyncio.get_event_loop()
req = requests.Request('GET', endpoint, headers=headers)
prepared_req = req.prepare()
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
async with session.get(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
if response.status != 200:
response_data = await response.text()
return json.loads(response_data)
return await response.json()
async def bedrock_send_fake_form(session, key_id="", secret_key="", region="us-east-1", form_data=""):
endpoint = f'https://bedrock.{region}.amazonaws.com/use-case-for-model-access'
headers = {
'authority': 'bedrock.us-east-1.amazonaws.com',
'accept': '*/*',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7',
'amz-sdk-invocation-id': '17a6e1df-4d2a-41c8-87fd-991da34b2a92',
'amz-sdk-request': 'attempt=1; max=3',
'origin': 'https://us-east-1.console.aws.amazon.com',
'referer': 'https://us-east-1.console.aws.amazon.com/',
'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
}
aws_auth = AWSSigV4("bedrock",
aws_access_key_id=key_id,
aws_secret_access_key=secret_key,
region=region
)
loop = asyncio.get_event_loop()
req = requests.Request('POST', endpoint, headers=headers)
prepared_req = req.prepare()
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
async with session.post(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
if response.status != 200:
response_data = await response.text()
return json.loads(response_data)
return await response.json()
async def send_signed_request_bedrock(session, payload, model="", key_id="", secret_key="", region="us-east-1"):
try:
model = model.replace(":","%3A")
except Exception as e:
pass
endpoint = f'https://bedrock-runtime.{region}.amazonaws.com/model/{model}/invoke'
headers = {
"Content-Type": "application/json"
}
aws_auth = AWSSigV4("bedrock",
aws_access_key_id=key_id,
aws_secret_access_key=secret_key,
region=region
)
loop = asyncio.get_event_loop()
req = requests.Request('POST', endpoint, data=payload, headers=headers)
prepared_req = req.prepare()
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
async with session.post(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
if response.status != 200:
response_data = await response.text()
return json.loads(response_data)
return await response.json()
async def send_signed_request_ce(session, service, payload, key_id="", secret_key=""):
ce_endpoint = 'https://ce.us-east-1.amazonaws.com'
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
aws_auth = AWSSigV4(service,
aws_access_key_id=key_id,
aws_secret_access_key=secret_key,
region="us-east-1"
)
# Creating the request with required headers for AWS Cost Explorer
headers = {
"Content-Type": "application/x-amz-json-1.1",
"Accept": "application/json",
"X-Amz-Target": f"AWSInsightsIndexService.GetCostAndUsage" # This header is specific for the GetCostAndUsage operation
}
loop = asyncio.get_event_loop()
req = PreparedRequest()
req.prepare_url(ce_endpoint, {})
req.prepare_method('POST')
req.prepare_headers(headers)
req.prepare_body(data=payload, files=None)
signed_req = await loop.run_in_executor(None, aws_auth, req)
async with session.post(ce_endpoint, headers=signed_req.headers, data=signed_req.body) as response:
response_text = await response.text()
if response.status != 200:
try:
return json.loads(response_text)
except:
return {}
return json.loads(response_text)
async def get_service_cost_and_usage(session, key, secret, service="Claude 3 Sonnet (Amazon Bedrock Edition)"):
now = datetime.now(UTC) # Use UTC for AWS requests
six_months_ago = now - relativedelta(months=6)
start_date = six_months_ago.replace(day=1).strftime('%Y-%m-%d')
end_date = now.replace(day=30).strftime('%Y-%m-%d')
payload = {
"TimePeriod": {
"Start": start_date,
"End": end_date
},
"Granularity": "MONTHLY",
"Metrics": ["BlendedCost"],
"Filter": {
"Dimensions": {
"Key": "SERVICE",
"Values": [service]
}
}
}
json_payload = json.dumps(payload)
return await send_signed_request_ce(session, "ce", json_payload, key_id=key, secret_key=secret), service |