目录

Gewu Service Development

When to Use

This capability is supported since API version 20. On-device inference helps protect user data privacy, lowers deployment costs, reduces latency, and provides high availability without relying on network connections. However, compared with cloud-side inference, on-device inference faces greater challenges because end-side devices have limited memory and compute resources, are sensitive to power consumption, and must keep the user experience smooth while running on-device inference services. To address these challenges, the Gewu service provides QoS-aware inference acceleration and resource management optimization. This section guides you in using the Gewu APIs.

Available APIs

[h2]Error Codes

The OH_QoS_GewuErrorCode enum is used as the error code type of Gewu. For the meaning of the error codes returned by each function API, see the corresponding API description.

[h2]Session Handle

A session handle is used for session management. It is obtained when a session is successfully created via OH_QoS_GewuCreateSession, and can be used to submit/abort requests and destroy the session.

typedef unsigned int OH_QoS_GewuSession;

[h2]Request Handle

A request handle is used for request management. It is obtained when a request is successfully submitted via OH_QoS_GewuSubmitRequest, and can be used to abort the request.

typedef unsigned int OH_QoS_GewuRequest;

[h2]Functions

Function Name Description
OH_QoS_GewuCreateSession Creates a session
OH_QoS_GewuDestroySession Destroys a session
OH_QoS_GewuSubmitRequest Submits a request
OH_QoS_GewuAbortRequest Aborts a request

OH_QoS_GewuCreateSession

Description

The OH_QoS_GewuCreateSession API is used to create a session.

This API processes requests asynchronously. That is, the API only initiates session creation and returns before session resource allocation and model loading are complete. Gewu optimizes on-device inference resource management and can dynamically load resources on demand.

The lifecycle of a session object begins when the OH_QoS_GewuCreateSession function returns and ends when OH_QoS_GewuDestroySession is called.

Declaration

typedef struct {
    OH_QoS_GewuSession session;
    OH_QoS_GewuErrorCode error;
} OH_QoS_GewuCreateSessionResult;

OH_QoS_GewuCreateSessionResult OH_QoS_GewuCreateSession(const char* attributes);

Parameters

const char* attributes: JSON string of session attributes. This JSON string supports the following field:

* **"model"**: string, indicating the path of the model used by the session.

Example of the attributes JSON string:

{
    "model": "/data/storage/el2/base/files/qwen2/"
}

Returns

If the session is created successfully, the error field in the returned OH_QoS_GewuCreateSessionResult is OH_QOS_GEWU_OK, and session is the handle of the created session.

If session creation fails, the error field in the returned OH_QoS_GewuCreateSessionResult indicates the cause of the error. OH_QOS_GEWU_NOMEM indicates that there is not enough memory to create the session.

OH_QoS_GewuDestroySession

Description

The OH_QoS_GewuDestroySession API is used to destroy a session.

Users are advised to wait until all requests are complete or aborted before calling this API to destroy the session. If there are still ongoing requests when this API is called, those requests will be aborted and users will not receive responses for them. Note that after this API is called, the session object can no longer be used.

Declaration

OH_QoS_GewuErrorCode OH_QoS_GewuDestroySession(OH_QoS_GewuSession session);

Parameters

Returns

If the session is destroyed successfully, the return value is OH_QOS_GEWU_OK.

If the session destruction fails, the return value indicates the error cause, where OH_QOS_GEWU_NOENT indicates that the session was not found.

OH_QoS_GewuSubmitRequest

Description

The OH_QoS_GewuSubmitRequest API is used to submit a request. This API executes the request asynchronously. That is, the API only initiates the request and does not directly return the result. The request may not have started when this API returns. The request result is returned to the user through the callback provided by the user.

Declaration

typedef struct {
    OH_QoS_GewuRequest request;
    OH_QoS_GewuErrorCode error;
} OH_QoS_GewuSubmitRequestResult;

typedef void (*OH_QoS_GewuOnResponse)(void* context, const char* response);

OH_QoS_GewuSubmitRequestResult OH_QoS_GewuSubmitRequest(OH_QoS_GewuSession session, const char* request,
    OH_QoS_GewuOnResponse callback, void* context);

Parameters

The parameters of OH_QoS_GewuSubmitRequest are as follows:

The parameters of the OH_QoS_GewuOnResponse callback function are as follows:

Returns

If the request is submitted successfully, the error field in the returned OH_QoS_GewuSubmitRequestResult is OH_QOS_GEWU_OK, and request is the request handle.

If the request fails to be submitted, the error field in the returned OH_QoS_GewuSubmitRequestResult indicates the cause of the error. OH_QOS_GEWU_NOMEM indicates that there is not enough memory to process the request.

OH_QoS_GewuAbortRequest

Description

The OH_QoS_GewuAbortRequest API is used to abort a request early.

Normally, after calling OH_QoS_GewuSubmitRequest to submit a request, users wait until inference is complete, that is, until a response with a non-empty "finish_reason" is received. In this case, OH_QoS_GewuAbortRequest does not need to be called.

Call OH_QoS_GewuAbortRequest only when users want to abort an inference request early.

After this function is called successfully, users will not receive any more responses for this request, and the request handle can no longer be used.

Declaration

OH_QoS_GewuErrorCode OH_QoS_GewuAbortRequest(OH_QoS_GewuSession session, OH_QoS_GewuRequest request);

