Skip to content

Commit 6249900

Browse files
committed
llmapi base
1 parent 91703e4 commit 6249900

30 files changed

+29149
-2
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,22 @@
3939

4040
# debug information files
4141
*.dwo
42+
43+
# Build directories
44+
build/
45+
.build/
46+
.xmake/
47+
48+
# IDE
49+
.vscode/
50+
.idea/
51+
*.swp
52+
*.swo
53+
*~
54+
55+
# OS
56+
.DS_Store
57+
Thumbs.db
58+
59+
# Logs
60+
*.log

LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Copyright (c) 2025-present mcpp community, speakshen@163.com
2+
3+
---
4+
15
Apache License
26
Version 2.0, January 2004
37
http://www.apache.org/licenses/

README.md

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,107 @@
1-
# openai
2-
mcpplibs | `import openai` in modern c++
1+
# llmapi
2+
3+
> Modern C++ LLM API client with openai-compatible support
4+
5+
[![C++23](https://img.shields.io/badge/C%2B%2B-23-blue.svg)](https://en.cppreference.com/w/cpp/23)
6+
[![C API](https://img.shields.io/badge/C_API-ok-green.svg)](https://en.cppreference.com/w/cpp/23)
7+
[![Module](https://img.shields.io/badge/module-ok-green.svg)](https://en.cppreference.com/w/cpp/language/modules)
8+
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE)
9+
[![OpenAI Compatible](https://img.shields.io/badge/OpenAI_API-Compatible-green.svg)](https://platform.openai.com/docs/api-reference)
10+
11+
| English - [简体中文](README.zh.md) - [繁體中文](README.zh.hant.md) |
12+
|:---:|
13+
| [Documentation](docs/) - [C++ API](docs/cpp-api.md) - [C API](docs/c-api.md) - [Examples](docs/examples.md) |
14+
15+
Clean, type-safe LLM API client using C++23 modules. Fluent interface with zero-cost abstractions. Works with OpenAI, Poe, DeepSeek and compatible endpoints.
16+
17+
## ✨ Features
18+
19+
- **C++23 Modules** - `import mcpplibs.llmapi`
20+
- **Auto-Save History** - Conversation history managed automatically
21+
- **Type-Safe Streaming** - Concept-constrained callbacks
22+
- **Fluent Interface** - Chainable methods
23+
- **C API** - Full C language support with OOP style
24+
- **Provider Agnostic** - OpenAI, Poe, and compatible endpoints
25+
26+
## 📦 Quick Start
27+
28+
### C++ API
29+
30+
```cpp
31+
import std;
32+
import mcpplibs.llmapi;
33+
34+
int main() {
35+
using namespace mcpplibs;
36+
37+
llmapi::Client client(std::getenv("OPENAI_API_KEY"), llmapi::URL::Poe);
38+
39+
client.model("gpt-5")
40+
.system("You are a helpful assistant.")
41+
.user("In one sentence, introduce modern C++. 并给出中文翻译")
42+
.request([](std::string_view chunk) {
43+
std::print("{}", chunk);
44+
std::cout.flush();
45+
});
46+
47+
return 0;
48+
}
49+
```
50+
51+
### C API
52+
53+
```c
54+
#include <stdio.h>
55+
56+
#include "llmapi.h"
57+
58+
void stream_print(const char* s, size_t len, void* data) {
59+
printf("%.*s", (int)len, s);
60+
fflush(stdout);
61+
}
62+
63+
int main(void) {
64+
llmapi_client_t* c = llmapi_client_create(getenv("OPENAI_API_KEY"), LLMAPI_URL_POE);
65+
66+
c->set_model(c, "gpt-5");
67+
c->add_system_message(c, "You are a helpful assistant.");
68+
c->add_user_message(c, "In one sentence, introduce modern C++. 并给出中文翻译");
69+
c->request_stream(c, stream_print, NULL);
70+
71+
c->destroy(c);
72+
return 0;
73+
}
74+
```
75+
76+
### Models / Providers
77+
78+
```cpp
79+
llmapi::Client client(apiKey, llmapi::URL::OpenAI); // OpenAI
80+
llmapi::Client client(apiKey, llmapi::URL::Poe); // Poe
81+
llmapi::Client client(apiKey, llmapi::URL::DeepSeek); // Deepseek
82+
llmapi::Client client(apiKey, "https://custom.com"); // Custom
83+
```
84+
85+
## 🛠️ Building
86+
87+
```bash
88+
xmake # Build
89+
xmake run basic # Run example(after cofig OPENAI_API_KEY)
90+
```
91+
92+
## 📚 API Reference
93+
94+
**C++ Core Methods:**
95+
- `model(name)` - Set model
96+
- `user/system/assistant(content)` - Add messages
97+
- `request()` - Non-streaming (returns JSON)
98+
- `request(callback)` - Streaming
99+
- `getAnswer()` - Get last assistant reply
100+
- `getMessages()` - Get conversation history
101+
- `clear()` - Clear history
102+
103+
**C API:** All methods available via function pointers (`client->method(client, ...)`)
104+
105+
## 📄 License
106+
107+
Apache-2.0 - see [LICENSE](LICENSE)

README.zh.hant.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# llmapi
2+
3+
> Modern C++ LLM API client with openai-compatible support
4+
5+
[![C++23](https://img.shields.io/badge/C%2B%2B-23-blue.svg)](https://en.cppreference.com/w/cpp/23)
6+
[![C API](https://img.shields.io/badge/C_API-ok-green.svg)](https://en.cppreference.com/w/cpp/23)
7+
[![Module](https://img.shields.io/badge/module-ok-green.svg)](https://en.cppreference.com/w/cpp/language/modules)
8+
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE)
9+
[![OpenAI Compatible](https://img.shields.io/badge/OpenAI_API-Compatible-green.svg)](https://platform.openai.com/docs/api-reference)
10+
11+
| [English](README.md) - [简体中文](README.zh.md) - 繁體中文 |
12+
|:---:|
13+
| [完整文件](docs/) - [C++ API](docs/cpp-api.md) - [C API](docs/c-api.md) - [範例](docs/examples.md) |
14+
15+
簡潔、型別安全的 LLM API 客戶端,使用 C++23 模組。流式介面設計,零成本抽象。支援 OpenAI、Poe、DeepSeek 及相容端點。
16+
17+
## ✨ 特性
18+
19+
- **C++23 模組** - `import mcpplibs.llmapi`
20+
- **自動儲存歷史** - 對話歷史自動管理
21+
- **型別安全串流** - 概念約束的回呼函式
22+
- **流式介面** - 可鏈式呼叫的方法
23+
- **C 語言 API** - 完整的 C 語言支援,物件導向風格
24+
- **提供商無關** - OpenAI、Poe 及相容端點
25+
26+
## 📦 快速開始
27+
28+
### C++ API
29+
30+
```cpp
31+
import std;
32+
import mcpplibs.llmapi;
33+
34+
int main() {
35+
using namespace mcpplibs;
36+
37+
llmapi::Client client(std::getenv("OPENAI_API_KEY"), llmapi::URL::Poe);
38+
39+
client.model("gpt-5")
40+
.system("You are a helpful assistant.")
41+
.user("In one sentence, introduce modern C++. 並給出中文翻譯")
42+
.request([](std::string_view chunk) {
43+
std::print("{}", chunk);
44+
std::cout.flush();
45+
});
46+
47+
return 0;
48+
}
49+
```
50+
51+
### C API
52+
53+
```c
54+
#include <stdio.h>
55+
56+
#include "llmapi.h"
57+
58+
void stream_print(const char* s, size_t len, void* data) {
59+
printf("%.*s", (int)len, s);
60+
fflush(stdout);
61+
}
62+
63+
int main(void) {
64+
llmapi_client_t* c = llmapi_client_create(getenv("OPENAI_API_KEY"), LLMAPI_URL_POE);
65+
66+
c->set_model(c, "gpt-5");
67+
c->add_system_message(c, "You are a helpful assistant.");
68+
c->add_user_message(c, "In one sentence, introduce modern C++. 並給出中文翻譯");
69+
c->request_stream(c, stream_print, NULL);
70+
71+
c->destroy(c);
72+
return 0;
73+
}
74+
```
75+
76+
### 模型 / 提供商
77+
78+
```cpp
79+
llmapi::Client client(apiKey, llmapi::URL::OpenAI); // OpenAI
80+
llmapi::Client client(apiKey, llmapi::URL::Poe); // Poe
81+
llmapi::Client client(apiKey, llmapi::URL::DeepSeek); // Deepseek
82+
llmapi::Client client(apiKey, "https://custom.com"); // 自訂
83+
```
84+
85+
## 🛠️ 建置
86+
87+
```bash
88+
xmake # 建置
89+
xmake run basic # 執行範例(需先設定 OPENAI_API_KEY)
90+
```
91+
92+
## 📚 API 參考
93+
94+
**C++ 核心方法:**
95+
- `model(name)` - 設定模型
96+
- `user/system/assistant(content)` - 新增訊息
97+
- `request()` - 非串流(回傳 JSON)
98+
- `request(callback)` - 串流輸出
99+
- `getAnswer()` - 取得最後的助手回覆
100+
- `getMessages()` - 取得對話歷史
101+
- `clear()` - 清空歷史
102+
103+
**C API:** 所有方法透過函式指標存取 (`client->method(client, ...)`)
104+
105+
## 📄 授權條款
106+
107+
Apache-2.0 - 詳見 [LICENSE](LICENSE)

README.zh.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# llmapi
2+
3+
> Modern C++ LLM API client with openai-compatible support
4+
5+
[![C++23](https://img.shields.io/badge/C%2B%2B-23-blue.svg)](https://en.cppreference.com/w/cpp/23)
6+
[![C API](https://img.shields.io/badge/C_API-ok-green.svg)](https://en.cppreference.com/w/cpp/23)
7+
[![Module](https://img.shields.io/badge/module-ok-green.svg)](https://en.cppreference.com/w/cpp/language/modules)
8+
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE)
9+
[![OpenAI Compatible](https://img.shields.io/badge/OpenAI_API-Compatible-green.svg)](https://platform.openai.com/docs/api-reference)
10+
11+
| [English](README.md) - 简体中文 - [繁體中文](README.zh.hant.md) |
12+
|:---:|
13+
| [完整文档](docs/) - [C++ API](docs/cpp-api.md) - [C API](docs/c-api.md) - [示例](docs/examples.md) |
14+
15+
简洁、类型安全的 LLM API 客户端,使用 C++23 模块。流式接口设计,零成本抽象。支持 OpenAI、Poe、DeepSeek 及兼容端点。
16+
17+
## ✨ 特性
18+
19+
- **C++23 模块** - `import mcpplibs.llmapi`
20+
- **自动保存历史** - 对话历史自动管理
21+
- **类型安全流式** - 概念约束的回调函数
22+
- **流式接口** - 可链式调用的方法
23+
- **C 语言 API** - 完整的 C 语言支持,面向对象风格
24+
- **提供商无关** - OpenAI、Poe 及兼容端点
25+
26+
## 📦 快速开始
27+
28+
### C++ API
29+
30+
```cpp
31+
import std;
32+
import mcpplibs.llmapi;
33+
34+
int main() {
35+
using namespace mcpplibs;
36+
37+
llmapi::Client client(std::getenv("OPENAI_API_KEY"), llmapi::URL::Poe);
38+
39+
client.model("gpt-5")
40+
.system("You are a helpful assistant.")
41+
.user("In one sentence, introduce modern C++. 并给出中文翻译")
42+
.request([](std::string_view chunk) {
43+
std::print("{}", chunk);
44+
std::cout.flush();
45+
});
46+
47+
return 0;
48+
}
49+
```
50+
51+
### C API
52+
53+
```c
54+
#include <stdio.h>
55+
56+
#include "llmapi.h"
57+
58+
void stream_print(const char* s, size_t len, void* data) {
59+
printf("%.*s", (int)len, s);
60+
fflush(stdout);
61+
}
62+
63+
int main(void) {
64+
llmapi_client_t* c = llmapi_client_create(getenv("OPENAI_API_KEY"), LLMAPI_URL_POE);
65+
66+
c->set_model(c, "gpt-5");
67+
c->add_system_message(c, "You are a helpful assistant.");
68+
c->add_user_message(c, "In one sentence, introduce modern C++. 并给出中文翻译");
69+
c->request_stream(c, stream_print, NULL);
70+
71+
c->destroy(c);
72+
return 0;
73+
}
74+
```
75+
76+
### 模型 / 提供商
77+
78+
```cpp
79+
llmapi::Client client(apiKey, llmapi::URL::OpenAI); // OpenAI
80+
llmapi::Client client(apiKey, llmapi::URL::Poe); // Poe
81+
llmapi::Client client(apiKey, llmapi::URL::DeepSeek); // Deepseek
82+
llmapi::Client client(apiKey, "https://custom.com"); // 自定义
83+
```
84+
85+
## 🛠️ 构建
86+
87+
```bash
88+
xmake # 构建
89+
xmake run basic # 运行示例(需要先配置 OPENAI_API_KEY)
90+
```
91+
92+
## 📚 API 参考
93+
94+
**C++ 核心方法:**
95+
- `model(name)` - 设置模型
96+
- `user/system/assistant(content)` - 添加消息
97+
- `request()` - 非流式(返回 JSON)
98+
- `request(callback)` - 流式输出
99+
- `getAnswer()` - 获取最后的助手回复
100+
- `getMessages()` - 获取对话历史
101+
- `clear()` - 清空历史
102+
103+
**C API:** 所有方法通过函数指针访问 (`client->method(client, ...)`)
104+
105+
## 📄 许可证
106+
107+
Apache-2.0 - 详见 [LICENSE](LICENSE)

docs/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# llmapi Documentation
2+
3+
Complete documentation for the llmapi library - a modern C++ LLM API client.
4+
5+
## 📚 Contents
6+
7+
- [Getting Started](getting-started.md) - Installation and first steps
8+
- [C++ API Guide](cpp-api.md) - Complete C++ API reference
9+
- [C API Guide](c-api.md) - Complete C API reference
10+
- [Examples](examples.md) - Code examples and use cases
11+
- [Providers](providers.md) - Supported LLM providers
12+
- [Advanced Usage](advanced.md) - Advanced features and patterns
13+
14+
## Quick Links
15+
16+
**For C++ Developers:**
17+
- Start with [Getting Started](getting-started.md)
18+
- See [C++ API Guide](cpp-api.md) for full API reference
19+
- Check [Examples](examples.md) for common patterns
20+
21+
**For C Developers:**
22+
- Start with [Getting Started](getting-started.md)
23+
- See [C API Guide](c-api.md) for full API reference
24+
- Check [Examples](examples.md) for C examples
25+
26+
## Features Overview
27+
28+
- **C++23 Modules** - Modern module system
29+
- **Auto-Save History** - Automatic conversation management
30+
- **Type-Safe Streaming** - Concept-constrained callbacks
31+
- **Fluent Interface** - Chainable API design
32+
- **C API Support** - Full C language bindings
33+
- **Provider Agnostic** - OpenAI, Poe, and custom endpoints
34+
35+
## License
36+
37+
Apache-2.0 - see [LICENSE](../LICENSE)

0 commit comments

Comments
 (0)