Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) OpenAI (ChatGTP) Simple RequestSee more OpenAI ChatGPT ExamplesDemonstrate a simple ChatGPT request with authentication using your OPENAI_API_KEY. For more information, see https://platform.openai.com/docs/api-reference/making-requests
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *Role; const wchar_t *Content; const wchar_t *finish_reason; int index; const wchar_t *id; const wchar_t *v_object; int created; const wchar_t *model; int Prompt_tokens; int Completion_tokens; int Total_tokens; int i; int count_i; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Implements the following CURL command: // curl https://api.openai.com/v1/chat/completions \ // -H "Content-Type: application/json" \ // -H "Authorization: Bearer $OPENAI_API_KEY" \ // -d '{ // "model": "gpt-3.5-turbo", // "messages": [{"role": "user", "content": "Say this is a test!"}], // "temperature": 0.7 // }' // Use the following online tool to generate HTTP code from a CURL command // Convert a cURL Command to HTTP Source Code // Use this online tool to generate code from sample JSON: // Generate Code to Create JSON // The following JSON is sent in the request body. // { // "model": "gpt-3.5-turbo", // "messages": [ // { // "role": "user", // "content": "Say this is a test!" // } // ], // "temperature": 0.7 // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"model",L"gpt-3.5-turbo"); CkJsonObjectW_UpdateString(json,L"messages[0].role",L"user"); CkJsonObjectW_UpdateString(json,L"messages[0].content",L"Say this is a test!"); CkJsonObjectW_UpdateNumber(json,L"temperature",L"0.7"); // Adds the "Authorization: Bearer $OPENAI_API_KEY" header. // This is NOT a real key. Change the "sk-vi...." to your own key. CkHttpW_putAuthToken(http,L"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"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpW_PostJson3(http,L"https://api.openai.com/v1/chat/completions",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "id": "chatcmpl-abc123", // "object": "chat.completion", // "created": 1677858242, // "model": "gpt-3.5-turbo-0301", // "usage": { // "prompt_tokens": 13, // "completion_tokens": 7, // "total_tokens": 20 // }, // "choices": [ // { // "message": { // "role": "assistant", // "content": "\n\nThis is a test!" // }, // "finish_reason": "stop", // "index": 0 // } // ] // } // Sample code for parsing the JSON response... // Use the following online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON id = CkJsonObjectW_stringOf(jResp,L"id"); v_object = CkJsonObjectW_stringOf(jResp,L"object"); created = CkJsonObjectW_IntOf(jResp,L"created"); model = CkJsonObjectW_stringOf(jResp,L"model"); Prompt_tokens = CkJsonObjectW_IntOf(jResp,L"usage.prompt_tokens"); Completion_tokens = CkJsonObjectW_IntOf(jResp,L"usage.completion_tokens"); Total_tokens = CkJsonObjectW_IntOf(jResp,L"usage.total_tokens"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"choices"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); Role = CkJsonObjectW_stringOf(jResp,L"choices[i].message.role"); Content = CkJsonObjectW_stringOf(jResp,L"choices[i].message.content"); finish_reason = CkJsonObjectW_stringOf(jResp,L"choices[i].finish_reason"); index = CkJsonObjectW_IntOf(jResp,L"choices[i].index"); i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.