DEV: adding a concept of prompt builder
This is half baked and not working, but starts demonstrating the idea
This commit is contained in:
parent
7790313b1b
commit
a8f54e2709
|
@ -219,6 +219,22 @@ module DiscourseAi
|
||||||
0
|
0
|
||||||
end
|
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")
|
def bot_prompt_with_topic_context(post, prompt: "topic")
|
||||||
messages = []
|
messages = []
|
||||||
conversation = conversation_context(post)
|
conversation = conversation_context(post)
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue