Class: MonadicApp
- Inherits:
-
Object
show all
- Includes:
- MonadicChat
- Defined in:
- lib/monadic_app.rb,
lib/monadic_chat/menu.rb,
lib/monadic_chat/tools.rb,
lib/monadic_chat/console.rb,
lib/monadic_chat/internals.rb,
lib/monadic_chat/formatting.rb,
lib/monadic_chat/parameters.rb,
lib/monadic_chat/interaction.rb
Constant Summary
Constants included
from MonadicChat
MonadicChat::APPS, MonadicChat::APPS_DIR, MonadicChat::APPS_DIR_LIST, MonadicChat::BLINGFIRE, MonadicChat::BULLET, MonadicChat::CONFIG, MonadicChat::GITHUB_STYLE, MonadicChat::HOME, MonadicChat::PASTEL, MonadicChat::PROMPT_ASSISTANT, MonadicChat::PROMPT_SYSTEM, MonadicChat::PROMPT_USER, MonadicChat::SETTINGS, MonadicChat::SPINNER, MonadicChat::TEMPLATES, MonadicChat::TEMP_HTML, MonadicChat::TEMP_JSON, MonadicChat::TEMP_MD, MonadicChat::TITLE_WIDTH, MonadicChat::USER_APPS_DIR, MonadicChat::VERSION
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_to_html(text, filepath) ⇒ Object
-
#ask_clear ⇒ Object
-
#ask_retrial(input, message = nil) ⇒ Object
-
#banner(title, desc, color) ⇒ Object
-
#bind(input, role: "user", num_retrials: 0) ⇒ Object
-
#bing_search(query, num_retrial: 3) ⇒ Object
-
#change_frequency_penalty ⇒ Object
-
#change_max_tokens ⇒ Object
-
#change_model ⇒ Object
-
#change_parameter ⇒ Object
methods for parametter setting.
-
#change_presence_penalty ⇒ Object
-
#change_temperature ⇒ Object
-
#change_top_p ⇒ Object
-
#check_file(path) ⇒ Object
-
#clear_screen ⇒ Object
-
#confirm_query(input) ⇒ Object
-
#count_lines_below ⇒ Object
methods for manipulating terminal screen.
-
#count_tokens(text) ⇒ Object
methods for preparation and updating.
-
#format_data ⇒ Object
-
#fulfill_placeholders ⇒ Object
-
#go_up_and_clear ⇒ Object
-
#initialize(mode:, params:, template_json:, template_md:, placeholders:, prop_accumulator:, prop_newdata:, update_proc:) ⇒ MonadicApp
constructor
A new instance of MonadicApp.
-
#load_data ⇒ Object
-
#objectify ⇒ Object
-
#parse(input = nil) ⇒ Object
methods for running monadic app.
-
#prepare_params(input_role, input) ⇒ Object
-
#reset ⇒ Object
-
#run ⇒ Object
-
#save_data ⇒ Object
-
#set_html ⇒ Object
-
#show_data ⇒ Object
-
#show_greet ⇒ Object
-
#show_html ⇒ Object
-
#show_menu ⇒ Object
methods for showing menu and menu items.
-
#show_params ⇒ Object
-
#show_template ⇒ Object
methods for formatting and presenting.
-
#unit(input) ⇒ Object
function to package plain text into a unit.
-
#update_template(res, role) ⇒ Object
-
#use_tool(res) ⇒ Object
function to have GPT use tools.
-
#user_input(text = "") ⇒ Object
methods for user interaction.
-
#wikipedia_search(keywords, cache = {}, num_retrial: 10) ⇒ Object
authenticate, create_app, delete_app, mdprint, open_readme, prompt_assistant, prompt_system, prompt_user, require_apps, tokenize
Constructor Details
#initialize(mode:, params:, template_json:, template_md:, placeholders:, prop_accumulator:, prop_newdata:, update_proc:) ⇒ MonadicApp
Returns a new instance of MonadicApp.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/monadic_app.rb', line 16
def initialize(mode:, params:, template_json:, template_md:, placeholders:, prop_accumulator:, prop_newdata:, update_proc:)
@mode = mode.to_sym
@placeholders = placeholders
@prop_accumulator = prop_accumulator
@prop_newdata = prop_newdata
@completion = nil
@update_proc = update_proc
@params_initial = params
@params = @params_initial.dup
@html = false
@method = OpenAI.model_to_method(@params["model"])
@metadata = {}
json = File.read(template_json)
.gsub("{{DATETIME}}", Time.now.strftime("%Y-%m-%d %H:%M:%S"))
.gsub("{{DATE}}", Time.now.strftime("%Y-%m-%d"))
@messages_initial = JSON.parse(json)["messages"]
@messages = @messages_initial.dup
@turns = 0
@template_initial = File.read(template_md)
@template = @template_initial.dup
@template_tokens = 0
end
|
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
14
15
16
|
# File 'lib/monadic_app.rb', line 14
def messages
@messages
end
|
#template ⇒ Object
Returns the value of attribute template.
14
15
16
|
# File 'lib/monadic_app.rb', line 14
def template
@template
end
|
#turns ⇒ Object
Returns the value of attribute turns.
14
15
16
|
# File 'lib/monadic_app.rb', line 14
def turns
@turns
end
|
Instance Method Details
#add_to_html(text, filepath) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/monadic_chat/formatting.rb', line 65
def add_to_html(text, filepath)
text = text.gsub(/(?<![\\>\s])(?!\n[\n<])\n/m) { "<br/>\n" }
text = text.gsub(/~~~(.+?)~~~/m) do
m = Regexp.last_match
"~~~#{m[1].gsub("<br/>\n") { "\n" }}~~~"
end
text = text.gsub(/`(.+?)`/) do
m = Regexp.last_match
"`#{m[1].gsub("<br/>\n") { "\n" }}`"
end
FileUtils.touch(filepath) unless File.exist?(filepath)
File.open(filepath, "w") do |f|
html = <<~HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
#{GITHUB_STYLE}
</style>
<title>Monadic Chat</title>
</head>
<body>
#{Kramdown::Document.new(text, syntax_highlighter: :rouge, syntax_highlighter_ops: {}).to_html}
</body>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
<script>
$(window).on("load", function() {
$("html, body").animate({ scrollTop: $(document).height() }, 500);
});
</script>
</html>
HTML
f.write html
end
end
|
#ask_clear ⇒ Object
23
24
25
26
27
28
|
# File 'lib/monadic_chat/console.rb', line 23
def ask_clear
PROMPT_SYSTEM.readline(PASTEL.red("Press Enter to clear screen"))
print TTY::Cursor.up
print TTY::Cursor.clear_screen_down
clear_screen
end
|
#ask_retrial(input, message = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/monadic_chat/menu.rb', line 70
def ask_retrial(input, message = nil)
print PROMPT_SYSTEM.prefix
print "Error: #{message.capitalize}\n" if message
retrial = PROMPT_USER.select("Do you want to try again?",
show_help: :never) do ||
.choice "Yes", "yes"
.choice "No", "no"
.choice "Show current contextual data", "show"
end
case retrial
when "yes"
input
when "no"
user_input
when "show"
show_data
ask_retrial(input)
end
end
|
#banner(title, desc, color) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/monadic_app.rb', line 91
def banner(title, desc, color)
screen_width = TTY::Screen.width - 2
width = screen_width < TITLE_WIDTH ? screen_width : TITLE_WIDTH
title = PASTEL.bold.send(color.to_sym, title.center(width, " "))
desc = desc.center(width, " ")
padding = "".center(width, " ")
banner = TTY::Box.frame "#{padding}\n#{title}\n#{desc}\n#{padding}"
print "\n", banner.strip, "\n"
end
|
#bind(input, role: "user", num_retrials: 0) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/monadic_chat/internals.rb', line 142
def bind(input, role: "user", num_retrials: 0)
case role
when "user"
@turns += 1
when "system" input = "\n\n#{input}"
end
print PROMPT_ASSISTANT.prefix, "\n"
params = prepare_params(role, input)
research_mode = @mode == :research
escaping = +""
finished = false
res = @completion.run(params,
research_mode: research_mode,
timeout_sec: SETTINGS["timeout_sec"],
num_retrials: num_retrials) do |chunk|
if chunk.instance_of?(Hash) && chunk["content"] == "DONE"
finished = true
elsif chunk.instance_of?(String) && !finished
if escaping
chunk = escaping + chunk
escaping = ""
end
if /(?:\\\z)/ =~ chunk
escaping += chunk
next
else
chunk = chunk.gsub('\\n') { "\n" }
end
print chunk
end
end
print "\n"
message = case role
when "system" { role: "assistant", content: @mode == :research ? unit(res) : res }
when "user" searched = use_tool(res)
if searched
@messages << { "role" => "assistant",
"content" => @mode == :research ? unit(res)["response"] : res }
if searched == "empty"
print PROMPT_SYSTEM.prefix, "Search results are empty", "\n"
return
else
bind(searched, role: "system")
return
end
else
{ role: "assistant", content: @mode == :researh ? unit(res) : res }
end
end
update_template(message[:content], message[:role])
set_html if @html
end
|
#bing_search(query, num_retrial: 3) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/monadic_chat/tools.rb', line 8
def bing_search(query, num_retrial: 3)
base_uri = "https://www.bing.com/search?setlang=en"
css_selector = "#b_results"
q = URI.encode_www_form(q: query)
doc = Nokogiri::HTML(URI.parse([base_uri, q].join("&")).read)
doc.css("script, link").each(&:remove)
doc.css(css_selector).text.squeeze(" \n")
rescue StandardError
num_retrial -= 1
if num_retrial.positive?
sleep 1
bing_search(keywords, num_retrial: num_retrial)
else
"empty"
end
end
|
#change_frequency_penalty ⇒ Object
60
61
62
63
64
65
|
# File 'lib/monadic_chat/parameters.rb', line 60
def change_frequency_penalty
PROMPT_SYSTEM.ask("Set value of frequency penalty [-2.0 to 2.0]:", convert: :float) do |q|
q.in "-2.0-2.0"
q.messages[:range?] = "Value out of expected range [-2.0 to 2.0]"
end
end
|
#change_max_tokens ⇒ Object
39
40
41
42
43
44
|
# File 'lib/monadic_chat/parameters.rb', line 39
def change_max_tokens
PROMPT_SYSTEM.ask("Set value of max tokens [1000 to 8000]:", convert: :int) do |q|
q.in "1000-8000"
q.messages[:range?] = "Value out of expected range [1000 to 2048]"
end
end
|
#change_model ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/monadic_chat/parameters.rb', line 74
def change_model
model = PROMPT_SYSTEM.select("Select a model:", per_page: 10, cycle: false, show_help: :never, filter: true, default: 1) do ||
.choice "#{BULLET} Cancel", "cancel"
TTY::Cursor.save
SPINNER.auto_spin
models = @completion.models
SPINNER.stop
TTY::Cursor.restore
case @mode
when :research
models.filter { |m| ["completions", "chat/completions"].include? OpenAI.model_to_method(m["id"]) }.sort_by { |m| -m["created"] }.each do |m|
.choice "#{BULLET} #{m["id"]}", m["id"]
end
when :normal
models.filter { |m| OpenAI.model_to_method(m["id"]) == "chat/completions" && OpenAI.model_to_method(m["id"]) }.sort_by { |m| -m["created"] }.each do |m|
.choice "#{BULLET} #{m["id"]}", m["id"]
end
end
end
if model == "cancel"
nil
else
model
end
end
|
#change_parameter ⇒ Object
methods for parametter setting
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/monadic_chat/parameters.rb', line 8
def change_parameter
parameter = PROMPT_SYSTEM.select("Select the parmeter to be set:", per_page: 7, cycle: true, show_help: :never, filter: true, default: 1) do ||
.choice "#{BULLET} Cancel", "cancel"
.choice "#{BULLET} model: #{@params["model"]}", "model"
.choice "#{BULLET} max_tokens: #{@params["max_tokens"]}", "max_tokens"
.choice "#{BULLET} temperature: #{@params["temperature"]}", "temperature"
.choice "#{BULLET} top_p: #{@params["top_p"]}", "top_p"
.choice "#{BULLET} frequency_penalty: #{@params["frequency_penalty"]}", "frequency_penalty"
.choice "#{BULLET} presence_penalty: #{@params["presence_penalty"]}", "presence_penalty"
end
return if parameter == "cancel"
case parameter
when "model"
value = change_model
@method = OpenAI.model_to_method(value)
when "max_tokens"
value = change_max_tokens
when "temperature"
value = change_temperature
when "top_p"
value = change_top_p
when "frequency_penalty"
value = change_frequency_penalty
when "presence_penalty"
value = change_presence_penalty
end
@params[parameter] = value if value
print "Parameter #{parameter} has been set to #{PASTEL.green(value)}\n" if value
end
|
#change_presence_penalty ⇒ Object
67
68
69
70
71
72
|
# File 'lib/monadic_chat/parameters.rb', line 67
def change_presence_penalty
PROMPT_SYSTEM.ask("Set value of presence penalty [-2.0 to 2.0]:", convert: :float) do |q|
q.in "-2.0-2.0"
q.messages[:range?] = "Value out of expected range [-2.0 to 2.0]"
end
end
|
#change_temperature ⇒ Object
46
47
48
49
50
51
|
# File 'lib/monadic_chat/parameters.rb', line 46
def change_temperature
PROMPT_SYSTEM.ask("Set value of temperature [0.0 to 1.0]:", convert: :float) do |q|
q.in "0.0-1.0"
q.messages[:range?] = "Value out of expected range [0.0 to 1.0]"
end
end
|
#change_top_p ⇒ Object
53
54
55
56
57
58
|
# File 'lib/monadic_chat/parameters.rb', line 53
def change_top_p
PROMPT_SYSTEM.ask("Set value of top_p [0.0 to 1.0]:", convert: :float) do |q|
q.in "0.0-1.0"
q.messages[:range?] = "Value out of expected range [0.0 to 1.0]"
end
end
|
#check_file(path) ⇒ Object
90
91
92
93
|
# File 'lib/monadic_chat/menu.rb', line 90
def check_file(path)
dirname = File.dirname(File.expand_path(path))
path == "" || (/\.json\z/ =~ path.strip && Dir.exist?(dirname)) ? true : false
end
|
#clear_screen ⇒ Object
19
20
21
|
# File 'lib/monadic_chat/console.rb', line 19
def clear_screen
print "\e[2J\e[f"
end
|
#confirm_query(input) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/monadic_chat/interaction.rb', line 29
def confirm_query(input)
if input.size < SETTINGS["min_query_size"]
PROMPT_SYSTEM.yes?("Would you like to proceed with this (very short) prompt?")
else
true
end
end
|
#count_lines_below ⇒ Object
methods for manipulating terminal screen
7
8
9
10
11
|
# File 'lib/monadic_chat/console.rb', line 7
def count_lines_below
screen_height = TTY::Screen.height
vpos = Cursor.pos[:row]
screen_height - vpos
end
|
#count_tokens(text) ⇒ Object
methods for preparation and updating
8
9
10
|
# File 'lib/monadic_chat/internals.rb', line 8
def count_tokens(text)
MonadicChat.tokenize(text).size
end
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/monadic_chat/formatting.rb', line 14
def format_data
contextual = []
accumulator = []
if @mode == :research
objectify.each do |key, val|
next if %w[prompt response messages].include? key
contextual << "- **#{key.split("_").map(&:capitalize).join(" ")}**: #{val.to_s.strip}"
end
contextual << "- **Num of Tokens in Template**: #{@template_tokens}"
end
@messages.each do |m|
accumulator << "#{m["role"].capitalize}: #{m["content"]}"
end
h1 = "# Monadic :: Chat / #{self.class.name}"
contextual.map!(&:strip).unshift "## Contextual Data\n" unless contextual.empty?
accum_label = @prop_accumulator.split("_").map(&:capitalize).join(" ")
accumulator.map!(&:strip).unshift "## #{accum_label}\n" unless accumulator.empty?
"#{h1}\n\n#{contextual.join("\n")}\n\n#{accumulator.join("\n\n")}"
end
|
#fulfill_placeholders ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/monadic_chat/internals.rb', line 12
def fulfill_placeholders
input = nil
replacements = []
mode = :replace
@placeholders.each do |key, val|
if key == "mode"
mode = val
next
end
input = if mode == :replace
val
else
PROMPT_SYSTEM.readline("#{val}: ")
end
unless input
replacements.clear
break
end
replacements << [key, input]
end
if replacements.empty?
false
else
replacements.each do |key, value|
@messages[0]["content"].gsub!(key, value)
messages[0]["content"]
end
true
end
end
|
#go_up_and_clear ⇒ Object
13
14
15
16
17
|
# File 'lib/monadic_chat/console.rb', line 13
def go_up_and_clear
print TTY::Cursor.up
print TTY::Cursor.clear_screen_down
print TTY::Cursor.up
end
|
#load_data ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/monadic_chat/menu.rb', line 150
def load_data
input = ""
loop do
print TTY::Cursor.save
path = PROMPT_SYSTEM.readline("Enter the file path for the JSON file (press Enter to cancel): ")
if check_file(path)
input = path
break
else
print TTY::Cursor.restore
print TTY::Cursor.clear_screen_down
end
end
print TTY::Cursor.save
return if input.to_s == ""
begin
filepath = File.expand_path(input.strip)
json = File.read(filepath)
data = JSON.parse(json)
case @mode
when :research
self.class.name.downcase.split("::")[-1]
raise unless data["mode"] == self.class.name.downcase.split("::")[-1]
@messages = data.delete "messages"
@template = @template.sub(/JSON:\n+```json\s*\{.+\}\s*```\n\n/m, "JSON:\n\n```json\n#{JSON.pretty_generate(data).strip}\n```\n\n")
when :normal
raise unless data["messages"] && data["messages"][0]["role"]
@messages = data["messages"]
end
print "Data has been loaded successfully\n"
true
rescue StandardError
print "The data structure is not valid for this app\n"
false
end
end
|
#objectify ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/monadic_chat/internals.rb', line 46
def objectify
case @mode
when :research
m = /JSON:\n+```json\s*(\{.+\})\s*```\n\n/m.match(@template)
json = m[1].gsub(/(?!\\\\\\)\\\\"/) { '\\\"' }
res = JSON.parse(json)
res["messages"] = @messages
res
when :normal
@messages
end
end
|
#parse(input = nil) ⇒ Object
methods for running monadic app
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/monadic_app.rb', line 46
def parse(input = nil)
loop do
case input
when TrueClass
input = user_input
next
when /\A\s*(?:help|menu|commands?|\?|h)\s*\z/i
return true unless
when /\A\s*(?:bye|exit|quit)\s*\z/i
break
when /\A\s*(?:reset)\s*\z/i
reset
when /\A\s*(?:data|context)\s*\z/i
show_data
when /\A\s*(?:html)\s*\z/i
@html = true
show_html
when /\A\s*(?:save)\s*\z/i
save_data
when /\A\s*(?:load)\s*\z/i
load_data
when /\A\s*(?:clear|clean)\s*\z/i
clear_screen
when /\A\s*(?:params?|parameters?|config|configuration)\s*\z/i
change_parameter
else
if input && confirm_query(input)
begin
bind(input, num_retrials: SETTINGS["num_retrials"])
rescue StandardError => e
input = ask_retrial(input, e.message)
next
end
end
end
if input.to_s == ""
input = false
clear_screen
end
input = user_input
end
rescue MonadicError
false
end
|
#prepare_params(input_role, input) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/monadic_chat/internals.rb', line 59
def prepare_params(input_role, input)
params = @params.dup
delimited_input = case input_role
when "user"
"NEW PROMPT: ###\n#{input}\n###"
when "system" "SEARCH SNIPPETS: ###\n#{input}\n###"
end
case @mode
when :research
messages = +""
system = +""
@messages.each do |mes|
role = mes["role"]
content = mes["content"]
case role
when "system"
system << "#{content}\n" if system == ""
else
messages << "- #{mes["role"].strip}: #{content}\n"
end
end
delimited_messages = "MESSAGES: ###\n#{messages}\n###"
template = @template.dup.sub("{{SYSTEM}}", system)
.sub("{{PROMPT}}", delimited_input)
.sub("{{MESSAGES}}", delimited_messages.strip)
@template_tokens = count_tokens(template)
File.open(TEMP_MD, "w") { |f| f.write template }
@messages << { "role" => input_role, "content" => input }
case @method
when "completions"
params["prompt"] = template
when "chat/completions"
params["messages"] = [{ "role" => "system", "content" => template }]
end
when :normal
@messages << { "role" => input_role, "content" => input }
params["messages"] = @messages
end
@update_proc.call unless input_role == "system"
params
end
|
#reset ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/monadic_chat/menu.rb', line 55
def reset
@html = false
@params = @params_initial.dup
@messages = @messages_initial.dup
@template = @template_initial.dup
@template_tokens = 0
if @placeholders.empty?
print PROMPT_SYSTEM.prefix
print "Context and parameters have been reset.\n"
else
fulfill_placeholders
end
end
|
#run ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/monadic_app.rb', line 101
def run
clear_screen
banner("MONADIC::CHAT / #{self.class.name}", self.class::DESC, self.class::COLOR)
show_greet
if @placeholders.empty?
parse(user_input)
else
loadfile = PROMPT_SYSTEM.select("\nLoad saved file? (Make sure the file is saved by the same app)", default: 2, show_help: :never) do ||
.choice "Yes", "yes"
.choice "No", "no"
end
parse(user_input) if loadfile == "yes" && load_data || fulfill_placeholders
end
end
|
#save_data ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/monadic_chat/menu.rb', line 95
def save_data
input = ""
loop do
print TTY::Cursor.save
path = PROMPT_SYSTEM.readline("Enter the file path for the JSON file (including the file name and .json extension): ")
if check_file(path)
input = path
break
else
print TTY::Cursor.restore
print TTY::Cursor.clear_screen_down
end
end
print TTY::Cursor.save
return if input.to_s == ""
filepath = File.expand_path(input.strip)
if File.exist? filepath
overwrite = PROMPT_SYSTEM.select("#{filepath} already exists.\nOverwrite?",
show_help: :never) do ||
.choice "Yes", "yes"
.choice "No", "no"
end
return if overwrite == "no"
end
FileUtils.touch(filepath)
unless File.exist? filepath
print "File cannot be created\n"
save_data
end
begin
File.open(filepath, "w") do |f|
case @mode
when :research
m = /JSON:\n+```json\s*(\{.+\})\s*```\n\n/m.match(@template)
data = JSON.parse(m[1])
data["messages"] = @messages
f.write JSON.pretty_generate(data)
when :normal
f.write JSON.pretty_generate({ "messages" => @messages })
end
print "Data has been saved successfully\n"
end
true
rescue StandardError
print "Error: Something went wrong"
false
end
end
|
#set_html ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/monadic_chat/formatting.rb', line 47
def set_html
res = format_data.sub(%r{::(.+?)/(.+?)\b}) do
" <span class='monadic_gray'>::</span> <span class='monadic_app'>#{Regexp.last_match(1)}</span> <span class='monadic_gray'>/</span> #{Regexp.last_match(2)}"
end
res = res.gsub("```") { "~~~" }
.gsub(/^(system):/i) { "<span class='monadic_system'> #{Regexp.last_match(1)} </span><br />" }
.gsub(/^(user):/i) { "<span class='monadic_user'> #{Regexp.last_match(1)} </span><br />" }
.gsub(/^(assistant|gpt):/i) { "<span class='monadic_chat'> #{Regexp.last_match(1)} </span><br />" }
add_to_html(res, TEMP_HTML)
end
|
#show_data ⇒ Object
40
41
42
43
44
45
|
# File 'lib/monadic_chat/formatting.rb', line 40
def show_data
print PROMPT_SYSTEM.prefix
res = format_data
print "\n#{TTY::Markdown.parse(res, indent: 0)}"
end
|
#show_greet ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/monadic_chat/interaction.rb', line 14
def show_greet
current_mode = case @mode
when :research
PASTEL.red("Research")
when :normal
PASTEL.green("Normal")
end
greet_md = <<~GREET
- You are currently in **#{current_mode}** mode (#{@params["model"]})
- Type **help** or **menu** to see available commands
GREET
print PROMPT_SYSTEM.prefix
print "\n#{TTY::Markdown.parse(greet_md, indent: 0).strip}\n"
end
|
#show_html ⇒ Object
58
59
60
61
62
63
|
# File 'lib/monadic_chat/formatting.rb', line 58
def show_html
set_html
print PROMPT_SYSTEM.prefix
print "HTML is ready\n"
Launchy.open(TEMP_HTML)
end
|
methods for showing menu and menu items
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/monadic_chat/menu.rb', line 8
def
clear_screen
print TTY::Cursor.save
parameter = PROMPT_SYSTEM.select("Select function:", per_page: 10, cycle: true, filter: true, default: 1, show_help: :never) do ||
.choice "#{BULLET} #{PASTEL.bold("cancel/return/escape")} Cancel this menu", "cancel"
.choice "#{BULLET} #{PASTEL.bold("params/settings/config")} Show and change values of parameters", "params"
.choice "#{BULLET} #{PASTEL.bold("data/context")} Show currrent contextual info", "data"
.choice "#{BULLET} #{PASTEL.bold("html")} View contextual info on the web browser", "html"
.choice "#{BULLET} #{PASTEL.bold("reset")} Reset context to initial state", "reset"
.choice "#{BULLET} #{PASTEL.bold("save")} Save current contextual info to file", "save"
.choice "#{BULLET} #{PASTEL.bold("load")} Load current contextual info from file", "load"
.choice "#{BULLET} #{PASTEL.bold("clear/clean")} Clear screen", "clear"
.choice "#{BULLET} #{PASTEL.bold("readme/documentation")} Open readme/documentation", "readme"
.choice "#{BULLET} #{PASTEL.bold("exit/bye/quit")} Go back to main menu", "exit"
end
print TTY::Cursor.restore
print TTY::Cursor.clear_screen_down
print TTY::Cursor.restore
case parameter
when "cancel"
return true
when "params"
change_parameter
when "data"
show_data
when "html"
@html = true
show_html
when "reset"
reset
when "save"
save_data
when "load"
load_data
when "clear"
clear_screen
print TTY::Cursor.clear_screen_down
when "readme"
MonadicChat.open_readme
when "exit"
return false
end
true
end
|
#show_params ⇒ Object
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/monadic_chat/parameters.rb', line 100
def show_params
params_md = "# Current Parameter Values\n\n"
@params.each do |key, val|
next if /\A(?:prompt|stream|logprobs|echo|stop)\z/ =~ key
params_md += "- #{key}: #{val}\n"
end
print prompt_system, "\n"
print "#{TTY::Markdown.parse(params_md, indent: 0).strip}\n\n"
end
|
#show_template ⇒ Object
methods for formatting and presenting
8
9
10
11
12
|
# File 'lib/monadic_chat/formatting.rb', line 8
def show_template
puts "-----------------------------------------"
puts @template
puts "-----------------------------------------"
end
|
#unit(input) ⇒ Object
function to package plain text into a unit
129
130
131
132
133
134
135
136
|
# File 'lib/monadic_chat/internals.rb', line 129
def unit(input)
if input.instance_of?(Hash)
input
else
@metadata["response"] = input
@metadata
end
end
|
#update_template(res, role) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/monadic_chat/internals.rb', line 112
def update_template(res, role)
case @mode
when :research
@metadata = res
@messages << { "role" => role, "content" => @metadata["response"] }
json = @metadata.to_json.strip
File.open(TEMP_JSON, "w") { |f| f.write json }
@template.sub!(/JSON:\n+```json.+```\n\n/m, "JSON:\n\n```json\n#{json}\n```\n\n")
when :normal
@messages << { "role" => "assistant", "content" => res }
end
end
|
function to have GPT use tools
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/monadic_chat/internals.rb', line 213
def use_tool(res)
case @mode
when :normal
text = res
when :research
text = res.is_a?(Hash) ? res["response"] : res
end
case text
when /\bSEARCH_WIKI\("?(.+?)"?\)/m
@wiki_search_cache ||= {}
search_key = Regexp.last_match(1)
wikipedia_search(search_key, @wiki_search_cache)
when /\bSEARCH_WEB\("?(.+?)"?\)/m
@web_search_cache ||= {}
search_key = Regexp.last_match(1)
bing_search(search_key, @web_searh_cache)
else
false
end
end
|
methods for user interaction
8
9
10
11
12
|
# File 'lib/monadic_chat/interaction.rb', line 8
def user_input(text = "")
res = PROMPT_USER.readline(text)
print TTY::Cursor.clear_line_after
res == "" ? nil : res
end
|
#wikipedia_search(keywords, cache = {}, num_retrial: 10) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/monadic_chat/tools.rb', line 26
def wikipedia_search(keywords, cache = {}, num_retrial: 10)
base_url = "https://en.wikipedia.org/w/api.php"
search_params = {
action: "query",
list: "search",
format: "json",
srsearch: keywords,
utf8: 1,
formatversion: 2
}
search_uri = URI(base_url)
search_uri.query = URI.encode_www_form(search_params)
search_response = Net::HTTP.get(search_uri)
search_data = JSON.parse(search_response)
raise if search_data["query"]["search"].empty?
title = search_data["query"]["search"][0]["title"]
return cache[title] if cache.keys.include?(title)
content_params = {
action: "query",
prop: "extracts",
format: "json",
titles: title,
explaintext: 1,
utf8: 1,
formatversion: 2
}
content_uri = URI(base_url)
content_uri.query = URI.encode_www_form(content_params)
content_response = Net::HTTP.get(content_uri)
content_data = JSON.parse(content_response)
result_data = content_data["query"]["pages"][0]["extract"]
tokenized = BLINGFIRE.text_to_ids(result_data)
if tokenized.size > SETTINGS["max_tokens_wiki"].to_i
ratio = SETTINGS["max_tokens_wiki"].to_f / tokenized.size
result_data = result_data[0..(result_data.size * ratio).to_i]
end
text = <<~TEXT
```MediaWiki
#{result_data}
```
TEXT
cache[title] = text
text
rescue StandardError
num_retrial -= 1
if num_retrial.positive?
sleep 1
wikipedia_search(keywords, num_retrial: num_retrial)
else
"empty"
end
end
|