import json, asyncio import requests from requests_auth_aws_sigv4 import AWSSigV4 # lib by drago # For getting form data | foundation-model-availability 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") # issue with urlencode just this so should be fine just doint it with replace 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() # For sending FAKE form, to detect quarantine / blocked zone 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()