Created
September 5, 2023 19:26
-
-
Save sansmoraxz/ab254d35b487667ab17afed8b163109f to your computer and use it in GitHub Desktop.
Invoke aws bedrock api directly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url = 'https://bedrock.us-east-1.amazonaws.com/model/amazon.titan-tg1-large/invoke' | |
headers = {'Content-Type': 'application/json'} | |
data = {"inputText": prompt} | |
from botocore.awsrequest import AWSRequest | |
from botocore.auth import SigV4Auth | |
from botocore.credentials import Credentials | |
import requests | |
import json | |
import boto3 | |
req = AWSRequest(method='POST', url=url, data=json.dumps(data), headers=headers) | |
SigV4Auth(boto3.Session().get_credentials(), 'bedrock', 'us-east-1').add_auth(req) | |
content_type = 'application/json' | |
accept = '*/*' | |
# response = requests.post(url, data=json.dumps(data), headers=dict(req.headers)) | |
response = requests.post(url, data=json.dumps(data), | |
headers={**dict(req.headers), 'Content-Type': content_type, 'Accept': accept}) | |
res_json = json.loads(response.text) | |
print(res_json['results'][0]['outputText']) | |
print(res_json['results'][0]['completionReason']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment