Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ai
string ls_SystemMsg
string ls_DeveloperMsg
string ls_ConversationName
oleobject loo_SbResponse
oleobject loo_Json

li_Success = 0

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

loo_Ai = create oleobject
li_rc = loo_Ai.ConnectToNewObject("Chilkat.Ai")
if li_rc < 0 then
    destroy loo_Ai
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity".
// Support for additional providers will be added in future versions of Chilkat.
loo_Ai.Provider = "claude"

// Use your provider's API key.
loo_Ai.ApiKey = "MY_API_KEY"

// Choose a model.
loo_Ai.Model = "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.
ls_SystemMsg = "You are a helpful assistant"
ls_DeveloperMsg = "Respond only with markdown"
ls_ConversationName = "test_conversation"
loo_Ai.NewConvo(ls_ConversationName,ls_SystemMsg,ls_DeveloperMsg)

// -------------------------------------------------------------------------------------------
// Make the first request...
// Add a text input.
loo_Ai.InputAddText("Say Hello")

// Ask the AI for text output.
li_Success = loo_Ai.Ask("text")
if li_Success = 0 then
    Write-Debug loo_Ai.LastErrorText
    destroy loo_Ai
    return
end if

// Get the text response.
loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")

loo_Ai.GetOutputTextSb(loo_SbResponse)
Write-Debug loo_SbResponse.GetAsString()
Write-Debug "----"

// -------------------------------------------------------------
// 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.
loo_Ai.InputAddText("Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious.")
li_Success = loo_Ai.Ask("text")
if li_Success = 0 then
    Write-Debug loo_Ai.LastErrorText
    destroy loo_Ai
    destroy loo_SbResponse
    return
end if

// Get the text response.
loo_SbResponse.Clear()
loo_Ai.GetOutputTextSb(loo_SbResponse)
Write-Debug loo_SbResponse.GetAsString()

// 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...
// 
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Ai.ExportConvo(ls_ConversationName,loo_Json)
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()

// 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. 🏔️"
//         }
//       ]
//     }
//   ]
// }


destroy loo_Ai
destroy loo_SbResponse
destroy loo_Json