11 lines
4.5 KiB
JSON
Raw Normal View History

2025-01-04 07:20:50 +00:00
{
"extension": ".h",
2025-01-04 07:44:34 +00:00
"source": "// Written by retoor@molodetz.nl\n\n// This code defines functionality for creating and managing JSON-based chat prompts \n// using a specific AI model configuration, providing easy integration with message handling \n// and HTTP communication for dynamic applications. \n\n// Non-standard imports: json-c library for handling JSON objects.\n\n\n// MIT License\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n\n#ifndef CALPACA_PROMPT_H\n#define CALPACA_PROMPT_H\n\n#include <json-c/json.h>\n#include \"messages.h\"\n#include \"http.h\"\n\nchar *prompt_model = \"gpt-4o-mini\";\nint prompt_max_tokens = 100;\ndouble prompt_temperature = 0.5;\n\njson_object *_prompt = NULL;\n\nvoid chat_free() {\n if (_prompt == NULL)\n return;\n\n json_object_put(_prompt);\n _prompt = NULL;\n}\n\nchar *chat_json(char *role, char *message) {\n chat_free();\n message_add(role, message);\n\n struct json_object *root_object = json_object_new_object();\n json_object_object_add(root_object, \"model\", json_object_new_string(prompt_model));\n json_object_object_add(root_object, \"messages\", message_list());\n json_object_object_add(root_object, \"max_tokens\", json_object_new_int(prompt_max_tokens));\n json_object_object_add(root_object, \"temperature\", json_object_new_double(prompt_temperature));\n\n return (char *)json_object_to_json_string_ext(root_object, JSON_C_TO_STRING_PRETTY);\n}\n\n#endif",
"review": "**Grade: 6**\n\n### Bugs\n- If `message_add()` or `message_list()` fail or return unexpected results, it might lead to incorrect JSON output without explicit error handling.\n- The return type of `json_object_to_json_string_ext()` is cast to `(char *)`, which may lead to memory management issues if the underlying JSON-C library does not provide a persistently allocated string.\n\n### Optimizations\n- Consider adding error handling for JSON creation and manipulation functions to ensure robustness.\n- Cache the result of `message_list()` if it doesn't change between calls, to improve performance.\n- Ensure the proper handling or casting of the string returned by `json_object_to_json_string_ext()` to prevent possible memory issues.\n- Improve modularity by separating concerns, such as moving JSON object creation into a dedicated function.\n\n### Good points\n- The code uses `json-c` effectively for JSON manipulation, facilitating easy JSON-based data handling.\n- The code is concise and focused on its purpose, with a straightforward structure.\n- Well-defined license and comments improve code understanding and legal clarity.\n\n### Summary\nThe provided code offers a simple yet functional way to create JSON strings that represent chat prompts by leveraging a JSON-handling library. While the implementation is straightforward and concise, adding error handling and optimizing internal function calls could enhance the stability and performance. Also, consider improving memory management based on how the JSON-C library handles string allocations. \n\n### Open source alternatives\n- Rasa (https://rasa.com): An open-source machine learning framework to automate text-and-voice-based assistants.\n- Botpress (https://botpress.com): An open-source conversational AI platform for developing chatbots.\n- Dialogflow (https://dialogflow.cloud.google.com/): Though primarily used as a Google service, the framework allows exporting and using APIs similarly for open-source initiatives.",
2025-01-04 07:20:50 +00:00
"filename": "chat.h",
"path": "chat.h",
"directory": "",
2025-01-04 07:44:34 +00:00
"grade": 6,
"size": 2399,
"line_count": 65
2025-01-04 07:20:50 +00:00
}