Skip to content

Commit

Permalink
Add converse example
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx committed Jan 15, 2025
1 parent c250004 commit 1f63855
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Update this with your real values
AWS_ACCESS_KEY_ID=key
AWS_SECRET_ACCESS_KEY=secret
AWS_DEFAULT_REGION=eu-central-1
# Uncomment and set if your credentials are temporary
# AWS_SESSION_TOKEN=

# Uncomment and change to your OTLP endpoint
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc

OTEL_SERVICE_NAME=opentelemetry-python-bedrock

# Uncomment if your OTLP endpoint doesn't support logs
# OTEL_LOGS_EXPORTER=console
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Bedrock Zero-Code Instrumentation Example
=========================================

This is an example of how to instrument Bedrock calls with zero code changes,
using `opentelemetry-instrument`.

When examples are run, it exports traces and logs to an OTLP
compatible endpoint. Traces include details such as the model used and the
duration of the chat request. Logs capture the chat request and the generated
response, providing a comprehensive view of the performance and behavior of
your OpenAI requests.

Note: `.env <.env>`_ file configures additional environment variables:

- `OTEL_LOGS_EXPORTER=otlp` to specify exporter type.

Available examples
------------------

- `converse.py` uses `bedrock-runtime` `Converse API <https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html>_`.

Setup
-----

Minimally, update the `.env <.env>`_ file with your "AWS_SECRET_ACCESS_KEY",
"AWS_SECRET_ACCESS_KEY", "AWS_DEFAULT_REGION" and if you are using temporary
credentials "AWS_SESSION_TOKEN". An
OTLP compatible endpoint should be listening for traces and logs on
http://localhost:4317. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well.

Next, set up a virtual environment like this:

::

python3 -m venv .venv
source .venv/bin/activate
pip install "python-dotenv[cli]"
pip install -r requirements.txt

Run
---

Run the example like this:

::

dotenv run -- opentelemetry-instrument python converse.py

You should see a poem generated by Bedrock while traces exported to your
configured observability tool.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

import boto3


def main():
client = boto3.client("bedrock-runtime")
response = client.converse(
modelId=os.getenv("CHAT_MODEL", "amazon.titan-text-lite-v1"),
messages=[
{
"role": "user",
"content": [{"text": "Write a short poem on OpenTelemetry."}],
},
],
)

print(response["output"]["message"]["content"][0]["text"])


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
boto3~=1.35.99

opentelemetry-sdk~=1.29.0
opentelemetry-exporter-otlp-proto-grpc~=1.29.0
opentelemetry-distro~=0.50b0
opentelemetry-instrumentation-botocore~=0.50b0

0 comments on commit 1f63855

Please sign in to comment.