|
| 1 | +"""Run a dice agent using AWS AgentCore with OTLP export. |
| 2 | +
|
| 3 | +Shows how to send AgentCore traces to agentevals without using the |
| 4 | +agentevals SDK. The OTLP exporter points at the local receiver endpoint. |
| 5 | +
|
| 6 | +AgentCore runs on Strands Agents with Amazon Bedrock models. This example |
| 7 | +sets up OTel via StrandsTelemetry and adds the OTLP exporter to the tracer |
| 8 | +provider before running the agent. |
| 9 | +
|
| 10 | +Prerequisites: |
| 11 | + 1. pip install -r requirements.txt |
| 12 | + 2. agentevals serve --dev |
| 13 | + 3. AWS credentials configured (aws configure, env vars, or IAM role) |
| 14 | + 4. Amazon Nova Pro model access enabled in your AWS account |
| 15 | +
|
| 16 | +Usage: |
| 17 | + python examples/zero-code-examples/agentcore/run.py |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import random |
| 22 | + |
| 23 | +from dotenv import load_dotenv |
| 24 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
| 25 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 26 | +from strands import Agent, tool |
| 27 | +from strands.models import BedrockModel |
| 28 | +from strands.telemetry import StrandsTelemetry |
| 29 | + |
| 30 | +load_dotenv(override=True) |
| 31 | + |
| 32 | +os.environ.setdefault("OTEL_SEMCONV_STABILITY_OPT_IN", "gen_ai_latest_experimental") |
| 33 | + |
| 34 | + |
| 35 | +@tool |
| 36 | +def roll_die(sides: int = 6) -> dict: |
| 37 | + """Roll a die with the given number of sides.""" |
| 38 | + result = random.randint(1, sides) |
| 39 | + return { |
| 40 | + "sides": sides, |
| 41 | + "result": result, |
| 42 | + "message": f"Rolled a {sides}-sided die and got {result}", |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@tool |
| 47 | +def check_prime(number: int) -> bool: |
| 48 | + """Return True if number is prime.""" |
| 49 | + if number < 2: |
| 50 | + return False |
| 51 | + for i in range(2, int(number**0.5) + 1): |
| 52 | + if number % i == 0: |
| 53 | + return False |
| 54 | + return True |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + if not os.getenv("AWS_DEFAULT_REGION"): |
| 59 | + print("AWS_DEFAULT_REGION not set.") |
| 60 | + return |
| 61 | + |
| 62 | + endpoint = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318") |
| 63 | + print(f"OTLP endpoint: {endpoint}") |
| 64 | + |
| 65 | + os.environ.setdefault( |
| 66 | + "OTEL_RESOURCE_ATTRIBUTES", |
| 67 | + "agentevals.eval_set_id=agentcore_eval,agentevals.session_name=agentcore-zero-code", |
| 68 | + ) |
| 69 | + |
| 70 | + telemetry = StrandsTelemetry() |
| 71 | + telemetry.tracer_provider.add_span_processor( |
| 72 | + BatchSpanProcessor(OTLPSpanExporter(), schedule_delay_millis=1000) |
| 73 | + ) |
| 74 | + |
| 75 | + agent = Agent( |
| 76 | + model=BedrockModel(model_id="us.amazon.nova-pro-v1:0"), |
| 77 | + tools=[roll_die, check_prime], |
| 78 | + system_prompt=( |
| 79 | + "You are a helpful assistant that can roll dice and check if numbers are prime. " |
| 80 | + "Use roll_die with the right number of sides when asked. " |
| 81 | + "Use check_prime when asked about prime numbers." |
| 82 | + ), |
| 83 | + name="dice_agent", |
| 84 | + ) |
| 85 | + |
| 86 | + test_queries = [ |
| 87 | + "Hi! Can you help me?", |
| 88 | + "Roll a 20-sided die for me", |
| 89 | + "Is the number you rolled prime?", |
| 90 | + ] |
| 91 | + |
| 92 | + for i, query in enumerate(test_queries, 1): |
| 93 | + print(f"\n[{i}/{len(test_queries)}] User: {query}") |
| 94 | + result = agent(query) |
| 95 | + print(f" Agent: {result}") |
| 96 | + |
| 97 | + print() |
| 98 | + telemetry.tracer_provider.force_flush() |
| 99 | + print("All traces flushed to OTLP receiver.") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments