working request to openai

This commit is contained in:
Santiago Lema 2025-05-07 02:33:01 -03:00
parent 720dcbb381
commit 47b1cbaf04
4 changed files with 25693 additions and 6 deletions

BIN
DumBer

Binary file not shown.

View file

@ -5,6 +5,8 @@
#include "MainWindow.h"
#include <cstring>
#include <Url.h>
#include <Application.h>
@ -15,9 +17,21 @@
#include <MenuBar.h>
#include <ScrollView.h>
#include <StringView.h>
#include <MimeType.h>
#include <memory.h>
#include <View.h>
#include <Path.h>
#include <FindDirectory.h>
#include <File.h>
#include <String.h>
#include <iostream>
#include <algorithm>
#include "json.hpp"
// #include <iostream>
#include <cstdio>
@ -45,6 +59,7 @@ MainWindow::MainWindow()
_inputField = new BTextControl("", "What is the matrix ?",
new BMessage(kQuestionChanged));
_progress = new BStatusBar("prog");
_progress->SetMaxValue(100);
_progress->SetTo(0);
@ -72,6 +87,10 @@ MainWindow::MainWindow()
_inputField->SetExplicitMinSize(BSize(B_SIZE_UNSET, askH));
_inputField->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, askH * 6));
_apiKey = ReadOpenAIKey();
printf("key is: %s", _apiKey.String());
BButton *sendButton =
new BButton("send", B_TRANSLATE("Send"), new BMessage(kSendPrompt),
B_WILL_DRAW | B_NAVIGABLE);
@ -79,11 +98,10 @@ MainWindow::MainWindow()
_answerView = new BTextView("answer", B_WILL_DRAW | B_FOLLOW_ALL);
_answerView->MakeEditable(false); // Disable editing
_answerView->MakeSelectable(true); // Enable text selection
//_answerView->SetWordWrap(true);
_answerView->SetWordWrap(true);
BScrollView *scrollView =
new BScrollView("scroll_view", _answerView, 0, 0, false,
true); // horizontal and vertical scrollbars
new BScrollView("scroll_view", _answerView, B_FOLLOW_ALL | B_WILL_DRAW ,0 , false, true); // horizontal and vertical scrollbars
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
@ -241,8 +259,60 @@ void MainWindow::sendQuery() {
_progress->SetMaxValue(100);
_progress->SetTo(0);
auto url = BUrl("https://www.lema.org/");
auto url = BUrl("https://api.openai.com/v1/chat/completions");
BHttpRequest request = BHttpRequest(url);
request.SetMethod(BHttpMethod::Post);
// if the API key file contains a new line bhttpfields will crash with invalid content
// .end() requires include algorithm
std::string key = _apiKey.String();
key.erase(std::remove(key.begin(), key.end(), '\n'), key.end());
key.erase(std::remove(key.begin(), key.end(), '\r'), key.end());
std::string bearer = std::string("Bearer ")+std::string(key);
BHttpFields fields = BHttpFields();
fields.AddField("Authorization", bearer );
// fields.AddField("Content-Type", "application/json"); //NO, this will crash, we set it in request
request.SetFields(fields);
nlohmann::json bodyJson = {
{ "model", "gpt-3.5-turbo" },
{ "messages", {
{
{ "role", "user" },
{ "content", "What is the capital of France?" }
}
}}
};
std::string body = bodyJson.dump();
BString mime = BString("application/json");
//request.SetRequestBody(body.c_str(),mime);
// 2. Wrap it in BMemoryIO
auto memoryIO = std::make_unique<BMemoryIO>(
body.c_str(), // pointer to data
body.size() // size of data
);
// 3. Set the request body with mime type
request.SetRequestBody(std::move(memoryIO), "application/json", body.size());
//curl https://api.openai.com/v1/chat/completions \
// -H "Authorization: Bearer YOUR_API_KEY" \
// -H "Content-Type: application/json" \
// -d '{
// "model": "gpt-3.5-turbo",
// "messages": [{"role": "user", "content": "What is the capital of France?"}]
// }'
printf("Sending Prompt to server: %s\n", url.UrlString().String());
_lastResult = _sharedSession.Execute(std::move(request), nullptr, this);
@ -288,3 +358,35 @@ BMenuBar *MainWindow::_BuildMenu() {
return menuBar;
}
BString MainWindow::ReadOpenAIKey() {
// /boot/home/config/openai_key
// or ~/.config/openai_key on linux
BPath configPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK)
return "error: couldn't find config directory";
configPath.Append("openai_key");
BFile file(configPath.Path(), B_READ_ONLY);
printf("full path:%s\n",configPath.Path());
if (file.InitCheck() != B_OK)
return "error: couldn't open key file ";
off_t size;
file.GetSize(&size);
char* buffer = new char[size + 1];
file.Read(buffer, size);
buffer[size] = '\0'; // null-terminate
BString result(buffer);
delete[] buffer;
return result;
}

View file

@ -10,6 +10,7 @@
#include <TextView.h>
#include <StatusBar.h>
#include <Window.h>
#include <String.h>
@ -22,6 +23,7 @@
#include "/boot/system/develop/headers/private/netservices2/HttpSession.h"
#include "/boot/system/develop/headers/private/netservices2/HttpRequest.h"
#include "/boot/system/develop/headers/private/netservices2/HttpResult.h"
#include "/boot/system/develop/headers/private/netservices2/HttpFields.h"
#include "/boot/system/develop/headers/private/netservices2/ErrorsExt.h"
//From private headers !
@ -43,9 +45,12 @@ public:
virtual void MessageReceived(BMessage *msg);
void sendQuery();
BString ReadOpenAIKey();
private:
BString _apiKey;
BHttpSession _sharedSession = BHttpSession ();
std::optional<BHttpResult> _lastResult;

25580
json.hpp Normal file

File diff suppressed because it is too large Load diff