harmony-protocol 0.1.0

Reverse-engineered OpenAI Harmony response format library for structured conversation handling
Documentation
//! 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 or compatible model that understands
//! the Harmony format tokens (<|start|>, <|message|>, <|end|>, etc.).
//!
//! This example demonstrates the fundamental operations:
//! - Creating messages and conversations
//! - Loading encodings
//! - Rendering conversations to tokens (ready for OpenAI model)
//! - What you need to do next: Send tokens to model, get response tokens

use harmony_response::{
    chat::{Conversation, Message, Role, SystemContent},
    load_harmony_encoding, HarmonyEncodingName,
};

fn main() -> anyhow::Result<()> {
    println!("🦀 Harmony Response Format - Basic Usage Example");
    println!("================================================");

    // Step 1: Create messages
    println!("\n📝 Step 1: Creating messages");

    let system_content = SystemContent::new()
        .with_model_identity("Example Assistant")
        .with_required_channels(["analysis", "commentary", "final"]);

    let system_msg = Message::from_role_and_content(Role::System, system_content);
    println!("✓ Created system message with identity and channels");

    let user_msg = Message::from_role_and_content(Role::User, "Hello, world!");
    println!("✓ Created user message: 'Hello, world!'");

    let assistant_msg = Message::from_role_and_content(Role::Assistant, "Hello! How can I help you today?")
        .with_channel("final");
    println!("✓ Created assistant message with 'final' channel");

    // Step 2: Create conversation
    println!("\n💬 Step 2: Creating conversation");
    let conversation = Conversation::from_messages([system_msg, user_msg, assistant_msg]);
    println!("✓ Created conversation with {} messages", conversation.messages.len());

    // Step 3: Load encoding (may fail without network)
    println!("\n🔧 Step 3: Loading encoding");
    match load_harmony_encoding(HarmonyEncodingName::HarmonyGptOss) {
        Ok(encoding) => {
            println!("✅ Successfully loaded encoding: {}", encoding.name());
            println!("   Tokenizer: {}", encoding.tokenizer_name());
            println!("   Max tokens: {}", encoding.max_message_tokens());

            // Step 4: Render conversation
            println!("\n🎨 Step 4: Rendering conversation");

            // Basic rendering
            let tokens = encoding.render_conversation(&conversation, None)?;
            println!("✓ Rendered conversation to {} tokens", tokens.len());

            // Render for completion
            let completion_tokens = encoding
                .render_conversation_for_completion(&conversation, Role::Assistant, None)?;
            println!("✓ Rendered for completion: {} tokens", completion_tokens.len());

            // Render for training
            let training_tokens = encoding
                .render_conversation_for_training(&conversation, None)?;
            println!("✓ Rendered for training: {} tokens", training_tokens.len());

            // Display first few tokens as example
            println!("\n📊 Token preview (first 10 tokens ready for OpenAI):");
            for (i, &token) in tokens.iter().take(10).enumerate() {
                println!("  {}: {}", i, token);
            }
            if tokens.len() > 10 {
                println!("  ... and {} more tokens", tokens.len() - 10);
            }

            println!("\n🎯 NEXT STEPS (You need to implement):");
            println!("  1. Send these tokens to OpenAI's API (or compatible model)");
            println!("  2. Receive response tokens from the model");
            println!("  3. Use this library to parse response tokens back to messages");
            println!("  Example: let messages = encoding.parse_messages_from_completion_tokens(response_tokens, Some(Role::Assistant))?;");

            println!("\n🎉 Formatting example completed successfully!");
        }
        Err(e) => {
            println!("⚠️  Encoding load failed (expected without network access)");
            println!("   Error: {}", e);
            println!("   This is normal in environments without internet connectivity.");
            println!("   The conversation structure was created successfully!");
        }
    }

    Ok(())
}