Parameters

Returns

If the request is aborted successfully, the return value is OH_QOS_GEWU_OK.

If the request fails to be aborted, the return value indicates the cause of the error. OH_QOS_GEWU_NOENT indicates that the request cannot be found.

Example

The example is as follows:

#include <future>
#define LOG_TAG "DEMO"
#include <hilog/log.h>
#include <nlohmann/json.hpp>
#include <qos/qos.h>
#include <string>

#define DEMO_LOGD(fmt, ...) OH_LOG_DEBUG(LOG_APP, fmt, ##__VA_ARGS__)
#define DEMO_LOGI(fmt, ...) OH_LOG_INFO(LOG_APP, fmt, ##__VA_ARGS__)
#define DEMO_LOGW(fmt, ...) OH_LOG_WARN(LOG_APP, fmt, ##__VA_ARGS__)
#define DEMO_LOGE(fmt, ...) OH_LOG_ERROR(LOG_APP, fmt, ##__VA_ARGS__)

using json = nlohmann::json;

/* Used to save the chat state. */
struct ChatContext {
public:
    ChatContext()
    {
        future = promise.get_future();
    }

    void Join()
    {
        assert(future.valid());
        std::string stopReasonStr = future.get();
        DEMO_LOGI("stopReasonStr=%s", stopReasonStr.c_str());
    }

    std::promise<std::string> promise;
    std::future<std::string> future;
    std::string responseContent;
    bool earlyAbort = false;
};

/* Callback function invoked when an inference result is received. */
void OnChatResponse(void *context, const char *response)
{
    ChatContext *chatContext = static_cast<ChatContext *>(context);
    if (chatContext->earlyAbort) {
        DEMO_LOGD("ignore response after early abort");
        return;
    }
    try {
        json responseJson = json::parse(response);
        std::string role = responseJson.at("message").at("role").get<std::string>();
        if (role != "assistant") {
            DEMO_LOGW("Discarding invalid response due to incorrect role");
            return;
        }
        chatContext->responseContent += responseJson.at("message").at("content").get<std::string>();
        json finishReasonJson = responseJson.at("finish_reason");
        if (!finishReasonJson.is_null()) {
            /* finish */
            std::string finishReasonStr = finishReasonJson.get<std::string>();
            chatContext->promise.set_value(finishReasonStr);
        } else if (chatContext->responseContent.find("\n") != std::string::npos) {
            /* customized stop */
            chatContext->promise.set_value("customized");
            chatContext->earlyAbort = true;
        } else {
            /* continue */
            ;
        }
    } catch (json::exception &e) {
        DEMO_LOGE("failed to parse response: %s", e.what());
    }
}

int Demo(void)
{
    DEMO_LOGI("Demo starts");
    json attrJson = {
        /* Model file location. Modify it based on the actual situation. */
        {"model", "/data/storage/el2/base/files/qwen2-awq"},
    };
    std::string attrStr = attrJson.dump(4);

    /* Create a session. */
    OH_QoS_GewuCreateSessionResult createResult = OH_QoS_GewuCreateSession(attrStr.c_str());
    if (createResult.error != OH_QOS_GEWU_OK) {
        DEMO_LOGE("failed to create session, error=%d", (int)createResult.error);
        return -1;
    }
    OH_QoS_GewuSession session = createResult.session;

    /* Create and submit a request. */
    ChatContext context;
    json requestJson = {
        {"messages", json::array({
            {{"role", "developer"}, {"content", "You are a helpful assistant"}},
            {{"role", "user"}, {"content", "What is LLM?"}},
        })},
        {"stream", true},
    };
    std::string requestStr = requestJson.dump(4);
    OH_QoS_GewuSubmitRequestResult submitResult = OH_QoS_GewuSubmitRequest(session, requestStr.c_str(),
                                                                           OnChatResponse, &context);
    if (submitResult.error != OH_QOS_GEWU_OK) {
        DEMO_LOGE("failed to submit request, error=%d", (int)submitResult.error);
        OH_QoS_GewuDestroySession(session);
        return -1;
    }
    OH_QoS_GewuRequest request = submitResult.request;
    context.Join();

    /* Abort the request in advance. */
    if (context.earlyAbort) {
        OH_QoS_GewuErrorCode error = OH_QoS_GewuAbortRequest(session, request);
        if (error != OH_QOS_GEWU_OK) {
            DEMO_LOGE("failed to abort request, error=%d", (int)error);
            OH_QoS_GewuDestroySession(session);
            return -1;
        }
    }

    /* Print the result. */
    DEMO_LOGI("response: %s", context.responseContent.c_str());

    /* Destroy the session. */
    OH_QoS_GewuErrorCode error = OH_QoS_GewuDestroySession(session);
    if (error != OH_QOS_GEWU_OK) {
        DEMO_LOGE("failed to destroy session, error=%d", (int)error);
        return -1;
    }
    return 0;
}

The demo code uses the third-party nlohmann/json library to simplify JSON parsing and construction. nlohmann/json is a modern C++ JSON library that provides an intuitive and concise way to process JSON data.

Its design concept is to make JSON operations feel as natural as using STL containers.

You can download the json.hpp file and place it in the include directory of the project. No additional library file needs to be linked.