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 already have the Network class done, with HTTPGet and HTTPPost methods, and JSON support with nlohmann::json library.
Do you understand my setup so I can ask you my problem?
- Assistant
Yes, I understand your setup. Here's a summary to ensure we're on the same page:
- You have a local server running via the command
ollama serve. - This server stays running until the process is manually terminated.
- You interact with this server using
ollama run MODEL_NAMEin another terminal, which initiates a chat with a specified model by sending HTTP requests. - You have a
Networkclass in your C++ wxWidgets application withHTTPGetandHTTPPostmethods that handle HTTP communication. - You're using the nlohmann::json library for JSON handling in your application.
Feel free to ask your question, and I'll do my best to help!
- You have a local server running via the command
- User
- Assistant
- User
I changed and added the logging and it still returns status code 400, I don't know why is this happening:
12:33:02: Debug: Model list updated. 12:33:04: Debug: Loading model... 12:33:04: Debug: Sending HTTP POST - Host: http://localhost:11434 Request: /api/generate Model: gemma:2b. 12:33:04: Debug: HTTP POST to http://localhost:11434/api/generate with payload: {"model":"gemma:2b","prompt":"","stream":"false"}
- Assistant
- User
I changed the model and the stream parameters in the JSON, and the boolean of the done key and now it works correctly! The model is now loaded into memory and works. But now, This start button has a double function which now I have to make now. The loading of an existing model works perfectly, but the combobox, can also be written with a new model and this run model should also be able to download a model and add it into our list. The API documentation for this is:
Pull a Model POST /api/pull Download a model from the ollama library. Cancelled pulls are resumed from where they left off, and multiple calls will share the same download progress.
Parameters name: name of the model to pull stream: (optional) if false the response will be returned as a single response object, rather than a stream of objects Examples Request curl http://localhost:11434/api/pull -d '{ "name": "llama3", "stream": "false", }'
if stream is set to false, then the response is a single JSON object:
{ "status": "success" }
So I want to be able to implement this if the model isn't on the list, so I need a way to check if the combobox selected string is not already on its list of string, and if not then call this HTTP POST request to the server to download the model.
- Assistant
- User
I want to ask the user for a confirmation with a messagebox asking if you want to download the new model or not, how can I do this? (PD: Don't write the load model part again)
- Assistant
- User
Cool, but I got an error, the GetStringSelection() method is returning me an empty string, since this element is not on the list, how can I get the string even if it is not selected?
- Assistant
- User
Perfect now it is correctly checking the string, but now two questions appeared in my mind.
1)If the smallest model is 400+ MB, will the HTTP POST wait until the success message or do we have to do wait the response in a while loop?
- Another even deeper doubt, which I want for us to get in detail after the first question. if it does in fact wait for a response, how should we deal with the user waiting until the model finishes downloading, if the program get frozen is not a good thing, at least it should show a message or something, but maybe this will have to be done using threads or something.
- Assistant
- User
Explain to me, how would the wxBusyInfo control would know that the downloadThread has finished? There is not a manual assignment or change of values, it is done automatically?
- Assistant
- User
OK BEfore we go to the events, let's backtrack and let's just keep how things were previously, I don't like this way of checking the thread, I will keep the busybox as it was before, so we deal first with a problem I got:
I clicked on download the Model and I got this error:
14:13:41: HTTP POST Error: HTTP POST request failed with status: 404 14:13:41: HTTP POST Error: HTTP POST request failed with status: 500
What does this means?
- Assistant
- User
OK I got the problem and it was the model name was wrongly typed, but now what I did is writting it correctly and now it is giving me a different error, something really weird, status code of 0, which I don't understand what is happening, and the strangest thing is that the model blob is actually downloaded and in the ollama server folder.
Terminal logs: 14:34:32: Debug: Model not found in the list. Pulling the model from the server... 14:34:32: Debug: HTTP POST to http://localhost:11434/api/pull with payload: {"model":"starcoder:1b-base-q3_K_S","stream":false} 14:34:37: Debug: Loading model... 14:34:37: Debug: Sending HTTP POST - Host: http://localhost:11434 Request: /api/generate Model: starcoder:1b-base-q3_K_S. 14:34:37: Debug: HTTP POST to http://localhost:11434/api/generate with payload: {"keep_alive":20,"model":"starcoder:1b-base-q3_K_S","stream":false}
- Assistant
- User
OK, so I manually created the server and sent the curl command and it works as expected, but it took like almost 2 minutes to complete, while in my own program, it takes a few second to continue the downloadmodel into the load model, so that means it doesn't wait for it to finish download or to receive the sucess status.
This is the output of both the server and the curl:
//server ./ollama serve ... time=2024-05-20T15:04:12.073-03:00 level=INFO source=download.go:136 msg="downloading e1efe5215177 in 6 100 MB part(s)" time=2024-05-20T15:05:02.216-03:00 level=INFO source=download.go:136 msg="downloading 1576480a555b in 1 36 B part(s)" time=2024-05-20T15:05:05.375-03:00 level=INFO source=download.go:136 msg="downloading cde92feeda9b in 1 313 B part(s)" [GIN] 2024/05/20 - 15:05:08 | 200 | 59.44556592s | 127.0.0.1 | POST "/api/pull"
//curl: curl -X POST http://localhost:11434/api/pull -H "Content-Type: application/json" -d '{"name":"starcoder:1b-base-q3_K_S","stream":false}' {"status":"success"}
- Assistant
- User
The library I am using is cpp-httplib, dunno if it supports this long timeouts, do you know if it do?
- Assistant
- User
well it worked somehow but with a few caveats!
- It froze and then the wxBusyInfo was shown in front of a messagedialog box with just an OK button, I checked the log and it was already downloaded and even loaded, so the busyinfo didn't disappear. I am going to try with threads this time. OK, after testing the thread version like this:
wxBusyInfo info(wxT("Downloading model. Please wait..."), this); std::thread downloadThread(&FrameMain::DownloadModel, this); downloadThread.detach();
It didn't even show the wxBusyInfo message this time, how can we deal with this?
-
The timeout should be able to be changed by a parameter, for example, depending on the model size the timeout will be longer or shorter.
-
How would I know for example if a 40 GB model is still being downloaded if there is no message in between? Should we calculate the timeout based on the size and internet speed of the hardware? Maybe we could try doing "stream: true" and deal with streaming of messages but will be harder I think.
- Assistant
- User
I like how it is going, but I will move it to its own DIalog, and I don't want to use custom events, i Will create the dialog in wxFormBuilder, so let me know what does it need to have, probably the wxTimer and a label or similar. We won't be using custom events, so just stick with command events, the downloading method and everything will be moved to this new dialog so we can free the main frame a little.
- Assistant
- User
OK, I have a problem here, we can't really cancel the download can we? It is not like we can send a message to the server to stop processing the download, so why is there a need for a cancel button in here?
- Assistant
- User
- Assistant
- User
Well, let's move to the model deletion, Ollama API documentation is this:
Delete a Model DELETE /api/delete Delete a model and its data.
Parameters name: model name to delete Examples Request curl -X DELETE http://localhost:11434/api/delete -d '{ "name": "llama3:13b" }' Response Returns a 200 OK if successful, 404 Not Found if the model to be deleted doesn't exist.
Let's do it in a way that asks the user if they want to delete the model, if it accept then delete it:
void OnDeleteModelClick(wxCommandEvent& event) override;
void FrameMain::OnDeleteModelClick(wxCommandEvent& event) {
}
- Assistant
- Assistant
- User
why did you check for if (j[“status”].getstd::string() == “success”), if the documentation only states that a 200 status code means model deleted?
- Assistant
- User
It is giving me an error when deleting a model:
In my application it shows this message box: HTTP DELETE Error: [json.exception.type_error.302] type must be string, but is null
and terminal it shows this: 01:31:51: Debug: Deleting model... 01:31:51: Debug: Sending HTTP DELETE - Host: http://localhost:11434 Request: /api/delete Model: tinydolphin:1.1b. 01:31:51: Debug: HTTP DELETE Response: null
The model is deleted afterwards but this error is shown nonetheless
- Assistant