import boto3
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

from openinference.instrumentation.bedrock import BedrockInstrumentor

endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

BedrockInstrumentor().instrument(tracer_provider=tracer_provider)

client = boto3.client(
    "bedrock-agent-runtime",
    region_name="us-east-1",
)


def run():
    attributes = dict(
        knowledgeBaseId="QKERWOBDH0", retrievalQuery={"text": "What is task Decomposition?"}
    )
    response = client.retrieve(**attributes)
    print(response)


def retrieve_and_generate():
    attributes = {
        "input": {"text": "What is Task Decomposition?"},
        "retrieveAndGenerateConfiguration": {
            "knowledgeBaseConfiguration": {
                "knowledgeBaseId": "QKERWOBDH0",
                "modelArn": "anthropic.claude-3-haiku-20240307-v1:0",
            },
            "type": "KNOWLEDGE_BASE",
        },
    }
    response = client.retrieve_and_generate_stream(**attributes)
    print(response)
    for res in response["stream"]:
        print(res)


if __name__ == "__main__":
    run()
    retrieve_and_generate()
