summaryrefslogtreecommitdiff
path: root/src/ai_providers
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2025-10-05 15:41:23 +0200
committerAldrik Ramaekers <aldrikboy@gmail.com>2025-10-05 15:41:23 +0200
commit8aa66a6c6c0d8984b7d2668c03bad5a3b29e3a33 (patch)
treed7d985151b5bcd6687aead3547bdfbdb0600a8c6 /src/ai_providers
parentb278d242d03ba614779243ec9e9495fc95abea3d (diff)
memops wrapper, remove unused includes
Diffstat (limited to 'src/ai_providers')
-rw-r--r--src/ai_providers/DeepSeek.cpp17
-rw-r--r--src/ai_providers/openAI.cpp23
2 files changed, 15 insertions, 25 deletions
diff --git a/src/ai_providers/DeepSeek.cpp b/src/ai_providers/DeepSeek.cpp
index 4dd4e0a..a1857e9 100644
--- a/src/ai_providers/DeepSeek.cpp
+++ b/src/ai_providers/DeepSeek.cpp
@@ -14,15 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#define _CRT_SECURE_NO_WARNINGS
-
-#include <fstream>
-#include <iostream>
-#include <string>
-
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
#include "strops.hpp"
+#include "memops.hpp"
#include "logger.hpp"
#include "importer.hpp"
@@ -41,7 +36,7 @@ static bool _DeepSeek_query_with_file(char* query, size_t query_length, char* fi
//cli.enable_server_certificate_verification(false);
//char* query_escaped = strops::prep_str_for_json(query, query_length);
- //free(query); // TODO why??
+ //memops::unalloc(query); // TODO why??
size_t file_size = strlen(query_buffer);
sprintf(query_buffer + file_size, "%s", query);
@@ -49,7 +44,7 @@ static bool _DeepSeek_query_with_file(char* query, size_t query_length, char* fi
char* query_escaped = strops::prep_str_for_json(query_buffer, strlen(query_buffer));
size_t body_size = file_size + QUERY_BUFFER_SIZE;
- char* body = (char*)malloc(body_size);
+ char* body = (char*)memops::alloc(body_size);
strops::format(body, body_size,
"{\"model\":\"deepseek-reasoner\", \"messages\": [ { \"role\": \"user\", \"content\": \"%s\" } ] }", query_escaped);
@@ -59,7 +54,7 @@ static bool _DeepSeek_query_with_file(char* query, size_t query_length, char* fi
headers.insert(std::make_pair("Accept", "application/json"));
httplib::Result res = cli.Post("/chat/completions", headers, body, "application/json");
- free(body);
+ memops::unalloc(body);
if (!res || res->status != 200) {
logger::error("ERROR Failed to query API.");
@@ -68,7 +63,7 @@ static bool _DeepSeek_query_with_file(char* query, size_t query_length, char* fi
}
char* response_body = (char*)res->body.c_str();
- *response = (char*)malloc(100000);
+ *response = (char*)memops::alloc(100000);
memset(*response, 0, 100000);
strncpy(*response, response_body, 100000);
@@ -95,7 +90,7 @@ static bool _DeepSeek_upload_file(char* file_path, char* file_id, size_t file_id
fseek(orig_file, 0, SEEK_SET);
size_t buffer_size = sz + QUERY_BUFFER_SIZE;
- char* file_content_buffer = (char*)malloc(buffer_size);
+ char* file_content_buffer = (char*)memops::alloc(buffer_size);
memset(file_content_buffer, 0, buffer_size);
query_buffer = file_content_buffer;
diff --git a/src/ai_providers/openAI.cpp b/src/ai_providers/openAI.cpp
index c4526ef..b55f191 100644
--- a/src/ai_providers/openAI.cpp
+++ b/src/ai_providers/openAI.cpp
@@ -14,14 +14,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#define _CRT_SECURE_NO_WARNINGS
-
-#include <fstream>
-#include <iostream>
-#include <string>
-
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.h"
+#include "memops.hpp"
#include "strops.hpp"
#include "logger.hpp"
#include "importer.hpp"
@@ -35,10 +30,10 @@ static bool _openAI_query_with_file(char* query, size_t query_length, char* file
//cli.enable_server_certificate_verification(false);
char* query_escaped = strops::prep_str_for_json(query, query_length);
- free(query);
+ memops::unalloc(query);
size_t body_size = query_length + 200;
- char* body = (char*)malloc(body_size);
+ char* body = (char*)memops::alloc(body_size);
strops::format(body, body_size,
"{\"model\":\"gpt-5-nano\", \"input\": [ { \"role\": \"user\", \"content\": [ { \"type\": \"input_file\", \"file_id\": \"%s\" }, "
"{ \"type\": \"input_text\", \"text\": \"%s\" } ] } ] }", file_id, query_escaped);
@@ -47,7 +42,7 @@ static bool _openAI_query_with_file(char* query, size_t query_length, char* file
headers.insert(std::make_pair("Authorization", std::string("Bearer ") + api_key));
httplib::Result res = cli.Post("/v1/responses", headers, body, "application/json");
- free(body);
+ memops::unalloc(body);
if (!res || res->status != 200) {
logger::error("ERROR Failed to query API.");
@@ -56,14 +51,14 @@ static bool _openAI_query_with_file(char* query, size_t query_length, char* file
}
char* response_body = (char*)res->body.c_str();
- *response = (char*)malloc(100000);
+ *response = (char*)memops::alloc(100000);
memset(*response, 0, 100000);
strncpy(*response, response_body, 100000);
strops::get_json_value(*response, "text", *response, 100000);
*response = strops::unprep_str_from_json(*response);
#else
- *response = (char*)malloc(100000);
+ *response = (char*)memops::alloc(100000);
memset(*response, 0, 100000);
strops::copy(*response, "<Invoice xmlns=\"urn:oasis:names:specification:ubl:schema:xsd:Invoice-2\" xmlns:cac=\"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2\" xmlns:cbc=\"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2\"> <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID> <cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID> <cbc:ID>492043632</cbc:ID> <cbc:IssueDate>2024-09-01</cbc:IssueDate> <cbc:DueDate>2024-09-01</cbc:DueDate> <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode> <cbc:DocumentCurrencyCode>USD</cbc:DocumentCurrencyCode> <cac:DespatchDocumentReference> <cbc:ID>Final invoice</cbc:ID> </cac:DespatchDocumentReference> <cac:AdditionalDocumentReference> <cbc:ID></cbc:ID> <cbc:DocumentDescription></cbc:DocumentDescription> </cac:AdditionalDocumentReference> <cac:OrderReference> <cbc:ID></cbc:ID> </cac:OrderReference> <cac:ProjectReference> <cbc:ID>do:team:67840ecb-44e2-472e-bc45-801bd4e1f1fe</cbc:ID> </cac:ProjectReference> <cbc:AccountingCost></cbc:AccountingCost> <cac:AccountingSupplierParty> <cac:Party> <cbc:EndpointID schemeID=""></cbc:EndpointID> <cac:PartyIdentification> <cbc:ID schemeID=\"ZZZ\"></cbc:ID> </cac:PartyIdentification> <cac:PartyName> <cbc:Name>DigitalOcean LLC</cbc:Name> </cac:PartyName> <cac:PostalAddress> <cbc:StreetName>101 Avenue of the Americas</cbc:StreetName> <cbc:AdditionalStreetName>2nd Floor</cbc:AdditionalStreetName> <cbc:CityName>New York</cbc:CityName> <cbc:PostalZone>10013</cbc:PostalZone> <cbc:CountrySubentity>NY</cbc:CountrySubentity> <cac:Country> <cbc:IdentificationCode>US</cbc:IdentificationCode> </cac:Country> </cac:PostalAddress> <cac:PartyTaxScheme> <cbc:CompanyID>EU528002224</cbc:CompanyID> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:PartyTaxScheme> <cac:PartyLegalEntity> <cbc:RegistrationName>DigitalOcean LLC</cbc:RegistrationName> </cac:PartyLegalEntity> <cac:Contact> <cbc:Name></cbc:Name> <cbc:Telephone></cbc:Telephone> <cbc:ElectronicMail></cbc:ElectronicMail> </cac:Contact> </cac:Party> </cac:AccountingSupplierParty> <cac:AccountingCustomerParty> <cac:Party> <cbc:EndpointID schemeID=""></cbc:EndpointID> <cac:PartyIdentification> <cbc:ID schemeID=\"ZZZ\"></cbc:ID> </cac:PartyIdentification> <cac:PartyName> <cbc:Name>My Team</cbc:Name> </cac:PartyName> <cac:PostalAddress> <cbc:StreetName>Keerderstraat 81</cbc:StreetName> <cbc:AdditionalStreetName></cbc:AdditionalStreetName> <cbc:CityName>Maastricht</cbc:CityName> <cbc:PostalZone>6226 XW</cbc:PostalZone> <cbc:CountrySubentity>LI</cbc:CountrySubentity> <cac:Country> <cbc:IdentificationCode>NL</cbc:IdentificationCode> </cac:Country> </cac:PostalAddress> <cac:PartyTaxScheme> <cbc:CompanyID></cbc:CompanyID> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:PartyTaxScheme> <cac:PartyLegalEntity> <cbc:RegistrationName></cbc:RegistrationName> </cac:PartyLegalEntity> <cac:Contact> <cbc:Name></cbc:Name> <cbc:Telephone></cbc:Telephone> <cbc:ElectronicMail>aldrikboy@gmail.com</cbc:ElectronicMail> </cac:Contact> </cac:Party> </cac:AccountingCustomerParty> <cac:Delivery> <cbc:ActualDeliveryDate></cbc:ActualDeliveryDate> <cac:DeliveryLocation> <cac:Address> <cbc:StreetName></cbc:StreetName> <cbc:AdditionalStreetName></cbc:AdditionalStreetName> <cbc:CityName></cbc:CityName> <cbc:PostalZone></cbc:PostalZone> <cbc:CountrySubentity></cbc:CountrySubentity> <cac:Country> <cbc:IdentificationCode></cbc:IdentificationCode> </cac:Country> </cac:Address> </cac:DeliveryLocation> <cac:DeliveryParty> <cac:PartyName> <cbc:Name></cbc:Name> </cac:PartyName> </cac:DeliveryParty> </cac:Delivery> <cac:PaymentMeans> <cbc:PaymentMeansCode></cbc:PaymentMeansCode> <cbc:PaymentID>492043632</cbc:PaymentID> <cac:PayeeFinancialAccount> <cbc:ID></cbc:ID> <cbc:Name></cbc:Name> <cac:FinancialInstitutionBranch> <cac:FinancialInstitution> <cbc:ID></cbc:ID> </cac:FinancialInstitution> </cac:FinancialInstitutionBranch> </cac:PayeeFinancialAccount> <cac:PayerFinancialAccount> <cbc:ID></cbc:ID> </cac:PayerFinancialAccount> </cac:PaymentMeans> <cac:TaxTotal> <cbc:TaxAmount currencyID=\"USD\">3.49</cbc:TaxAmount> <cac:TaxSubtotal> <cbc:TaxableAmount currencyID=\"USD\">15.60</cbc:TaxableAmount> <cbc:TaxAmount currencyID=\"USD\">3.28</cbc:TaxAmount> <cac:TaxCategory> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:TaxCategory> </cac:TaxSubtotal> <cac:TaxSubtotal> <cbc:TaxableAmount currencyID=\"USD\">1.00</cbc:TaxableAmount> <cbc:TaxAmount currencyID=\"USD\">0.21</cbc:TaxAmount> <cac:TaxCategory> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:TaxCategory> </cac:TaxSubtotal> </cac:TaxTotal> <cac:LegalMonetaryTotal> <cbc:LineExtensionAmount currencyID=\"USD\">16.60</cbc:LineExtensionAmount> <cbc:TaxExclusiveAmount currencyID=\"USD\">16.60</cbc:TaxExclusiveAmount> <cbc:TaxInclusiveAmount currencyID=\"USD\">20.09</cbc:TaxInclusiveAmount> <cbc:PayableAmount currencyID=\"USD\">20.09</cbc:PayableAmount> </cac:LegalMonetaryTotal> <cac:InvoiceLine> <cbc:ID>1</cbc:ID> <cbc:InvoicedQuantity unitCode=""></cbc:InvoicedQuantity> <cbc:LineExtensionAmount currencyID=\"USD\">16.60</cbc:LineExtensionAmount> <cac:AllowanceCharge> <cbc:ChargeIndicator>false</cbc:ChargeIndicator> <cbc:AllowanceChargeReason>Discount</cbc:AllowanceChargeReason> <cbc:MultiplierFactorNumeric></cbc:MultiplierFactorNumeric> <cbc:Amount currencyID=\"USD\"></cbc:Amount> <cbc:BaseAmount currencyID=\"USD\"></cbc:BaseAmount> </cac:AllowanceCharge> <cac:Item> <cbc:Name>Product Usage Charges</cbc:Name> <cac:AdditionalItemProperty> <cbc:Name>Internal Tax Rate ID</cbc:Name> <cbc:Value></cbc:Value> </cac:AdditionalItemProperty> <cac:ClassifiedTaxCategory> <cbc:ID></cbc:ID> <cbc:Percent></cbc:Percent> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:ClassifiedTaxCategory> </cac:Item> <cac:Price> <cbc:PriceAmount currencyID=\"USD\"></cbc:PriceAmount> </cac:Price> </cac:InvoiceLine></Invoice>", 100000);
#endif
@@ -108,7 +103,7 @@ static bool _openAI_upload_file(char* file_path, char* file_id, size_t file_id_l
size_t part_size = 64000000; // 64mb
logger::info("Created upload %s with part size %zu.", upload_id, part_size);
- char *buffer = (char*)malloc(part_size);
+ char *buffer = (char*)memops::alloc(part_size);
char completion_body[1048];
strops::format(completion_body, sizeof(completion_body), "{\"part_ids\": [");
@@ -135,7 +130,7 @@ static bool _openAI_upload_file(char* file_path, char* file_id, size_t file_id_l
if (!part_res || part_res->status != 200) {
logger::error("Failed to upload part %d.", part_number);
logger::error(part_res->body.c_str());
- free(buffer);
+ memops::unalloc(buffer);
fclose(orig_file);
return 0;
}
@@ -152,7 +147,7 @@ static bool _openAI_upload_file(char* file_path, char* file_id, size_t file_id_l
strops::format(completion_body+strlen(completion_body), sizeof(completion_body)-strlen(completion_body), "]}");
- free(buffer);
+ memops::unalloc(buffer);
fclose(orig_file);
// ---------- Step 3: Complete upload ----------