Skip to content

Commit 96ef624

Browse files
docs: add AWS Bedrock notebook example
1 parent cf3c838 commit 96ef624

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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": "21659c33",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"import json\n",
46+
"import boto3\n",
47+
"\n",
48+
"# Initialize a session using Amazon Bedrock\n",
49+
"session = boto3.Session(\n",
50+
" aws_access_key_id='YOUR_AWS_ACCESS_KEY_ID_HERE',\n",
51+
" aws_secret_access_key='YOUR_AWS_SECRET_ACCESS_KEY_HERE',\n",
52+
" region_name='us-east-1' # Change to your desired region\n",
53+
")\n"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"id": "017c53be",
59+
"metadata": {},
60+
"source": [
61+
"## 3. Wrap the Bedrock client in Openlayer's `trace_bedrock` function"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"id": "24ddd361",
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"from openlayer.lib import trace_bedrock"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"id": "fecb56cb",
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"bedrock_client = trace_bedrock(session.client(service_name='bedrock-runtime'))"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"id": "4eb11465",
87+
"metadata": {},
88+
"source": [
89+
"## 4. Invoke the model normally\n",
90+
"\n",
91+
"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!"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"id": "c3f97c28",
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"# Define the model ID and the input prompt\n",
102+
"model_id = 'anthropic.claude-3-5-sonnet-20240620-v1:0' # Replace with your model ID\n",
103+
"input_data = {\n",
104+
" \"max_tokens\": 256,\n",
105+
" \"messages\": [{\"role\": \"user\", \"content\": \"Hello, world\"}],\n",
106+
" \"anthropic_version\": \"bedrock-2023-05-31\"\n",
107+
"}\n",
108+
"\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"id": "1dcd33b8",
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"# Invoke the model\n",
119+
"response = bedrock_client.invoke_model(\n",
120+
" body=json.dumps(input_data),\n",
121+
" contentType='application/json',\n",
122+
" accept='application/json',\n",
123+
" modelId=model_id\n",
124+
")"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"id": "3a647127",
130+
"metadata": {},
131+
"source": []
132+
}
133+
],
134+
"metadata": {
135+
"kernelspec": {
136+
"display_name": "bedrock-test",
137+
"language": "python",
138+
"name": "python3"
139+
},
140+
"language_info": {
141+
"codemirror_mode": {
142+
"name": "ipython",
143+
"version": 3
144+
},
145+
"file_extension": ".py",
146+
"mimetype": "text/x-python",
147+
"name": "python",
148+
"nbconvert_exporter": "python",
149+
"pygments_lexer": "ipython3",
150+
"version": "3.12.3"
151+
}
152+
},
153+
"nbformat": 4,
154+
"nbformat_minor": 5
155+
}

0 commit comments

Comments
 (0)