These C++ examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
C++ Typed Generation
Runs a small typed generation program against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- cpp src/examples/cpp/generation/basic_generation.cpp - Source: src/examples/cpp/generation/basic_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("question:string -> answer:string");
axllm::Value output = program.forward(client, axllm::object({{"question", "In one sentence, explain Ax as a language-agnostic LLM programming library."}}));
std::cout << axllm::stringify(output) << "\n";
}C++ Structured Extraction
Extracts structured fields and labels from support text with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- cpp src/examples/cpp/generation/structured_generation.cpp - Source: src/examples/cpp/generation/structured_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("ticket:string -> priority:class \"high, normal, low\", summary:string, labels:string[]");
axllm::Value output = program.forward(client, axllm::object({{"ticket", "Checkout has failed for enterprise customers since 09:00. Support wants a concise summary and tags."}}));
std::cout << axllm::stringify(output) << "\n";
}C++ Contextual Generation
Answers from supplied context and returns compact citations with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- cpp src/examples/cpp/generation/context_generation.cpp - Source: src/examples/cpp/generation/context_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("context:string, question:string -> answer:string, citations:string[]");
axllm::Value output = program.forward(client, axllm::object({{"context", "Ax uses signatures, ai(), ax(), agent(), flow(), and optimize()."}, {"question", "How should a new developer think about Ax?"}}));
std::cout << axllm::stringify(output) << "\n";
}