Skip to content

Commit 1f63855

Browse files
committed
Add converse example
1 parent c250004 commit 1f63855

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Update this with your real values
2+
AWS_ACCESS_KEY_ID=key
3+
AWS_SECRET_ACCESS_KEY=secret
4+
AWS_DEFAULT_REGION=eu-central-1
5+
# Uncomment and set if your credentials are temporary
6+
# AWS_SESSION_TOKEN=
7+
8+
# Uncomment and change to your OTLP endpoint
9+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
10+
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
11+
12+
OTEL_SERVICE_NAME=opentelemetry-python-bedrock
13+
14+
# Uncomment if your OTLP endpoint doesn't support logs
15+
# OTEL_LOGS_EXPORTER=console
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Bedrock Zero-Code Instrumentation Example
2+
=========================================
3+
4+
This is an example of how to instrument Bedrock calls with zero code changes,
5+
using `opentelemetry-instrument`.
6+
7+
When examples are run, it exports traces and logs to an OTLP
8+
compatible endpoint. Traces include details such as the model used and the
9+
duration of the chat request. Logs capture the chat request and the generated
10+
response, providing a comprehensive view of the performance and behavior of
11+
your OpenAI requests.
12+
13+
Note: `.env <.env>`_ file configures additional environment variables:
14+
15+
- `OTEL_LOGS_EXPORTER=otlp` to specify exporter type.
16+
17+
Available examples
18+
------------------
19+
20+
- `converse.py` uses `bedrock-runtime` `Converse API <https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html>_`.
21+
22+
Setup
23+
-----
24+
25+
Minimally, update the `.env <.env>`_ file with your "AWS_SECRET_ACCESS_KEY",
26+
"AWS_SECRET_ACCESS_KEY", "AWS_DEFAULT_REGION" and if you are using temporary
27+
credentials "AWS_SESSION_TOKEN". An
28+
OTLP compatible endpoint should be listening for traces and logs on
29+
http://localhost:4317. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well.
30+
31+
Next, set up a virtual environment like this:
32+
33+
::
34+
35+
python3 -m venv .venv
36+
source .venv/bin/activate
37+
pip install "python-dotenv[cli]"
38+
pip install -r requirements.txt
39+
40+
Run
41+
---
42+
43+
Run the example like this:
44+
45+
::
46+
47+
dotenv run -- opentelemetry-instrument python converse.py
48+
49+
You should see a poem generated by Bedrock while traces exported to your
50+
configured observability tool.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
import boto3
4+
5+
6+
def main():
7+
client = boto3.client("bedrock-runtime")
8+
response = client.converse(
9+
modelId=os.getenv("CHAT_MODEL", "amazon.titan-text-lite-v1"),
10+
messages=[
11+
{
12+
"role": "user",
13+
"content": [{"text": "Write a short poem on OpenTelemetry."}],
14+
},
15+
],
16+
)
17+
18+
print(response["output"]["message"]["content"][0]["text"])
19+
20+
21+
if __name__ == "__main__":
22+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
boto3~=1.35.99
2+
3+
opentelemetry-sdk~=1.29.0
4+
opentelemetry-exporter-otlp-proto-grpc~=1.29.0
5+
opentelemetry-distro~=0.50b0
6+
opentelemetry-instrumentation-botocore~=0.50b0

0 commit comments

Comments
 (0)