I am making a C++ wxWidgets application, my desktop program is an interface for an LLM…
- User
I am making a C++ wxWidgets application, my desktop program is an interface for an LLM chat external command-line application, called Ollama.
Ollama is a lightweight, extensible framework for building and running language models on the local machine. It provides a simple API for creating, running, and managing models, as well as a library of pre-built models that can be easily used in a variety of applications.
It works like this,
- First, we run in one terminal: "ollama serve", which in that terminal starts a server that listen all the commands and works as a webserver inside localhost, and the only way to turn it off is by killing the process.
- Second, in another terminal, we can use "ollama run MODEL_NAME", to start a chat with said model, communicating with the server using HTTP and JSON.
I Have a Network class using cpp-httplib, with methods like HTTPGet, HTTPPost and HTTPDelete, to deal with the HTTP requests.
And for the JSON part I am using nlohmann/json library.
Here’s a quick recap to make sure we’re both on the same page:
-
Ollama Framework: I am using the Ollama framework to build and run language models locally. The framework starts a server when you run ollama serve and it listens for commands, which you then interact with using HTTP requests.
-
Communication: For communication with the Ollama server, I have a Network class utilizing the cpp-httplib library to make HTTP requests (GET, POST, DELETE).
-
Data Handling: I am using the nlohmann/json library to handle JSON data, which is used for communication with the Ollama server.
I am planning to build a wxWidgets application that acts as an interface to communicate with the Ollama server, allowing you to interact with language models via HTTP and JSON.
Tell me if you understand my setup so we can continue.
- Assistant
Yes, I understand your setup. Here's a summary:
-
Ollama Framework:
- Ollama is a framework for running language models locally.
- It involves starting a server using the command
ollama serve. - You communicate with this server via HTTP and JSON requests using commands like
ollama run MODEL_NAME.
-
Communication:
- You have a Network class that uses the cpp-httplib library to make HTTP requests (GET, POST, DELETE).
-
Data Handling:
- You are using the nlohmann/json library to handle JSON data sent to and received from the Ollama server.
-
wxWidgets Application:
- You are building a desktop application using wxWidgets which will act as an interface to interact with the Ollama server.
- This interface will use your Network class to send HTTP requests and handle responses using the nlohmann/json library.
-
- User
- Assistant
- User
- Assistant
- User
This code just added a linebreak in my first message, which is something I don't want! Also, it doesn't format my message correctly, this is how it ended:
" Hello! Hello! It's nice to hear from you. What can I do for you today? 😊 How are you? I'm doing well, thank you for asking! I'm enjoying learning and interacting with you. What can I do for you today? "
and it should have been:
"Hello! [line breal] Hello! It's nice to hear from you. What can I do for you today? 😊 [line breal] How are you? [line breal] I'm doing well, thank you for asking! I'm enjoying learning and interacting with you. What can I do for you today? "
- Assistant
- Assistant
- User
- Assistant
- User
- Assistant
- User
Ok, this works better, O like this approach a lot. Now I want something to change now, if you see my Network class, it is dealing with the JSON inside, and this should be part of the Chat class not the Network, I want the Network class to abstract itself from the message and just pass it on.
Another thing you have to take into account, in your last code, I changed it to add the fully completed message to be saved in the chatHistory, take this into account when dealing with the new code, don't forget this part since chatHistory should have only full messages, not the separated chunks.
// code I had to change to make your previous code work correctly. std::string response = network.HTTPPostStream([this](const std::string& responseChunk){ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED); event.SetString(wxString::FromUTF8(responseChunk.c_str())); wxPostEvent(this, event); }, host.ToStdString(), "/api/chat", requestBody.dump(), "application/json", 120);
- Assistant
- User
- Assistant
- User
OK I based my code in yours and made it to work, I implemented several things to fix some problems your last code had, this is the working code:
std::string fullResponse;
network.HTTPPostStream([this, &fullResponse](const std::string& responseChunk){ wxLogDebug("Received chunk: %s", responseChunk.c_str());
nlohmann::json jsonChunk = nlohmann::json::parse(responseChunk); if (jsonChunk.contains("message") && jsonChunk["message"].contains("content")) { std::string content = jsonChunk["message"]["content"].get<std::string>(); wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED); event.SetString(wxString::FromUTF8(content.c_str())); wxPostEvent(this, event); fullResponse += content; if (jsonChunk["done"].get<bool>()) { if (!fullResponse.empty()) { wxLogDebug("AI message content: %s", fullResponse.c_str()); chatHistory.push_back({ {"role", "assistant"}, {"content", fullResponse} }); } else { wxLogDebug("Received empty response from server"); } } } }, host.ToStdString(), "/api/chat", requestBody.dump(), "application/json", 120); - Assistant
- User
it works perfectly! Now there is a bug I don't like. The non-streaming messages are correctly formated, which means every single part of the rich format works:
style.SetAlignment(wxTEXT_ALIGNMENT_LEFT); style.SetBackgroundColour(wxColour("#4A67CF")); style.SetTextColour(wxColour("#FFFFFF")); style.SetLeftIndent(20); style.SetRightIndent(5); style.SetParagraphSpacingBefore(20); style.SetParagraphSpacingAfter(15);
But when using it as streaming, the message only for partially formatted, which means only these are shown: style.SetAlignment(wxTEXT_ALIGNMENT_LEFT); style.SetBackgroundColour(wxColour("#4A67CF")); style.SetTextColour(wxColour("#FFFFFF"));
The rest are omitted and I don't know why, let's try to fix this error which I don't understand why it is happening.
- Assistant
- User
Your code was wrong, the isStreaming if has the same code in both the if and its else, this doesn't do anything. Also another thing to add, it is not just the streaming part, the user message doesn't have the format too, even though it is common with the non-streaming part. So why it is changing here but not in the streaming part? What I can see is when the streaming text is appended it looks like a refresh is going on the screen, and takes the format out. I tested both the streaming and the non-streaming ways, and the streaming part the first text is already formatted, while in the non-streaming one even the first text is not formatted, why is this happening?
Also maybe, do you think having a global style variable would help here? So we only change when the streaming is finished?
- Assistant
- User
it is giving me errors at the richTextChatBox->WriteText(message);, it gives me a weird error.
Process terminated with status -11 (0 minute(s), 19 second(s))
Aso soon as it writes the user message, then I changed it back to richTextChatBox->WriteText(FormatMessageString(message) + (streaming ? "" : "\n")); and this doesn't throw me any error, what could be happening?
I even tried doing if (!message.empty()) but still throws the same error and crashes.
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
This gave me many errors, so let's go back to the previous working versio, and let's add something different this time, in case you don't know, text streaming means each token is sent separately so they can be written in the rich text box as soon as they arrive, but the current code, the write part is frozen until the thread has finished working, even though we are using events, the tokens aren't written in real time, how can we do this?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.