inference_c_api.cpp¶
src/inference/inference_c_api.cpp
@file inference_c_api.cpp @brief C API wrappers for InferenceBackend.
Owners: @tvanfossen
entropic_inference_activate
entropic_error_t entropic_inference_activate(entropic_inference_backend_t backend)
Activate model on GPU (WARM → ACTIVE).
src/inference/inference_c_api.cpp:238
Source · 11 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_activate(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
return to_backend(backend)->activate() ? ENTROPIC_OK : ENTROPIC_ERROR_LOAD_FAILED;
} catch (const std::exception& e) {
logger->error("inference_activate exception: {}", e.what());
return ENTROPIC_ERROR_LOAD_FAILED;
}
}Activity · 3 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_activate(entropic_inference_backend_t backend)
Plugin C API: promote backend from WARM to ACTIVE (GPU load).
src/inference/inference_c_api.cpp:238
Source · 11 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_activate(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
return to_backend(backend)->activate() ? ENTROPIC_OK : ENTROPIC_ERROR_LOAD_FAILED;
} catch (const std::exception& e) {
logger->error("inference_activate exception: {}", e.what());
return ENTROPIC_ERROR_LOAD_FAILED;
}
}Activity · 3 steps
Requirements
entropic_inference_complete
entropic_error_t entropic_inference_complete(entropic_inference_backend_t backend, const char *prompt, const char *params_json, char **result_json)
Raw text completion without chat template.
src/inference/inference_c_api.cpp:519
Called by TEST_CASE
Calls alloc_string, complete, parse_params_json, serialize_result_json, to_backend
Source · 19 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_complete(
entropic_inference_backend_t backend,
const char* prompt,
const char* params_json,
char** result_json)
{
if (!backend || !prompt || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
try {
auto params = parse_params_json(params_json);
auto result = to_backend(backend)->complete(prompt, params);
*result_json = alloc_string(serialize_result_json(result));
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_complete exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 3 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_complete(entropic_inference_backend_t backend, const char *prompt, const char *params_json, char **result_json)
Plugin C API: raw text completion without chat template.
src/inference/inference_c_api.cpp:519
Called by TEST_CASE
Calls alloc_string, complete, parse_params_json, serialize_result_json, to_backend
Source · 19 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_complete(
entropic_inference_backend_t backend,
const char* prompt,
const char* params_json,
char** result_json)
{
if (!backend || !prompt || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
try {
auto params = parse_params_json(params_json);
auto result = to_backend(backend)->complete(prompt, params);
*result_json = alloc_string(serialize_result_json(result));
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_complete exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 3 steps
Requirements
entropic_inference_count_tokens
int entropic_inference_count_tokens(entropic_inference_backend_t backend, const char *text, size_t text_len)
Count tokens in text using model's tokenizer.
src/inference/inference_c_api.cpp:548
Source · 15 lines
ENTROPIC_EXPORT int entropic_inference_count_tokens(
entropic_inference_backend_t backend,
const char* text,
size_t text_len)
{
// v2.3.10: null guards. text=nullptr is a real misuse (caller
// owes us bytes). Returning 0 keeps the contract simple (no
// tokens to count when there's no string).
if (!backend || !text) { return 0; }
try {
return to_backend(backend)->count_tokens(std::string(text, text_len));
} catch (...) {
return static_cast<int>(text_len) / 4;
}
}Activity · 3 steps
ENTROPIC_EXPORT int entropic_inference_count_tokens(entropic_inference_backend_t backend, const char *text, size_t text_len)
Plugin C API: count tokens for a text span (exact when loaded, estimate when COLD).
src/inference/inference_c_api.cpp:548
Source · 15 lines
ENTROPIC_EXPORT int entropic_inference_count_tokens(
entropic_inference_backend_t backend,
const char* text,
size_t text_len)
{
// v2.3.10: null guards. text=nullptr is a real misuse (caller
// owes us bytes). Returning 0 keeps the contract simple (no
// tokens to count when there's no string).
if (!backend || !text) { return 0; }
try {
return to_backend(backend)->count_tokens(std::string(text, text_len));
} catch (...) {
return static_cast<int>(text_len) / 4;
}
}Activity · 3 steps
Requirements
entropic_inference_deactivate
entropic_error_t entropic_inference_deactivate(entropic_inference_backend_t backend)
Deactivate model (ACTIVE → WARM).
src/inference/inference_c_api.cpp:257
Source · 12 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_deactivate(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
to_backend(backend)->deactivate();
return ENTROPIC_OK;
} catch (const std::exception& e) {
logger->error("inference_deactivate exception: {}", e.what());
return ENTROPIC_ERROR_INTERNAL;
}
}Activity · 3 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_deactivate(entropic_inference_backend_t backend)
Plugin C API: demote backend from ACTIVE to WARM (release GPU).
src/inference/inference_c_api.cpp:257
Source · 12 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_deactivate(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
to_backend(backend)->deactivate();
return ENTROPIC_OK;
} catch (const std::exception& e) {
logger->error("inference_deactivate exception: {}", e.what());
return ENTROPIC_ERROR_INTERNAL;
}
}Activity · 3 steps
Requirements
entropic_inference_destroy
void entropic_inference_destroy(entropic_inference_backend_t backend)
Destroy backend instance and free all resources.
src/inference/inference_c_api.cpp:570
Source · 5 lines
ENTROPIC_EXPORT void entropic_inference_destroy(
entropic_inference_backend_t backend)
{
delete to_backend(backend);
}Activity · 1 steps
ENTROPIC_EXPORT void entropic_inference_destroy(entropic_inference_backend_t backend)
Plugin C API: destroy the backend and free its resources.
src/inference/inference_c_api.cpp:570
Source · 5 lines
ENTROPIC_EXPORT void entropic_inference_destroy(
entropic_inference_backend_t backend)
{
delete to_backend(backend);
}Activity · 1 steps
Requirements
entropic_inference_generate
entropic_error_t entropic_inference_generate(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, char **result_json)
Generate a response from messages (batch mode).
src/inference/inference_c_api.cpp:317
Called by TEST_CASE
Calls alloc_string, generate, parse_messages_json, parse_params_json, serialize_result_json, to_backend
Source · 23 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
char** result_json)
{
if (!backend || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
logger->info("C API: inference_generate");
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
auto result = to_backend(backend)->generate(msgs, params);
*result_json = alloc_string(serialize_result_json(result));
logger->info("C API: inference_generate -> {}",
result.ok() ? "ok" : "error");
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 4 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, char **result_json)
Plugin C API: blocking generation returning full result.
src/inference/inference_c_api.cpp:317
Called by TEST_CASE
Calls alloc_string, generate, parse_messages_json, parse_params_json, serialize_result_json, to_backend
Source · 23 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
char** result_json)
{
if (!backend || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
logger->info("C API: inference_generate");
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
auto result = to_backend(backend)->generate(msgs, params);
*result_json = alloc_string(serialize_result_json(result));
logger->info("C API: inference_generate -> {}",
result.ok() ? "ok" : "error");
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 4 steps
Requirements
entropic_inference_generate_streaming
entropic_error_t entropic_inference_generate_streaming(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, void(*on_token)(const char *token, size_t len, void *user_data), void *user_data, int *cancel_flag)
Generate with streaming token callback.
src/inference/inference_c_api.cpp:476
Called by TEST_CASE
Calls generate_streaming, ok, on_token, parse_messages_json, parse_params_json, to_backend
Source · 32 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_streaming(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
void (*on_token)(const char* token, size_t len, void* user_data),
void* user_data,
int* cancel_flag)
{
if (!backend || !on_token) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
std::atomic<bool> cancel{false};
auto callback = [on_token, user_data, cancel_flag, &cancel]
(std::string_view token) {
on_token(token.data(), token.size(), user_data);
if (cancel_flag && *cancel_flag) {
cancel.store(true, std::memory_order_release);
}
};
auto result = to_backend(backend)->generate_streaming(
msgs, params, callback, cancel);
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate_streaming exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 3 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_streaming(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, void(*on_token)(const char *token, size_t len, void *user_data), void *user_data, int *cancel_flag)
Plugin C API: streaming generation with token callback and cancel flag.
src/inference/inference_c_api.cpp:476
Called by TEST_CASE
Calls generate_streaming, ok, on_token, parse_messages_json, parse_params_json, to_backend
Source · 32 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_streaming(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
void (*on_token)(const char* token, size_t len, void* user_data),
void* user_data,
int* cancel_flag)
{
if (!backend || !on_token) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
std::atomic<bool> cancel{false};
auto callback = [on_token, user_data, cancel_flag, &cancel]
(std::string_view token) {
on_token(token.data(), token.size(), user_data);
if (cancel_flag && *cancel_flag) {
cancel.store(true, std::memory_order_release);
}
};
auto result = to_backend(backend)->generate_streaming(
msgs, params, callback, cancel);
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate_streaming exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 3 steps
Requirements
entropic_inference_generate_with_cancel
entropic_error_t entropic_inference_generate_with_cancel(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, char **result_json, int *cancel_flag)
Generate a response (batch mode) with mid-decode cancellation.
src/inference/inference_c_api.cpp:437
Called by TEST_CASE
Calls alloc_string, flag, generate, parse_messages_json, parse_params_json, serialize_result_json, to_backend
Source · 26 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_with_cancel(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
char** result_json,
int* cancel_flag)
{
if (!backend || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
logger->info("C API: inference_generate_with_cancel");
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
CancelFlagBridge bridge(cancel_flag);
auto result = to_backend(backend)->generate(
msgs, params, bridge.flag());
*result_json = alloc_string(serialize_result_json(result));
logger->info("C API: inference_generate_with_cancel -> {}",
result.ok() ? "ok" : "error");
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate_with_cancel exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 4 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_with_cancel(entropic_inference_backend_t backend, const char *messages_json, const char *params_json, char **result_json, int *cancel_flag)
Plugin C API: batch generation with mid-decode cancel.
src/inference/inference_c_api.cpp:437
Called by TEST_CASE
Calls alloc_string, generate, parse_messages_json, parse_params_json, serialize_result_json, to_backend
Source · 26 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_generate_with_cancel(
entropic_inference_backend_t backend,
const char* messages_json,
const char* params_json,
char** result_json,
int* cancel_flag)
{
if (!backend || !result_json) {
return ENTROPIC_ERROR_INVALID_ARGUMENT;
}
logger->info("C API: inference_generate_with_cancel");
try {
auto msgs = entropic::parse_messages_json(messages_json);
auto params = parse_params_json(params_json);
CancelFlagBridge bridge(cancel_flag);
auto result = to_backend(backend)->generate(
msgs, params, bridge.flag());
*result_json = alloc_string(serialize_result_json(result));
logger->info("C API: inference_generate_with_cancel -> {}",
result.ok() ? "ok" : "error");
return result.ok() ? ENTROPIC_OK : result.error_code;
} catch (const std::exception& e) {
logger->error("inference_generate_with_cancel exception: {}", e.what());
return ENTROPIC_ERROR_GENERATE_FAILED;
}
}Activity · 4 steps
Requirements
entropic_inference_load
entropic_error_t entropic_inference_load(entropic_inference_backend_t backend, const char *config_json)
Load a model from config (COLD → WARM).
src/inference/inference_c_api.cpp:207
Source · 23 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_load(
entropic_inference_backend_t backend,
const char* config_json)
{
// v2.3.10: null-handle guard. Pre-v2.3.10 to_backend(nullptr)
// returned nullptr and the next ->load() dereferenced it, taking
// the whole process down with SIGSEGV. The plugin ABI is the
// load-bearing boundary for misuse; rejecting null with a clean
// error code is safer than crashing.
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
logger->info("C API: inference_load");
try {
auto config = parse_config_json(config_json);
auto rc = to_backend(backend)->load(config)
? ENTROPIC_OK : ENTROPIC_ERROR_LOAD_FAILED;
logger->info("C API: inference_load -> {}",
static_cast<int>(rc));
return rc;
} catch (const std::exception& e) {
logger->error("inference_load exception: {}", e.what());
return ENTROPIC_ERROR_LOAD_FAILED;
}
}Activity · 4 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_load(entropic_inference_backend_t backend, const char *config_json)
Plugin C API: load a model into the inference backend.
src/inference/inference_c_api.cpp:207
Source · 23 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_load(
entropic_inference_backend_t backend,
const char* config_json)
{
// v2.3.10: null-handle guard. Pre-v2.3.10 to_backend(nullptr)
// returned nullptr and the next ->load() dereferenced it, taking
// the whole process down with SIGSEGV. The plugin ABI is the
// load-bearing boundary for misuse; rejecting null with a clean
// error code is safer than crashing.
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
logger->info("C API: inference_load");
try {
auto config = parse_config_json(config_json);
auto rc = to_backend(backend)->load(config)
? ENTROPIC_OK : ENTROPIC_ERROR_LOAD_FAILED;
logger->info("C API: inference_load -> {}",
static_cast<int>(rc));
return rc;
} catch (const std::exception& e) {
logger->error("inference_load exception: {}", e.what());
return ENTROPIC_ERROR_LOAD_FAILED;
}
}Activity · 4 steps
Requirements
entropic_inference_log_silence
void entropic_inference_log_silence(void)
Silence all llama/ggml log output.
src/inference/inference_c_api.cpp:717
Called by TEST_CASE, entropic_create, entropic_destroy
Calls ggml_log_silence_locked
Source · 4 lines
void entropic_inference_log_silence(void) {
std::lock_guard lk(s_ggml_log_mu);
ggml_log_silence_locked();
}Activity · 2 steps
void entropic_inference_log_silence(void)
Silence all llama/ggml output.
src/inference/inference_c_api.cpp:717
Called by TEST_CASE, entropic_create, entropic_destroy
Calls ggml_log_silence_locked
Source · 4 lines
void entropic_inference_log_silence(void) {
std::lock_guard lk(s_ggml_log_mu);
ggml_log_silence_locked();
}Activity · 2 steps
Flows
entropic_inference_state
int entropic_inference_state(entropic_inference_backend_t backend)
Query model state (lock-free).
src/inference/inference_c_api.cpp:297
Source · 9 lines
ENTROPIC_EXPORT int entropic_inference_state(
entropic_inference_backend_t backend)
{
// v2.3.10: null-handle returns COLD (0) — the safest "I don't
// own anything" answer. Callers gating activation on state should
// treat null + COLD identically anyway.
if (!backend) { return static_cast<int>(ENTROPIC_MODEL_STATE_COLD); }
return static_cast<int>(to_backend(backend)->state());
}Activity · 3 steps
ENTROPIC_EXPORT int entropic_inference_state(entropic_inference_backend_t backend)
Plugin C API: query current lifecycle state (lock-free).
src/inference/inference_c_api.cpp:297
Source · 9 lines
ENTROPIC_EXPORT int entropic_inference_state(
entropic_inference_backend_t backend)
{
// v2.3.10: null-handle returns COLD (0) — the safest "I don't
// own anything" answer. Callers gating activation on state should
// treat null + COLD identically anyway.
if (!backend) { return static_cast<int>(ENTROPIC_MODEL_STATE_COLD); }
return static_cast<int>(to_backend(backend)->state());
}Activity · 3 steps
Requirements
entropic_inference_unload
entropic_error_t entropic_inference_unload(entropic_inference_backend_t backend)
Unload model completely (→ COLD).
src/inference/inference_c_api.cpp:277
Source · 12 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_unload(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
to_backend(backend)->unload();
return ENTROPIC_OK;
} catch (const std::exception& e) {
logger->error("inference_unload exception: {}", e.what());
return ENTROPIC_ERROR_INTERNAL;
}
}Activity · 3 steps
ENTROPIC_EXPORT entropic_error_t entropic_inference_unload(entropic_inference_backend_t backend)
Plugin C API: release the loaded model (transition to COLD).
src/inference/inference_c_api.cpp:277
Source · 12 lines
ENTROPIC_EXPORT entropic_error_t entropic_inference_unload(
entropic_inference_backend_t backend)
{
if (!backend) { return ENTROPIC_ERROR_INVALID_ARGUMENT; }
try {
to_backend(backend)->unload();
return ENTROPIC_OK;
} catch (const std::exception& e) {
logger->error("inference_unload exception: {}", e.what());
return ENTROPIC_ERROR_INTERNAL;
}
}