Unicode C
Unicode C
Upload a File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads a file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat Unicode C Downloads
#include <C_CkAiW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkAiW ai;
const wchar_t *contentType;
const wchar_t *localFilePath;
const wchar_t *filenameOnServer;
const wchar_t *file_id;
HCkStringBuilderW sb;
HCkBinDataW bd;
success = FALSE;
ai = CkAiW_Create();
// The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
CkAiW_putProvider(ai,L"openai");
// Use your provider's API key.
CkAiW_putApiKey(ai,L"MY_API_KEY");
// We can upload directly from a file in the filesystem, or from a Chilkat StringBuilder, or from a Chilkat BinData.
// Some AI providers require a content-type.
// Also, some AI providers are picky about what content-type's are accepted.
// Check the AI provider's documentation.
// "application/json" is generally always acceptable.
// "text/plain" can be used as a fallback for any text file.
contentType = L"application/json";
localFilePath = L"qa_data/hamlet.json";
filenameOnServer = L"hamlet.json";
// Upload directly from a file.
file_id = CkAiW_uploadFile(ai,localFilePath,contentType);
if (CkAiW_getLastMethodSuccess(ai) == FALSE) {
wprintf(L"%s\n",CkAiW_lastErrorText(ai));
wprintf(L"AI File Upload Failed.\n");
}
else {
wprintf(L"File ID: %s\n",file_id);
wprintf(L"File uploaded.\n");
}
// Upload from the contents of a StringBuilder
sb = CkStringBuilderW_Create();
success = CkStringBuilderW_LoadFile(sb,localFilePath,L"utf-8");
if (success == FALSE) {
wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sb));
CkAiW_Dispose(ai);
CkStringBuilderW_Dispose(sb);
return;
}
file_id = CkAiW_uploadFileSb(ai,sb,filenameOnServer,contentType);
if (CkAiW_getLastMethodSuccess(ai) == FALSE) {
wprintf(L"%s\n",CkAiW_lastErrorText(ai));
wprintf(L"AI File Upload Failed.\n");
}
else {
wprintf(L"File ID: %s\n",file_id);
wprintf(L"File uploaded.\n");
}
// Upload from the contents of a BinData
bd = CkBinDataW_Create();
success = CkBinDataW_LoadFile(bd,localFilePath);
if (success == FALSE) {
wprintf(L"%s\n",CkBinDataW_lastErrorText(bd));
CkAiW_Dispose(ai);
CkStringBuilderW_Dispose(sb);
CkBinDataW_Dispose(bd);
return;
}
file_id = CkAiW_uploadFileBd(ai,bd,filenameOnServer,contentType);
if (CkAiW_getLastMethodSuccess(ai) == FALSE) {
wprintf(L"%s\n",CkAiW_lastErrorText(ai));
wprintf(L"AI File Upload Failed.\n");
}
else {
wprintf(L"File ID: %s\n",file_id);
wprintf(L"File uploaded.\n");
}
CkAiW_Dispose(ai);
CkStringBuilderW_Dispose(sb);
CkBinDataW_Dispose(bd);
}