Skip to content

Commit 8fb3156

Browse files
docs: add AWS Bedrock notebook example
1 parent 707f830 commit 8fb3156

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "091d7544",
6+
"metadata": {},
7+
"source": [
8+
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/bedrock/bedrock_tracing.ipynb)\n",
9+
"\n",
10+
"\n",
11+
"# <a id=\"top\">Tracing a AWS Bedrock model invocation</a>\n",
12+
"\n",
13+
"## 1. Set the environment variables"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "5c1adbce",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import os\n",
24+
"\n",
25+
"# Openlayer env variables\n",
26+
"os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n",
27+
"os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE\""
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"id": "13c44cbd",
33+
"metadata": {},
34+
"source": [
35+
"## 2. Initialize the session"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "9c82b04f",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"import json\n",
46+
"\n",
47+
"import boto3"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"id": "21659c33",
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"# Initialize a session using Amazon Bedrock\n",
58+
"session = boto3.Session(\n",
59+
" aws_access_key_id='YOUR_AWS_ACCESS_KEY_ID_HERE',\n",
60+
" aws_secret_access_key='YOUR_AWS_SECRET_ACCESS_KEY_HERE',\n",
61+
" region_name='us-east-1' # Change to your desired region\n",
62+
")\n"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"id": "017c53be",
68+
"metadata": {},
69+
"source": [
70+
"## 3. Wrap the Bedrock client in Openlayer's `trace_bedrock` function"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"id": "24ddd361",
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"from openlayer.lib import trace_bedrock"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"id": "fecb56cb",
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"bedrock_client = trace_bedrock(session.client(service_name='bedrock-runtime'))"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"id": "4eb11465",
96+
"metadata": {},
97+
"source": [
98+
"## 4. Invoke the model normally\n",
99+
"\n",
100+
"That's it! Now you can continue using the traced Bedrock client normally. The data is automatically published to Openlayer and you can start creating tests around it!"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"id": "c3f97c28",
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"# Define the model ID and the input prompt\n",
111+
"model_id = 'anthropic.claude-3-5-sonnet-20240620-v1:0' # Replace with your model ID\n",
112+
"input_data = {\n",
113+
" \"max_tokens\": 256,\n",
114+
" \"messages\": [{\"role\": \"user\", \"content\": \"Hello, world\"}],\n",
115+
" \"anthropic_version\": \"bedrock-2023-05-31\"\n",
116+
"}\n",
117+
"\n"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"id": "1dcd33b8",
124+
"metadata": {},
125+
"outputs": [],
126+
"source": [
127+
"# Invoke the model\n",
128+
"response = bedrock_client.invoke_model(\n",
129+
" body=json.dumps(input_data),\n",
130+
" contentType='application/json',\n",
131+
" accept='application/json',\n",
132+
" modelId=model_id\n",
133+
")"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"id": "3a647127",
139+
"metadata": {},
140+
"source": []
141+
}
142+
],
143+
"metadata": {
144+
"kernelspec": {
145+
"display_name": "bedrock-test",
146+
"language": "python",
147+
"name": "python3"
148+
},
149+
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
156+
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
159+
"version": "3.12.3"
160+
}
161+
},
162+
"nbformat": 4,
163+
"nbformat_minor": 5
164+
}

0 commit comments

Comments
 (0)