11 lines
5.0 KiB
JSON
Raw Normal View History

2025-01-04 07:20:50 +00:00
{
"extension": ".h",
"source": "#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{\n const char *hostname = \"api.openai.com\";\n char *url = \"/v1/models\";\n char *result = http_get(hostname, url);\n return result;\n}\n\nbool openai_system(char * content){\n bool is_done = false;\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 if(result){\n is_done = true;\n \n free(result);\n }\n return is_done;\n}\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 // Get the first element of the \"choices\" array\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 // Extract the \"message\" object\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 // Print the \"message\" object\n // printf(\"Message object:\\n%s\\n\", json_object_to_json_string_ext(message_object, JSON_C_TO_STRING_PRETTY));\n message_add(\"assistant\",(char *)json_object_get_string(json_object_object_get(message_object, \"content\")));\n // Clean up\n free(data);\n free(result);\n result = strdup((char *)json_object_get_string(json_object_object_get(message_object, \"content\")));\n \n json_object_put(parsed_json);\n\n //printf(\"Parsed JSON:\\n%s\\n\", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));\n\n return result;\n}\n\n#endif ",
"review": "9\n\n### Bugs\n- The `http_get` and `http_post` functions are not defined here, they must be defined elsewhere or included appropriately.\n- Potential memory leak if `strstr` operations do not find the expected substrings, causing undefined behaviour.\n- `free(result)` is called in `openai_system` before the result is utilized, potentially causing a premature freeing of the resource.\n- Calling `free(data)` and `free(result)` should be conditional based on their allocation status.\n\n### Optimizations\n- Error handling could be improved by checking for NULL pointers more consistently throughout the functions.\n- The handling for the response body in `openai_chat` using multiple `strstr` calls is fragile; consider using a proper JSON parsing mechanism from the start rather than manually manipulating string pointers.\n- The use of `strdup` should be safeguarded with a check for available memory and to ensure `content` is valid.\n- Instead of multiple `strstr` calls, consider tokenizing or parsing JSON correctly with the right tools.\n- Use of constants for string sizes to ensure buffer overflow issues are managed.\n\n### Good Points\n- The code uses clear and understandable function names that describe their purpose neatly.\n- JSON parsing is attempted, showing an effort to manage API responses systematically.\n- The use of `json_object` to handle JSON responses is a good approach for robust data handling.\n\n### Summary\nThe code is written with clear intent and manages to achieve basic API functionality with JSON. However, it contains potential memory leaks and lacks comprehensive error handling, which can affect stability and performance. Improving string management and parsing will significantly enhance robustness.\n\n### Open source alternatives\n- **OpenAI GPT-3 API Wrapper in Python**: You can use libraries like `openai` in Python which offer a more user-friendly interface and comprehensive error handling with good community support.\n- **Curl**: For simple HTTP requests directly from CLI or integrated into different systems.\n- **libcurl**: A robust C library for making HTTP requests, including handling headers, user-agent strings, etc.",
"filename": "openai.h",
"path": "openai.h",
"directory": "",
"grade": 9,
"size": 2582,
"line_count": 84
}