File tree 2 files changed +32
-8
lines changed
2 files changed +32
-8
lines changed Original file line number Diff line number Diff line change 62
62
#include " Base64.h"
63
63
64
64
#include < string>
65
+ #include < memory>
65
66
#include < fstream>
66
67
#include < iostream>
68
+ #include < numeric>
67
69
#include < functional>
68
70
#include < exception>
69
71
#include < initializer_list>
@@ -421,13 +423,22 @@ class Ollama
421
423
std::string request_string = request.dump ();
422
424
if (ollama::log_requests) std::cout << request_string << std::endl;
423
425
424
- auto stream_callback = [on_receive_token](const char *data, size_t data_length)->bool {
426
+ std::shared_ptr<std::vector<std::string>> partial_responses = std::make_shared<std::vector<std::string>>();
427
+
428
+ auto stream_callback = [on_receive_token, partial_responses](const char *data, size_t data_length)->bool {
425
429
426
430
std::string message (data, data_length);
427
431
if (ollama::log_replies) std::cout << message << std::endl;
428
- ollama::response response (message);
429
- on_receive_token (response);
430
-
432
+ try
433
+ {
434
+ partial_responses->push_back (message);
435
+ std::string total_response = std::accumulate (partial_responses->begin (), partial_responses->end (), std::string (" " ));
436
+ ollama::response response (total_response);
437
+ partial_responses->clear ();
438
+ on_receive_token (response);
439
+ }
440
+ catch (...) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
441
+
431
442
return true ;
432
443
};
433
444
@@ -810,6 +821,7 @@ class Ollama
810
821
return true ;
811
822
}
812
823
824
+
813
825
std::string server_url;
814
826
httplib::Client *cli;
815
827
Original file line number Diff line number Diff line change @@ -34852,8 +34852,10 @@ class Base64 {
34852
34852
*/
34853
34853
34854
34854
#include <string>
34855
+ #include <memory>
34855
34856
#include <fstream>
34856
34857
#include <iostream>
34858
+ #include <numeric>
34857
34859
#include <functional>
34858
34860
#include <exception>
34859
34861
#include <initializer_list>
@@ -35211,13 +35213,22 @@ class Ollama
35211
35213
std::string request_string = request.dump();
35212
35214
if (ollama::log_requests) std::cout << request_string << std::endl;
35213
35215
35214
- auto stream_callback = [on_receive_token](const char *data, size_t data_length)->bool{
35216
+ std::shared_ptr<std::vector<std::string>> partial_responses = std::make_shared<std::vector<std::string>>();
35217
+
35218
+ auto stream_callback = [on_receive_token, partial_responses](const char *data, size_t data_length)->bool{
35215
35219
35216
35220
std::string message(data, data_length);
35217
35221
if (ollama::log_replies) std::cout << message << std::endl;
35218
- ollama::response response(message);
35219
- on_receive_token(response);
35220
-
35222
+ try
35223
+ {
35224
+ partial_responses->push_back(message);
35225
+ std::string total_response = std::accumulate(partial_responses->begin(), partial_responses->end(), std::string(""));
35226
+ ollama::response response(total_response);
35227
+ partial_responses->clear();
35228
+ on_receive_token(response);
35229
+ }
35230
+ catch (...) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
35231
+
35221
35232
return true;
35222
35233
};
35223
35234
@@ -35600,6 +35611,7 @@ class Ollama
35600
35611
return true;
35601
35612
}
35602
35613
35614
+
35603
35615
std::string server_url;
35604
35616
httplib::Client *cli;
35605
35617
You can’t perform that action at this time.
0 commit comments