Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) 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.

Note: This example requires Chilkat v11.2.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoAi
    String sSystemMsg
    String sDeveloperMsg
    String sConversationName
    Variant vSbResponse
    Handle hoSbResponse
    Variant vJson
    Handle hoJson
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End

    // The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity".
    // Support for additional providers will be added in future versions of Chilkat.
    Set ComProvider Of hoAi To "claude"

    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"

    // Choose a model.
    Set ComModel Of hoAi To "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.
    Move "You are a helpful assistant" To sSystemMsg
    Move "Respond only with markdown" To sDeveloperMsg
    Move "test_conversation" To sConversationName
    Get ComNewConvo Of hoAi sConversationName sSystemMsg sDeveloperMsg To iSuccess

    // -------------------------------------------------------------------------------------------
    // Make the first request...
    // Add a text input.
    Get ComInputAddText Of hoAi "Say Hello" To iSuccess

    // Ask the AI for text output.
    Get ComAsk Of hoAi "text" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the text response.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComGetOutputTextSb Of hoAi vSbResponse To iSuccess
    Get ComGetAsString Of hoSbResponse To sTemp1
    Showln sTemp1
    Showln "----"

    // -------------------------------------------------------------
    // 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.
    Get ComInputAddText Of hoAi "Thanks for saying Hello. Please give me a 2 sentence bit of wisdom from Confucious." To iSuccess
    Get ComAsk Of hoAi "text" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the text response.
    Send ComClear To hoSbResponse
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComGetOutputTextSb Of hoAi vSbResponse To iSuccess
    Get ComGetAsString Of hoSbResponse To sTemp1
    Showln sTemp1

    // 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...
    // 
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get pvComObject of hoJson to vJson
    Get ComExportConvo Of hoAi sConversationName vJson To iSuccess
    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

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


End_Procedure

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.