|
| 1 | +# AWS Bedrock |
| 2 | + |
| 3 | +To use AWS Bedrock with `aisuite` you will need to create an AWS account and |
| 4 | +navigate to `https://us-west-2.console.aws.amazon.com/bedrock/home`. This route |
| 5 | +will be redirected to your default region. In this example the region has been set to |
| 6 | +`us-west-2`. Anywhere that is listed can be replaced with your desired region. |
| 7 | + |
| 8 | +Navigate to the `[overview](https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/overview)` page |
| 9 | +directly or by clicking on the `Get started` link. |
| 10 | + |
| 11 | +## Foundation Model Access |
| 12 | + |
| 13 | +You will first need to give your AWS account access to the foundation models by |
| 14 | +visiting the `[modelaccess](https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/modelaccess)` |
| 15 | +page to enable the models you would like to use. Make note of the `Model ID` for the model you would like to use, |
| 16 | +this will be used when using the chat completion example below. |
| 17 | + |
| 18 | +Once that has been enabled set your Access Key and Secret in the env variables: |
| 19 | + |
| 20 | +```shell |
| 21 | +export AWS_ACCESS_KEY="your-access-key" |
| 22 | +export AWS_SECRET_KEY="your-secret-key" |
| 23 | +export AWS_REGION_NAME="region-name" |
| 24 | +``` |
| 25 | +## Create a Chat Completion |
| 26 | + |
| 27 | +Install the boto3 client using your package installer |
| 28 | + |
| 29 | +Example with pip |
| 30 | +```shell |
| 31 | +pip install boto3 |
| 32 | +``` |
| 33 | + |
| 34 | +Example with poetry |
| 35 | +```shell |
| 36 | +poetry add boto3 |
| 37 | +``` |
| 38 | + |
| 39 | +In your code: |
| 40 | +```python |
| 41 | +import aisuite as ai |
| 42 | +client = ai.Client() |
| 43 | + |
| 44 | + |
| 45 | +model_id = "meta.llama3-1-405b-instruct-v1:0" # Mode ID from above |
| 46 | +provider = "aws-bedrock" |
| 47 | + |
| 48 | +messages = [ |
| 49 | + {"role": "system", "content": "Respond in Pirate English."}, |
| 50 | + {"role": "user", "content": "Tell me a joke."}, |
| 51 | +] |
| 52 | + |
| 53 | +response = client.chat.completions.create( |
| 54 | + model=f"{provider}:{model_id}", |
| 55 | + messages=messages, |
| 56 | +) |
| 57 | + |
| 58 | +print(response.choices[0].message.content) |
| 59 | +``` |
| 60 | + |
| 61 | +Happy coding! If you would like to contribute, please read our [Contributing Guide](CONTRIBUTING.md). |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments