Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the flexibility of the c streamer #1941

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions samples/c/text_generation/chat_sample_c.c
Original file line number Diff line number Diff line change
@@ -8,17 +8,16 @@
#include "openvino/genai/c/llm_pipeline.h"

#define MAX_PROMPT_LENGTH 64
#define MAX_OUTPUT_LENGTH 1024

#define CHECK_STATUS(return_status) \
if (return_status != OK) { \
fprintf(stderr, "[ERROR] return status %d, line %d\n", return_status, __LINE__); \
goto err; \
}

ov_genai_streamming_status_e print_callback(const char* args) {
if (args) {
fprintf(stdout, "%s", args);
ov_genai_streamming_status_e print_callback(const char* str, void* args) {
if (str) {
// If args is not null, it needs to be cast to its actual type.
fprintf(stdout, "%s", str);
fflush(stdout);
return OV_GENAI_STREAMMING_STATUS_RUNNING;
} else {
@@ -37,7 +36,8 @@ int main(int argc, char* argv[]) {

ov_genai_generation_config* config = NULL;
ov_genai_llm_pipeline* pipeline = NULL;
stream_callback streamer = &print_callback;
streamer_callback streamer;
streamer.callback_func = print_callback;
char prompt[MAX_PROMPT_LENGTH];

CHECK_STATUS(ov_genai_llm_pipeline_create(models_path, device, &pipeline));
14 changes: 11 additions & 3 deletions src/c/include/openvino/genai/c/llm_pipeline.h
Original file line number Diff line number Diff line change
@@ -96,9 +96,17 @@ typedef enum {
} ov_genai_streamming_status_e;

/**
* @brief Callback function for streaming output.
* @brief Structure for streamer callback functions with arguments.
*
* The callback function takes two parameters:
* - `const char* str`: A constant string extracted from the decoded result for processing
* - `void* args`: A pointer to additional arguments, allowing flexible data passing.
*/
typedef ov_genai_streamming_status_e(OPENVINO_C_API_CALLBACK* stream_callback)(const char*);
typedef struct {
ov_genai_streamming_status_e(
OPENVINO_C_API_CALLBACK* callback_func)(const char* str, void* args); //!< Pointer to the callback function
void* args; //!< Pointer to the arguments passed to the callback function
} streamer_callback;

/**
* @brief Generate results by ov_genai_llm_pipeline
@@ -114,7 +122,7 @@ typedef ov_genai_streamming_status_e(OPENVINO_C_API_CALLBACK* stream_callback)(c
OPENVINO_GENAI_C_EXPORTS ov_status_e ov_genai_llm_pipeline_generate(ov_genai_llm_pipeline* pipe,
const char* inputs,
const ov_genai_generation_config* config,
const stream_callback* streamer,
const streamer_callback* streamer,
ov_genai_decoded_results** results);
/**
* @brief Start chat with keeping history in kv cache.
4 changes: 2 additions & 2 deletions src/c/src/llm_pipeline.cpp
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ void ov_genai_llm_pipeline_free(ov_genai_llm_pipeline* pipe) {
ov_status_e ov_genai_llm_pipeline_generate(ov_genai_llm_pipeline* pipe,
const char* inputs,
const ov_genai_generation_config* config,
const stream_callback* streamer,
const streamer_callback* streamer,
ov_genai_decoded_results** results) {
if (!pipe || !(pipe->object) || !inputs || !(streamer || results)) {
return ov_status_e::INVALID_C_PARAM;
@@ -102,7 +102,7 @@ ov_status_e ov_genai_llm_pipeline_generate(ov_genai_llm_pipeline* pipe,
ov::genai::StringInputs input = {input_str};
if (streamer) {
auto callback = [streamer](std::string word) -> ov::genai::StreamingStatus {
return static_cast<ov::genai::StreamingStatus>((*streamer)(word.c_str()));
return static_cast<ov::genai::StreamingStatus>((streamer->callback_func)(word.c_str(), streamer->args));
};
*(_results->object) = (config && config->object)
? pipe->object->generate(input, *(config->object), callback)