From 7de81149999ca8f248885036961fedf0e02b1b22 Mon Sep 17 00:00:00 2001 From: James Montgomery Date: Sat, 17 Aug 2024 18:39:46 -0400 Subject: [PATCH 1/2] Add support for context in generate endpoint. --- include/ollama.hpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/include/ollama.hpp b/include/ollama.hpp index 2cd8e73..6da2e0a 100644 --- a/include/ollama.hpp +++ b/include/ollama.hpp @@ -381,6 +381,13 @@ class Ollama Ollama(): Ollama("http://localhost:11434") {} ~Ollama() { delete this->cli; } + ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector& images=std::vector()) + { + ollama::request request(model, prompt, options, false, images); + if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"]; + return generate(request); + } + ollama::response generate(const std::string& model,const std::string& prompt, const json& options=nullptr, const std::vector& images=std::vector()) { ollama::request request(model, prompt, options, false, images); @@ -411,6 +418,13 @@ class Ollama return response; } + bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function on_receive_token, const json& options=nullptr, const std::vector& images=std::vector()) + { + ollama::request request(model, prompt, options, true, images); + if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"]; + return generate(request, on_receive_token); + } + bool generate(const std::string& model,const std::string& prompt, std::function on_receive_token, const json& options=nullptr, const std::vector& images=std::vector()) { ollama::request request(model, prompt, options, true, images); @@ -850,11 +864,16 @@ namespace ollama ollama.setServerURL(server_url); } - inline ollama::response generate(const std::string& model,const std::string& prompt,const json& options=nullptr, const std::vector& images=std::vector()) + inline ollama::response generate(const std::string& model, const std::string& prompt, const json& options=nullptr, const std::vector& images=std::vector()) { return ollama.generate(model, prompt, options, images); } + ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector& images=std::vector()) + { + return ollama.generate(model, prompt, context, options, images); + } + inline ollama::response generate(const ollama::request& request) { return ollama.generate(request); @@ -865,6 +884,11 @@ namespace ollama return ollama.generate(model, prompt, on_receive_response, options, images); } + inline bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function on_receive_response, const json& options=nullptr, const std::vector& images=std::vector()) + { + return ollama.generate(model, prompt, context, on_receive_response, options, images); + } + inline bool generate(ollama::request& request, std::function on_receive_response) { return ollama.generate(request, on_receive_response); From de85c5b1b653bba54f1f11d8c03ea2b61baa9814 Mon Sep 17 00:00:00 2001 From: James Montgomery Date: Sun, 18 Aug 2024 07:13:54 -0400 Subject: [PATCH 2/2] Add singleheader and tests for generation with context. --- singleheader/ollama.hpp | 26 +++++++++++++++++++++++++- test/test.cpp | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/singleheader/ollama.hpp b/singleheader/ollama.hpp index 3f68add..0505473 100644 --- a/singleheader/ollama.hpp +++ b/singleheader/ollama.hpp @@ -35171,6 +35171,13 @@ class Ollama Ollama(): Ollama("http://localhost:11434") {} ~Ollama() { delete this->cli; } + ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector& images=std::vector()) + { + ollama::request request(model, prompt, options, false, images); + if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"]; + return generate(request); + } + ollama::response generate(const std::string& model,const std::string& prompt, const json& options=nullptr, const std::vector& images=std::vector()) { ollama::request request(model, prompt, options, false, images); @@ -35201,6 +35208,13 @@ class Ollama return response; } + bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function on_receive_token, const json& options=nullptr, const std::vector& images=std::vector()) + { + ollama::request request(model, prompt, options, true, images); + if ( context.as_json().contains("context") ) request["context"] = context.as_json()["context"]; + return generate(request, on_receive_token); + } + bool generate(const std::string& model,const std::string& prompt, std::function on_receive_token, const json& options=nullptr, const std::vector& images=std::vector()) { ollama::request request(model, prompt, options, true, images); @@ -35640,11 +35654,16 @@ namespace ollama ollama.setServerURL(server_url); } - inline ollama::response generate(const std::string& model,const std::string& prompt,const json& options=nullptr, const std::vector& images=std::vector()) + inline ollama::response generate(const std::string& model, const std::string& prompt, const json& options=nullptr, const std::vector& images=std::vector()) { return ollama.generate(model, prompt, options, images); } + ollama::response generate(const std::string& model,const std::string& prompt, const ollama::response& context, const json& options=nullptr, const std::vector& images=std::vector()) + { + return ollama.generate(model, prompt, context, options, images); + } + inline ollama::response generate(const ollama::request& request) { return ollama.generate(request); @@ -35655,6 +35674,11 @@ namespace ollama return ollama.generate(model, prompt, on_receive_response, options, images); } + inline bool generate(const std::string& model,const std::string& prompt, ollama::response& context, std::function on_receive_response, const json& options=nullptr, const std::vector& images=std::vector()) + { + return ollama.generate(model, prompt, context, on_receive_response, options, images); + } + inline bool generate(ollama::request& request, std::function on_receive_response) { return ollama.generate(request, on_receive_response); diff --git a/test/test.cpp b/test/test.cpp index 58ebc11..d50e2a2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -108,6 +108,14 @@ TEST_SUITE("Ollama Tests") { CHECK( response.as_json().contains("response") == true ); } + TEST_CASE("Generation with Context") { + + ollama::response context = ollama::generate(test_model, "Why is the sky blue?", options); + + ollama::response response = ollama::generate(test_model, "Tell me more about this.", context, options); + + CHECK( response.as_json().contains("response") == true ); + } std::atomic done{false}; std::string streamed_response; @@ -130,6 +138,16 @@ TEST_SUITE("Ollama Tests") { CHECK( streamed_response != "" ); } + TEST_CASE("Streaming Generation with Context") { + + ollama::response context = ollama::generate(test_model, "Why is the sky blue?", options); + + std::function response_callback = on_receive_response; + ollama::generate(test_model, "Tell me more about this.", context, response_callback, options); + + CHECK( streamed_response!="" ); + } + TEST_CASE("Non-Singleton Generation") { Ollama my_ollama_server("http://localhost:11434");