import base64
import os

import openai
import requests
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 import TraceConfig
from openinference.instrumentation.openai import OpenAIInstrumentor

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()))


if __name__ == "__main__":
    client = openai.OpenAI()

    img_url = "https://a1cf74336522e87f135f-2f21ace9a6cf0052456644b80fa06d4f.ssl.cf2.rackcdn.com/images/characters/large/800/Homer-Simpson.The-Simpsons.webp"
    img_response = requests.get(img_url)
    if img_response.status_code != 200:
        raise ValueError("Error: Could not retrieve image from URL.")

    base64str = base64.b64encode(img_response.content).decode("utf-8")
    # Set up environment variables:
    os.environ["OPENINFERENCE_BASE64_IMAGE_MAX_LENGTH"] = str(
        100_000
    )  # Base64 encoded images with more than 100k character will appear as __REDACTED__
    os.environ["OPENINFERENCE_HIDE_INPUTS"] = str(True)  # Will hide all inputs
    config = TraceConfig(
        hide_inputs=False,  # Overwrites the environment value setting
        hide_output_text=True,  # The text in output messages will appear as __REDACTED__
    )
    OpenAIInstrumentor().instrument(tracer_provider=tracer_provider, config=config)

    response = client.responses.create(
        model="gpt-4o-2024-08-06",
        input=[
            {
                "role": "user",
                "content": [
                    {"type": "input_text", "text": "What is in this image?"},
                    {
                        "type": "input_image",
                        "image_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",
                    },
                ],
            }
        ],
        max_output_tokens=300,
    )
    print(response)
