From a8f54e270942f2c969aa4704f0d72e59a611774a Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 25 Aug 2023 15:54:16 +1000 Subject: [PATCH] DEV: adding a concept of prompt builder This is half baked and not working, but starts demonstrating the idea --- lib/modules/ai_bot/bot.rb | 16 ++++++++ .../prompting/anthropic_prompt_builder.rb | 0 .../prompting/open_ai_prompt_builder.rb | 0 lib/shared/prompting/prompt_builder.rb | 38 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 lib/shared/prompting/anthropic_prompt_builder.rb create mode 100644 lib/shared/prompting/open_ai_prompt_builder.rb create mode 100644 lib/shared/prompting/prompt_builder.rb diff --git a/lib/modules/ai_bot/bot.rb b/lib/modules/ai_bot/bot.rb index d0159e78..d1d1be82 100644 --- a/lib/modules/ai_bot/bot.rb +++ b/lib/modules/ai_bot/bot.rb @@ -219,6 +219,22 @@ module DiscourseAi 0 end + def bot_prompt_with_topic_context2(post) + builder = tokenizer.build_prompt(max_tokens: prompt_limit) + builder << { user: bot_user.username, content: rendered_system_prompt, type: :system } + + conversation_context(post).each do |raw, username, function| + builder.unshift( + user: username, + content: raw, + type: :username == bot_user.username ? :assistant : :user, + ) + break if builder.full? + end + + builder.generate + end + def bot_prompt_with_topic_context(post, prompt: "topic") messages = [] conversation = conversation_context(post) diff --git a/lib/shared/prompting/anthropic_prompt_builder.rb b/lib/shared/prompting/anthropic_prompt_builder.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/shared/prompting/open_ai_prompt_builder.rb b/lib/shared/prompting/open_ai_prompt_builder.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/shared/prompting/prompt_builder.rb b/lib/shared/prompting/prompt_builder.rb new file mode 100644 index 00000000..d0892892 --- /dev/null +++ b/lib/shared/prompting/prompt_builder.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module DiscourseAi + module Prompting + class PromptBuilder + def initialize(tokenizer:, max_tokens:) + @tokenizer = tokenizer + @max_tokens = max_tokens + @contents = [] + end + + def full? + end + + def <<(content:, type:, user: nil) + validate_type(type) + + @contents << { content: content, type: type, user: user } + end + + def unshift(content:, type:, user: nil) + validate_type(type) + + @contents.unshift(content: content, type: type, user: user) + end + + def generate + raise NotImplemented + end + + def validate_type(type) + if !%i[system assistant user].include?(type) + raise ArgumentError, "type must be one of :system, :assistant, :user" + end + end + end + end +end