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 based on what you've described:
-
Ollama Framework: You're using the Ollama framework to build and run language models on your local machine. This involves running a command (
ollama serve) to start a server that listens for HTTP requests. -
Server Communication: The server started by
ollama servelistens for commands and interacts with models using HTTP and JSON. You send commands likeollama run MODEL_NAMEto interact with specific models. -
HTTP Requests and JSON Handling:
- You have a
Networkclass that uses thecpp-httpliblibrary to make HTTP GET, POST, and DELETE requests. - For handling JSON data, you're using the
nlohmann/jsonlibrary.
- You have a
-
wxWidgets Application: You're planning to build a wxWidgets-based desktop application that serves as an interface for the Ollama server. This application will use the class and JSON handling capabilities to interact with the language models hosted by the Ollama server.
-
- User
- Assistant
Yes, I understand the problem. You need a way to handle streaming responses from your Ollama server in your wxWidgets application. Specifically, you want to continuously receive and process chunks of the response until a completion signal (
"done": true) is received.To achieve this, you would maintain a connection to the Ollama server, repeatedly reading the incoming data and processing each JSON object until the
"done": truefield is encountered.I am ready to look at your current code and the cpp-httplib headers. Once I have reviewed them, we can discuss how to handle the streaming responses appropriately, making sure to respect your guidelines about not hallucinating or repeating already working code.
Please post your code and the cpp-httplib headers when you are ready.
- User
Wait until my signal to start writing code, here is the documentation first:
Generate a chat completion POST /api/chat Generate the next message in a chat with a provided model. This is a streaming endpoint, so there will be a series of responses. Streaming can be disabled using "stream": false. The final response object will include statistics and additional data from the request.
Parameters model: (required) the model name messages: the messages of the chat, this can be used to keep a chat memory The message object has the following fields:
role: the role of the message, either system, user or assistant content: the content of the message images (optional): a list of images to include in the message (for multimodal models such as llava) Advanced parameters (optional):
format: the format to return a response in. Currently the only accepted value is json options: additional model parameters listed in the documentation for the Modelfile such as temperature stream: if false the response will be returned as a single response object, rather than a stream of objects keep_alive: controls how long the model will stay loaded into memory following the request (default: 5m) Examples Chat Request (Streaming) Request Send a chat message with a streaming response.
- Assistant
Thank you for providing the detailed documentation. I understand the endpoint you need to communicate with and the structure of the streaming responses.
Here is a summary of the key points:
-
Endpoint:
POST /api/chat -
Payload:
model: The model name (required).messages: A list of message objects, each withroleandcontent(required).- Optional parameters like
stream,format,options, andkeep_alive.
-
Response: A stream of JSON objects, where each object can contain:
"done": falseto signify that more messages are coming."done": trueto signify the end of the stream, along with additional statistics and data.
The goal is to send a POST request to this endpoint and then keep reading and processing the JSON objects from the stream until we receive an object where .
-
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
Can you make it without using wxDECLARE_EVENT_TABLE();? Like using Bind directly?
- Assistant
- User
can you do it without a custom class?
- Assistant
- User
- Assistant
- User
OK, the new code has the same problem as before, the main frame is frozen as long as the network method is being processed, so the UI doesn't show the text until it finish. Maybe we need to implement something in the FrameMain were the Chat class is called?
class FrameMain : public FrameMainView { public: FrameMain(wxWindow* parent); ~FrameMain(); //... void OnSendClick(wxCommandEvent& event) override;
private: //... void StartChat(); //... Chat* chat;};
void FrameMain::OnSendClick(wxCommandEvent& event) { wxString userMessage = textCtrlChatInput->GetValue(); if (!userMessage.empty()) { chat->SendMessage(userMessage); textCtrlChatInput->Clear(); } }
void FrameMain::StartChat() { EnableChatControls(true); richTextChatBox->Clear(); chat = new Chat(host, bcomboBoxModel->GetStringSelection(), keepAliveMinutes, streaming, richTextChatBox); }
- Assistant
- User
It is randomly crashing, sometimes at the first message sometimes at the second but it crashes with status code -11. But besides that if it does in fact doesn't crash, it doesn't make each of the token appear faster, the only thing that appear faster is my text.
- Assistant
- User
It is still crashing, even with the mutex, any alternative?
- Assistant
- User
In file included from /usr/local/include/wx-3.1/wx/wx.h:24, from include/Chat.h:34, from /home/.../src/Chat.cpp:25: /usr/local/include/wx-3.1/wx/event.h: In instantiation of ‘wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>::wxEventFunctorMethod(void (Class::)(EventArg&), EventHandler) [with EventTag = wxEventTypeTag; Class = Chat; EventArg = wxThreadEvent; EventHandler = Chat]’: /usr/local/include/wx-3.1/wx/event.h:612:12: required from ‘wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>* wxNewEventFunctor(const EventTag&, void (Class::)(EventArg&), EventHandler) [with EventTag = wxEventTypeTag; Class = Chat; EventArg = wxThreadEvent; EventHandler = Chat]’ /usr/local/include/wx-3.1/wx/event.h:3838:36: required from ‘void wxEvtHandler::Bind(const EventTag&, void (Class::, int, int, wxObject*) [with EventTag = wxEventTypeTag; Class = Chat; EventArg = wxThreadEvent; EventHandler = Chat]’ /home/.../src/Chat.cpp:36:9: required from here /usr/local/include/wx-3.1/wx/event.h:408:29: error: cannot convert ‘wxEventFunctorMethod<wxEventTypeTag, Chat, wxThreadEvent, Chat>::EventClass*’ {aka ‘wxCommandEvent*’} to ‘wxThreadEvent*’ 408 | CheckHandlerArgument(static_cast<EventClass ) [with EventTag = wxEventTypeTag; Class = Chat; EventArg = wxThreadEvent; EventHandler = Chat]’ 391 | static void CheckHandlerArgument(EventArg *) { } | ^~~~~~~~~~ Process terminated with status 1 (0 minute(s), 7 second(s))
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.