|
1 | 1 | # llmapi |
2 | 2 |
|
3 | | -> Modern C++ LLM API client with openai-compatible support |
| 3 | +> 使用 C++23 模組建構的現代 LLM 客戶端 |
4 | 4 |
|
5 | 5 | [](https://en.cppreference.com/w/cpp/23) |
6 | 6 | [](https://en.cppreference.com/w/cpp/language/modules) |
7 | 7 | [](LICENSE) |
8 | | -[](https://platform.openai.com/docs/api-reference) |
| 8 | +[](https://platform.openai.com/docs/api-reference) |
9 | 9 |
|
10 | 10 | | [English](README.md) - [简体中文](README.zh.md) - 繁體中文 | |
11 | 11 | |:---:| |
12 | | -| [完整文件](docs/) - [C++ API](docs/cpp-api.md) - [C API](docs/c-api.md) - [範例](docs/examples.md) | |
| 12 | +| [中文文件](docs/README.zh.hant.md) - [C++ API](docs/cpp-api.zh.hant.md) - [範例](docs/examples.zh.hant.md) | |
13 | 13 |
|
14 | | -簡潔、型別安全的 LLM API 客戶端,使用 C++23 模組。流式介面設計,零成本抽象。支援 OpenAI、Poe、DeepSeek 及相容端點。 |
| 14 | +`llmapi` 提供型別化的 `Client<Provider>` API,涵蓋聊天、串流輸出、embeddings、工具呼叫與對話持久化。預設別名 `Config` 對應 OpenAI 風格設定,常見情況下不需要顯式寫出 `openai::OpenAI(...)`。 |
15 | 15 |
|
16 | | -## ✨ 特性 |
| 16 | +## 特性 |
17 | 17 |
|
18 | | -- **C++23 模組** - `import mcpplibs.llmapi` |
19 | | -- **自動儲存歷史** - 對話歷史自動管理 |
20 | | -- **型別安全串流** - 概念約束的回呼函式 |
21 | | -- **流式介面** - 可鏈式呼叫的方法 |
22 | | -- **提供商無關** - OpenAI、Poe 及相容端點 |
| 18 | +- C++23 模組:`import mcpplibs.llmapi` |
| 19 | +- 強型別訊息、工具與回應結構 |
| 20 | +- 同步、非同步、串流聊天介面 |
| 21 | +- OpenAI Provider 支援 embeddings |
| 22 | +- 支援儲存 / 載入對話歷史 |
| 23 | +- 可透過 `baseUrl` 存取 OpenAI 相容端點 |
23 | 24 |
|
24 | 25 | ## 快速開始 |
25 | 26 |
|
26 | | - |
27 | 27 | ```cpp |
28 | | -import std; |
29 | 28 | import mcpplibs.llmapi; |
| 29 | +import std; |
30 | 30 |
|
31 | 31 | int main() { |
32 | | - using namespace mcpplibs; |
33 | | - |
34 | | - llmapi::Client client(std::getenv("OPENAI_API_KEY"), llmapi::URL::Poe); |
35 | | - |
36 | | - client.model("gpt-5") |
37 | | - .system("You are a helpful assistant.") |
38 | | - .user("In one sentence, introduce modern C++. 並給出中文翻譯") |
39 | | - .request([](std::string_view chunk) { |
40 | | - std::print("{}", chunk); |
41 | | - std::cout.flush(); |
42 | | - }); |
| 32 | + using namespace mcpplibs::llmapi; |
| 33 | + |
| 34 | + auto apiKey = std::getenv("OPENAI_API_KEY"); |
| 35 | + if (!apiKey) { |
| 36 | + std::cerr << "OPENAI_API_KEY not set\n"; |
| 37 | + return 1; |
| 38 | + } |
43 | 39 |
|
| 40 | + auto client = Client(Config{ |
| 41 | + .apiKey = apiKey, |
| 42 | + .model = "gpt-4o-mini", |
| 43 | + }); |
| 44 | + |
| 45 | + client.system("You are a concise assistant."); |
| 46 | + auto resp = client.chat("用兩句話解釋 C++23 模組的價值。"); |
| 47 | + |
| 48 | + std::cout << resp.text() << '\n'; |
44 | 49 | return 0; |
45 | 50 | } |
46 | 51 | ``` |
47 | 52 |
|
48 | | -### 模型 / 提供商 |
| 53 | +## Provider |
| 54 | + |
| 55 | +- `Config`:`openai::Config` 的匯出別名,預設走 OpenAI 風格 |
| 56 | +- `openai::OpenAI`:OpenAI 聊天、串流、工具呼叫、embeddings |
| 57 | +- `AnthropicConfig` / `anthropic::Anthropic`:Anthropic 聊天與串流 |
| 58 | + |
| 59 | +相容端點範例: |
49 | 60 |
|
50 | 61 | ```cpp |
51 | | -llmapi::Client client(apiKey, llmapi::URL::OpenAI); // OpenAI |
52 | | -llmapi::Client client(apiKey, llmapi::URL::Poe); // Poe |
53 | | -llmapi::Client client(apiKey, llmapi::URL::DeepSeek); // Deepseek |
54 | | -llmapi::Client client(apiKey, "https://custom.com"); // 自訂 |
| 62 | +auto client = Client(Config{ |
| 63 | + .apiKey = std::getenv("DEEPSEEK_API_KEY"), |
| 64 | + .baseUrl = std::string(URL::DeepSeek), |
| 65 | + .model = "deepseek-chat", |
| 66 | +}); |
55 | 67 | ``` |
56 | 68 |
|
57 | | -## 🛠️ 建置 |
| 69 | +## 建置與執行 |
58 | 70 |
|
59 | 71 | ```bash |
60 | | -xmake # 建置 |
61 | | -xmake run basic # 執行範例(需先設定 OPENAI_API_KEY) |
| 72 | +xmake |
| 73 | +xmake run hello_mcpp |
| 74 | +xmake run basic |
| 75 | +xmake run chat |
62 | 76 | ``` |
63 | 77 |
|
64 | | -## 📦 在建置工具中使用 |
65 | | - |
66 | | -### xmake |
| 78 | +## 套件管理使用 |
67 | 79 |
|
68 | 80 | ```lua |
69 | | --- 0 - 新增 mcpplibs 索引倉庫 |
70 | | -add_repositories("mcpplibs-index https://github.com/mcpplibs/llmapi.git") |
71 | | - |
72 | | --- 1 - 新增需要的函式庫和版本 |
| 81 | +add_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git") |
73 | 82 | add_requires("llmapi 0.0.2") |
74 | | -``` |
75 | | - |
76 | | -> More: [mcpplibs-index](https://github.com/mcpplibs/mcpplibs-index) |
77 | 83 |
|
78 | | -### cmake |
79 | | - |
80 | | -``` |
81 | | -todo... |
| 84 | +target("demo") |
| 85 | + set_kind("binary") |
| 86 | + set_languages("c++23") |
| 87 | + set_policy("build.c++.modules", true) |
| 88 | + add_files("src/*.cpp") |
| 89 | + add_packages("llmapi") |
82 | 90 | ``` |
83 | 91 |
|
84 | | -## 📄 授權條款 |
| 92 | +更多內容見 [docs/getting-started.zh.hant.md](docs/getting-started.zh.hant.md) 與 [docs/providers.zh.hant.md](docs/providers.zh.hant.md)。 |
| 93 | + |
| 94 | +## 授權 |
85 | 95 |
|
86 | | -Apache-2.0 - 詳見 [LICENSE](LICENSE) |
| 96 | +Apache-2.0,詳見 [LICENSE](LICENSE) |
0 commit comments