From a28f61bc8b5485af2909e323906da165a64cf562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Mon, 9 Dec 2024 15:02:58 -0500 Subject: [PATCH] examples: add prompt composer --- examples/Advanced/AI/PromptComposer.hpp | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 examples/Advanced/AI/PromptComposer.hpp diff --git a/examples/Advanced/AI/PromptComposer.hpp b/examples/Advanced/AI/PromptComposer.hpp new file mode 100644 index 00000000..0798fac8 --- /dev/null +++ b/examples/Advanced/AI/PromptComposer.hpp @@ -0,0 +1,69 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#include + +/* SPDX-License-Identifier: GPL-3.0-or-later */ + +namespace ai +{ +struct PromptComposer +{ + halp_meta(name, "Prompt composer") + halp_meta(c_name, "prompt_composer") + halp_meta(author, "Jean-Michaƫl Celerier") + halp_meta(category, "AI") + halp_meta(description, "Generate a prompt with percentages") + halp_meta(manual_url, "https://ossia.io/score-docs/processes/prompt-composer.html") + halp_meta(uuid, "a4227e94-cf7d-4776-9aa0-2f384be7d97f") + + struct inputs + { + struct : halp::lineedit<"Keywords", ""> + { + static std::function + on_controller_interaction() + { + return [](PromptComposer& object, std::string_view value) { + int n = std::count(value.begin(), value.end(), ','); + object.inputs.in_i.request_port_resize(n + 1); + }; + } + } controller; + + halp::dynamic_port> in_i; + } inputs; + + struct + { + halp::val_port<"Output", std::string> out; + } outputs; + + void operator()() + { + outputs.out.value = ""; + thread_local std::vector strs; + static const auto loc = std::locale("C"); + strs.clear(); + boost::split(strs, inputs.controller.value, boost::is_any_of(",")); + auto it = strs.begin(); + for(auto& val : inputs.in_i.ports) + { + boost::trim(*it, loc); + outputs.out.value += fmt::format("({}:{}), ", *it, val.value); + ++it; + } + if(outputs.out.value.ends_with(", ")) + { + outputs.out.value.pop_back(); + outputs.out.value.pop_back(); + } + } +}; + +}