-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot_with_streaming.rb
executable file
·290 lines (242 loc) · 6.64 KB
/
chatbot_with_streaming.rb
1
2
3
4
5
6
7
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
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
90
91
92
93
94
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env ruby
# frozen_string_literal: true
# Simple chatbot example -- run with -h argument to see options.
require 'bundler/setup'
require 'dotenv/load'
require 'readline'
require 'optparse'
require 'mistral'
MODEL_LIST = %w[
mistral-tiny-latest
mistral-small-latest
mistral-medium-latest
codestral-latest
].freeze
DEFAULT_MODEL = 'mistral-small-latest'
DEFAULT_TEMPERATURE = 0.7
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
# A hash of all commands and their arguments, used for tab completion.
COMMAND_LIST = {
'/new' => {},
'/help' => {},
'/model' => MODEL_LIST.map { |model| [model, {}] }.to_h, # Nested completions for models
'/system' => {},
'/temperature' => {},
'/config' => {},
'/quit' => {},
'/exit' => {}
}.freeze
$logger = Logger.new($stdout)
$logger.level = Logger::INFO
$logger.formatter = proc do |severity, datetime, _, msg|
"#{datetime.strftime("%Y-%m-%d %H:%M:%S")} - #{severity} - #{msg}\n"
end
def find_completions(command_dict, parts)
return command_dict.keys if parts.empty?
if command_dict.key?(parts[0])
find_completions(command_dict[parts[0]], parts[1..])
else
command_dict.keys.select { |cmd| cmd.start_with?(parts[0]) }
end
end
# Enable tab completion
Readline.completion_proc = proc do |_input|
line_parts = Readline.line_buffer.lstrip.split(' ')
options = find_completions(COMMAND_LIST, line_parts[0..-2])
options.select { |option| option.start_with?(line_parts[-1]) }
end
class ChatBot
def initialize(api_key, model, system_message = nil, temperature = DEFAULT_TEMPERATURE)
raise ArgumentError, 'An API key must be provided to use the Mistral API.' if api_key.nil?
@client = Mistral::Client.new(api_key: api_key)
@model = model
@temperature = temperature
@system_message = system_message
end
def opening_instructions
puts '
To chat: type your message and hit enter
To start a new chat: /new
To switch model: /model <model name>
To switch system message: /system <message>
To switch temperature: /temperature <temperature>
To see current config: /config
To exit: /exit, /quit, or hit CTRL+C
To see this help: /help
'
end
def new_chat
puts ''
puts "Starting new chat with model: #{@model}, temperature: #{@temperature}"
puts ''
@messages = []
@messages << Mistral::ChatMessage.new(role: 'system', content: @system_message) if @system_message
end
def switch_model(input)
model = get_arguments(input)
if MODEL_LIST.include?(model)
@model = model
$logger.info("Switching model: #{model}")
else
$logger.error("Invalid model name: #{model}")
end
end
def switch_system_message(input)
system_message = get_arguments(input)
if system_message
@system_message = system_message
$logger.info("Switching system message: #{system_message}")
new_chat
else
$logger.error("Invalid system message: #{system_message}")
end
end
def switch_temperature(input)
temperature = get_arguments(input)
begin
temperature = Float(temperature)
raise ArgumentError if temperature.negative? || temperature > 1
@temperature = temperature
$logger.info("Switching temperature: #{temperature}")
rescue ArgumentError
$logger.error("Invalid temperature: #{temperature}")
end
end
def show_config
puts ''
puts "Current model: #{@model}"
puts "Current temperature: #{@temperature}"
puts "Current system message: #{@system_message}"
puts ''
end
def collect_user_input
puts ''
print 'YOU: '
gets.chomp
end
def run_inference(content)
puts ''
puts 'MISTRAL:'
puts ''
@messages << Mistral::ChatMessage.new(role: 'user', content: content)
assistant_response = ''
$logger.debug("Running inference with model: #{@model}, temperature: #{@temperature}")
$logger.debug("Sending messages: #{@messages}")
@client.chat_stream(model: @model, temperature: @temperature, messages: @messages).each do |chunk|
response = chunk.choices[0].delta.content
if response
print response
assistant_response += response
end
end
puts ''
@messages << Mistral::ChatMessage.new(role: 'assistant', content: assistant_response) if assistant_response
$logger.debug("Current messages: #{@messages}")
end
def get_command(input)
input.split[0].strip
end
def get_arguments(input)
input.split[1..].join(' ')
rescue IndexError
''
end
def is_command?(input)
COMMAND_LIST.key?(get_command(input))
end
def execute_command(input)
command = get_command(input)
case command
when '/exit', '/quit'
exit
when '/help'
opening_instructions
when '/new'
new_chat
when '/model'
switch_model(input)
when '/system'
switch_system_message(input)
when '/temperature'
switch_temperature(input)
when '/config'
show_config
end
end
def start
opening_instructions
new_chat
loop do
input = collect_user_input
if is_command?(input)
execute_command(input)
else
run_inference(input)
end
rescue Interrupt
exit
end
end
def exit
$logger.debug('Exiting chatbot')
puts 'Goodbye!'
Kernel.exit(0)
end
end
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: chatbot.rb [options]'
opts.on(
'--api-key KEY',
'Mistral API key. Defaults to environment variable MISTRAL_API_KEY'
) do |key|
options[:api_key] = key
end
opts.on(
'-m',
'--model MODEL',
MODEL_LIST,
"Model for chat inference. Choices are #{MODEL_LIST.join(", ")}. Defaults to #{DEFAULT_MODEL}"
) do |model|
options[:model] = model
end
opts.on(
'-s',
'--system-message MESSAGE',
'Optional system message to prepend'
) do |message|
options[:system_message] = message
end
opts.on(
'-t',
'--temperature FLOAT',
Float,
"Optional temperature for chat inference. Defaults to #{DEFAULT_TEMPERATURE}"
) do |temp|
options[:temperature] = temp
end
opts.on(
'-d',
'--debug',
'Enable debug logging'
) do
options[:debug] = true
end
end.parse!
api_key = options[:api_key] || ENV.fetch('MISTRAL_API_KEY')
model = options[:model] || DEFAULT_MODEL
system_message = options[:system_message]
temperature = options[:temperature] || DEFAULT_TEMPERATURE
$logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
$logger.debug(
"Starting chatbot with model: #{model}, " \
"temperature: #{temperature}, " \
"system message: #{system_message}"
)
begin
bot = ChatBot.new(api_key, model, system_message, temperature)
bot.start
rescue StandardError => e
$logger.error(e)
exit(1)
end