Sample code for 30+ languages & platforms
SQL Server

Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads an image file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.

Chilkat SQL Server Downloads

SQL Server
-- 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

    -- Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file 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 BinData.

    DECLARE @contentType nvarchar(4000)
    SELECT @contentType = 'image/jpeg'
    DECLARE @localFilePath nvarchar(4000)
    SELECT @localFilePath = 'qa_data/jpg/starfish.jpg'
    DECLARE @filenameOnServer nvarchar(4000)
    SELECT @filenameOnServer = 'starfish.jpg'

    -- 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 Chilkat 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 @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 @bd


END
GO