![]()  | 
  
Chilkat  HOME  Android™  AutoIt  C  C#  C++  Chilkat2-Python  CkPython  Classic ASP  DataFlex  Delphi DLL  Go  Java  Node.js  Objective-C  PHP Extension  Perl  PowerBuilder  PowerShell  PureBasic  Ruby  SQL Server  Swift  Tcl  Unicode C  Unicode C++  VB.NET  VBScript  Visual Basic 6.0  Visual FoxPro  Xojo Plugin
 
      (Unicode C) AI: Simple ConversationSee more AI ExamplesInitiates a conversation and sends two requests. The second request includes the transcript of the messages between the user and assistant from the first request.Note: This example requires Chilkat v11.2.0 or greater. 
 #include <C_CkAiW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { BOOL success; HCkAiW ai; const wchar_t *systemMsg; const wchar_t *developerMsg; const wchar_t *conversationName; HCkStringBuilderW sbResponse; HCkJsonObjectW json; success = FALSE; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. ai = CkAiW_Create(); // The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity". // Support for additional providers will be added in future versions of Chilkat. CkAiW_putProvider(ai,L"claude"); // Use your provider's API key. CkAiW_putApiKey(ai,L"MY_API_KEY"); // Choose a model. CkAiW_putModel(ai,L"claude-opus-4-1-20250805"); // Create a new conversation to be maintained locally in memory. // If the conversation is the first to be created, it is also automatically selected. systemMsg = L"You are a helpful assistant"; developerMsg = L"Respond only with markdown"; conversationName = L"test_conversation"; CkAiW_NewConvo(ai,conversationName,systemMsg,developerMsg); // ------------------------------------------------------------------------------------------- // Make the first request... // Add a text input. CkAiW_InputAddText(ai,L"Say Hello"); // Ask the AI for text output. success = CkAiW_Ask(ai,L"text"); if (success == FALSE) { wprintf(L"%s\n",CkAiW_lastErrorText(ai)); CkAiW_Dispose(ai); return; } // Get the text response. sbResponse = CkStringBuilderW_Create(); CkAiW_GetOutputTextSb(ai,sbResponse); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse)); wprintf(L"----\n"); // ------------------------------------------------------------- // 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. CkAiW_InputAddText(ai,L"Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious."); success = CkAiW_Ask(ai,L"text"); if (success == FALSE) { wprintf(L"%s\n",CkAiW_lastErrorText(ai)); CkAiW_Dispose(ai); CkStringBuilderW_Dispose(sbResponse); return; } // Get the text response. CkStringBuilderW_Clear(sbResponse); CkAiW_GetOutputTextSb(ai,sbResponse); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse)); // 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... // json = CkJsonObjectW_Create(); CkAiW_ExportConvo(ai,conversationName,json); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // 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. 🏔️" // } // ] // } // ] // } CkAiW_Dispose(ai); CkStringBuilderW_Dispose(sbResponse); CkJsonObjectW_Dispose(json); }  | 
  ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.