# CMakeList.txt: gpt_0 的 CMake 项目,在此处包括源代码并定义 # 项目特定的逻辑。 # cmake_minimum_required (VERSION 3.8) # Enable Hot Reload for MSVC compilers if supported. if (POLICY CMP0141) cmake_policy(SET CMP0141 NEW) set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>") endif() # 安装了vcpkg, 又通过vcpkg 安装了curl, jsoncpp等。 # 使用vcpkg安装时,注意加参数,否则会安装成32位库 project ("gpt_0") set(ZLIB_INCLUDE_DIR "C:/tools/vcpkg/installed/x64-windows/include") set(ZLIB_LIBRARY "C:/tools/vcpkg/installed/x64-windows/lib") set(CURL_DIR "C:/tools/vcpkg/packages/curl_x64-windows/share/curl") set(JSONCPP_DIR "C:/tools/vcpkg/packages/jsoncpp_x64-windows/share/jsoncpp") # 定义CURL作为外部库,允许他从外部导入,省却了copy 该库到执行文件目录下的尴尬 add_library(CURL SHARED IMPORTED GLOBAL) set_target_properties(CURL PROPERTIES IMPORTED_LOCATION "C:/tools/vcpkg/packages/curl_x64-windows/bin/libcurl.dll" ) # 使用curl 的 cmake文件 去CURL_DIR目录下去找curlConfig.cmake文件,根据该文件找其配置 find_package(CURL CONFIG REQUIRED) find_package(JSONCPP CONFIG REQUIRED) # 将源代码添加到此项目的可执行文件。 add_executable (gpt_0 "gpt_0.cpp" "gpt_0.h") target_link_directories(gpt_0 PRIVATE "C:/tools/vcpkg/packages/curl_x64-windows/lib" "C:/tools/vcpkg/packages/zlib_x64-windows/lib" "C:/tools/vcpkg/packages/jsoncpp_x64-windows/lib" ) target_include_directories(gpt_0 PRIVATE "C:/tools/vcpkg/packages/curl_x64-windows/include" "C:/tools/vcpkg/packages/zlib_x64-windows/include" "C:/tools/vcpkg/packages/jsoncpp_x64-windows/include" ) target_link_libraries(gpt_0 PRIVATE CURL::libcurl JsonCpp::JsonCpp) if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET gpt_0 PROPERTY CXX_STANDARD 20) endif()
c++ 代码:
#include <iostream> #include <curl/curl.h> #include <json/json.h> using namespace std; // 回调函数,用于处理curl接收到的响应数据 size_t WriteCallback(char* ptr, size_t size, size_t nmemb, string* userdata) { size_t len = size * nmemb; userdata->append(ptr, len); return len; } int main() { // 设置API请求URL和参数 string url = "https://api.openai.com/v1/engines/davinci-codex/completions"; string api_key = "YOUR_API_KEY_HERE"; string prompt = "I want to write a C++ program"; string parameters = "{\"prompt\": \"" + prompt + "\", \"max_tokens\": 50, \"n\": 1}"; // 初始化cURL会话 CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { // 设置HTTP请求头和参数 struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key).c_str()); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, parameters.c_str()); // 发送HTTP请求,并解析JSON响应 string response; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); if (res == CURLE_OK) { // 解析JSON响应,提取回答 Json::Value root; Json::Reader reader; bool parsingSuccessful = reader.parse(response, root); if (parsingSuccessful) { string answer = root["choices"][0]["text"].asString(); cout << "模型回答: " << answer << endl; } else { cout << "解析JSON响应失败" << endl; } } else { cout << "HTTP请求失败" << endl; } // 清理资源 curl_easy_cleanup(curl); curl_slist_free_all(headers); } curl_global_cleanup(); return 0; }
标签:windows,x64,示范,vcpkg,设置,简单,curl,packages,tools From: https://www.cnblogs.com/kingkaixuan/p/17226121.html