Class: Promptcraft::Conversation
- Inherits:
-
Object
- Object
- Promptcraft::Conversation
- Extended by:
- Helpers
- Includes:
- Helpers
- Defined in:
- lib/promptcraft/conversation.rb
Instance Attribute Summary collapse
-
#llm ⇒ Object
Returns the value of attribute llm.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#system_prompt ⇒ Object
Returns the value of attribute system_prompt.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
Class Method Summary collapse
- .build_from(doc) ⇒ Object
-
.from_messages(messages) ⇒ Object
Class method to create a Conversation from an array of messages.
- .load_from_file(filename) ⇒ Object
- .load_from_io(io = $stdin) ⇒ Object
Instance Method Summary collapse
- #add_message(role:, content:) ⇒ Object
-
#initialize(system_prompt:, messages: [], llm: nil, temperature: 0) ⇒ Conversation
constructor
A new instance of Conversation.
- #save_to_file(filename) ⇒ Object
- #to_json ⇒ Object
- #to_messages ⇒ Object
-
#to_yaml ⇒ Object
system_prompt: ‘I like to solve maths problems.’ messages: - role: “user” content: “What is 2+2?” - role: assistant content: 2 + 2 = 4.
Methods included from Helpers
deep_stringify_keys, deep_symbolize_keys
Constructor Details
#initialize(system_prompt:, messages: [], llm: nil, temperature: 0) ⇒ Conversation
Returns a new instance of Conversation.
10 11 12 13 14 15 |
# File 'lib/promptcraft/conversation.rb', line 10 def initialize(system_prompt:, messages: [], llm: nil, temperature: 0) @system_prompt = system_prompt = @llm = llm @temperature = temperature end |
Instance Attribute Details
#llm ⇒ Object
Returns the value of attribute llm.
8 9 10 |
# File 'lib/promptcraft/conversation.rb', line 8 def llm @llm end |
#messages ⇒ Object
Returns the value of attribute messages.
7 8 9 |
# File 'lib/promptcraft/conversation.rb', line 7 def end |
#system_prompt ⇒ Object
Returns the value of attribute system_prompt.
7 8 9 |
# File 'lib/promptcraft/conversation.rb', line 7 def system_prompt @system_prompt end |
#temperature ⇒ Object
Returns the value of attribute temperature.
7 8 9 |
# File 'lib/promptcraft/conversation.rb', line 7 def temperature @temperature end |
Class Method Details
.build_from(doc) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/promptcraft/conversation.rb', line 47 def build_from(doc) if doc.is_a?(Hash) doc = deep_symbolize_keys(doc) elsif doc.is_a?(String) doc = {messages: [{role: "user", content: doc}]} else raise ArgumentError, "Invalid document type: #{doc.class}" end system_prompt = doc[:system_prompt] = doc[:messages] || [] convo = new(system_prompt: system_prompt, messages: ) if (llm = doc[:llm]) convo.llm = Promptcraft::Llm.from_h(llm) end convo end |
.from_messages(messages) ⇒ Object
Class method to create a Conversation from an array of messages
66 67 68 69 70 71 72 73 74 |
# File 'lib/promptcraft/conversation.rb', line 66 def () if .empty? || .first[:role] != "system" raise ArgumentError, "First message must be from 'system' with the prompt" end system_prompt = .first[:content] = [1..] # all messages after the first new(system_prompt:, messages: ) end |
.load_from_file(filename) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/promptcraft/conversation.rb', line 36 def load_from_file(filename) conversations = [] File.open(filename, "r") do |file| YAML.parse_stream(file) do |doc| next unless doc conversations << build_from(doc.to_ruby) end end conversations end |
.load_from_io(io = $stdin) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/promptcraft/conversation.rb', line 22 def load_from_io(io = $stdin) conversations = [] begin YAML.load_stream(io) do |doc| next unless doc conversations << build_from(doc) end rescue Psych::SyntaxError => e warn "Error: #{e.message}" warn "Contents:\n#{io.read}" end conversations end |
Instance Method Details
#add_message(role:, content:) ⇒ Object
17 18 19 |
# File 'lib/promptcraft/conversation.rb', line 17 def (role:, content:) << {role:, content:} end |
#save_to_file(filename) ⇒ Object
77 78 79 |
# File 'lib/promptcraft/conversation.rb', line 77 def save_to_file(filename) File.write(filename, to_yaml) end |
#to_json ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/promptcraft/conversation.rb', line 95 def to_json deep_stringify_keys({ system_prompt: @system_prompt&.strip, llm: @llm&.to_h, messages: }.compact).to_json end |
#to_messages ⇒ Object
103 104 105 |
# File 'lib/promptcraft/conversation.rb', line 103 def [{role: "system", content: @system_prompt}] + end |
#to_yaml ⇒ Object
system_prompt: ‘I like to solve maths problems.’ messages:
-
role: “user” content: “What is 2+2?”
-
role: assistant content: 2 + 2 = 4
87 88 89 90 91 92 93 |
# File 'lib/promptcraft/conversation.rb', line 87 def to_yaml YAML.dump(deep_stringify_keys({ system_prompt: system_prompt&.strip, llm: llm&.to_h, messages: }.compact)) end |