# Get a tracer
tracer = trace.get_tracer("example-tracer")

def main():
    # Simulate user request
    user_request = "What's the weather like today?"

    # Create a parent span representing the entire chatbot conversation
    with tracer.start_as_current_span("chatbot_conversation") as conversation_span:
        print(f"User request: {user_request}")

        # Add user request as an attribute to the parent span
        conversation_span.set_attribute("input", user_request)
        conversation_span.set_attribute("conversation.id", "conv_12345")
        conversation_span.set_attribute("conversation.type", "weather_inquiry")

        # Add thread ID as an attribute to the parent span to group related spans into
        # a single conversational thread
        conversation_span.set_attribute("thread_id", "user_12345")

        # Process the user request

        # Simulate initial processing
        time.sleep(0.2)

        # Create a child span for LLM generation using GenAI conventions
        with tracer.start_as_current_span("llm_completion") as llm_span:
            print("Generating LLM response...")

            # Create a prompt for the LLM
            llm_prompt = f"User question: {user_request}\n\nProvide a concise answer about the weather."

            # Add GenAI semantic convention attributes
            llm_span.set_attribute("gen_ai.operation.name", "completion")
            llm_span.set_attribute("gen_ai.system", "gpt")
            llm_span.set_attribute("gen_ai.request.model", "gpt-4")
            llm_span.set_attribute("gen_ai.response.model", "gpt-4")
            llm_span.set_attribute("gen_ai.request.input", llm_prompt)  # Add the prompt
            llm_span.set_attribute("gen_ai.usage.input_tokens", 10)  # Example token count
            llm_span.set_attribute("gen_ai.usage.output_tokens", 25)  # Example token count
            llm_span.set_attribute("gen_ai.usage.total_tokens", 35)   # Example token count
            llm_span.set_attribute("gen_ai.request.temperature", 0.7)
            llm_span.set_attribute("gen_ai.request.max_tokens", 100)

            # Simulate LLM thinking time
            time.sleep(0.5)

            # Generate chatbot response
            chatbot_response = "It's sunny with a high of 75°F in your area today!"

            # Set response in the LLM span
            llm_span.set_attribute("gen_ai.response.output", chatbot_response)

            print("LLM generation completed")

        # Back in parent span context
        conversation_span.set_attribute("output", chatbot_response)
        # Response has been generated

        print(f"Chatbot response: {chatbot_response}")

if __name__ == "__main__":
    main()

    # Ensure all spans are flushed before the program exits
    tracer_provider.shutdown()

    print("\nSpans have been sent to OpenTelemetry collector.")
    print("If you configured Comet.com, you can view the traces in your Comet project.")
```

Using `thread_id` as a span attribute allows you to group related spans into a single conversational thread.
Created threads can be used to evaluate multi-turn conversations as described in the [Multi-turn conversations](/evaluation/evaluate_threads) guide.