def example_1_basic_thread_and_turn(): """例1: 単一のターンを持つ基本的なスレッド""" print("\n=== Example 1: Basic Thread and Turn ===") # スレッドコンテキストの作成 thread_id = "thread_example_1" # このスパンはターンを表す(スレッドの直接の子) with tracer.start_as_current_span("process_user_message") as turn_span: # スレッド属性の設定 turn_span.set_attribute("wandb.thread_id", thread_id) turn_span.set_attribute("wandb.is_turn", True) # 例となる属性の追加 turn_span.set_attribute("input.value", "Hello, help me with setup") # ネストされたスパンでの処理のシミュレート with tracer.start_as_current_span("generate_response") as nested_span: # これはターン内のネストされた呼び出しなので、is_turnはfalseまたは未設定にする nested_span.set_attribute("wandb.thread_id", thread_id) # wandb.is_turnはネストされた呼び出しには設定しないかFalseに設定 response = "I'll help you get started with the setup process." nested_span.set_attribute("output.value", response) turn_span.set_attribute("output.value", response) print(f"Turn completed in thread: {thread_id}")def main(): example_1_basic_thread_and_turn()if __name__ == "__main__": main()
1つのスレッドIDを共有するマルチターン会話のトレース
不正なコードを報告
コピー
AIに質問
def example_2_multiple_turns(): """例2: 1つのスレッド内の複数のターン""" print("\n=== Example 2: Multiple Turns in Thread ===") thread_id = "thread_conversation_123" # ターン 1 with tracer.start_as_current_span("process_message_turn1") as turn1_span: turn1_span.set_attribute("wandb.thread_id", thread_id) turn1_span.set_attribute("wandb.is_turn", True) turn1_span.set_attribute("input.value", "What programming languages do you recommend?") # ネストされた操作 with tracer.start_as_current_span("analyze_query") as analyze_span: analyze_span.set_attribute("wandb.thread_id", thread_id) # ネストされたスパンにはis_turn属性を設定しないか、Falseにする response1 = "I recommend Python for beginners and JavaScript for web development." turn1_span.set_attribute("output.value", response1) print(f"Turn 1 completed in thread: {thread_id}") # ターン 2 with tracer.start_as_current_span("process_message_turn2") as turn2_span: turn2_span.set_attribute("wandb.thread_id", thread_id) turn2_span.set_attribute("wandb.is_turn", True) turn2_span.set_attribute("input.value", "Can you explain Python vs JavaScript?") # ネストされた操作 with tracer.start_as_current_span("comparison_analysis") as compare_span: compare_span.set_attribute("wandb.thread_id", thread_id) compare_span.set_attribute("wandb.is_turn", False) # ネストされた場合は明示的にFalse response2 = "Python excels at data science while JavaScript dominates web development." turn2_span.set_attribute("output.value", response2) print(f"Turn 2 completed in thread: {thread_id}")def main(): example_2_multiple_turns()if __name__ == "__main__": main()
深くネストされた操作をトレースし、最も外側のスパンのみをターンとしてマークする
不正なコードを報告
コピー
AIに質問
def example_3_complex_nested_structure(): """例3: 複数レベルの複雑なネスト構造""" print("\n=== Example 3: Complex Nested Structure ===") thread_id = "thread_complex_456" # 複数レベルのネストを持つターン with tracer.start_as_current_span("handle_complex_request") as turn_span: turn_span.set_attribute("wandb.thread_id", thread_id) turn_span.set_attribute("wandb.is_turn", True) turn_span.set_attribute("input.value", "Analyze this code and suggest improvements") # レベル1のネストされた操作 with tracer.start_as_current_span("code_analysis") as analysis_span: analysis_span.set_attribute("wandb.thread_id", thread_id) # ネストされた操作にはis_turnを設定しない # レベル2のネストされた操作 with tracer.start_as_current_span("syntax_check") as syntax_span: syntax_span.set_attribute("wandb.thread_id", thread_id) syntax_span.set_attribute("result", "No syntax errors found") # 別のレベル2のネストされた操作 with tracer.start_as_current_span("performance_check") as perf_span: perf_span.set_attribute("wandb.thread_id", thread_id) perf_span.set_attribute("result", "Found 2 optimization opportunities") # 別のレベル1のネストされた操作 with tracer.start_as_current_span("generate_suggestions") as suggest_span: suggest_span.set_attribute("wandb.thread_id", thread_id) suggestions = ["Use list comprehension", "Consider caching results"] suggest_span.set_attribute("suggestions", json.dumps(suggestions)) turn_span.set_attribute("output.value", "Analysis complete with 2 improvement suggestions") print(f"Complex turn completed in thread: {thread_id}")def main(): example_3_complex_nested_structure()if __name__ == "__main__": main()
スレッドに属するがターンではないバックグラウンド操作をトレースする
不正なコードを報告
コピー
AIに質問
def example_4_non_turn_operations(): """例4: スレッドの一部だがターンではない操作""" print("\n=== Example 4: Non-Turn Thread Operations ===") thread_id = "thread_background_789" # スレッドの一部だがターンではないバックグラウンド操作 with tracer.start_as_current_span("background_indexing") as bg_span: bg_span.set_attribute("wandb.thread_id", thread_id) # wandb.is_turnが未設定またはFalse - これはターンではない bg_span.set_attribute("wandb.is_turn", False) bg_span.set_attribute("operation", "Indexing conversation history") print(f"Background operation in thread: {thread_id}") # 同じスレッド内の実際のターン with tracer.start_as_current_span("user_query") as turn_span: turn_span.set_attribute("wandb.thread_id", thread_id) turn_span.set_attribute("wandb.is_turn", True) turn_span.set_attribute("input.value", "Search my previous conversations") turn_span.set_attribute("output.value", "Found 5 relevant conversations") print(f"Turn completed in thread: {thread_id}")def main(): example_4_non_turn_operations()if __name__ == "__main__": main()