Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.2.0+

Conversation with Streaming Responses

See more AI Examples

Demonstrates an AI conversation with receiving streaming responses.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let ai = chilkat::Ai::new();

// The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity".
// Support for additional providers will be added in future versions of Chilkat.
ai.set_provider("google");

// Use your provider's API key.
ai.set_api_key("MY_API_KEY");

// Choose a model.
ai.set_model("gemini-3.6-flash");

// Indicate streaming mode is to be used.
ai.set_streaming(true);

// Create a new conversation to be maintained locally in memory.
// If the conversation is the first to be created, it is also automatically selected.
let system_msg = "You are a creative storyteller".to_string();
let developer_msg = String::new();
let conversation_name = "test_conversation".to_string();
let _ = ai.new_convo(&conversation_name, &system_msg, &developer_msg);

// Add a text input.
let _ = ai.input_add_text("Write a detailed story about a turtle who decides to run a bakery.  Describe the setting, the kinds of pastries, how the turtle feels, and include at least three paragraphs.");

// Ask the AI for text output.
if ai.ask("text").is_err() {
    println!("{}", ai.last_error_text());
    return;
}

let sb_event_name = chilkat::StringBuilder::new();
let sb_delta = chilkat::StringBuilder::new();
let sb_full_response = chilkat::StringBuilder::new();
let mut finished = false;
let abort_flag = false;
let max_wait_ms = 5000;

while !finished {

    let result = ai.poll_ai(abort_flag);
    if result == 1 {
        // We have output waiting.  It should be instantly available.  The maxWaitMs is just-in-case.
        if ai.next_ai_event(max_wait_ms, &sb_event_name, &sb_delta).is_err() {
            println!("{}", ai.last_error_text());
            return;
        }

        // Some AI providers send many "empty" events.  Just ignore them.
        if !sb_event_name.contents_equal("empty", true) {

            // The delta contains the new output.  This could be emitted to a display or the terminal
            // as real-time output.
            if sb_event_name.contents_equal("delta", true) {
                // This example will emit each delta to its own line.
                println!("{}", sb_delta.get_as_string().unwrap_or_default());

                // Accumulate the delta's so we can show the full response later.
                let _ = sb_full_response.append_sb(&sb_delta);
            } else {
                // A streaming AI response is always terminated by a single "null_terminator" event.
                finished = sb_event_name.contents_equal("null_terminator", true);
            }

        }

    } else {
        if result == 0 {
            // Nothing is immediately available. Sleep for 1/10 of a second before polling again.
            ai.sleep_ms(100);
        } else {
            // Something failed..
            println!("{}", ai.last_error_text());
            finished = true;
        }

    }

}

// -------------------------------------------------------------
// The response is in markdown format.
// Also see Markdown to HTML Conversion Examples.
// -------------------------------------------------------------

// Show the accumulated (full) response.
println!("----");
println!("{}", sb_full_response.get_as_string().unwrap_or_default());
println!("----");

// ----------------------------------------------------------------------------------------------------------
// For the 2nd request in this conversation, ask for a shorter version of the story.
let _ = ai.input_add_text("Rewrite the story, but this time make it shorter, about one third as long.");
if ai.ask("text").is_err() {
    println!("{}", ai.last_error_text());
    return;
}

sb_full_response.clear();
finished = false;

while !finished {

    let result = ai.poll_ai(abort_flag);
    if result == 1 {
        // We have output waiting.  It should be instantly available.  The maxWaitMs is just-in-case.
        if ai.next_ai_event(max_wait_ms, &sb_event_name, &sb_delta).is_err() {
            println!("{}", ai.last_error_text());
            return;
        }

        // Some AI providers send many "empty" events.  Just ignore them.
        if !sb_event_name.contents_equal("empty", true) {

            // The delta contains the new output.  This could be emitted to a display or the terminal
            // as real-time output.
            if sb_event_name.contents_equal("delta", true) {
                // This example will emit each delta to its own line.
                println!("{}", sb_delta.get_as_string().unwrap_or_default());

                // Accumulate the delta's so we can show the full response later.
                let _ = sb_full_response.append_sb(&sb_delta);
            } else {
                // A streaming AI response is always terminated by a single "null_terminator" event.
                finished = sb_event_name.contents_equal("null_terminator", true);
            }

        }

    } else {
        if result == 0 {
            // Nothing is immediately available. Sleep for 1/10 of a second before polling again.
            ai.sleep_ms(100);
        } else {
            // Something failed..
            println!("{}", ai.last_error_text());
            finished = true;
        }

    }

}

println!("----");
println!("{}", sb_full_response.get_as_string().unwrap_or_default());