Last active
July 10, 2025 21:14
-
-
Save mulle-nat/be34fbbbcb10f9b79f01a21ff911d006 to your computer and use it in GitHub Desktop.
aws bedrock LLM dialog
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
#!/usr/bin/env mulle-bash | |
# Uses mulle-bashfunctions for escape and logging support: | |
# https://github.com/mulle-nat/mulle-bashfunctions | |
# Needs to have the aws commandline client installed: | |
# https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions | |
# | |
case "${1:-}" in | |
-h|--help) | |
cat <<EOF >&2 | |
usage: `basename -- "${MULLE_EXECUTABLE}"` | |
Reads the prompt from stdin, and sends it to aws bedrock. | |
EOF | |
exit 1 | |
esac | |
# get prompt from stdin | |
MESSAGE="`cat`" | |
# escape message for JSON content | |
r_escaped_json "${MESSAGE}" | |
MESSAGE="${RVAL}" | |
if [ -z "${MESSAGE}" ] | |
then | |
fail "empty prompt" | |
fi | |
# Ensure AWS CLI is configured with your credentials | |
# Run 'aws configure' if you haven't set it up yet | |
# Get temporary credentials | |
creds=$(aws sts get-session-token) # --duration-seconds 900) | |
export AWS_ACCESS_KEY_ID=$(echo $creds | jq -r .Credentials.AccessKeyId) | |
export AWS_SECRET_ACCESS_KEY=$(echo $creds | jq -r .Credentials.SecretAccessKey) | |
export AWS_SESSION_TOKEN=$(echo $creds | jq -r .Credentials.SessionToken) | |
# AWS region | |
AWS_REGION="us-east-1" # Change this to your preferred region | |
# Bedrock API endpoint | |
# BEDROCK_ENDPOINT="https://bedrock-runtime.${AWS_REGION}.amazonaws.com" | |
# Claude 3.5 Sonnet model ID | |
MODEL_ID="anthropic.claude-3-5-sonnet-20240620-v1:0" | |
# Create the request payload | |
PAYLOAD=$(cat <<EOF | |
{ | |
"anthropic_version": "bedrock-2023-05-31", | |
"max_tokens": ${TOKENS:-1024}, | |
"messages": [ | |
{ | |
"role": "user", | |
"content": "$MESSAGE" | |
} | |
] | |
} | |
EOF | |
) | |
# create tmp file with JSON input | |
r_make_tmp_file "input" "json" | |
input_filename="${RVAL}" | |
redirect_exekutor "${input_filename}" printf "%s\n" "${PAYLOAD}" | |
# make tmp file for aws output | |
r_make_tmp_file "output" "json" | |
output_filename="${RVAL}" | |
# set -x | |
if ! aws bedrock-runtime invoke-model \ | |
--model-id "${MODEL_ID}" \ | |
--body "file://${input_filename}" \ | |
--region "${AWS_REGION}" \ | |
--cli-binary-format raw-in-base64-out \ | |
--content-type application/json \ | |
"${output_filename}" | |
then | |
exit 1 | |
fi | |
# Extract and print Claude's response | |
cat "${output_filename}" | jq -r '.content[0].text' | |
rm "${output_filename}" | |
rm "${input_filename}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment