Skip to content

Commit 49c2c74

Browse files
authored
Merge pull request #23 from jmont-dev/manual_requests
Add support for context in generate endpoint
2 parents d70ded4 + de85c5b commit 49c2c74

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

Diff for: include/ollama.hpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ class Ollama
381381
Ollama(): Ollama("http://localhost:11434") {}
382382
~Ollama() { delete this->cli; }
383383

384+
ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
385+
{
386+
ollama::request request(model, prompt, options, false, images);
387+
if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"];
388+
return generate(request);
389+
}
390+
384391
ollama::response generate(const std::string& model,const std::string& prompt, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
385392
{
386393
ollama::request request(model, prompt, options, false, images);
@@ -411,6 +418,13 @@ class Ollama
411418
return response;
412419
}
413420

421+
bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function<void(const ollama::response&)> on_receive_token, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
422+
{
423+
ollama::request request(model, prompt, options, true, images);
424+
if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"];
425+
return generate(request, on_receive_token);
426+
}
427+
414428
bool generate(const std::string& model,const std::string& prompt, std::function<void(const ollama::response&)> on_receive_token, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
415429
{
416430
ollama::request request(model, prompt, options, true, images);
@@ -850,11 +864,16 @@ namespace ollama
850864
ollama.setServerURL(server_url);
851865
}
852866

853-
inline ollama::response generate(const std::string& model,const std::string& prompt,const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
867+
inline ollama::response generate(const std::string& model, const std::string& prompt, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
854868
{
855869
return ollama.generate(model, prompt, options, images);
856870
}
857871

872+
ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
873+
{
874+
return ollama.generate(model, prompt, context, options, images);
875+
}
876+
858877
inline ollama::response generate(const ollama::request& request)
859878
{
860879
return ollama.generate(request);
@@ -865,6 +884,11 @@ namespace ollama
865884
return ollama.generate(model, prompt, on_receive_response, options, images);
866885
}
867886

887+
inline bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function<void(const ollama::response&)> on_receive_response, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
888+
{
889+
return ollama.generate(model, prompt, context, on_receive_response, options, images);
890+
}
891+
868892
inline bool generate(ollama::request& request, std::function<void(const ollama::response&)> on_receive_response)
869893
{
870894
return ollama.generate(request, on_receive_response);

Diff for: singleheader/ollama.hpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -35171,6 +35171,13 @@ class Ollama
3517135171
Ollama(): Ollama("http://localhost:11434") {}
3517235172
~Ollama() { delete this->cli; }
3517335173

35174+
ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
35175+
{
35176+
ollama::request request(model, prompt, options, false, images);
35177+
if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"];
35178+
return generate(request);
35179+
}
35180+
3517435181
ollama::response generate(const std::string& model,const std::string& prompt, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
3517535182
{
3517635183
ollama::request request(model, prompt, options, false, images);
@@ -35201,6 +35208,13 @@ class Ollama
3520135208
return response;
3520235209
}
3520335210

35211+
bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function<void(const ollama::response&)> on_receive_token, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
35212+
{
35213+
ollama::request request(model, prompt, options, true, images);
35214+
if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"];
35215+
return generate(request, on_receive_token);
35216+
}
35217+
3520435218
bool generate(const std::string& model,const std::string& prompt, std::function<void(const ollama::response&)> on_receive_token, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
3520535219
{
3520635220
ollama::request request(model, prompt, options, true, images);
@@ -35640,11 +35654,16 @@ namespace ollama
3564035654
ollama.setServerURL(server_url);
3564135655
}
3564235656

35643-
inline ollama::response generate(const std::string& model,const std::string& prompt,const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
35657+
inline ollama::response generate(const std::string& model, const std::string& prompt, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
3564435658
{
3564535659
return ollama.generate(model, prompt, options, images);
3564635660
}
3564735661

35662+
ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
35663+
{
35664+
return ollama.generate(model, prompt, context, options, images);
35665+
}
35666+
3564835667
inline ollama::response generate(const ollama::request& request)
3564935668
{
3565035669
return ollama.generate(request);
@@ -35655,6 +35674,11 @@ namespace ollama
3565535674
return ollama.generate(model, prompt, on_receive_response, options, images);
3565635675
}
3565735676

35677+
inline bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function<void(const ollama::response&)> on_receive_response, const json& options=nullptr, const std::vector<std::string>& images=std::vector<std::string>())
35678+
{
35679+
return ollama.generate(model, prompt, context, on_receive_response, options, images);
35680+
}
35681+
3565835682
inline bool generate(ollama::request& request, std::function<void(const ollama::response&)> on_receive_response)
3565935683
{
3566035684
return ollama.generate(request, on_receive_response);

Diff for: test/test.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ TEST_SUITE("Ollama Tests") {
108108
CHECK( response.as_json().contains("response") == true );
109109
}
110110

111+
TEST_CASE("Generation with Context") {
112+
113+
ollama::response context = ollama::generate(test_model, "Why is the sky blue?", options);
114+
115+
ollama::response response = ollama::generate(test_model, "Tell me more about this.", context, options);
116+
117+
CHECK( response.as_json().contains("response") == true );
118+
}
111119

112120
std::atomic<bool> done{false};
113121
std::string streamed_response;
@@ -130,6 +138,16 @@ TEST_SUITE("Ollama Tests") {
130138
CHECK( streamed_response != "" );
131139
}
132140

141+
TEST_CASE("Streaming Generation with Context") {
142+
143+
ollama::response context = ollama::generate(test_model, "Why is the sky blue?", options);
144+
145+
std::function<void(const ollama::response&)> response_callback = on_receive_response;
146+
ollama::generate(test_model, "Tell me more about this.", context, response_callback, options);
147+
148+
CHECK( streamed_response!="" );
149+
}
150+
133151
TEST_CASE("Non-Singleton Generation") {
134152

135153
Ollama my_ollama_server("http://localhost:11434");

0 commit comments

Comments
 (0)