Repository: ollama/ollama
File: llm/ext_server/server.cpp (C++)

Code before:
                {
                    prompt = body["content"];
                }
                else
                {
                    prompt = "";
                }

                // create and queue the task
                json responses;
                {
                    const int id_task = llama.queue_tasks.get_new_id();
                    llama.queue_results.add_waiting_task_id(id_task);
                    llama.request_completion(id_task, {{"prompt", prompt}}, true, -1);

                    // get the result
                    task_result result = llama.queue_results.recv(id_task);
                    llama.queue_results.remove_waiting_task_id(id_task);
                    if (!result.error) {
                        if (result.result_json.count("results")) {
                            // result for multi-task
                            responses = result.result_json.at("results");
                        } else {
                            // result for single task
                            responses = std::vector<json>(1, result.result_json);
                        }
                        json embeddings = json::array();
                        for (auto & elem : responses) {
                            embeddings.push_back(elem.at("embedding"));
                        }
                        // send the result
                        json result = json{{"embedding", embeddings}};
                        return res.set_content(result.dump(), "application/json; charset=utf-8");
                    } else {
                        // return error
                        return res.set_content(result.result_json.dump(), "application/json; charset=utf-8");
                    }
                }
            });

    // GG: if I put the main loop inside a thread, it crashes on the first request when build in Debug!?
    //     "Bus error: 10" - this is on macOS, it does not crash on Linux
    //std::thread t2([&]()
    /*{
        bool running = true;
        while (running)
        {
            running = llama.update_slots();
        }
    }*/
    //);

Review comment (suggestion):
```suggestion
                        responses = result.result_json.value("results", std::vector<json>{result.result_json});
```

Code after:
                {
                    prompt = body["content"];
                }
                else
                {
                    prompt = "";
                }

                if (prompt.size() == 1) {
                    prompt = prompt[0];
                }

                // create and queue the task
                json responses;
                {
                    const int id_task = llama.queue_tasks.get_new_id();
                    llama.queue_results.add_waiting_task_id(id_task);
                    llama.request_completion(id_task, {{"prompt", prompt}}, true, -1);

                    // get the result
                    task_result result = llama.queue_results.recv(id_task);
                    llama.queue_results.remove_waiting_task_id(id_task);
                    if (result.error) {
                        return res.set_content(result.result_json.dump(), "application/json; charset=utf-8");
                    }

                    responses = result.result_json.value("results", std::vector<json>{result.result_json});
                    json embeddings = json::array();
                    for (auto & elem : responses) {
                        embeddings.push_back(elem.at("embedding"));
                    }
                    // send the result
                    json embedding_res = json{{"embedding", embeddings}};
                    return res.set_content(embedding_res.dump(), "application/json; charset=utf-8");
                }
            });

    // GG: if I put the main loop inside a thread, it crashes on the first request when build in Debug!?
    //     "Bus error: 10" - this is on macOS, it does not crash on Linux
    //std::thread t2([&]()
    /*{
        bool running = true;
        while (running)
        {
            running = llama.update_slots();
        }
    }*/
    //);

    llama.queue_tasks.on_new_task(std::bind(
        &llama_server_context::process_single_task, &llama, std::placeholders::_1));