Ai C# Reference Documentation
Ai
Current Version: 11.1.0
Chilkat’s AI API acts as a unified interface for working with multiple AI providers.
Instead of learning and coding separately for OpenAI
(ChatGPT), Google
(Gemini), Anthropic
(Claude), XAi
(Grok), DeepSeek
, and Perplexity
, etc., developers can use Chilkat’s consistent set of functions. Chilkat takes care of the differences between each provider’s REST API—such as authentication, request formatting, and response handling.
This has two main benefits:
- Cross-platform and multi-language support – The same Chilkat API works in many programming languages (C#, C++, Python, VB6, Delphi, etc.) and across Windows, Linux, macOS, etc.
- Simplified development – Developers can write code once and easily switch providers, or even support multiple providers, without rewriting everything for each vendor’s unique API.
In short: Chilkat provides a common wrapper over many AI REST APIs, making them uniform and portable across languages and operating systems.
Includes features such as:
- Conversation Handling Create, save, and select conversations to be stored locally. Conversations are automatically updated with responses, and the conversation history is provided in each request in a multi-turn conversation.
- Multimodal Inputs You can add text, image data, files, image URLs, file URLs etc. to requests.
- Streaming Support Instead of waiting for the full response, the Chilkat API can return partial results in real time (usually token by token or chunk by chunk). This is a faster perceived response (user sees text as it’s generated, like typing).
Object Creation
Chilkat.Ai obj = new Chilkat.Ai();
Properties
ApiKey
DebugLogFilePath
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
FullTranscript
For multimodal inputs like image data, image URLs, file data, and file URLs, the full data or URL is sent initially. If the property is set to true
, the full data/URL is re-sent in subsequent interactions. If set to false
, only the image or file summary is sent thereafter.
By default, this property is false
.
Full transcript style:
always send the entire conversation (including files/images).
This guarantees the model has all context, but can get expensive.
Summarized transcript style:
for previously sent multimodal items, sends the summary message (e.g. “Earlier, I uploaded a PDF with account statements”) instead of the raw file or url itself.
HeartbeatMs
The interval in milliseconds between each AbortCheck
event callback, which enables an application to abort certain method calls before they complete. By default, HeartbeatMs is set to 0
, meaning no AbortCheck event callbacks will trigger.
HostedConvo
This applies to providers like OpenAI that can maintain conversation state on the server. If set to true
, only the conversation ID is sent with each query. If set to false
, the entire conversation transcript is sent with each query, making each query stateless.
This only applies if a conversation is selected.
The default value of this property is false
.
LastErrorHtml
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
Indicates the success or failure of the most recent method call: true
means success, false
means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Model
The model to be used for AI conversations. For example, GPT-4o
or GPT-5
. The list of available models can be obtained by calling GetModels
.
The default value is the empty string. A model must always be specified. Chilkat won't try to guess or default to a model.
topProvider
This is the AI provider. Possible values are: openai
, google
, claude
, deepseek
, perplexity
, and xai
.
The default value is openai
.
SelectedConvo
The currently selected conversation can be set to your desired conversation name. Once set, all future queries will be part of that conversation, updating its state with each input and output. By default, no conversation is selected, indicated by an empty string, leading to stateless queries. The first conversation created or imported will be automatically selected. If you attempt to select a non-existent conversation, the selection will not change. You can reset this property to an empty string to revert to stateless querying.
topStreaming
When set to true
, the Ask
method operates in streaming mode, returning immediately after the request is sent. You can then use the PollAi and NextAiEvent methods to receive streaming events. By default, this property is false
.
UncommonOptions
This is a catch-all property to be used for uncommon needs. This property defaults to the empty string, and should typically remain empty.
topVerboseLogging
If set to true
, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false
. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
Methods
Ask
Sends the current potentially multimodal input within the context of the selected conversation and receives the model's output. Currently, the desired outputType can only be text
. Future versions of Chilkat will support image
, audio
, or a JSON schema
to tell the model to return data in the specified JSON structured output.
Accumulated outputs are cleared on entry to this method. Accumulated inputs are cleared on the successful return of this method.
This method cannot be called while a streaming Ask is in progress. In such a case the return value is false
and nothing will occur.
Returns true for success, false for failure.
topAskAsync (C#) (PowerShell)
Creates an asynchronous task to call the Ask method with the arguments provided.
Note: Async method event callbacks happen in the background thread. Accessing and updating UI elements existing in the main thread may require special considerations.
Returns null
on failure
DeleteConvo
Deletes the conversation by its name. If name equals *
then all conversations are deleted.
ExportConvo
Exports the conversation specified by name to json, replacing json's contents with the exported transcript. You can later import a conversation using ImportConvo
. The currently selected conversation can be exported by passing *
for the name.
GetLastJsonData
Provides details about the last method called on this object instance. While many methods do not generate information, some allow you to retrieve details by accessing the last JSON data immediately after the method call returns.
Methods that produce last JSON information include: GetModels
.
GetModels
Returns a list of models available to your account, such as GPT-4o
and GPT-5
. The models are listed in st, sorted in ascending, case-insensitive order. To access the raw JSON response after calling this method, use GetLastJsonData
.
Returns true for success, false for failure.
topGetModelsAsync (C#) (PowerShell)
Creates an asynchronous task to call the GetModels method with the arguments provided.
Note: Async method event callbacks happen in the background thread. Accessing and updating UI elements existing in the main thread may require special considerations.
Returns null
on failure
GetOutputText
GetOutputTextSb
Returns the text output, appended to sb, from the last call to Ask
.
Returns true for success, false for failure.
topImportConvo
Imports the conversation from json and assigns it the name in name.
topInputAddFileData
Adds file data, such as a PDF, to the potentially multimodal input that will be sent in the next call to Ask
. The bd contains the file data.
If FullTranscript
is _CKFALSE_, summary is sent in subsequent interactions instead of the file data. If FullTranscript
is true
, summary is not sent, and the file data is re-sent in each interaction.
This method cannot be used with DeepSeek
.
Returns true for success, false for failure.
topInputAddFileUrl
Adds a file located at a url to the potentially multimodal input that will be sent in the next call to Ask
.
If FullTranscript
is _CKFALSE_, summary is sent in subsequent interactions instead of the file URL. If FullTranscript
is true
, summary is not sent, and the file URL is re-sent in each interaction.
This method cannot be used with DeepSeek
.
Returns true for success, false for failure.
topInputAddImageData
Adds an image to the potentially multimodal input that will be sent in the next call to Ask
. The bd contains the images data. The image data must be jpeg, png, or webp format.
If FullTranscript
is _CKFALSE_, summary is sent in subsequent interactions instead of the image data. If FullTranscript
is true
, summary is not sent, and the image data is re-sent in each interaction.
This method cannot be used with DeepSeek
. DeepSeek does not appear to expose an OpenAI-compatible vision model (yet). You cannot send image URLs or image data to DeepSeek.
Returns true for success, false for failure.
topInputAddImageUrl
Adds an image located at a url to the potentially multimodal input that will be sent in the next call to Ask
.
If FullTranscript
is _CKFALSE_, summary is sent in subsequent interactions instead of the image URL. If FullTranscript
is true
, summary is not sent, and the image URL is re-sent in each interaction.
This method cannot be used with DeepSeek
. DeepSeek does not appear to expose an OpenAI-compatible vision model (yet). You cannot send image URLs or image data to DeepSeek.
Returns true for success, false for failure.
topInputAddText
Adds text to the potentially multimodal input that will be sent in the next call to Ask
.
Returns true for success, false for failure.
topInputAddTextSb
Adds text to the potentially multimodal input that will be sent in the next call to Ask
.
Returns true for success, false for failure.
topInputClear
ListConvos
Returns the full list of conversation names in st.
topNewConvo
Creates and initializes a transcript for a new conversation. The name is a unique name for the conversation. You can reference the conversation by this name, such as for the ExportConvo
method.
The sysMsg sets the broad behavior, tone, and role of the assistant.
Example: "You are a polite tutor who explains concepts clearly and simply."
Think of it as the high-level persona or instructions the model follows throughout the conversation. The devMsg provides more technical or policy-level guidance.
Example: "Always respond in Markdown. Do not reveal internal reasoning. Keep answers under 200 words."
It’s like hidden scaffolding to enforce app rules, formatting, or safety constraints.
Note: The first conversation created is automatically set as the SelectedConvo
. You can use any unique name for a conversation except *
.
NextAiEvent
Retrieves the next available AI streaming mode response update. Use the PollAi method to check if a response update is immediately accessible. The event name is returned in sbName. If the event name is delta
, the text is returned in sbDelta.
For the complete raw event JSON, use the GetLastJsonData
function. Note that the raw JSON format varies by AI provider.
The sbName can be one of the following:
response_created empty -- can be heartbeat/keep-alive, boundary between tokens, in_progress etc. output_item_added content_part_added delta output_text_done content_part_done output_item_done response_completed null_terminator
maxWaitMs defines the maximum wait time to receive the complete event. For instance, if PollAi
signals an available streaming update but only part of the event JSON has been received, the rest is probably on its way. In such cases, this method must wait to receive the entire JSON.
Returns true for success, false for failure.
topPollAi
Returns immediately with one of the following values:
-1
- Error
0
- No streaming event updates are available.
1
- A streaming event updates is ready for retrieval via NextAiEvent
2
- The streaming AI response is complete.
Returns true for success, false for failure.
topSetConnectionSettings
This method copies connection settings, including HTTP proxy, SOCKS proxy, network interface IP address, and other related settings from sock. It's optional; if not used, default settings apply. Call this method before sending REST API requests to an AI provider.
Returns true for success, false for failure.
topEvents
AbortCheck
Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs
property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.
Chilkat .NET Framework Event Implementation
Args are passed using Chilkat.AbortCheckEventArgs
Event callback implementation:
private void ai_OnAbortCheck(object sender, Chilkat.AbortCheckEventArgs args) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); ai.OnAbortCheck += ai_OnAbortCheck;
Chilkat .NET Core Event Implementation
Event callback implementation:
public void handleAbortCheck(out bool abort) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); // ... Chilkat.Ai.AbortCheck abortCheck = new Chilkat.Ai.AbortCheck(handleAbortCheck); ai.setAbortCheckCb(abortCheck);
PercentDone
This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).
The PercentDone callback also acts as an AbortCheck
event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.
To abort the operation, set the abort output argument to true
. This will cause the method to terminate and return a failure status or corresponding failure value.
Chilkat .NET Framework Event Implementation
Args are passed using Chilkat.PercentDoneEventArgs
Event callback implementation:
private void ai_OnPercentDone(object sender, Chilkat.PercentDoneEventArgs args) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); ai.OnPercentDone += ai_OnPercentDone;
Chilkat .NET Core Event Implementation
Event callback implementation:
public void handlePercentDone(int pctDone, out bool abort) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); // ... Chilkat.Ai.PercentDone percentDone = new Chilkat.Ai.PercentDone(handlePercentDone); ai.setPercentDoneCb(percentDone);
ProgressInfo
This event callback provides tag name
/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.
Chilkat .NET Framework Event Implementation
Args are passed using Chilkat.ProgressInfoEventArgs
Event callback implementation:
private void ai_OnProgressInfo(object sender, Chilkat.ProgressInfoEventArgs args) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); ai.OnProgressInfo += ai_OnProgressInfo;
Chilkat .NET Core Event Implementation
Event callback implementation:
public void handleProgressInfo(string name, string value) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); // ... Chilkat.Ai.ProgressInfo progressInfo = new Chilkat.Ai.ProgressInfo(handleProgressInfo); ai.setProgressInfoCb(progressInfo);
TaskCompleted
Called from the background thread when an asynchronous task completes.
Chilkat .NET Framework Event Implementation
Args are passed using Chilkat.TaskCompletedEventArgs
Event callback implementation:
private void ai_OnTaskCompleted(object sender, Chilkat.TaskCompletedEventArgs args) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); ai.OnTaskCompleted += ai_OnTaskCompleted;
Chilkat .NET Core Event Implementation
Event callback implementation:
public void handleTaskIdCompleted(int taskId) { // application code goes here. }
To add an event handler:
Chilkat.Ai ai = new Chilkat.Ai(); // ... Chilkat.Ai.TaskIdCompleted taskIdCompleted = new Chilkat.Ai.TaskIdCompleted(handleTaskIdCompleted); ai.setTaskIdCompletedCb(taskIdCompleted);