快速開始

June 10, 2026 · View on GitHub

環境需求

  • 建置工具:mcpp — 工具鏈與相依 (如 tinyhttps)自動解析
  • 也可以在 xmake 專案中使用,見使用 xmake 整合

從模板建立專案(推薦)

mcpp new myagent --template llmapi
cd myagent
mcpp run

列出函式庫提供的模板:

mcpp new --list-templates llmapi

在既有專案中引入

mcpp add llmapi

或在 mcpp.toml 中宣告:

[dependencies.mcpplibs]
llmapi = "0.2.8"

從原始碼建置

git clone https://github.com/mcpplibs/llmapi.git
cd llmapi
mcpp build

第一個範例

import mcpplibs.llmapi;
import std;

int main() {
    using namespace mcpplibs::llmapi;

    auto client = Client(Config{
        .apiKey = std::getenv("OPENAI_API_KEY"),
        .model = "gpt-4o-mini",
    });

    client.system("You are a helpful assistant.");
    auto resp = client.chat("請用一句話介紹現代 C++。");
    std::cout << resp.text() << '\n';
}

切換 Provider

OpenAI:

auto client = Client(Config{
    .apiKey = std::getenv("OPENAI_API_KEY"),
    .model = "gpt-4o-mini",
});

Anthropic:

auto client = Client(AnthropicConfig{
    .apiKey = std::getenv("ANTHROPIC_API_KEY"),
    .model = "claude-sonnet-4-20250514",
});

相容端點:

auto client = Client(Config{
    .apiKey = std::getenv("DEEPSEEK_API_KEY"),
    .baseUrl = std::string(URL::DeepSeek),
    .model = "deepseek-chat",
});