clean question parsin in json
This commit is contained in:
parent
47b1cbaf04
commit
efbe679a6b
8 changed files with 328 additions and 317 deletions
138
MainWindow.cpp
138
MainWindow.cpp
|
@ -15,27 +15,25 @@
|
|||
#include <LayoutBuilder.h>
|
||||
#include <Menu.h>
|
||||
#include <MenuBar.h>
|
||||
#include <MimeType.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 <FindDirectory.h>
|
||||
#include <Path.h>
|
||||
#include <String.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
// #include <iostream>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
#include "include/json.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
using namespace BPrivate::Network;
|
||||
|
||||
#undef B_TRANSLATION_CONTEXT
|
||||
|
@ -59,7 +57,6 @@ MainWindow::MainWindow()
|
|||
_inputField = new BTextControl("", "What is the matrix ?",
|
||||
new BMessage(kQuestionChanged));
|
||||
|
||||
|
||||
_progress = new BStatusBar("prog");
|
||||
_progress->SetMaxValue(100);
|
||||
_progress->SetTo(0);
|
||||
|
@ -87,9 +84,8 @@ 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());
|
||||
_apiKey = ReadOpenAIKey();
|
||||
printf("key is: %s", _apiKey.String());
|
||||
|
||||
BButton *sendButton =
|
||||
new BButton("send", B_TRANSLATE("Send"), new BMessage(kSendPrompt),
|
||||
|
@ -99,9 +95,10 @@ MainWindow::MainWindow()
|
|||
_answerView->MakeEditable(false); // Disable editing
|
||||
_answerView->MakeSelectable(true); // Enable text selection
|
||||
_answerView->SetWordWrap(true);
|
||||
|
||||
|
||||
BScrollView *scrollView =
|
||||
new BScrollView("scroll_view", _answerView, B_FOLLOW_ALL | B_WILL_DRAW ,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)
|
||||
|
||||
|
@ -198,7 +195,16 @@ void MainWindow::MessageReceived(BMessage *message) {
|
|||
// that the request is done.
|
||||
BHttpBody body = _lastResult->Body();
|
||||
if (body.text.has_value())
|
||||
_answerView->SetText(body.text.value());
|
||||
{
|
||||
|
||||
json parsed = json::parse(body.text.value().String());
|
||||
|
||||
std::string content = parsed["choices"][0]["message"]["content"];
|
||||
_answerView->SetText(content.c_str());
|
||||
|
||||
// _answerView->SetText(body.text.value());
|
||||
|
||||
}
|
||||
else
|
||||
_answerView->SetText("nuthin'");
|
||||
}
|
||||
|
@ -263,56 +269,31 @@ void MainWindow::sendQuery() {
|
|||
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
|
||||
// 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);
|
||||
|
||||
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
|
||||
fields.AddField("Authorization", bearer);
|
||||
// fields.AddField("Content-Type", "application/json"); //NO, this will
|
||||
// crash, we set it in request
|
||||
request.SetFields(fields);
|
||||
|
||||
json bodyJson = {
|
||||
{"model", "gpt-3.5-turbo"},
|
||||
{"messages", {{{"role", "user"}, {"content", _inputField->Text()}}}}};
|
||||
|
||||
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?"}]
|
||||
// }'
|
||||
|
||||
std::string body = bodyJson.dump();
|
||||
|
||||
BString mime = BString("application/json");
|
||||
// request.SetRequestBody(body.c_str(),mime);
|
||||
|
||||
auto memoryIO = std::make_unique<BMemoryIO>(body.c_str(), body.size());
|
||||
request.SetRequestBody(std::move(memoryIO), "application/json", body.size());
|
||||
|
||||
printf("Sending Prompt to server: %s\n", url.UrlString().String());
|
||||
_lastResult = _sharedSession.Execute(std::move(request), nullptr, this);
|
||||
|
@ -360,33 +341,32 @@ BMenuBar *MainWindow::_BuildMenu() {
|
|||
}
|
||||
|
||||
|
||||
|
||||
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";
|
||||
// /boot/home/config/openai_key
|
||||
// or ~/.config/openai_key on linux
|
||||
|
||||
configPath.Append("openai_key");
|
||||
BPath configPath;
|
||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK)
|
||||
return "error: couldn't find config directory";
|
||||
|
||||
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 ";
|
||||
configPath.Append("openai_key");
|
||||
|
||||
off_t size;
|
||||
file.GetSize(&size);
|
||||
|
||||
char* buffer = new char[size + 1];
|
||||
file.Read(buffer, size);
|
||||
buffer[size] = '\0'; // null-terminate
|
||||
BFile file(configPath.Path(), B_READ_ONLY);
|
||||
|
||||
BString result(buffer);
|
||||
delete[] buffer;
|
||||
printf("full path:%s\n", configPath.Path());
|
||||
if (file.InitCheck() != B_OK)
|
||||
return "error: couldn't open key file ";
|
||||
|
||||
return result;
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue