You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
while (!done) { std::this_thread::sleep_for(std::chrono::microseconds(100) ); }
271
277
new_thread.join();
272
278
```
279
+
The return value of the function determines whether to continue streaming or stop. This is useful in cases where you want to stop immediately instead of waiting for an entire response to return.
280
+
273
281
### Using Images
274
282
Generations can include images for vision-enabled models such as `llava`. The `ollama::image` class can load an image from a file and encode it as a [base64](https://en.wikipedia.org/wiki/Base64) string.
275
283
@@ -352,14 +360,17 @@ The default chat generation does not stream tokens and will return the entire re
catch (const ollama::invalid_json_exception& e) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
459
461
460
-
returntrue;
462
+
returncontinue_stream;
461
463
};
462
464
463
465
if (auto res = this->cli->Post("/api/generate", request_string, "application/json", stream_callback)) { returntrue; }
464
-
else { if (ollama::use_exceptions) throwollama::exception( "No response from server returned at URL"+this->server_url+" Error: "+httplib::to_string( res.error() ) ); }
466
+
elseif (res.error()==httplib::Error::Canceled) { /* Request cancelled by user. */returntrue; }
467
+
else { if (ollama::use_exceptions) throwollama::exception( "No response from server returned at URL "+this->server_url+" Error: "+httplib::to_string( res.error() ) ); }
auto stream_callback = [on_receive_token, partial_responses](constchar *data, size_t data_length)->bool{
519
522
520
523
std::string message(data, data_length);
524
+
bool continue_stream = true;
525
+
521
526
if (ollama::log_replies) std::cout << message << std::endl;
522
527
try
523
528
{
@@ -527,14 +532,15 @@ class Ollama
527
532
partial_responses->clear();
528
533
529
534
if ( response.has_error() ) { if (ollama::use_exceptions) throwollama::exception("Ollama response returned error: "+response.get_error() ); }
530
-
on_receive_token(response);
535
+
continue_stream = on_receive_token(response);
531
536
}
532
537
catch (const ollama::invalid_json_exception& e) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
533
538
534
-
returntrue;
539
+
returncontinue_stream;
535
540
};
536
541
537
542
if (auto res = this->cli->Post("/api/chat", request_string, "application/json", stream_callback)) { returntrue; }
543
+
elseif (res.error()==httplib::Error::Canceled) { /* Request cancelled by user. */returntrue; }
538
544
else { if (ollama::use_exceptions) throwollama::exception( "No response from server returned at URL"+this->server_url+" Error: "+httplib::to_string( res.error() ) ); }
0 commit comments