-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathollama_api.cpp
More file actions
1293 lines (1102 loc) · 52.5 KB
/
ollama_api.cpp
File metadata and controls
1293 lines (1102 loc) · 52.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "lemon/ollama_api.h"
#include "lemon/model_types.h"
#include <iostream>
#include <lemon/utils/aixlog.hpp>
#include <sstream>
#include <algorithm>
#include <thread>
#include <vector>
namespace lemon {
// ============================================================================
// extract parameter size from model name
// e.g. "Qwen3-0.6B-GGUF" → "0.6B", "Gemma-3-4b-it-GGUF" → "4B"
// ============================================================================
static std::string extract_parameter_size(const std::string& model_name) {
std::string result;
size_t i = 0;
while (i < model_name.size()) {
size_t seg_start = i;
while (i < model_name.size() && model_name[i] != '-') i++;
std::string segment = model_name.substr(seg_start, i - seg_start);
if (i < model_name.size()) i++; // skip hyphen
// Match pattern: [0-9.]+[BbMm]
if (segment.size() >= 2) {
char last = segment.back();
if (last == 'B' || last == 'b' || last == 'M' || last == 'm') {
bool valid = true;
for (size_t j = 0; j < segment.size() - 1; j++) {
if (!std::isdigit(segment[j]) && segment[j] != '.') {
valid = false;
break;
}
}
if (valid) {
// Normalize suffix to uppercase (e.g. "4b" → "4B")
result = segment.substr(0, segment.size() - 1) +
static_cast<char>(std::toupper(last));
}
}
}
}
return result; // returns last match (param size typically follows version numbers)
}
// ============================================================================
// extract quantization level from checkpoint string
// e.g. "unsloth/Qwen3-0.6B-GGUF:Q4_0" → "Q4_0"
// "ggml-org/gemma-3-4b-it-GGUF:Q4_K_M" → "Q4_K_M"
// "unsloth/gemma-3-270m-it-GGUF:gemma-3-270m-it-UD-IQ2_M.gguf" → "IQ2_M"
// ============================================================================
static std::string extract_quantization_level(const std::string& checkpoint) {
// Search in the filename part (after colon) first, fallback to full string
std::string search_str = checkpoint;
size_t colon_pos = checkpoint.find(':');
if (colon_pos != std::string::npos) {
search_str = checkpoint.substr(colon_pos + 1);
}
// Look for [I]?Q[0-9][A-Za-z0-9_]* at a word boundary
for (size_t i = 0; i < search_str.size(); i++) {
size_t start = i;
// Optional 'I' prefix (for IQ patterns)
if (search_str[i] == 'I' && i + 1 < search_str.size() && search_str[i + 1] == 'Q') {
i++;
}
if (search_str[i] == 'Q' && i + 1 < search_str.size() && std::isdigit(search_str[i + 1])) {
// Check word boundary before start
if (start > 0 && std::isalnum(search_str[start - 1])) {
i = start; // not a boundary, skip
continue;
}
// Scan to end of quant token
size_t end = i + 1;
while (end < search_str.size() &&
(std::isdigit(search_str[end]) || std::isalpha(search_str[end]) ||
search_str[end] == '_')) {
end++;
}
return search_str.substr(start, end - start);
}
i = start; // reset to start for next iteration's i++
}
return "";
}
// ============================================================================
// Ollama → OpenAI option name mapping (ollama_key → openai_key)
// Options where both names are the same use identical strings.
// ============================================================================
struct OptionMapping { const char* ollama_key; const char* openai_key; };
static const OptionMapping OPTION_MAPPINGS[] = {
{"temperature", "temperature"},
{"top_p", "top_p"},
{"seed", "seed"},
{"stop", "stop"},
{"num_predict", "max_tokens"},
{"repeat_penalty", "frequency_penalty"},
};
// Apply the option mappings from an Ollama request to an OpenAI request.
// Checks the "options" sub-object first, then top-level keys (top-level wins).
static void map_ollama_options(const json& ollama_request, json& openai_req) {
// From options sub-object
if (ollama_request.contains("options") && ollama_request["options"].is_object()) {
const auto& opts = ollama_request["options"];
for (const auto& m : OPTION_MAPPINGS) {
if (opts.contains(m.ollama_key)) {
openai_req[m.openai_key] = opts[m.ollama_key];
}
}
}
// Top-level overrides (Ollama also accepts these at the top level)
for (const auto& m : OPTION_MAPPINGS) {
if (ollama_request.contains(m.ollama_key)) {
openai_req[m.openai_key] = ollama_request[m.ollama_key];
}
}
}
OllamaApi::OllamaApi(Router* router, ModelManager* model_manager)
: router_(router), model_manager_(model_manager) {
}
void OllamaApi::register_routes(httplib::Server& server) {
// Capture shared_ptr to keep OllamaApi alive as long as route handlers exist
auto self = shared_from_this();
// Chat completion
server.Post("/api/chat", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_chat(req, res);
});
// Text generation (completion)
server.Post("/api/generate", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_generate(req, res);
});
// List models
server.Get("/api/tags", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_tags(req, res);
});
// Show model info
server.Post("/api/show", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_show(req, res);
});
// Delete model
server.Delete("/api/delete", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_delete(req, res);
});
// Pull (download) model
server.Post("/api/pull", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_pull(req, res);
});
// Embeddings (new format)
server.Post("/api/embed", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_embed(req, res);
});
// Embeddings (legacy format)
server.Post("/api/embeddings", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_embeddings(req, res);
});
// Running models
server.Get("/api/ps", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_ps(req, res);
});
// Version
server.Get("/api/version", [self](const httplib::Request& req, httplib::Response& res) {
self->handle_version(req, res);
});
register_anthropic_routes(server, self);
// 501 stubs for unsupported endpoints
auto not_supported = [](const httplib::Request&, httplib::Response& res) {
res.status = 501;
res.set_content(R"({"error":"not supported by Lemonade"})", "application/json");
};
server.Post("/api/create", not_supported);
server.Post("/api/copy", not_supported);
server.Post("/api/push", not_supported);
server.Post(R"(/api/blobs/(.+))", not_supported);
LOG(DEBUG, "OllamaApi") << "Ollama-compatible routes registered" << std::endl;
}
// ============================================================================
// normalize model name (strip ":latest" suffix)
// ============================================================================
std::string OllamaApi::normalize_model_name(const std::string& name) {
const std::string suffix = ":latest";
if (name.size() > suffix.size() &&
name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0) {
return name.substr(0, name.size() - suffix.size());
}
return name;
}
// ============================================================================
// auto-load model if needed (mirrors Server::auto_load_model_if_needed)
// ============================================================================
void OllamaApi::auto_load_model(const std::string& model) {
std::string name = normalize_model_name(model);
if (router_->is_model_loaded(name)) {
return;
}
LOG(INFO, "OllamaApi") << "Auto-loading model: " << name << std::endl;
if (!model_manager_->model_exists(name)) {
throw std::runtime_error("model '" + name + "' not found");
}
auto info = model_manager_->get_model_info(name);
// Download if not cached
if (info.recipe != "flm" && !model_manager_->is_model_downloaded(name)) {
LOG(INFO, "OllamaApi") << "Model not cached, downloading..." << std::endl;
model_manager_->download_registered_model(info, true);
info = model_manager_->get_model_info(name);
}
router_->load_model(name, info, RecipeOptions(info.recipe, json::object()), true);
LOG(INFO, "OllamaApi") << "Model loaded: " << name << std::endl;
}
// build Ollama model entry from ModelInfo
// build Ollama "details" object from model name, recipe, and checkpoint
static json build_ollama_details(const std::string& model_name,
const std::string& recipe,
const std::string& checkpoint) {
return {
{"parent_model", ""},
{"format", "gguf"},
{"family", recipe},
{"families", json::array({recipe})},
{"parameter_size", extract_parameter_size(model_name)},
{"quantization_level", extract_quantization_level(checkpoint)}
};
}
json OllamaApi::build_ollama_model_entry(const std::string& id, const ModelInfo& info) {
// Compute size in bytes (info.size is in GB)
int64_t size_bytes = static_cast<int64_t>(info.size * 1073741824.0); // 1 GB = 2^30 bytes
json entry = {
{"name", id + ":latest"},
{"model", id + ":latest"},
{"modified_at", "2024-01-01T00:00:00Z"},
{"size", size_bytes},
{"digest", "sha256:0000000000000000000000000000000000000000000000000000000000000000"},
{"details", build_ollama_details(id, info.recipe, info.checkpoint())}
};
return entry;
}
// ============================================================================
// Request conversion: Ollama chat → OpenAI chat
// ============================================================================
json OllamaApi::convert_ollama_to_openai_chat(const json& ollama_request) {
json openai_req;
// Map model (normalize name)
std::string model = normalize_model_name(ollama_request.value("model", ""));
openai_req["model"] = model;
// Map messages
if (ollama_request.contains("messages")) {
json messages = json::array();
for (const auto& msg : ollama_request["messages"]) {
json openai_msg;
openai_msg["role"] = msg.value("role", "user");
// Handle content - could be string or have images
if (msg.contains("images") && msg["images"].is_array() && !msg["images"].empty()) {
// Multimodal: convert to OpenAI content array format
json content_parts = json::array();
if (msg.contains("content") && !msg["content"].get<std::string>().empty()) {
content_parts.push_back({{"type", "text"}, {"text", msg["content"]}});
}
for (const auto& img : msg["images"]) {
content_parts.push_back({
{"type", "image_url"},
{"image_url", {{"url", "data:image/png;base64," + img.get<std::string>()}}}
});
}
openai_msg["content"] = content_parts;
} else {
openai_msg["content"] = msg.value("content", "");
}
// Forward tool_calls if present, with two normalisations required
// by llama.cpp's strict OpenAI-spec validation:
// 1. Inject "type":"function" — Ollama clients omit this field.
// 2. Skip tool calls whose arguments are not valid JSON — these
// are streaming artifacts (e.g. arguments="{") that an Ollama
// client may persist to history mid-stream; forwarding them
// causes func_args_not_string() to throw a parse error 500.
if (msg.contains("tool_calls")) {
json tool_calls = json::array();
for (auto tc : msg["tool_calls"]) {
if (!tc.contains("type")) {
tc["type"] = "function";
}
// Validate arguments JSON if present
if (tc.contains("function") && tc["function"].contains("arguments")) {
const auto& args = tc["function"]["arguments"];
if (args.is_string()) {
try {
json::parse(args.get<std::string>());
} catch (const std::exception&) {
// Skip this tool call — arguments are not valid JSON
continue;
}
}
}
tool_calls.push_back(tc);
}
if (!tool_calls.empty()) {
openai_msg["tool_calls"] = tool_calls;
}
}
messages.push_back(openai_msg);
}
openai_req["messages"] = messages;
}
// Map options (from "options" sub-object and top-level)
map_ollama_options(ollama_request, openai_req);
// Map tools if present
if (ollama_request.contains("tools")) {
openai_req["tools"] = ollama_request["tools"];
}
// Map format: "json" → response_format
if (ollama_request.contains("format") && ollama_request["format"].is_string() &&
ollama_request["format"].get<std::string>() == "json") {
openai_req["response_format"] = {{"type", "json_object"}};
}
// Map think parameter → enable_thinking (controls reasoning output)
if (ollama_request.contains("think")) {
openai_req["enable_thinking"] = ollama_request["think"];
}
// Stream flag is handled by the caller
openai_req["stream"] = false;
return openai_req;
}
// ============================================================================
// Request conversion: Ollama generate → OpenAI completion
// ============================================================================
json OllamaApi::convert_ollama_to_openai_completion(const json& ollama_request) {
json openai_req;
std::string model = normalize_model_name(ollama_request.value("model", ""));
openai_req["model"] = model;
// For /api/generate, if there's a "system" field and "prompt", combine into messages
// for chat completion (since many backends don't support raw completion)
if (ollama_request.contains("prompt")) {
openai_req["prompt"] = ollama_request["prompt"];
}
// Map options (from "options" sub-object and top-level)
map_ollama_options(ollama_request, openai_req);
openai_req["stream"] = false;
return openai_req;
}
// ============================================================================
// Response conversion: OpenAI chat response → Ollama chat response
// ============================================================================
json OllamaApi::convert_openai_chat_to_ollama(const json& openai_response, const std::string& model) {
json ollama_res;
ollama_res["model"] = model;
ollama_res["created_at"] = "2024-01-01T00:00:00Z";
// Extract message from first choice
if (openai_response.contains("choices") && !openai_response["choices"].empty()) {
const auto& choice = openai_response["choices"][0];
if (choice.contains("message")) {
const auto& message = choice["message"];
json msg;
msg["role"] = (message.contains("role") && message["role"].is_string())
? message["role"].get<std::string>() : "assistant";
msg["content"] = (message.contains("content") && message["content"].is_string())
? message["content"].get<std::string>() : "";
// Map reasoning_content → thinking (Ollama uses "thinking" for reasoning models)
if (message.contains("reasoning_content") && message["reasoning_content"].is_string()) {
msg["thinking"] = message["reasoning_content"].get<std::string>();
}
// Forward tool_calls if present
if (message.contains("tool_calls")) {
msg["tool_calls"] = message["tool_calls"];
}
ollama_res["message"] = msg;
}
ollama_res["done_reason"] = (choice.contains("finish_reason") && choice["finish_reason"].is_string())
? choice["finish_reason"].get<std::string>() : "stop";
}
ollama_res["done"] = true;
// Extract usage → Ollama duration/count fields
if (openai_response.contains("usage")) {
const auto& usage = openai_response["usage"];
int prompt_tokens = usage.value("prompt_tokens", 0);
int completion_tokens = usage.value("completion_tokens", 0);
ollama_res["prompt_eval_count"] = prompt_tokens;
ollama_res["eval_count"] = completion_tokens;
// Provide reasonable timing estimates (nanoseconds)
// These are approximations since the OpenAI response doesn't include timings
ollama_res["total_duration"] = 0;
ollama_res["load_duration"] = 0;
ollama_res["prompt_eval_duration"] = 0;
ollama_res["eval_duration"] = 0;
}
// If timings are available (from llama.cpp backends), use them
if (openai_response.contains("timings")) {
const auto& timings = openai_response["timings"];
if (timings.contains("prompt_n"))
ollama_res["prompt_eval_count"] = timings["prompt_n"];
if (timings.contains("predicted_n"))
ollama_res["eval_count"] = timings["predicted_n"];
if (timings.contains("prompt_ms"))
ollama_res["prompt_eval_duration"] = static_cast<int64_t>(timings["prompt_ms"].get<double>() * 1000000);
if (timings.contains("predicted_ms"))
ollama_res["eval_duration"] = static_cast<int64_t>(timings["predicted_ms"].get<double>() * 1000000);
}
return ollama_res;
}
// ============================================================================
// Response conversion: OpenAI streaming delta → Ollama streaming chunk
// ============================================================================
json OllamaApi::convert_openai_delta_to_ollama(const json& openai_chunk, const std::string& model) {
json ollama_chunk;
ollama_chunk["model"] = model;
ollama_chunk["created_at"] = "2024-01-01T00:00:00Z";
ollama_chunk["done"] = false;
// Extract delta content
if (openai_chunk.contains("choices") && !openai_chunk["choices"].empty()) {
const auto& choice = openai_chunk["choices"][0];
if (choice.contains("delta")) {
const auto& delta = choice["delta"];
json msg;
msg["role"] = (delta.contains("role") && delta["role"].is_string())
? delta["role"].get<std::string>() : "assistant";
msg["content"] = (delta.contains("content") && delta["content"].is_string())
? delta["content"].get<std::string>() : "";
// Map reasoning_content → thinking (Ollama uses "thinking" for reasoning models)
if (delta.contains("reasoning_content") && delta["reasoning_content"].is_string()) {
msg["thinking"] = delta["reasoning_content"].get<std::string>();
}
if (delta.contains("tool_calls")) {
msg["tool_calls"] = delta["tool_calls"];
}
ollama_chunk["message"] = msg;
}
// Check for finish_reason
if (choice.contains("finish_reason") && !choice["finish_reason"].is_null()) {
ollama_chunk["done"] = true;
ollama_chunk["done_reason"] = choice["finish_reason"];
}
}
return ollama_chunk;
}
// ============================================================================
// Common streaming adapter: SSE → NDJSON
// Parses OpenAI SSE stream, converts each chunk via convert_chunk, writes
// NDJSON to the client, and sends a final done message via build_done.
// ============================================================================
void OllamaApi::stream_sse_to_ndjson(const std::string& openai_body,
httplib::DataSink& client_sink,
ChunkConverter convert_chunk,
DoneBuilder build_done,
StreamFn call_router) {
httplib::DataSink adapter_sink;
std::string sse_buffer;
int eval_count = 0;
int prompt_eval_count = 0;
adapter_sink.is_writable = client_sink.is_writable;
adapter_sink.write = [&client_sink, &sse_buffer, &eval_count,
&prompt_eval_count, &convert_chunk](const char* data, size_t len) -> bool {
sse_buffer.append(data, len);
size_t pos;
while ((pos = sse_buffer.find('\n')) != std::string::npos) {
std::string line = sse_buffer.substr(0, pos);
sse_buffer.erase(0, pos + 1);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty() || line.find("data: ") != 0) {
continue;
}
std::string json_str = line.substr(6);
if (json_str == "[DONE]") {
continue;
}
try {
auto openai_chunk = json::parse(json_str);
// Track token counts from usage in final chunk
if (openai_chunk.contains("usage")) {
const auto& usage = openai_chunk["usage"];
if (usage.contains("prompt_tokens"))
prompt_eval_count = usage["prompt_tokens"].get<int>();
if (usage.contains("completion_tokens"))
eval_count = usage["completion_tokens"].get<int>();
}
auto ollama_chunk = convert_chunk(openai_chunk);
std::string ndjson = ollama_chunk.dump() + "\n";
if (!client_sink.write(ndjson.c_str(), ndjson.size())) {
return false;
}
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Failed to parse SSE chunk: " << e.what() << std::endl;
}
}
return true;
};
adapter_sink.done = [&client_sink, &eval_count, &prompt_eval_count, &build_done]() {
json done_msg = build_done(prompt_eval_count, eval_count);
std::string ndjson = done_msg.dump() + "\n";
client_sink.write(ndjson.c_str(), ndjson.size());
client_sink.done();
};
call_router(openai_body, adapter_sink);
}
// ============================================================================
// POST /api/chat — Ollama chat completion
// ============================================================================
void OllamaApi::handle_chat(const httplib::Request& req, httplib::Response& res) {
try {
auto request_json = json::parse(req.body);
std::string model = normalize_model_name(request_json.value("model", ""));
if (model.empty()) {
res.status = 400;
res.set_content(R"({"error":"model is required"})", "application/json");
return;
}
// Unload model if empty messages + keep_alive=0 (Ollama unload convention)
auto messages = request_json.value("messages", json::array());
if (messages.empty() && request_json.contains("keep_alive") &&
request_json["keep_alive"] == 0) {
LOG(INFO, "OllamaApi") << "POST /api/chat - Unloading model: " << model << std::endl;
try {
router_->unload_model(model);
} catch (...) {
// Model may not be loaded, that's fine
}
json ollama_res = {
{"model", model},
{"created_at", "2024-01-01T00:00:00Z"},
{"message", {{"role", "assistant"}, {"content", ""}}},
{"done", true},
{"done_reason", "unload"}
};
res.set_content(ollama_res.dump(), "application/json");
return;
}
// Auto-load the model
try {
auto_load_model(model);
} catch (const std::exception& e) {
res.status = 404;
json error = {{"error", "model '" + model + "' not found, try pulling it first"}};
res.set_content(error.dump(), "application/json");
return;
}
// Determine streaming
bool stream = request_json.value("stream", true); // Ollama defaults to streaming
// Convert to OpenAI format
auto openai_req = convert_ollama_to_openai_chat(request_json);
if (stream) {
LOG(INFO, "OllamaApi") << "POST /api/chat - Streaming (model: " << model << ")" << std::endl;
// Set streaming body as OpenAI format with stream=true
openai_req["stream"] = true;
std::string openai_body = openai_req.dump();
res.set_chunked_content_provider(
"application/x-ndjson",
[this, openai_body, model](size_t offset, httplib::DataSink& sink) {
if (offset > 0) return false;
stream_sse_to_ndjson(openai_body, sink,
// Convert each SSE chunk to Ollama chat format
[this, &model](const json& chunk) {
return convert_openai_delta_to_ollama(chunk, model);
},
// Build final done message
[&model](int prompt_eval_count, int eval_count) -> json {
return {
{"model", model}, {"created_at", "2024-01-01T00:00:00Z"},
{"message", {{"role", "assistant"}, {"content", ""}}},
{"done", true}, {"done_reason", "stop"},
{"total_duration", 0}, {"load_duration", 0},
{"prompt_eval_count", prompt_eval_count}, {"prompt_eval_duration", 0},
{"eval_count", eval_count}, {"eval_duration", 0}
};
},
// Router stream function
[this](const std::string& body, httplib::DataSink& s) {
router_->chat_completion_stream(body, s);
}
);
return false;
}
);
} else {
LOG(INFO, "OllamaApi") << "POST /api/chat - Non-streaming (model: " << model << ")" << std::endl;
auto openai_response = router_->chat_completion(openai_req);
auto ollama_response = convert_openai_chat_to_ollama(openai_response, model);
res.set_content(ollama_response.dump(), "application/json");
}
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Error in /api/chat: " << e.what() << std::endl;
res.status = 500;
json error = {{"error", std::string(e.what())}};
res.set_content(error.dump(), "application/json");
}
}
// ============================================================================
// POST /api/generate — Ollama text generation
// ============================================================================
void OllamaApi::handle_generate(const httplib::Request& req, httplib::Response& res) {
try {
auto request_json = json::parse(req.body);
std::string model = normalize_model_name(request_json.value("model", ""));
if (model.empty()) {
res.status = 400;
res.set_content(R"({"error":"model is required"})", "application/json");
return;
}
// Unload model if empty prompt + keep_alive=0 (Ollama unload convention)
std::string prompt = request_json.value("prompt", "");
if (prompt.empty() && request_json.contains("keep_alive") &&
request_json["keep_alive"] == 0) {
LOG(INFO, "OllamaApi") << "POST /api/generate - Unloading model: " << model << std::endl;
try {
router_->unload_model(model);
} catch (...) {
// Model may not be loaded, that's fine
}
json ollama_res = {
{"model", model},
{"created_at", "2024-01-01T00:00:00Z"},
{"response", ""},
{"done", true},
{"done_reason", "unload"}
};
res.set_content(ollama_res.dump(), "application/json");
return;
}
try {
auto_load_model(model);
} catch (const std::exception& e) {
res.status = 404;
json error = {{"error", "model '" + model + "' not found, try pulling it first"}};
res.set_content(error.dump(), "application/json");
return;
}
// Check if this is an image generation model
auto model_info = model_manager_->get_model_info(model);
ModelType model_type = get_model_type_from_labels(model_info.labels);
if (model_type == ModelType::IMAGE) {
// Route to image generation
handle_generate_image(request_json, res, model);
return;
}
bool stream = request_json.value("stream", true); // Ollama defaults to streaming
auto openai_req = convert_ollama_to_openai_completion(request_json);
if (stream) {
LOG(INFO, "OllamaApi") << "POST /api/generate - Streaming (model: " << model << ")" << std::endl;
openai_req["stream"] = true;
std::string openai_body = openai_req.dump();
res.set_chunked_content_provider(
"application/x-ndjson",
[this, openai_body, model](size_t offset, httplib::DataSink& sink) {
if (offset > 0) return false;
stream_sse_to_ndjson(openai_body, sink,
// Convert each SSE chunk to Ollama generate format
[&model](const json& openai_chunk) -> json {
json ollama_chunk = {
{"model", model}, {"created_at", "2024-01-01T00:00:00Z"},
{"done", false}
};
if (openai_chunk.contains("choices") && !openai_chunk["choices"].empty()) {
const auto& choice = openai_chunk["choices"][0];
if (choice.contains("text"))
ollama_chunk["response"] = choice["text"];
else if (choice.contains("delta") && choice["delta"].contains("content"))
ollama_chunk["response"] = choice["delta"]["content"];
else
ollama_chunk["response"] = "";
if (choice.contains("finish_reason") && !choice["finish_reason"].is_null()) {
ollama_chunk["done"] = true;
ollama_chunk["done_reason"] = choice["finish_reason"];
}
}
return ollama_chunk;
},
// Build final done message
[&model](int prompt_eval_count, int eval_count) -> json {
return {
{"model", model}, {"created_at", "2024-01-01T00:00:00Z"},
{"response", ""}, {"done", true}, {"done_reason", "stop"},
{"context", json::array()},
{"total_duration", 0}, {"load_duration", 0},
{"prompt_eval_count", prompt_eval_count}, {"prompt_eval_duration", 0},
{"eval_count", eval_count}, {"eval_duration", 0}
};
},
// Router stream function
[this](const std::string& body, httplib::DataSink& s) {
router_->completion_stream(body, s);
}
);
return false;
}
);
} else {
LOG(INFO, "OllamaApi") << "POST /api/generate - Non-streaming (model: " << model << ")" << std::endl;
auto openai_response = router_->completion(openai_req);
// Convert to Ollama generate format
json ollama_res;
ollama_res["model"] = model;
ollama_res["created_at"] = "2024-01-01T00:00:00Z";
ollama_res["done"] = true;
ollama_res["done_reason"] = "stop";
// Extract text from completion response
if (openai_response.contains("choices") && !openai_response["choices"].empty()) {
const auto& choice = openai_response["choices"][0];
ollama_res["response"] = choice.value("text", "");
} else {
ollama_res["response"] = "";
}
// Usage stats
if (openai_response.contains("usage")) {
const auto& usage = openai_response["usage"];
ollama_res["prompt_eval_count"] = usage.value("prompt_tokens", 0);
ollama_res["eval_count"] = usage.value("completion_tokens", 0);
}
ollama_res["total_duration"] = 0;
ollama_res["load_duration"] = 0;
ollama_res["prompt_eval_duration"] = 0;
ollama_res["eval_duration"] = 0;
ollama_res["context"] = json::array();
res.set_content(ollama_res.dump(), "application/json");
}
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Error in /api/generate: " << e.what() << std::endl;
res.status = 500;
json error = {{"error", std::string(e.what())}};
res.set_content(error.dump(), "application/json");
}
}
// ============================================================================
// Image generation helper for /api/generate (when model is an image model)
// ============================================================================
void OllamaApi::handle_generate_image(const json& request_json, httplib::Response& res, const std::string& model) {
try {
LOG(INFO, "OllamaApi") << "POST /api/generate - Image generation (model: " << model << ")" << std::endl;
std::string prompt = request_json.value("prompt", "");
// Extract image generation parameters from top-level or options
int width = 512;
int height = 512;
int steps = 0;
double cfg_scale = 0.0;
int seed = -1;
// Check top-level params first, then options
if (request_json.contains("width")) {
width = request_json["width"].get<int>();
} else if (request_json.contains("options") && request_json["options"].contains("width")) {
width = request_json["options"]["width"].get<int>();
}
if (request_json.contains("height")) {
height = request_json["height"].get<int>();
} else if (request_json.contains("options") && request_json["options"].contains("height")) {
height = request_json["options"]["height"].get<int>();
}
if (request_json.contains("steps")) {
steps = request_json["steps"].get<int>();
} else if (request_json.contains("options") && request_json["options"].contains("steps")) {
steps = request_json["options"]["steps"].get<int>();
}
if (request_json.contains("cfg_scale")) {
cfg_scale = request_json["cfg_scale"].get<double>();
} else if (request_json.contains("options") && request_json["options"].contains("cfg_scale")) {
cfg_scale = request_json["options"]["cfg_scale"].get<double>();
}
if (request_json.contains("seed")) {
seed = request_json["seed"].get<int>();
} else if (request_json.contains("options") && request_json["options"].contains("seed")) {
seed = request_json["options"]["seed"].get<int>();
}
// Build OpenAI-compatible image generation request
json openai_req;
openai_req["model"] = model;
openai_req["prompt"] = prompt;
openai_req["size"] = std::to_string(width) + "x" + std::to_string(height);
openai_req["response_format"] = "b64_json";
if (steps > 0) {
openai_req["steps"] = steps;
}
if (cfg_scale > 0.0) {
openai_req["cfg_scale"] = cfg_scale;
}
if (seed >= 0) {
openai_req["seed"] = seed;
}
auto openai_response = router_->image_generations(openai_req);
// Convert OpenAI response to Ollama format
json ollama_res;
ollama_res["model"] = model;
ollama_res["created_at"] = "2024-01-01T00:00:00Z";
ollama_res["response"] = "";
ollama_res["done"] = true;
// Extract base64 image from OpenAI response
if (openai_response.contains("data") && !openai_response["data"].empty()) {
ollama_res["image"] = openai_response["data"][0].value("b64_json", "");
} else {
ollama_res["image"] = "";
}
res.set_content(ollama_res.dump(), "application/json");
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Error in image generation: " << e.what() << std::endl;
res.status = 500;
json error = {{"error", std::string(e.what())}};
res.set_content(error.dump(), "application/json");
}
}
// ============================================================================
// GET /api/tags — List models
// ============================================================================
void OllamaApi::handle_tags(const httplib::Request& req, httplib::Response& res) {
try {
auto models = model_manager_->get_downloaded_models();
json response;
response["models"] = json::array();
for (const auto& [id, info] : models) {
response["models"].push_back(build_ollama_model_entry(id, info));
}
res.set_content(response.dump(), "application/json");
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Error in /api/tags: " << e.what() << std::endl;
res.status = 500;
json error = {{"error", std::string(e.what())}};
res.set_content(error.dump(), "application/json");
}
}
// ============================================================================
// POST /api/show — Show model info
// ============================================================================
void OllamaApi::handle_show(const httplib::Request& req, httplib::Response& res) {
try {
auto request_json = json::parse(req.body);
std::string name = normalize_model_name((!request_json.value("model", "").empty() && request_json.value("name", "").empty()) ?
request_json.value("model", "") : request_json.value("name", ""));
if (name.empty()) {
res.status = 400;
res.set_content(R"({"error":"name is required"})", "application/json");
return;
}
if (!model_manager_->model_exists(name)) {
res.status = 404;
json error = {{"error", "model '" + name + "' not found"}};
res.set_content(error.dump(), "application/json");
return;
}
auto info = model_manager_->get_model_info(name);
json response = {
{"modelfile", "# Modelfile generated by Lemonade\nFROM " + info.checkpoint()},
{"parameters", ""},
{"template", ""},
{"details", build_ollama_details(name, info.recipe, info.checkpoint())},
{"model_info", {
{"general.architecture", info.recipe},
{"general.file_type", 0},
{"general.parameter_count", 0},
{"general.quantization_version", 0}
}}
};
res.set_content(response.dump(), "application/json");
} catch (const std::exception& e) {
LOG(ERROR, "OllamaApi") << "Error in /api/show: " << e.what() << std::endl;
res.status = 500;
json error = {{"error", std::string(e.what())}};
res.set_content(error.dump(), "application/json");
}
}
// ============================================================================
// DELETE /api/delete — Delete a model
// ============================================================================
void OllamaApi::handle_delete(const httplib::Request& req, httplib::Response& res) {
try {
auto request_json = json::parse(req.body);
// Ollama uses "name" field (not "model")
std::string name = normalize_model_name(request_json.value("name", request_json.value("model", "")));