Skip to main content

OpenInference Bedrock Instrumentation

Project description

OpenInference AWS Bedrock Instrumentation

Python autoinstrumentation library for AWS Bedrock calls made using boto3.

This package implements OpenInference tracing for invoke_model and converse calls made using a boto3 bedrock-runtime client. These traces are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as Arize phoenix.

pypi

[!NOTE]
The Converse API was introduced in botocore v1.34.116. Please use v1.34.116 or above to utilize converse.

Supported Models

Find the list of Bedrock-supported models and their IDs here. Future testing is planned for additional models.

Model Supported Methods
Anthropic Claude 2.0 converse, invoke
Anthropic Claude 2.1 converse, invoke
Anthropic Claude 3 Sonnet 1.0 converse
Anthropic Claude 3.5 Sonnet converse
Anthropic Claude 3 Haiku converse
Meta Llama 3 8b Instruct converse
Meta Llama 3 70b Instruct converse
Mistral AI Mistral 7B Instruct converse
Mistral AI Mixtral 8X7B Instruct converse
Mistral AI Mistral Large converse
Mistral AI Mistral Small converse

Installation

pip install openinference-instrumentation-bedrock

Quickstart

[!IMPORTANT]
OpenInference for AWS Bedrock supports both invoke_model and converse. For models that use the Messages API, such as Anthropic Claude 3 and Anthropic Claude 3.5, use the Converse API instead.

In a notebook environment (jupyter, colab, etc.) install openinference-instrumentation-bedrock, arize-phoenix and boto3.

You can test out this quickstart guide in Google Colab!

pip install openinference-instrumentation-bedrock arize-phoenix boto3

Ensure that boto3 is configured with AWS credentials.

First, import dependencies required to autoinstrument AWS Bedrock and set up phoenix as an collector for OpenInference traces.

from urllib.parse import urljoin

import boto3
import phoenix as px

from openinference.instrumentation.bedrock import BedrockInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

Next, we'll start a phoenix server and set it as a collector.

px.launch_app()
session_url = px.active_session().url
phoenix_otlp_endpoint = urljoin(session_url, "v1/traces")
phoenix_exporter = OTLPSpanExporter(endpoint=phoenix_otlp_endpoint)
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter=phoenix_exporter))
trace_api.set_tracer_provider(tracer_provider=tracer_provider)

Instrumenting boto3 is simple:

BedrockInstrumentor().instrument()

Now, all calls to invoke_model are instrumented and can be viewed in the phoenix UI.

session = boto3.session.Session()
client = session.client("bedrock-runtime")
prompt = b'{"prompt": "Human: Hello there, how are you? Assistant:", "max_tokens_to_sample": 1024}'
response = client.invoke_model(modelId="anthropic.claude-v2", body=prompt)
response_body = json.loads(response.get("body").read())
print(response_body["completion"])

Alternatively, all calls to converse are instrumented and can be viewed in the phoenix UI.

session = boto3.session.Session()
client = session.client("bedrock-runtime")

message1 = {
            "role": "user",
            "content": [{"text": "Create a list of 3 pop songs."}]
}
message2 = {
        "role": "user",
        "content": [{"text": "Make sure the songs are by artists from the United Kingdom."}]
}
messages = []

messages.append(message1)
response = client.converse(
    modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
    messages=messages
)
out = response["output"]["message"]
messages.append(out)
print(out.get("content")[-1].get("text"))

messages.append(message2)
response = client.converse(
    modelId="anthropic.claude-v2:1",
    messages=messages
)
out = response['output']['message']
print(out.get("content")[-1].get("text"))

More Info

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file openinference_instrumentation_bedrock-0.1.12.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_bedrock-0.1.12.tar.gz
Algorithm Hash digest
SHA256 91eabef59ef0c5c7ff5ad93a997ee0ee29af51f2c6519ff536b7e5d72499c655
MD5 893a32927146dcd0ccde9f6cbf7fa3aa
BLAKE2b-256 9a0a26b529fcc28d6ea816476d44e3004404ad20e7945905dcbabebe4a04cd23

See more details on using hashes here.

File details

Details for the file openinference_instrumentation_bedrock-0.1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_bedrock-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4b5943e03d55b67a1ce76de9e599d880f6ed4cb38a8581af656e3078e0be8d3e
MD5 29cb4699a24ecdfa7a8dfba62a12adf2
BLAKE2b-256 7a574732dd2c6ddd14075f3e97d31cfb5fa880eefcf28c12092b68d66054691a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page