54 lines
1.3 KiB
Crystal
54 lines
1.3 KiB
Crystal
|
|
require "pretty_print"
|
|
require "colorize"
|
|
|
|
class OpenAIChatBuilder
|
|
alias OpenAIMessage = NamedTuple(role: String, content: String)
|
|
alias OpenAIChat = Array(OpenAIMessage)
|
|
|
|
getter verbose : Bool
|
|
def initialize(@verbose)
|
|
|
|
end
|
|
|
|
# skip prelude_zone
|
|
# skip future_zone
|
|
def build(prompt : Prompt) : OpenAIChat
|
|
chat = [] of OpenAIMessage
|
|
|
|
token_limit = 2_900
|
|
mandatory_token_count = (
|
|
prompt.system_zone.token_count +
|
|
prompt.present_zone.token_count
|
|
)
|
|
|
|
## Build mandatory system messages
|
|
prompt.system_zone.each do |content|
|
|
chat << { role: "system", content: content }
|
|
end
|
|
|
|
## Build mandatory system messages
|
|
tmp_chat = [] of OpenAIMessage
|
|
tmp_token_count = 0
|
|
prompt.past_zone.reverse_each do |content|
|
|
estimated_token_count = (content.size/4) + tmp_token_count + mandatory_token_count
|
|
pp tmp_chat.reverse if @verbose
|
|
puts "ESTIMATE: #{estimated_token_count} (limit=#{token_limit})".colorize(:yellow).to_s if @verbose
|
|
|
|
break if estimated_token_count >= token_limit
|
|
|
|
tmp_chat << { role: "user", content: content }
|
|
tmp_token_count += (content.size / 4)
|
|
end
|
|
chat.concat(tmp_chat.reverse)
|
|
|
|
prompt.present_zone.each do |content|
|
|
chat << { role: "user", content: content }
|
|
end
|
|
|
|
# pp chat
|
|
chat
|
|
end
|
|
end
|
|
|