2025-01-04 08:40:31 +01:00
// Written by retoor@molodetz.nl
// 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.
// Imports the "http" library for handling HTTP requests and the "chat" library for JSON handling related to chat content.
// 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.
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
# ifndef CALPACA_OPENAI_H
# define CALPACA_OPENAI_H
# include "http.h"
# include "chat.h"
# include <string.h>
# include <stdbool.h>
2025-01-04 08:40:31 +01:00
char * openai_get_models ( ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
const char * hostname = " api.openai.com " ;
char * url = " /v1/models " ;
2025-01-04 08:40:31 +01:00
return http_get ( hostname , url ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
}
2025-01-04 08:40:31 +01:00
bool openai_system ( char * content ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
const char * hostname = " api.openai.com " ;
char * url = " /v1/chat/completions " ;
char * data = chat_json ( " system " , content ) ;
char * result = http_post ( hostname , url , data ) ;
2025-01-04 08:40:31 +01:00
bool is_done = result ! = NULL ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
2025-01-04 08:40:31 +01:00
free ( result ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
return is_done ;
}
2025-01-04 08:40:31 +01:00
char * openai_chat ( char * role , char * content ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
const char * hostname = " api.openai.com " ;
char * url = " /v1/chat/completions " ;
char * data = chat_json ( role , content ) ;
char * result = http_post ( hostname , url , data ) ;
2025-01-04 08:40:31 +01:00
char * body = strstr ( result , " \r \n \r \n " ) + 4 ;
body = strstr ( body , " \r \n " ) ;
body = strstr ( body , " \r \n " ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
* ( body - 5 ) = 0 ;
struct json_object * parsed_json = json_tokener_parse ( body ) ;
2025-01-04 08:40:31 +01:00
if ( ! parsed_json ) {
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
fprintf ( stderr , " Failed to parse JSON. \n " ) ;
return NULL ;
}
2025-01-04 08:40:31 +01:00
struct json_object * choices_array ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
if ( ! json_object_object_get_ex ( parsed_json , " choices " , & choices_array ) ) {
fprintf ( stderr , " Failed to get 'choices' array. \n " ) ;
json_object_put ( parsed_json ) ;
return NULL ;
}
struct json_object * first_choice = json_object_array_get_idx ( choices_array , 0 ) ;
if ( ! first_choice ) {
fprintf ( stderr , " Failed to get the first element of 'choices'. \n " ) ;
json_object_put ( parsed_json ) ;
return NULL ;
}
struct json_object * message_object ;
if ( ! json_object_object_get_ex ( first_choice , " message " , & message_object ) ) {
fprintf ( stderr , " Failed to get 'message' object. \n " ) ;
json_object_put ( parsed_json ) ;
return NULL ;
}
2025-01-04 08:40:31 +01:00
char * content_str = ( char * ) json_object_get_string ( json_object_object_get ( message_object , " content " ) ) ;
message_add ( " assistant " , content_str ) ;
free ( data ) ;
free ( result ) ;
char * final_result = strdup ( content_str ) ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
json_object_put ( parsed_json ) ;
2025-01-04 08:40:31 +01:00
return final_result ;
chore: scaffold project with gitignore, makefile, cgi scripts, and core c headers
Add initial project structure including .gitignore for build artifacts,
Makefile with gcc build and run targets, README placeholder, cgi-bin
directory with gpt.py and gpt_template.html, and core C headers for
chat, http, line input, markdown rendering, and messages.
2025-01-04 06:00:03 +01:00
}
2025-01-04 08:35:39 +01:00
# endif