DEV Community

Cover image for The smallest Amazon Bedrock call in Python (Converse API)
Ram Surya CH
Ram Surya CH

Posted on

The smallest Amazon Bedrock call in Python (Converse API)

If Amazon Bedrock feels big, start with one function call. This is the smallest useful thing you can do with it from Python: send a message to a model and read the answer. Once this works, everything else (tools, agents, guardrails) is the same call with more around it.

Before you start

  • Python 3.10 or newer
  • An AWS account, with access turned on for one text model in the Amazon Bedrock console
  • AWS credentials on your machine (aws configure), for an IAM user or role that can call Bedrock
python3 -m venv .venv && source .venv/bin/activate
pip install boto3
Enter fullscreen mode Exit fullscreen mode

Find a model ID or inference profile you can call in your Region:

aws bedrock list-inference-profiles --region us-east-1 \
  --query "inferenceProfileSummaries[].inferenceProfileId" --output text
Enter fullscreen mode Exit fullscreen mode

The call

import boto3

client = boto3.client("bedrock-runtime", region_name="us-east-1")

response = client.converse(
    modelId="PASTE_YOUR_MODEL_ID_HERE",
    messages=[
        {"role": "user", "content": [{"text": "Explain what an AWS Region is in two sentences."}]}
    ],
    inferenceConfig={"maxTokens": 200, "temperature": 0.3},
)

print(response["output"]["message"]["content"][0]["text"])
print(response["stopReason"])
print(response["usage"])
Enter fullscreen mode Exit fullscreen mode

Three things to notice:

  1. messages is a list of turns. To continue a conversation you append the model's reply and your next message, then call converse again.
  2. stopReason tells you why the model stopped. For a plain answer it is end_turn. Hold on to this field, because it becomes important in a minute.
  3. usage has the token counts. Print it while you learn, so you get a feel for what a request weighs.

The next step: one tool

The same call takes a toolConfig. You describe a tool, and the model can ask for it:

tool_config = {
    "tools": [{
        "toolSpec": {
            "name": "read_note",
            "description": "Read one note file by name, for example week1.md.",
            "inputSchema": {"json": {
                "type": "object",
                "properties": {"name": {"type": "string"}},
                "required": ["name"],
            }},
        }
    }]
}
Enter fullscreen mode Exit fullscreen mode

Pass it as toolConfig=tool_config. Now there's a new possible stopReason: tool_use. When you see it, the model hasn't answered yet. It's asking your code to run the tool and send the result back.

That's the whole idea of an agent: call, check stopReason, run the tool if asked, call again. One rule I'd add from day one: put a turn limit on the loop, so a bug can't make it run forever.

Over to you

What was the first thing you made a model do from code? And if you've used the Converse API with tools, what tripped you up first? I'd like to hear it in the comments.

Top comments (0)