OpenAI-C ++库是一个社区维护的库,可从用C ++语言编写的应用程序来方便地访问OpenAI API。
该库很小,带有两个标题文件(仅当您已经使用Nlohmann JSON时)。
没有特别的要求。您应该已经拥有这些:
库应在OpenAI参考文献上实施所有请求。如果有任何丢失(由于更新),请随时打开问题。
| API参考 | 方法 | 示例文件 |
|---|---|---|
| API模型 | 列表模型✅ | 1 model.cpp |
| API模型 | 检索模型✅ | 1 model.cpp |
| API完成 | 创建完成✅ | 2 completion.cpp |
| API编辑 | 创建完成 | 3-edit.cpp |
| API图像 | 创建图像✅ | 4-image.cpp |
| API图像 | 创建图像编辑✅ | 4-image.cpp |
| API图像 | 创建图像变化✅ | 4-image.cpp |
| API嵌入 | 创建嵌入✅ | 5- embedding.cpp |
| API文件 | 列表文件✅ | 6-File.cpp |
| API文件 | 上传文件✅ | 6-File.cpp |
| API文件 | 删除文件✅ | 6-File.cpp |
| API文件 | 检索文件✅ | 6-File.cpp |
| API文件 | 检索文件内容✅ | 6-File.cpp |
| API微型 | 创建微调✅ | 7-Fine-tune.cpp |
| API微型 | 列出微调✅ | 7-Fine-tune.cpp |
| API微型 | 检索微调✅ | 7-Fine-tune.cpp |
| API微型 | 取消微调✅ | 7-Fine-tune.cpp |
| API微型 | 列出微调事件✅ | 7-Fine-tune.cpp |
| API微型 | 删除微调模型✅ | 7-Fine-tune.cpp |
| API聊天 | 创建聊天完成✅ | 10-chat.cpp |
| API音频 | 创建转录✅ | 11-audio.cpp |
| API音频 | 创建翻译✅ | 11-audio.cpp |
| API节制 | 创建节制✅ | 12模型 |
该库由两个文件组成:包括/openai/openai.hpp,包括/openai/nlohmann/json.hpp。
只需复制项目中的inclage/OpenAICPP文件夹,您就可以在代码中使用#include "openai.hpp" 。仅此而已。
注意: OpenAi-CPP使用nlohmann JSON,可在
include/json.hpp中使用。随意使用自己的副本以更快的编译时间构建。
需要使用您的帐户的秘密密钥配置该库,该密钥可在网站上使用。建议在使用库之前设置OPENAI_API_KEY环境变量(或者您还可以直接在代码中设置API键):
export OPENAI_API_KEY= ' sk-... '以下代码可在示例/00-showcase.cpp中找到。
# include " openai.hpp "
# include < iostream >
int main () {
openai::start (); // Will use the api key provided by `OPENAI_API_KEY` environment variable
// openai::start("your_API_key", "optional_organization"); // Or you can handle it yourself
auto completion = openai::completion (). create ( R"( {
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
} )" _json); // Using user-defined (raw) string literals
std::cout << " Response is: n " << completion. dump ( 2 ) << ' n ' ;
auto image = openai::image (). create ({
{ " prompt " , " A cute koala playing the violin " },
{ " n " , 1 },
{ " size " , " 512x512 " }
}); // Using initializer lists
std::cout << " Image URL is: " << image[ " data " ][ 0 ][ " url " ] << ' n ' ;
}接收到的输出看起来像:
>> request: https://api.openai.com/v1/completions { " max_tokens " :7, " model " : " text-davinci-003 " , " prompt " : " Say this is a test " , " temperature " :0}
Response is:
{
" choices " : [
{
" finish_reason " : " length " ,
" index " : 0,
" logprobs " : null,
" text " : " nnThis is indeed a test "
}
],
" created " : 1674121840,
" id " : " cmpl-6aLr6jPhtxpLyu9rNsJFKDHU3SHpe " ,
" model " : " text-davinci-003 " ,
" object " : " text_completion " ,
" usage " : {
" completion_tokens " : 7,
" prompt_tokens " : 5,
" total_tokens " : 12
}
}
>> request: https://api.openai.com/v1/images/generations { " n " :1, " prompt " : " A cute koala playing the violin " , " size " : " 512x512 " }
Image URL is: " https://oaidalleapiprodscus.blob.core.windows.net/private/org-WaIMDdGHNwJiXAmjegDHE6AM/user-bCrYDjR21ly46316ZbdgqvKf/img-sysAePXF2c8yu28AIoZLLmEG.png?st=2023-01-19T20%3A35%3A19Z&se=2023-01-19T22%3A35%3A19Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-19T18%3A10%3A41Z&ske=2023-01-20T18%3A10%3A41Z&sks=b&skv=2021-08-06&sig=nWkcGTTCsWigHHocYP%2BsyiV5FJL6izpAe3OVvX1GLuI%3D " 
由于Openai::Json是Nlohmann :: JSON的打字,因此您将获得后者提供的所有功能(转换,喜欢访问,...)。
mkdir build && cd build
cmake .. && make
examples/[whatever]在您的项目中,如果您想像运行示例时一样获得详细的输出,则可以定义#define OPENAI_VERBOSE_OUTPUT 。
默认情况下,如果curl请求未成功,则OpenAI-CPP将抛出运行时错误异常。您可以按照自己喜欢的方式处理这些例外。您可以通过设置setThrowException(false)来防止投掷异常(请参见示例/09-instances.cpp中的示例)。如果这样做,将会显示警告。
您可以使用openai::post()或openai::get()方法充分控制您发送的内容(例如,当有OpenAI API的新方法可用并且尚未由OpenAI-CPP提供时,可以很有用)。
这是在程序中保持OpenAi-CPP会话的两种方法,以便您可以随时随地使用它。
这是默认行为。 OpenAi-CPP提供免费的方便功能: openai::start(const std::string& token)和openai::instance() 。初始化和配置OpenAI-CPP实例:
auto & openai = openai::start();当您处于另一个范围并丢失了openai参考时,您可以再次抓住它:
auto & openai = openai::instance();这可能不是建议的方式,但是由于我们通常只想处理一个OpenAI实例(一个令牌),因此这种方法非常方便。
另一种方法是通过参考,将其存储并在需要时调用适当的方法来传递OpenAI实例。
void bar (openai::OpenAI& openai) {
openai. completion . create ({
{ " model " , " text-davinci-003 " },
{ " prompt " , " Say bar() function called " }
});
}
int main () {
openai::OpenAI openai_instance{ " your_api_key " };
bar (openai_instance);
}您可以使用示例/09-instances.cpp中所示的std :: Referent_wrapper。
如果您必须使用不同的秘密密钥管理几个OpenAi-CPP实例,则此策略将很有用。
注意:如果您使用的是WSL,那么您并不关心以下内容。
根据Windows上的安装卷发
Windows 10随着版本1804以来与操作系统捆绑在一起的卷发工具
但是,您可能仍然很难处理cmake抛出的libcurl Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) 。
您可以尝试按照窗口上的卷曲安装卷曲提出的两种方式遵循一种。
解决此问题的另一种方法是在此处捕获Windows的卷曲版本,请在路径中可见的适当文件夹中复制include的内容(例如,如果在您的git安装[...]/Git/mingw64/include/ )。您还需要从此处获取curl.lib和libcurl.dll文件,然后将其复制到适当的文件夹中(例如,如果在您的git安装[...]/Git/mingw64/lib/ )中。
mkdir build && cd build
cmake .. -DCMAKE_GENERATOR_PLATFORM=x64
cmake --build .
cmake --build . --target 00-showcase # For a specific target或者,如果您喜欢在Windows上使用GNU GCC
cmake -G " MSYS Makefiles " -D CMAKE_CXX_COMPILER=g++ ..
make麻省理工学院
这项工作主要是受到CPR的松弛和卷曲包装代码的启发。
奥里亚