"source":"// Written by retoor@molodetz.nl\n\n// This code provides functions to interact with OpenAI's APIs. It includes functionalities for fetching available models, system interactions, and engaging in chat-based conversations using the OpenAI API.\n\n\n// Imports the \"http\" library for handling HTTP requests and the \"chat\" library for JSON handling related to chat content.\n\n\n// MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, to deal in the Software without restriction, including the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\n#ifndef CALPACA_OPENAI_H\n#define CALPACA_OPENAI_H\n#include \"http.h\"\n#include \"chat.h\"\n#include <string.h>\n#include <stdbool.h>\n\nchar *openai_get_models() {\n const char *hostname = \"api.openai.com\";\n char *url = \"/v1/models\";\n return http_get(hostname, url);\n}\n\nbool openai_system(char *content) {\n const char *hostname = \"api.openai.com\";\n char *url = \"/v1/chat/completions\";\n char *data = chat_json(\"system\", content);\n char *result = http_post(hostname, url, data);\n bool is_done = result != NULL;\n \n free(result);\n return is_done;\n}\n\nchar *openai_chat(char *role, char *content) {\n const char *hostname = \"api.openai.com\";\n char *url = \"/v1/chat/completions\";\n char *data = chat_json(role, content);\n char *result = http_post(hostname, url, data);\n char *body = strstr(result, \"\\r\\n\\r\\n\") + 4;\n body = strstr(body, \"\\r\\n\");\n body = strstr(body, \"\\r\\n\");\n *(body - 5) = 0;\n struct json_object *parsed_json = json_tokener_parse(body);\n if (!parsed_json) {\n fprintf(stderr, \"Failed to parse JSON.\\n\");\n return NULL;\n }\n\n struct json_object *choices_array;\n if (!json_object_object_get_ex(parsed_json, \"choices\", &choices_array)) {\n fprintf(stderr, \"Failed to get 'choices' array.\\n\");\n json_object_put(parsed_json);\n return NULL;\n }\n\n struct json_object *first_choice = json_object_array_get_idx(choices_array, 0);\n if (!first_choice) {\n fprintf(stderr, \"Failed to get the first element of 'choices'.\\n\");\n json_object_put(parsed_json);\n return NULL;\n }\n\n struct json_object *message_object;\n if (!json_object_object_get_ex(first_choice, \"message\", &message_object)) {\n fprintf(stderr, \"Failed to get 'message' object.\\n\");\n json_object_put(parsed_json);\n return NULL;\n }\n\n char *content_str = (char *)json_object_get_string(json_object_object_get(message_object, \"content\"));\n message_add(\"assistant\", content_str);\n free(data);\n free(result);\n char *final_result = strdup(content_str);\n \n json_object_put(parsed_json);\n\n return final_result;\n}\n\n#endif",
"review":"# 7\n\n## Bugs\n- **Memory Management**: Potential memory leaks due to missing `free()` calls for dynamically allocated strings (`data`, `result`) in `openai_chat`.\n- **Buffer Overflows**: Pointer arithmetic in `openai_chat` might lead to undefined behavior if there's an unexpected response format.\n- **Invalid Access**: In `openai_chat`, the manipulation to null-terminate the response string (`*(body - 5) = 0;`) depends on assumptions that might not always hold, potentially causing segmentation faults.\n\n## Optimizations\n- **Memory Management**: Ensure all dynamically allocated memory is properly freed to prevent memory leaks.\n- **Error Handling**: Improve handling for a wider range of possible errors in network interactions and JSON parsing.\n- **Reuse Connections**: Use persistent connections for HTTP requests if supported by the library to reduce latency.\n- **String Handling**: Use safer string manipulation methods to avoid buffer overflows and segmentation faults.\n- **Modularization**: Consider splitting functionalities into smaller functions to improve code readability and maintainability.\n\n## Good points\n- **Library Usage**: Correctly utilizes external libraries to manage HTTP requests and JSON parsing.\n- **Code Organization**: The separation of functionalities into different functions is clear and well-structured.\n- **License Clarity**: The inclusion of the MIT license ensures clarity on usage permissions.\n\n## Summary\nOverall, this code provides a straightforward implementation for interacting with OpenAI's APIs using basic HTTP request and JSON handling mechanisms. However, there are several areas, especially concerning memory management and error handling, that require improvement for increased reliability and efficiency. With optimizations in these areas, the code could be robust enough for production environments. \n\n## Open source alternatives\n- **Langchain**: A framework for developing applications with language models which can interface with OpenAI.\n- **GPT-3 Python Client**: A Python package for interacting with OpenAI's APIs.\n- **openai-cpp**: A C++ wrapper for OpenAI's API which may cover similar functionalities with additional abstractions for ease of use.",