#!/usr/bin/env python3
"""
Generated test file for Conversation ID LLM Test
Framework: litellm
Execution mode: sync
Streaming: yes"""

import os
import sentry_sdk

import time

import litellm
from sentry_sdk.integrations.litellm import LiteLLMIntegration
from sentry_sdk.integrations.openai import OpenAIIntegration


sentry_sdk.init(
    dsn=os.environ.get("SENTRY_DSN"),
    traces_sample_rate=1.0,
    send_default_pii=True,
    stream_gen_ai_spans=True,
    integrations=[LiteLLMIntegration(include_prompts=True)],
    disabled_integrations=[OpenAIIntegration()],
)


def main():
    # Turn 1
    sentry_sdk.ai.set_conversation_id("conv-a")
    stream = litellm.completion(
        model="openai/gpt-5-nano",
        messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "What is the capital of France?"
        },
    ]
,
        stream=True,
    )
    
    collected_content = []
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            collected_content.append(chunk.choices[0].delta.content)
    
    full_response = "".join(collected_content)
    print(f"Turn 1 Response: {full_response}")
    time.sleep(0.1)  # sleep is necessary for LiteLLM because it needs threaded callbacks to finish

    # Turn 2
    sentry_sdk.ai.set_conversation_id("conv-b")
    stream = litellm.completion(
        model="openai/gpt-5-nano",
        messages=[
        {
            "role": "system",
            "content": "You are a math tutor."
        },
        {
            "role": "user",
            "content": "What is 2 + 2?"
        },
    ]
,
        stream=True,
    )
    
    collected_content = []
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            collected_content.append(chunk.choices[0].delta.content)
    
    full_response = "".join(collected_content)
    print(f"Turn 2 Response: {full_response}")
    time.sleep(0.1)  # sleep is necessary for LiteLLM because it needs threaded callbacks to finish

    # Turn 3
    sentry_sdk.ai.set_conversation_id("conv-a")
    stream = litellm.completion(
        model="openai/gpt-5-nano",
        messages=[
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "What is the capital of France?"
        },
        {
            "role": "assistant",
            "content": "The capital of France is Paris."
        },
        {
            "role": "user",
            "content": "What about Germany?"
        },
    ]
,
        stream=True,
    )
    
    collected_content = []
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            collected_content.append(chunk.choices[0].delta.content)
    
    full_response = "".join(collected_content)
    print(f"Turn 3 Response: {full_response}")
    time.sleep(0.1)  # sleep is necessary for LiteLLM because it needs threaded callbacks to finish

    # Turn 4
    sentry_sdk.ai.set_conversation_id("conv-b")
    stream = litellm.completion(
        model="openai/gpt-5-nano",
        messages=[
        {
            "role": "system",
            "content": "You are a math tutor."
        },
        {
            "role": "user",
            "content": "What is 2 + 2?"
        },
        {
            "role": "assistant",
            "content": "2 + 2 equals 4."
        },
        {
            "role": "user",
            "content": "What about 3 + 3?"
        },
    ]
,
        stream=True,
    )
    
    collected_content = []
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            collected_content.append(chunk.choices[0].delta.content)
    
    full_response = "".join(collected_content)
    print(f"Turn 4 Response: {full_response}")
    time.sleep(0.1)  # sleep is necessary for LiteLLM because it needs threaded callbacks to finish

if __name__ == "__main__":
    with sentry_sdk.start_transaction(op="test", name="Conversation ID LLM Test"):
        main()
    sentry_sdk.flush(timeout=5)
