-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbedrock_multimodal.py
76 lines (59 loc) · 2.37 KB
/
bedrock_multimodal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from typing import Tuple
import boto3
import requests
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.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from openinference.instrumentation.bedrock import BedrockInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
BedrockInstrumentor().instrument()
session = boto3.session.Session()
client = session.client("bedrock-runtime", "us-east-1")
def multimodal_example():
model_id = "anthropic.claude-3-5-sonnet-20240620-v1:0"
input_text = "What's in this image?"
img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
img_url = "https://a1cf74336522e87f135f-2f21ace9a6cf0052456644b80fa06d4f.ssl.cf2.rackcdn.com/images/characters/large/800/Homer-Simpson.The-Simpsons.webp"
img_bytes, format = download_img(img_url)
message = {
"role": "user",
"content": [
{
"text": input_text,
},
{
"image": {
"format": format,
"source": {
"bytes": img_bytes,
},
}
},
],
}
response = client.converse(
modelId=model_id,
messages=[message],
)
out = response["output"]["message"]
print(out.get("content")[-1].get("text"))
def download_img(url: str) -> Tuple[bytes, str]:
format = sanitize_format(os.path.splitext(url)[-1].lstrip("."))
resp = requests.get(url)
if resp.status_code != 200:
raise ValueError(f"Error: Could not retrieve image from URL: {url}")
return resp.content, format
def sanitize_format(fmt: str) -> str:
if fmt == "jpg":
return "jpeg"
return fmt
if __name__ == "__main__":
multimodal_example()