Class: PromptManager::Prompt
- Inherits:
-
Object
- Object
- PromptManager::Prompt
- Defined in:
- lib/prompt_manager/prompt.rb
Overview
This class is responsible for managing prompts which can be utilized by generative AI processes. This includes creation, retrieval, storage management, as well as building prompts with replacement of parameterized values and comment removal. It communicates with a storage system through a storage adapter.
Directives are collected into an Array where each entry is an Array of two elements. The first is the directive name as a String. The second is a string of parameters used by the directive.
Directives are collected from the prompt after keyword substitution has occured. This means that directives within a prompt can be dynamic.
PromptManager does not execute directives. They are made available to be passed on to down stream process.
Constant Summary collapse
- COMMENT_SIGNAL =
lines beginning with this are a comment
'#'
- DIRECTIVE_SIGNAL =
Like the old IBM JCL
'//'
- DEFAULT_PARAMETER_REGEX =
/(\[[A-Z _|]+\])/
Class Attribute Summary collapse
-
.parameter_regex ⇒ Object
Returns the value of attribute parameter_regex.
-
.storage_adapter ⇒ Object
Returns the value of attribute storage_adapter.
Instance Attribute Summary collapse
-
#db ⇒ Object
SMELL: Does the db (aka storage adapter) really need to be accessible by the main program?.
-
#directives ⇒ Object
SMELL: Does the db (aka storage adapter) really need to be accessible by the main program?.
-
#id ⇒ Object
SMELL: Does the db (aka storage adapter) really need to be accessible by the main program?.
-
#parameters ⇒ Object
SMELL: Does the db (aka storage adapter) really need to be accessible by the main program?.
-
#text ⇒ Object
SMELL: Does the db (aka storage adapter) really need to be accessible by the main program?.
Class Method Summary collapse
- .create(id:, text: "", parameters: {}) ⇒ Object
- .method_missing(method_name, *args, &block) ⇒ Object
- .respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- .search(for_what) ⇒ Object
Instance Method Summary collapse
-
#build ⇒ Object
Build the @prompt String by replacing the keywords with there parameterized values and removing all the comments.
-
#delete ⇒ Object
Delete this prompt from the Storage system.
-
#initialize(id: nil, context: []) ⇒ Prompt
constructor
Retrieve the specific prompt ID from the Storage system.
- #keywords ⇒ Object
-
#save ⇒ Object
Save the prompt to the Storage system.
-
#to_s ⇒ Object
(also: #prompt)
Return tje prompt text suitable for passing to a gen-AI process.
-
#validate_arguments(prompt_id, prompts_db) ⇒ Object
Make sure the ID and DB are good-to-go.
Constructor Details
#initialize(id: nil, context: []) ⇒ Prompt
Retrieve the specific prompt ID from the Storage system.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/prompt_manager/prompt.rb', line 69 def initialize( id: nil, # A String name for the prompt context: [] # FIXME: Array of Strings or Pathname? ) @id = id @db = self.class.storage_adapter validate_arguments(@id, @db) @record = db.get(id: id) @text = @record[:text] @parameters = @record[:parameters] @keywords = [] # Array of String @directives = [] # Array of arrays. directive is first entry, rest are parameters update_keywords build end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
Let the storage adapter instance take a crake at these unknown methods. Don’t care what the args are, just pass the prompt’s ID.
209 210 211 212 213 214 215 |
# File 'lib/prompt_manager/prompt.rb', line 209 def method_missing(method_name, *args, &block) if db.respond_to?(method_name) db.send(method_name, id, &block) else super end end |
Class Attribute Details
.parameter_regex ⇒ Object
Returns the value of attribute parameter_regex.
29 30 31 |
# File 'lib/prompt_manager/prompt.rb', line 29 def parameter_regex @parameter_regex end |
.storage_adapter ⇒ Object
Returns the value of attribute storage_adapter.
29 30 31 |
# File 'lib/prompt_manager/prompt.rb', line 29 def storage_adapter @storage_adapter end |
Instance Attribute Details
#db ⇒ Object
SMELL: Does the db (aka storage adapter) really need
to be accessible by the main program?
65 66 67 |
# File 'lib/prompt_manager/prompt.rb', line 65 def db @db end |
#directives ⇒ Object
SMELL: Does the db (aka storage adapter) really need
to be accessible by the main program?
65 66 67 |
# File 'lib/prompt_manager/prompt.rb', line 65 def directives @directives end |
#id ⇒ Object
SMELL: Does the db (aka storage adapter) really need
to be accessible by the main program?
65 66 67 |
# File 'lib/prompt_manager/prompt.rb', line 65 def id @id end |
#parameters ⇒ Object
SMELL: Does the db (aka storage adapter) really need
to be accessible by the main program?
65 66 67 |
# File 'lib/prompt_manager/prompt.rb', line 65 def parameters @parameters end |
#text ⇒ Object
SMELL: Does the db (aka storage adapter) really need
to be accessible by the main program?
65 66 67 |
# File 'lib/prompt_manager/prompt.rb', line 65 def text @text end |
Class Method Details
.create(id:, text: "", parameters: {}) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/prompt_manager/prompt.rb', line 33 def create(id:, text: "", parameters: {}) storage_adapter.save( id: id, text: text, parameters: parameters ) new(id: id) end |
.method_missing(method_name, *args, &block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/prompt_manager/prompt.rb', line 49 def method_missing(method_name, *args, &block) if storage_adapter.respond_to?(method_name) storage_adapter.send(method_name, *args, &block) else super end end |
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
58 59 60 |
# File 'lib/prompt_manager/prompt.rb', line 58 def respond_to_missing?(method_name, include_private = false) storage_adapter.respond_to?(method_name, include_private) || super end |
.search(for_what) ⇒ Object
44 45 46 |
# File 'lib/prompt_manager/prompt.rb', line 44 def search(for_what) storage_adapter.search(for_what) end |
Instance Method Details
#build ⇒ Object
Build the @prompt String by replacing the keywords with there parameterized values and removing all the comments.
126 127 128 129 130 131 132 133 134 |
# File 'lib/prompt_manager/prompt.rb', line 126 def build @prompt = text.gsub(self.class.parameter_regex) do |match| param_name = match Array(parameters[param_name]).last || match end save_directives(@prompt) remove_comments end |
#delete ⇒ Object
Delete this prompt from the Storage system
117 118 119 |
# File 'lib/prompt_manager/prompt.rb', line 117 def delete db.delete(id: id) end |
#keywords ⇒ Object
137 138 139 |
# File 'lib/prompt_manager/prompt.rb', line 137 def keywords update_keywords end |
#save ⇒ Object
Save the prompt to the Storage system
107 108 109 110 111 112 113 |
# File 'lib/prompt_manager/prompt.rb', line 107 def save db.save( id: id, text: text, parameters: parameters ) end |
#to_s ⇒ Object Also known as: prompt
Return tje prompt text suitable for passing to a gen-AI process.
100 101 102 |
# File 'lib/prompt_manager/prompt.rb', line 100 def to_s build end |
#validate_arguments(prompt_id, prompts_db) ⇒ Object
Make sure the ID and DB are good-to-go
92 93 94 95 |
# File 'lib/prompt_manager/prompt.rb', line 92 def validate_arguments(prompt_id, prompts_db) raise ArgumentError, 'id cannot be blank' if prompt_id.nil? || id.strip.empty? raise(ArgumentError, 'storage_adapter is not set') if prompts_db.nil? end |