Skip to content

add test script for aws bedrock access #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
config.txt
__pycache__

# Ignore local Ruby config necessary for our custom AWS auth solution
.ruby-version

# pyenv config
.python-version

Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.5
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ set python 3.11 for the aiproxy repo:
create a python virtual environment at the top of the directory:
* `python -m venv .venv`

ensure aws access for accessing aws bedrock models:
* from the code-dot-org repo root, run:
* `bin/aws_access`
* from this repo's root, run:
* `gem install aws-google`

#### run

Activate the virtual environment:
Expand Down
32 changes: 32 additions & 0 deletions bin/aws_llama_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

import subprocess
import boto3
import json

# check aws access
try:
result = subprocess.run('aws sts get-caller-identity', shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"AWS access configured: {result.stdout}")
except subprocess.CalledProcessError as e:
print(f"AWS access not configured: {e} {e.stderr}Please see README.md and make sure you ran `gem install aws-google` and `bin/aws_access`")
exit(1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validated as follows:

(.venv) Dave-MBP:~/src/aiproxy (aws-bedrock-llama *)$ ./bin/aws_llama_test.py
AWS access configured: {
    "UserId": "xxx:xxx@code.org",
    "Account": "xxx",
    "Arn": "arn:aws:sts::xxx:assumed-role/xxx/xxx@code.org"
}

 Sure, I'd be happy to help! Black holes are really cool and kind of mind-blowing, so let's dive in.

(.venv) Dave-MBP:~/src/aiproxy (aws-bedrock-llama *)$ gem uninstall aws-google
Successfully uninstalled aws-google-0.2.0
(.venv) Dave-MBP:~/src/aiproxy (aws-bedrock-llama *)$ ./bin/aws_llama_test.py 
AWS access not configured: Command 'aws sts get-caller-identity' returned non-zero exit status 255. 
Error when retrieving credentials from custom-process: rbenv: aws-google: command not found

The `aws-google' command exists in these Ruby versions:
  2.5.0
  2.6.6
  2.7.5

Please see README.md and make sure you ran `gem install aws-google` and `bin/aws_access`


bedrock = boto3.client(service_name='bedrock-runtime')

body = json.dumps({
"prompt": "\n\nHuman:explain black holes to 8th graders\n\nAssistant:",
"max_gen_len": 128,
"temperature": 0.1,
"top_p": 0.9,
})

modelId = 'meta.llama2-13b-chat-v1'
accept = 'application/json'
contentType = 'application/json'

response = bedrock.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)

response_body = json.loads(response.get('body').read())

print(response_body.get('generation'))
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ requests-mock==1.11.0
coverage==7.3.2
scikit-learn~=1.3.2
gdown~=4.7.1
boto3==1.34.30