70 lines
2.4 KiB
C
Raw Normal View History

2025-01-04 07:40:31 +00:00
// Written by retoor@molodetz.nl
// This code defines functionality for creating and managing JSON-based chat prompts
// using a specific AI model configuration, providing easy integration with message handling
// and HTTP communication for dynamic applications.
// Non-standard imports: json-c library for handling JSON objects.
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation 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.
2025-01-05 21:59:51 +00:00
#ifndef R_PROMPT_H
#define R_PROMPT_H
2025-01-04 07:40:31 +00:00
2025-01-04 05:00:03 +00:00
#include <json-c/json.h>
#include "messages.h"
#include "http.h"
2025-01-04 07:40:31 +00:00
2025-01-05 21:59:51 +00:00
#ifndef FREE_VERSION
2025-01-04 07:40:31 +00:00
char *prompt_model = "gpt-4o-mini";
2025-01-05 21:59:51 +00:00
#else
char *prompt_model = "gpt-3.5-turbo";
#endif
int prompt_max_tokens = 2048;
2025-01-04 05:00:03 +00:00
double prompt_temperature = 0.5;
2025-01-04 07:40:31 +00:00
json_object *_prompt = NULL;
2025-01-04 05:00:03 +00:00
2025-01-04 07:40:31 +00:00
void chat_free() {
if (_prompt == NULL)
2025-01-04 05:00:03 +00:00
return;
2025-01-04 07:40:31 +00:00
2025-01-04 05:00:03 +00:00
json_object_put(_prompt);
_prompt = NULL;
}
2025-01-04 07:40:31 +00:00
char *chat_json(char *role, char *message) {
2025-01-04 05:00:03 +00:00
chat_free();
2025-01-04 07:40:31 +00:00
message_add(role, message);
2025-01-04 05:00:03 +00:00
struct json_object *root_object = json_object_new_object();
2025-01-04 07:40:31 +00:00
json_object_object_add(root_object, "model", json_object_new_string(prompt_model));
2025-01-04 05:00:03 +00:00
json_object_object_add(root_object, "messages", message_list());
json_object_object_add(root_object, "max_tokens", json_object_new_int(prompt_max_tokens));
2025-01-04 07:40:31 +00:00
json_object_object_add(root_object, "temperature", json_object_new_double(prompt_temperature));
return (char *)json_object_to_json_string_ext(root_object, JSON_C_TO_STRING_PRETTY);
2025-01-04 05:00:03 +00:00
}
2025-01-05 21:59:51 +00:00
#endif