SQL Server
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @ai int
EXEC @hr = sp_OACreate 'Chilkat.Ai', @ai OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
EXEC sp_OASetProperty @ai, 'Provider', 'openai'
-- Use your provider's API key.
EXEC sp_OASetProperty @ai, 'ApiKey', '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.
DECLARE @contentType nvarchar(4000)
SELECT @contentType = 'application/json'
DECLARE @localFilePath nvarchar(4000)
SELECT @localFilePath = 'qa_data/hamlet.json'
DECLARE @filenameOnServer nvarchar(4000)
SELECT @filenameOnServer = 'hamlet.json'
-- Upload directly from a file.
DECLARE @file_id nvarchar(4000)
EXEC sp_OAMethod @ai, 'UploadFile', @file_id OUT, @localFilePath, @contentType
EXEC sp_OAGetProperty @ai, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ai, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
PRINT 'AI File Upload Failed.'
END
ELSE
BEGIN
PRINT 'File ID: ' + @file_id
PRINT 'File uploaded.'
END
-- Upload from the contents of a StringBuilder
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @sb, 'LoadFile', @success OUT, @localFilePath, 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sb, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ai
EXEC @hr = sp_OADestroy @sb
RETURN
END
EXEC sp_OAMethod @ai, 'UploadFileSb', @file_id OUT, @sb, @filenameOnServer, @contentType
EXEC sp_OAGetProperty @ai, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ai, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
PRINT 'AI File Upload Failed.'
END
ELSE
BEGIN
PRINT 'File ID: ' + @file_id
PRINT 'File uploaded.'
END
-- Upload from the contents of a BinData
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, @localFilePath
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ai
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @bd
RETURN
END
EXEC sp_OAMethod @ai, 'UploadFileBd', @file_id OUT, @bd, @filenameOnServer, @contentType
EXEC sp_OAGetProperty @ai, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ai, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
PRINT 'AI File Upload Failed.'
END
ELSE
BEGIN
PRINT 'File ID: ' + @file_id
PRINT 'File uploaded.'
END
EXEC @hr = sp_OADestroy @ai
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @bd
END
GO