Roman Rizzi 30242a27e6
REFACTOR: Move personas into its own module. (#1233)
This change moves all the personas code into its own module. We want to treat them as a building block features can built on top of, same as `Completions::Llm`.

The code to title a message was moved from `Bot` to `Playground`.
2025-03-31 14:42:33 -03:00

54 lines
1.4 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module Personas
module ArtifactUpdateStrategies
class InvalidFormatError < StandardError
end
class Base
attr_reader :post, :user, :artifact, :artifact_version, :instructions, :llm
def initialize(llm:, post:, user:, artifact:, artifact_version:, instructions:)
@llm = llm
@post = post
@user = user
@artifact = artifact
@artifact_version = artifact_version
@instructions = instructions
end
def apply(&progress)
changes = generate_changes(&progress)
parsed_changes = parse_changes(changes)
apply_changes(parsed_changes)
end
private
def generate_changes(&progress)
response = +""
llm.generate(build_prompt, user: user) do |partial|
progress.call(partial) if progress
response << partial
end
response
end
def build_prompt
# To be implemented by subclasses
raise NotImplementedError
end
def parse_changes(response)
# To be implemented by subclasses
raise NotImplementedError
end
def apply_changes(changes)
# To be implemented by subclasses
raise NotImplementedError
end
end
end
end
end