harmony-protocol 0.1.0

Reverse-engineered OpenAI Harmony response format library for structured conversation handling
Documentation
#!/usr/bin/env python3
"""
Basic usage example for the Harmony Response Format library

⚠️ IMPORTANT: This library does NOT include an AI model.
You need to integrate with OpenAI's API to send the formatted tokens
to a model and receive response tokens back.

This example shows the formatting step only.
"""

import harmony_response as hr

def main():
    try:
        # Load the encoding
        encoding_name = hr.HarmonyEncodingName.harmony_gpt_oss()
        encoding = hr.load_harmony_encoding(encoding_name)
        print(f"Loaded encoding: {encoding.name()}")

        # Create system content
        system_content = (hr.SystemContent()
                         .with_model_identity("Test Assistant")
                         .with_reasoning_effort(hr.ReasoningEffort.medium())
                         .with_required_channels(["analysis", "commentary", "final"])
                         .with_browser_tool())

        # Create messages
        system_msg = hr.Message.from_role_and_content(hr.Role.system(), system_content)
        user_msg = hr.Message.from_role_and_content(hr.Role.user(), "What is 2 + 2?")

        assistant_analysis = (hr.Message.from_role_and_content(hr.Role.assistant(), "This is basic arithmetic.")
                             .with_channel("analysis"))

        assistant_final = (hr.Message.from_role_and_content(hr.Role.assistant(), "2 + 2 = 4")
                          .with_channel("final"))

        # Create conversation
        conversation = hr.Conversation.from_messages([
            system_msg,
            user_msg,
            assistant_analysis,
            assistant_final,
        ])

        print(f"Created conversation with {len(conversation)} messages")

        # Render conversation (ready for OpenAI model)
        tokens = encoding.render_conversation(conversation)
        print(f"Rendered to {len(tokens)} tokens ready for OpenAI")

        # Render for completion (input for OpenAI model)
        completion_tokens = encoding.render_conversation_for_completion(
            conversation,
            hr.Role.assistant()
        )
        print(f"Completion input tokens: {len(completion_tokens)}")

        print("\n🎯 NEXT STEPS (You need to implement):")
        print("  1. Send completion_tokens to OpenAI's API")
        print("  2. Receive response_tokens from the model")
        print("  3. Use encoding.parse_messages_from_completion_tokens(response_tokens)")

        print("✅ Formatting example completed successfully!")

    except Exception as e:
        print(f"❌ Error (expected without network): {e}")

if __name__ == "__main__":
    main()