A C++ DLL that interfaces with a Clarion application to provide AI functionality. The DLL connects to an SQLite database and makes API calls to various AI providers.
This repository contains NO API keys or sensitive data. When using this code:
- Never commit API keys to version control
- Store API keys securely in your local database only
- *The SQLite database files (*.sqlite, .db) are excluded from this repository
- You must provide your own API keys for each AI provider
API keys should be stored in the SQLite database's aiProviders table. Create your own clarionai.sqlite database and add your API keys there. Never include the database file in version control.
ClarionChat.h- Main header file with exported functionsClarionChat.cpp- Implementation of exported functionsClarionChat.def- Module definition fileDatabaseManager.h- Database access layer headerDatabaseManager.cpp- Database access layer implementationProvider.h- Base provider interfaceProviderFactory.h- Provider factory headerProviderFactory.cpp- Provider factory implementationAnthropicProvider.h- Anthropic-specific provider headerAnthropicProvider.cpp- Anthropic-specific provider implementationUtils.h- Utility functions headerUtils.cpp- Utility functions implementationsqlite3.h- SQLite header filesqlite3.c- SQLite implementationCMakeLists.txt- CMake build configurationbuild.bat- Build script
- Visual Studio 2019 or later
- CMake 3.10 or later
- Windows SDK
- Run the
build.batscript to build the DLL:
build.bat
- The DLL will be created in the
build\bin\Releasedirectory.
Use Clarion's LibMaker to create a .LIB file from the ClarionChat.dll:
- Run LibMaker.exe
- Open the ClarionChat.dll
- Save as ClarionChat.LIB
- Verify that all exports are visible
Add the following module declarations to your Clarion application:
MODULE('ClarionChat.LIB')
GetProviderModels(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('GetProviderModels')
SendChatMessage(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('SendChatMessage')
GetProviderModelsJson(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('GetProviderModelsJson')
SendChatMessageJson(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('SendChatMessageJson')
FreeString(LONG),PROC,PASCAL,NAME('FreeString')
ENDExample usage of the GetProviderModels function:
providerGuid STRING('ASOPELDURYCVLOPA') ! Provider GUID
result CSTRING ! Pointer to result string
result = GetProviderModels(providerGuid)
IF SUB(result, 1, 4) = 'fail'
MESSAGE('Error: ' & result)
ELSE
MESSAGE('Models updated successfully')
END
! Free the memory when done
! Convert CSTRING to LONG for FreeString
FreeString(ADDRESS(result))IMPORTANT: Always call FreeString to free memory allocated by the DLL functions.
void __stdcall FreeString(char* str);Frees memory allocated by DLL functions.
- Parameters:
str- Pointer to string to free
- Returns:
- None
IMPORTANT: All strings returned by the DLL functions must be freed using this function to prevent memory leaks.
const char* __stdcall GetProviderModels(const char* providerGuid);Gets the models for a specific provider.
- Parameters:
providerGuid- The GUID of the provider
- Returns:
- A string containing "success" or "fail: [reason]"
- Memory Management:
- The returned string must be freed using FreeString
const char* __stdcall SendChatMessage(const char* chatHeaderGuid);Sends a message to the AI provider and returns the response.
- Parameters:
chatHeaderGuid- The GUID of the chat header
- Returns:
- The response from the AI provider or "fail: [reason]" if an error occurred
- Memory Management:
- The returned string must be freed using FreeString
const char* __stdcall GetProviderModelsJson(const char* providerInfoJson);Gets the models for a specific provider and returns them as a JSON string. This function does not access the SQLite database.
- Parameters:
providerInfoJson- A JSON string containing the provider information
- Returns:
- A JSON string containing the models or "fail: [reason]" if an error occurred
- Memory Management:
- The returned string must be freed using FreeString
const char* __stdcall SendChatMessageJson(const char* chatInfoJson);Sends a chat message to a specific provider using JSON input and output. This function does not access the SQLite database.
- Parameters:
chatInfoJson- A JSON string containing the provider information, chat header, and chat detail
- Returns:
- A JSON string containing the assistant's response
- Memory Management:
- The returned string must be freed using FreeString
All strings returned by the DLL functions are dynamically allocated and must be freed by the caller using the FreeString function. Failure to do so will result in memory leaks.
! 1. Call the DLL function
result = SendChatMessage(chatHeaderGuid)
! 2. Use the result
IF SUB(result, 1, 4) = 'fail'
MESSAGE('Error: ' & result)
ELSE
MESSAGE('Response: ' & result)
END
! 3. Free the memory when done
! Convert CSTRING to LONG for FreeString
FreeString(ADDRESS(result))// 1. Call the DLL function
const char* result = SendChatMessage(chatHeaderGuid);
// 2. Use the result
if (strncmp(result, "fail", 4) == 0) {
std::cerr << "Error: " << result << std::endl;
} else {
std::cout << "Response: " << result << std::endl;
}
// 3. Free the memory when done
FreeString((char*)result);AIParm CSTRING('{"Provider": {"name": "OpenAI","api_key": "your_api_key","api_endpoint": ""}}')
AIResult CSTRING
! Call the function
AIResult = GetProviderModelsJson(AIParm)
! Use the result
stModel.SetValue(AIResult)
! Free the memory - IMPORTANT: Use ADDRESS() to convert CSTRING to LONG
FreeString(ADDRESS(AIResult))The DLL interacts with an SQLite database located at H:\Dev\C11Apps\ClarionAIDemo\clarionai.sqlite. The database contains two main tables:
CREATE TABLE aiProviders (
GUID CHAR(16) PRIMARY KEY,
NAME CHAR(20),
DESCRIPTION CHAR(300),
API_KEY CHAR(200),
API_ENDPOINT CHAR(200)
);CREATE TABLE aiModels (
GUID CHAR(16) PRIMARY KEY,
PROVIDERGUID CHAR(16),
NAME CHAR(20),
DESCRIPTION CHAR(20),
COST_PER_1M_TOKENS CHAR(10),
IS_AVAILABLE CHAR(20)
);This project is licensed under the MIT License - see the LICENSE file for details.