add:init版本。
This commit is contained in:
commit
7806473762
17
.clang-format
Normal file
17
.clang-format
Normal file
@ -0,0 +1,17 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
PointerAlignment: Left
|
||||
AccessModifierOffset: -4
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterFunction: true
|
||||
AfterClass: true
|
||||
Cpp11BracedListStyle: true
|
||||
ReflowComments: true
|
||||
SpacesBeforeTrailingComments: 3
|
||||
TabWidth: 4
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ColumnLimit: 130
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortEnumsOnASingleLine: false
|
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
build
|
||||
.vs
|
||||
.cache
|
||||
cmake-*
|
96
.vscode/settings.json
vendored
Normal file
96
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
{
|
||||
"files.autoSave": "onFocusChange",
|
||||
"editor.fontSize": 14,
|
||||
"editor.fontFamily": "'Source Code Pro', 'Courier New', monospace",
|
||||
"terminal.integrated.fontFamily": "Source Code Pro",
|
||||
"cmake.configureOnOpen": true,
|
||||
"cmake.debugConfig": {
|
||||
"console": "integratedTerminal",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "-gdb-set charset utf-8",
|
||||
"text": "-gdb-set charset UTF-8"
|
||||
},
|
||||
{
|
||||
"description": "Enable gdb pretty-printing",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
],
|
||||
"args": [
|
||||
]
|
||||
},
|
||||
"cmake.configureSettings": {
|
||||
//"CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
|
||||
},
|
||||
"cmake.options.statusBarVisibility": "visible",
|
||||
"cmake.generator": "Ninja",
|
||||
"C_Cpp.default.compileCommands": "${workspaceRoot}/build/compile_commands.json",
|
||||
"C_Cpp.default.cppStandard": "c++17",
|
||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
||||
"editor.inlayHints.enabled": "off",
|
||||
"editor.unicodeHighlight.allowedLocales": {
|
||||
"ja": true,
|
||||
"zh-hant": true,
|
||||
"zh-hans": true
|
||||
},
|
||||
"files.associations": {
|
||||
"ostream": "cpp",
|
||||
"string": "cpp",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"map": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ranges": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp"
|
||||
}
|
||||
}
|
24765
3rd/nlohmann/json.hpp
Normal file
24765
3rd/nlohmann/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
176
3rd/nlohmann/json_fwd.hpp
Normal file
176
3rd/nlohmann/json_fwd.hpp
Normal file
@ -0,0 +1,176 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.11.3
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
|
||||
#include <cstdint> // int64_t, uint64_t
|
||||
#include <map> // map
|
||||
#include <memory> // allocator
|
||||
#include <string> // string
|
||||
#include <vector> // vector
|
||||
|
||||
// #include <nlohmann/detail/abi_macros.hpp>
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.11.3
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
|
||||
|
||||
// This file contains all macro definitions affecting or depending on the ABI
|
||||
|
||||
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
|
||||
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
|
||||
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3
|
||||
#warning "Already included a different version of the library!"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum)
|
||||
|
||||
#ifndef JSON_DIAGNOSTICS
|
||||
#define JSON_DIAGNOSTICS 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
|
||||
#endif
|
||||
|
||||
#if JSON_DIAGNOSTICS
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
|
||||
#endif
|
||||
|
||||
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
|
||||
#endif
|
||||
|
||||
// Construct the namespace ABI tags component
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
|
||||
|
||||
#define NLOHMANN_JSON_ABI_TAGS \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
||||
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
|
||||
|
||||
// Construct the namespace version component
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
|
||||
_v ## major ## _ ## minor ## _ ## patch
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
|
||||
|
||||
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION
|
||||
#else
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
|
||||
NLOHMANN_JSON_VERSION_MINOR, \
|
||||
NLOHMANN_JSON_VERSION_PATCH)
|
||||
#endif
|
||||
|
||||
// Combine namespace components
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
|
||||
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE
|
||||
#define NLOHMANN_JSON_NAMESPACE \
|
||||
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION)
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
|
||||
namespace nlohmann \
|
||||
{ \
|
||||
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION) \
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_END
|
||||
#define NLOHMANN_JSON_NAMESPACE_END \
|
||||
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
|
||||
} // namespace nlohmann
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
@brief namespace for Niels Lohmann
|
||||
@see https://github.com/nlohmann
|
||||
@since version 1.0.0
|
||||
*/
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
|
||||
/*!
|
||||
@brief default JSONSerializer template argument
|
||||
|
||||
This serializer ignores the template arguments and uses ADL
|
||||
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
|
||||
for serialization.
|
||||
*/
|
||||
template<typename T = void, typename SFINAE = void>
|
||||
struct adl_serializer;
|
||||
|
||||
/// a class to store JSON values
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/
|
||||
template<template<typename U, typename V, typename... Args> class ObjectType =
|
||||
std::map,
|
||||
template<typename U, typename... Args> class ArrayType = std::vector,
|
||||
class StringType = std::string, class BooleanType = bool,
|
||||
class NumberIntegerType = std::int64_t,
|
||||
class NumberUnsignedType = std::uint64_t,
|
||||
class NumberFloatType = double,
|
||||
template<typename U> class AllocatorType = std::allocator,
|
||||
template<typename T, typename SFINAE = void> class JSONSerializer =
|
||||
adl_serializer,
|
||||
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
|
||||
class CustomBaseClass = void>
|
||||
class basic_json;
|
||||
|
||||
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
|
||||
/// @sa https://json.nlohmann.me/api/json_pointer/
|
||||
template<typename RefStringType>
|
||||
class json_pointer;
|
||||
|
||||
/*!
|
||||
@brief default specialization
|
||||
@sa https://json.nlohmann.me/api/json/
|
||||
*/
|
||||
using json = basic_json<>;
|
||||
|
||||
/// @brief a minimal map-like container that preserves insertion order
|
||||
/// @sa https://json.nlohmann.me/api/ordered_map/
|
||||
template<class Key, class T, class IgnoredLess, class Allocator>
|
||||
struct ordered_map;
|
||||
|
||||
/// @brief specialization that maintains the insertion order of object keys
|
||||
/// @sa https://json.nlohmann.me/api/ordered_json/
|
||||
using ordered_json = basic_json<nlohmann::ordered_map>;
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
30
CMakeLists.txt
Normal file
30
CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(deepseek-use LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if (MSVC)
|
||||
add_compile_options(/utf-8)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||
MESSAGE(STATUS "Add MinGW Param.")
|
||||
add_compile_options(-finput-charset=utf-8)
|
||||
add_compile_options(-fexec-charset=gbk)
|
||||
endif()
|
||||
|
||||
set(CMAKE_DEBUG_POSTFIX "d")
|
||||
message(STATUS "System: ${CMAKE_SYSTEM_NAME}")
|
||||
message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}")
|
||||
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/)
|
||||
|
||||
include_directories(3rd)
|
||||
find_package(CURL REQUIRED)
|
||||
|
||||
#add_executable(deepseek-use1 main.cpp)
|
||||
add_executable(deepseek-use main.cxx zapi.h zapi.cxx jsondata.h jsondata.cxx)
|
||||
#target_link_libraries(deepseek-use1 PRIVATE CURL::libcurl)
|
||||
target_link_libraries(deepseek-use PRIVATE CURL::libcurl)
|
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# DeepSeek访问
|
||||
|
||||
尝试实现一个可以提交大量附件,并让`DeepSeek`帮分析提取数据等的一个工具。
|
||||
|
||||
# 说明
|
||||
|
||||
如果是`windows`环境,必须使用`windows terminal`(或者其他支持读入`utf-8`字符串的终端)以支持从`cmd`命令行读入`utf-8`字符串。
|
64
jsondata.cxx
Normal file
64
jsondata.cxx
Normal file
@ -0,0 +1,64 @@
|
||||
#include "jsondata.h"
|
||||
#include <iostream>
|
||||
|
||||
CJsonOper::CJsonOper()
|
||||
{
|
||||
}
|
||||
|
||||
CJsonOper::~CJsonOper()
|
||||
{
|
||||
}
|
||||
|
||||
std::string CJsonOper::format_request(const std::string& content)
|
||||
{
|
||||
// 定义变量
|
||||
std::string model = "deepseek-r1";
|
||||
std::string role = "user";
|
||||
|
||||
// 构造 JSON 对象
|
||||
nlohmann::json json_data = {{"model", model}, {"messages", {{{"role", role}, {"content", content}}}}};
|
||||
return json_data.dump();
|
||||
}
|
||||
|
||||
Message CJsonOper::parse(const std::string& data)
|
||||
{
|
||||
Message re;
|
||||
json j = json::parse(data);
|
||||
if (j.contains("choices") && j["choices"].is_array() && !j["choices"].empty()) {
|
||||
const auto& message = j["choices"][0]["message"];
|
||||
if (message.contains("content")) {
|
||||
re.message_content = message["content"].get<std::string>();
|
||||
}
|
||||
if (message.contains("reasoning_content")) {
|
||||
re.reasoning_content = message["reasoning_content"].get<std::string>();
|
||||
}
|
||||
}
|
||||
if (j.contains("usage")) {
|
||||
const auto& usage = j["usage"];
|
||||
if (usage.contains("prompt_tokens")) {
|
||||
re.prompt_tokens = usage["prompt_tokens"].get<int>();
|
||||
}
|
||||
if (usage.contains("completion_tokens")) {
|
||||
re.completion_tokens = usage["completion_tokens"].get<int>();
|
||||
}
|
||||
if (usage.contains("total_tokens")) {
|
||||
re.total_tokens = usage["total_tokens"].get<int>();
|
||||
}
|
||||
}
|
||||
if (j.contains("id")) {
|
||||
re.id = j["id"].get<std::string>();
|
||||
}
|
||||
return re;
|
||||
}
|
||||
|
||||
bool CJsonOper::save_md(const std::string& data, const std::string& id)
|
||||
{
|
||||
std::ofstream of(id + ".md");
|
||||
if (!of.is_open()) {
|
||||
std::cout << "can't open " << id << std::endl;
|
||||
return false;
|
||||
}
|
||||
of << data;
|
||||
of.close();
|
||||
return true;
|
||||
}
|
31
jsondata.h
Normal file
31
jsondata.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef JSON_DATA
|
||||
#define JSON_DATA
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
struct Message {
|
||||
std::string message_content;
|
||||
std::string reasoning_content;
|
||||
std::string id;
|
||||
int prompt_tokens{};
|
||||
int completion_tokens{};
|
||||
int total_tokens{};
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
class CJsonOper
|
||||
{
|
||||
public:
|
||||
CJsonOper();
|
||||
~CJsonOper();
|
||||
|
||||
public:
|
||||
std::string format_request(const std::string& content);
|
||||
Message parse(const std::string& data);
|
||||
static bool save_md(const std::string& data, const std::string& id);
|
||||
};
|
||||
|
||||
#endif
|
90
main.cpp
Normal file
90
main.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include <curl/curl.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
// 回调函数,用于接收API响应
|
||||
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s)
|
||||
{
|
||||
size_t newLength = size * nmemb;
|
||||
try {
|
||||
s->append((char*)contents, newLength);
|
||||
} catch (std::bad_alloc& e) {
|
||||
// 处理内存不足的情况
|
||||
return 0;
|
||||
}
|
||||
return newLength;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
#ifdef _WIN32
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
SetConsoleCP(CP_UTF8);
|
||||
#endif
|
||||
|
||||
// DeepSeek API endpoint
|
||||
std::string url = "https://api.deepseek.com/v1/extract";
|
||||
|
||||
// 你的API密钥
|
||||
std::string api_key = "your_api_key_here";
|
||||
|
||||
// 要上传的文件路径
|
||||
std::string file_path = "path/to/your/project.zip";
|
||||
|
||||
// 初始化libcurl
|
||||
CURL* curl;
|
||||
CURLcode res;
|
||||
std::string response_string;
|
||||
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl = curl_easy_init();
|
||||
|
||||
if (curl) {
|
||||
// 设置API URL
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
|
||||
// 设置HTTP头
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key).c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
// 设置POST请求
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
|
||||
// 设置文件上传
|
||||
curl_mime* mime;
|
||||
curl_mimepart* part;
|
||||
mime = curl_mime_init(curl);
|
||||
part = curl_mime_addpart(mime);
|
||||
curl_mime_name(part, "file");
|
||||
curl_mime_filedata(part, file_path.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
|
||||
|
||||
// 设置回调函数以接收响应
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
|
||||
|
||||
// 执行请求
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
// 检查请求是否成功
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
||||
} else {
|
||||
std::cout << "API Response: " << response_string << std::endl;
|
||||
}
|
||||
|
||||
// 清理
|
||||
curl_mime_free(mime);
|
||||
curl_easy_cleanup(curl);
|
||||
curl_slist_free_all(headers);
|
||||
}
|
||||
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
40
main.cxx
Normal file
40
main.cxx
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
#include "zapi.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "jsondata.h"
|
||||
|
||||
std::string get_key()
|
||||
{
|
||||
char* v = getenv("DASHSCOPE_API_KEY");
|
||||
if (v) {
|
||||
return std::string(v);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string api_key = get_key();
|
||||
if (api_key.empty()) {
|
||||
std::cerr << "api key not found." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto api = std::make_shared<COpenAI>();
|
||||
auto json_oper = std::make_shared<CJsonOper>();
|
||||
api->set_base("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions", api_key);
|
||||
|
||||
// 请求的 JSON 数据
|
||||
std::string question("DeepSeek R1 API提交附件,如果不支持zip,拿我有多个文件的话,需要一个一个上传吗?");
|
||||
std::string q = json_oper->format_request(question);
|
||||
|
||||
std::string recv;
|
||||
if (api->post(q, recv)) {
|
||||
auto re = json_oper->parse(recv);
|
||||
CJsonOper::save_md(re.message_content + "\n" + re.reasoning_content, re.id);
|
||||
std::cout << "success." << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
50
zapi.cxx
Normal file
50
zapi.cxx
Normal file
@ -0,0 +1,50 @@
|
||||
#include "zapi.h"
|
||||
#include <iostream>
|
||||
|
||||
COpenAI::COpenAI()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl_ = curl_easy_init();
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, recv_call);
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &recv_);
|
||||
}
|
||||
|
||||
COpenAI::~COpenAI()
|
||||
{
|
||||
curl_easy_cleanup(curl_);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
void COpenAI::set_base(const std::string& api_url, const std::string& key)
|
||||
{
|
||||
api_key_ = key;
|
||||
api_url_ = api_url;
|
||||
curl_easy_setopt(curl_, CURLOPT_URL, api_url_.c_str());
|
||||
}
|
||||
|
||||
bool COpenAI::post(const std::string& json, std::string& out)
|
||||
{
|
||||
recv_.clear();
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key_).c_str());
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json.c_str());
|
||||
|
||||
auto res = curl_easy_perform(curl_);
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "curl post failed: " << curl_easy_strerror(res) << std::endl;
|
||||
} else {
|
||||
//std::cout << recv_ << std::endl;
|
||||
out = recv_;
|
||||
}
|
||||
curl_slist_free_all(headers);
|
||||
return res == CURLE_OK;
|
||||
}
|
||||
|
||||
size_t COpenAI::recv_call(void* contents, size_t size, size_t nmemb, std::string* output)
|
||||
{
|
||||
size_t total_size = size * nmemb;
|
||||
output->append(reinterpret_cast<char*>(contents), total_size);
|
||||
return total_size;
|
||||
}
|
27
zapi.h
Normal file
27
zapi.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef API_H
|
||||
#define API_H
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <string>
|
||||
|
||||
class COpenAI
|
||||
{
|
||||
public:
|
||||
COpenAI();
|
||||
~COpenAI();
|
||||
|
||||
public:
|
||||
void set_base(const std::string& api_url, const std::string& key);
|
||||
bool post(const std::string& json, std::string& out);
|
||||
|
||||
private:
|
||||
static size_t recv_call(void* contents, size_t size, size_t nmemb, std::string* output);
|
||||
|
||||
private:
|
||||
std::string api_key_{};
|
||||
std::string api_url_{};
|
||||
std::string recv_{};
|
||||
CURL* curl_{};
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user