forked from Arize-ai/openinference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedrock_agent.py
More file actions
56 lines (45 loc) · 1.77 KB
/
Copy pathbedrock_agent.py
File metadata and controls
56 lines (45 loc) · 1.77 KB
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
import time
import boto3
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://localhost:6006/v1/traces"
resource = Resource(
attributes={
"service.name": "bedrock-agent",
"openinference.project.name": "amazon-bedrock-agent",
}
)
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-agent-runtime", "us-east-2")
def run():
agent_id = "<AgentId>"
agent_alias_id = "<AgentAliasId>"
session_id = f"default-session1_{int(time.time())}"
attributes = dict(
inputText="When is a good time to visit the Taj Mahal?",
agentId=agent_id,
agentAliasId=agent_alias_id,
sessionId=session_id,
enableTrace=True,
)
response = client.invoke_agent(**attributes)
for idx, event in enumerate(response["completion"]):
if "chunk" in event:
print(event)
chunk_data = event["chunk"]
if "bytes" in chunk_data:
output_text = chunk_data["bytes"].decode("utf8")
print(output_text)
elif "trace" in event:
print(event["trace"])
if __name__ == "__main__":
run()