-
Notifications
You must be signed in to change notification settings - Fork 303
Update C chat samples with using ChatHistory class #3045
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
base: master
Are you sure you want to change the base?
Changes from all commits
5a41c21
c28ec23
f114d11
04089cc
bbfceac
0dc1226
ce474f7
6948a30
584652c
74fbb66
38db49d
9852ddc
ef4eb1d
49ab11b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,14 +6,74 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "openvino/genai/c/llm_pipeline.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "openvino/genai/c/chat_history.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "openvino/genai/c/json_container.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define MAX_PROMPT_LENGTH 64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define MAX_PROMPT_LENGTH 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define MAX_JSON_LENGTH 4096 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define CHECK_STATUS(return_status) \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (return_status != OK) { \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "[ERROR] return status %d, line %d\n", return_status, __LINE__); \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| goto err; \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define CHECK_CHAT_HISTORY_STATUS(return_status) \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (return_status != OV_GENAI_CHAT_HISTORY_OK) { \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "[ERROR] chat history status %d, line %d\n", return_status, __LINE__); \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| goto err; \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define CHECK_JSON_CONTAINER_STATUS(return_status) \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (return_status != OV_GENAI_JSON_CONTAINER_OK) { \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "[ERROR] json container status %d, line %d\n", return_status, __LINE__); \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| goto err; \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static void json_escape_string(const char* input, char* output, size_t output_size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t i = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t j = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (input[i] != '\0' && j < output_size - 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (input[i]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case '"': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (j < output_size - 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '"'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case '\\': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (j < output_size - 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case '\n': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (j < output_size - 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = 'n'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case '\r': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (j < output_size - 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = 'r'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case '\t': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (j < output_size - 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = '\\'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = 't'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j++] = input[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output[j] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+37
to
+74
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (input[i]) { | |
| case '"': | |
| if (j < output_size - 2) { | |
| output[j++] = '\\'; | |
| output[j++] = '"'; | |
| } | |
| break; | |
| case '\\': | |
| if (j < output_size - 2) { | |
| output[j++] = '\\'; | |
| output[j++] = '\\'; | |
| } | |
| break; | |
| case '\n': | |
| if (j < output_size - 2) { | |
| output[j++] = '\\'; | |
| output[j++] = 'n'; | |
| } | |
| break; | |
| case '\r': | |
| if (j < output_size - 2) { | |
| output[j++] = '\\'; | |
| output[j++] = 'r'; | |
| } | |
| break; | |
| case '\t': | |
| if (j < output_size - 2) { | |
| output[j++] = '\\'; | |
| output[j++] = 't'; | |
| } | |
| break; | |
| default: | |
| output[j++] = input[i]; | |
| break; | |
| } | |
| i++; | |
| } | |
| output[j] = '\0'; | |
| unsigned char c = (unsigned char)input[i]; | |
| if (c == '\"' || c == '\\') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = c; | |
| } else { | |
| break; | |
| } | |
| } else if (c == '\b') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = 'b'; | |
| } else { | |
| break; | |
| } | |
| } else if (c == '\f') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = 'f'; | |
| } else { | |
| break; | |
| } | |
| } else if (c == '\n') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = 'n'; | |
| } else { | |
| break; | |
| } | |
| } else if (c == '\r') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = 'r'; | |
| } else { | |
| break; | |
| } | |
| } else if (c == '\t') { | |
| if (j + 2 < output_size) { | |
| output[j++] = '\\'; | |
| output[j++] = 't'; | |
| } else { | |
| break; | |
| } | |
| } else if (c < 0x20) { | |
| // Control characters: use \u00XX | |
| if (j + 6 < output_size) { | |
| snprintf(&output[j], output_size - j, "\\u%04x", c); | |
| j += 6; | |
| } else { | |
| break; | |
| } | |
| } else { | |
| if (j + 1 < output_size) { | |
| output[j++] = c; | |
| } else { | |
| break; | |
| } | |
| } | |
| i++; | |
| } | |
| // Always null-terminate | |
| if (j < output_size) | |
| output[j] = '\0'; | |
| else if (output_size > 0) | |
| output[output_size - 1] = '\0'; |
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON escape function has incomplete escape handling. It's missing essential JSON escape sequences like '\b' (backspace) and '\f' (form feed). Additionally, control characters (0x00-0x1F) should be escaped as "\uXXXX" according to JSON specification. Consider using a more complete JSON escaping implementation or a JSON library.
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop continues when an empty line is encountered, but this will cause "question:" to not be printed after skipping. This means multiple empty lines will result in no prompt being shown. Consider printing the prompt after the continue statement, or restructure the logic to handle empty lines more gracefully.
| if (strlen(prompt) == 0) { | |
| if (strlen(prompt) == 0) { | |
| printf("question:\n"); |
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The snprintf call doesn't check for truncation. If the formatted string exceeds MAX_JSON_LENGTH, snprintf will truncate it and return a value >= MAX_JSON_LENGTH. This could result in malformed JSON being passed to ov_genai_json_container_create_from_json_string, leading to JSON parsing errors. Consider checking the return value of snprintf to ensure the formatted string fits within the buffer.
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential buffer overflow: snprintf is called with sizeof(message_json) (4096 bytes) but writes escaped_prompt which can be up to (MAX_PROMPT_LENGTH - 1) * 2 + 1 = 2047 bytes, plus the JSON template string {"role": "user", "content": ""} (32 bytes). If the user enters close to MAX_PROMPT_LENGTH characters with many escapable characters, the result could exceed the 4096 byte buffer. The buffer size calculation should account for the worst-case scenario of the escaped string plus JSON formatting overhead.
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation of output_size before using output_buffer. If ov_genai_decoded_results_get_string modifies output_size to be larger than sizeof(output_buffer), or if it fails, the buffer content may be invalid. The return status should be checked before proceeding to use output_buffer.
| if (output_size > sizeof(output_buffer)) { | |
| fprintf(stderr, "[ERROR] output_size (%zu) exceeds output_buffer size (%zu), line %d\n", output_size, sizeof(output_buffer), __LINE__); | |
| goto err; | |
| } |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The snprintf calls on lines 132-133 and 158-159 are vulnerable to buffer overflow. If the escaped string is too long, snprintf will truncate the JSON, resulting in malformed JSON (e.g., missing closing quote or brace). This could cause the subsequent ov_genai_json_container_create_from_json_string to fail. Consider checking the return value of snprintf and handling cases where the buffer is too small.
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,4 +53,4 @@ int main(int argc, char* argv[]) { | |
| free(output); | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } | ||
| } | ||
zhaohb marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,181 @@ | ||||||||||||||||||||||||||||||
| // Copyright (C) 2025 Intel Corporation | ||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| // This is a C wrapper for ov::genai::ChatHistory class. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #pragma once | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #include "visibility.h" | ||||||||||||||||||||||||||||||
| #include <stddef.h> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Forward declaration for JsonContainer | ||||||||||||||||||||||||||||||
| typedef struct ov_genai_json_container_opaque ov_genai_json_container; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * @struct ov_genai_chat_history | ||||||||||||||||||||||||||||||
| * @brief Opaque type for ChatHistory | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| typedef struct ov_genai_chat_history_opaque ov_genai_chat_history; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * @brief Status codes for chat history operations | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| typedef enum { | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_OK = 0, | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_INVALID_PARAM = -1, | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_OUT_OF_BOUNDS = -2, | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_EMPTY = -3, | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_INVALID_JSON = -4, | ||||||||||||||||||||||||||||||
| OV_GENAI_CHAT_HISTORY_ERROR = -5 | ||||||||||||||||||||||||||||||
| } ov_genai_chat_history_status_e; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * @brief Create a new empty ChatHistory instance. | ||||||||||||||||||||||||||||||
| * @param history A pointer to the newly created ov_genai_chat_history. | ||||||||||||||||||||||||||||||
| * @return ov_genai_chat_history_status_e A status code, return OK(0) if successful. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| OPENVINO_GENAI_C_EXPORTS ov_genai_chat_history_status_e ov_genai_chat_history_create(ov_genai_chat_history** history); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * @brief Create a ChatHistory instance from a JsonContainer (array). | ||||||||||||||||||||||||||||||
| * @param history A pointer to the newly created ov_genai_chat_history. | ||||||||||||||||||||||||||||||
| * @param messages A JsonContainer containing an array of message objects. | ||||||||||||||||||||||||||||||
| * @return ov_genai_chat_history_status_e A status code, return OK(0) if successful. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| OPENVINO_GENAI_C_EXPORTS ov_genai_chat_history_status_e ov_genai_chat_history_create_from_json_container( | ||||||||||||||||||||||||||||||
| ov_genai_chat_history** history, | ||||||||||||||||||||||||||||||
| const ov_genai_json_container* messages | ||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+47
|
||||||||||||||||||||||||||||||
| * @param history A pointer to the newly created ov_genai_chat_history. | |
| * @param messages A JsonContainer containing an array of message objects. | |
| * @return ov_genai_chat_history_status_e A status code, return OK(0) if successful. | |
| */ | |
| OPENVINO_GENAI_C_EXPORTS ov_genai_chat_history_status_e ov_genai_chat_history_create_from_json_container( | |
| ov_genai_chat_history** history, | |
| const ov_genai_json_container* messages | |
| * @param messages A JsonContainer containing an array of message objects. | |
| * @param history A pointer to the newly created ov_genai_chat_history. | |
| * @return ov_genai_chat_history_status_e A status code, return OK(0) if successful. | |
| */ | |
| OPENVINO_GENAI_C_EXPORTS ov_genai_chat_history_status_e ov_genai_chat_history_create_from_json_container( | |
| const ov_genai_json_container* messages, | |
| ov_genai_chat_history** history |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address Copilot's proposal - align order of input/output params
Uh oh!
There was an error while loading. Please reload this page.