forked from drnic/groq-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-chat-streaming.rb
executable file
·128 lines (104 loc) · 3.04 KB
/
user-chat-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
#!/usr/bin/env ruby
require "optparse"
require "groq"
require "yaml"
include Groq::Helpers
@options = {
model: "llama3-70b-8192",
agent_prompt_path: File.join(File.dirname(__FILE__), "agent-prompts/helloworld.yml"),
timeout: 20
}
OptionParser.new do |opts|
opts.banner = "Usage: ruby script.rb [options]"
opts.on("-m", "--model MODEL", "Model name") do |v|
@options[:model] = v
end
opts.on("-a", "--agent-prompt PATH", "Path to agent prompt file") do |v|
@options[:agent_prompt_path] = v
end
opts.on("-t", "--timeout TIMEOUT", "Timeout in seconds") do |v|
@options[:timeout] = v.to_i
end
opts.on("-d", "--debug", "Enable debug mode") do |v|
@options[:debug] = v
end
end.parse!
raise "Missing --model option" if @options[:model].nil?
raise "Missing --agent-prompt option" if @options[:agent_prompt_path].nil?
# Read the agent prompt from the file
agent_prompt = YAML.load_file(@options[:agent_prompt_path])
user_emoji = agent_prompt["user_emoji"]
agent_emoji = agent_prompt["agent_emoji"]
system_prompt = agent_prompt["system_prompt"] || agent_prompt["system"]
can_go_first = agent_prompt["can_go_first"]
# Initialize the Groq client
@client = Groq::Client.new(model_id: @options[:model], request_timeout: @options[:timeout]) do |f|
if @options[:debug]
require "logger"
# Create a logger instance
logger = Logger.new($stdout)
logger.level = Logger::DEBUG
f.response :logger, logger, bodies: true # Log request and response bodies
end
end
puts "Welcome to the AI assistant! I'll respond to your queries."
puts "You can quit by typing 'exit'."
def produce_summary(messages)
combined = messages.map do |message|
if message["role"] == "user"
"User: #{message["content"]}"
else
"Assistant: #{message["content"]}"
end
end.join("\n")
response = @client.chat([
S("You are excellent at reading a discourse between a human and an AI assistant and summarising the current conversation."),
U("Here is the current conversation:\n\n------\n\n#{combined}")
])
puts response["content"]
end
messages = [S(system_prompt)]
if can_go_first
print "#{agent_emoji} "
message_bits = []
response = @client.chat(messages) do |content|
# content == nil on last message; and "" on first message
next unless content
print(content)
message_bits << content
end
puts
messages << A(message_bits.join(""))
end
class MessageBits
def initialize(emoji)
print "#{emoji} "
@bits = []
end
def call(content)
if content.nil?
puts
else
print(content)
@bits << content
end
end
def to_assistant_message
Assistant(@bits.join(""))
end
end
loop do
print "#{user_emoji} "
user_input = gets.chomp
break if user_input.downcase == "exit"
# produce summary
if user_input.downcase == "summary"
produce_summary(messages)
next
end
messages << U(user_input)
# Use Groq to generate a response
message_bits = MessageBits.new(agent_emoji)
@client.chat(messages, stream: message_bits)
messages << message_bits.to_assistant_message
end