Rust Requires Chilkat v11.2.0+
Rust
AI: Simple Conversation
See more AI Examples
Initiates a conversation and sends two requests. The second request includes the transcript of the messages between the user and assistant from the first request.Chilkat Rust Downloads
// 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("claude");
// Use your provider's API key.
ai.set_api_key("MY_API_KEY");
// Choose a model.
ai.set_model("claude-opus-5");
// 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 helpful assistant".to_string();
let developer_msg = "Respond only with markdown".to_string();
let conversation_name = "test_conversation".to_string();
let _ = ai.new_convo(&conversation_name, &system_msg, &developer_msg);
// -------------------------------------------------------------------------------------------
// Make the first request...
// Add a text input.
let _ = ai.input_add_text("Say Hello");
// Ask the AI for text output.
if ai.ask("text").is_err() {
println!("{}", ai.last_error_text());
return;
}
// Get the text response.
let sb_response = chilkat::StringBuilder::new();
let _ = ai.get_output_text_sb(&sb_response);
println!("{}", sb_response.get_as_string().unwrap_or_default());
println!("----");
// -------------------------------------------------------------
// The response is in markdown format.
// Also see Markdown to HTML Conversion Examples.
// -------------------------------------------------------------
// Sample output:
// # Hello! 👋
// ## Welcome!
// It's **great** to meet you. How can I *assist* you today?
// ---
// Feel free to ask me anything, and I'll respond in `markdown` format.
// -------------------------------------------------------------------------------------------
// Send the 2nd request in the conversation.
let _ = ai.input_add_text("Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious.");
if ai.ask("text").is_err() {
println!("{}", ai.last_error_text());
return;
}
// Get the text response.
sb_response.clear();
let _ = ai.get_output_text_sb(&sb_response);
println!("{}", sb_response.get_as_string().unwrap_or_default());
// Sample output:
// ## Wisdom from Confucius
// > *"The man who moves a mountain begins by carrying away small stones. It does not matter how slowly you go as long as you do not stop."*
// These words remind us that **persistence** and **patience** are the keys to achieving even the most daunting goals. 🏔️
// -------------------------------------------------------------------------------------------
// Look at the conversation so far...
//
let json = chilkat::JsonObject::new();
let _ = ai.export_convo(&conversation_name, &json);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// Note: The conversation transcript format exported and imported by Chilkat is always the OpenAI format,
// regardless of the AI provider. (Chilkat translates the format from OpenAI to whatever is needed by the actual AI provider being used.)
// {
// "input": [
// {
// "role": "system",
// "content": [
// {
// "type": "input_text",
// "text": "You are a helpful assistant"
// }
// ]
// },
// {
// "role": "developer",
// "content": [
// {
// "type": "input_text",
// "text": "Respond only with markdown"
// }
// ]
// },
// {
// "role": "user",
// "content": [
// {
// "type": "input_text",
// "text": "Say Hello"
// }
// ]
// },
// {
// "role": "assistant",
// "content": [
// {
// "type": "output_text",
// "text": "# Hello! 👋\n\n**Welcome!** It's *great* to meet you.\n\nHow can I assist you today?"
// }
// ]
// },
// {
// "role": "user",
// "content": [
// {
// "type": "input_text",
// "text": "Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious."
// }
// ]
// },
// {
// "role": "assistant",
// "content": [
// {
// "type": "output_text",
// "text": "## Wisdom from Confucius\n\n> *\"The man who moves a mountain begins by carrying away small stones. It does not matter how slowly you go as long as you do not stop.\"*\n\nThese words remind us that **persistence** and **patience** are the keys to achieving even the most daunting goals. 🏔️"
// }
// ]
// }
// ]
// }