ruby_bots 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6340ca62c9070462527e782f8003e099a278da7e0c70beb4d3bc87da7609a89
|
4
|
+
data.tar.gz: 0a9207838ae9ce3ac5b1e00cb8ab1e0a32e591dd5cf03efddf11feb5a4921681
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28cb37f96735cb4341ff5f58260db3e568b15933b39b423d9c5b862ddd73f6d8f42a2e4d73aa17e2e74c6643a1d3e377926bcdf01fa70214212618ad6c63f30a
|
7
|
+
data.tar.gz: efa7b38c6cf1ce93e9dd225eb6d7dae0f58f57b8951b8b2287c188e5aba529ca813ca8af5c8a614a59598a499669e6610b84dacaca55d7bde4918b70122514a1
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module RubyBots
|
2
|
+
class OpenAIChatBot < OpenAIBot
|
3
|
+
include RubyBots::Chattable
|
4
|
+
|
5
|
+
DEFAULT_DESCRIPTION = 'This bot will use OpenAI to determine the appropriate tool and use it. It will also chat responses to the user to clarify their request.'.freeze
|
6
|
+
|
7
|
+
def initialize(tools:, name: 'OpenAI chat bot', description: DEFAULT_DESCRIPTION)
|
8
|
+
super(tools:, name:, description:)
|
9
|
+
end
|
10
|
+
|
11
|
+
def system_instructions
|
12
|
+
<<~PROMPT
|
13
|
+
You are an assistant that is chatting with a user and also using tools.
|
14
|
+
You can use the following tools (name - description):
|
15
|
+
#{tools.map { |t| "#{t.name} - #{t.description}" }.join('\n')}
|
16
|
+
|
17
|
+
Select from the following tools (name - description):
|
18
|
+
#{tools.map { |t| "#{t.name} - #{t.description}" }.join('\n')}
|
19
|
+
|
20
|
+
If it is clear that a tool best fits the user's request, return only the tool name and nothing more.
|
21
|
+
If no tool clearly matches the user's request respond with questions to help you clarify the user's response.
|
22
|
+
PROMPT
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def run(input, &block)
|
28
|
+
@messages = [
|
29
|
+
{ role: :system, content: system_instructions },
|
30
|
+
{ role: :user, content: input }
|
31
|
+
]
|
32
|
+
|
33
|
+
bot_output = ''
|
34
|
+
|
35
|
+
until ['exit', 'quit', 'q', 'stop', 'bye'].include?(input.chomp.downcase)
|
36
|
+
response = client.chat(parameters: params)
|
37
|
+
|
38
|
+
bot_output = response.dig('choices', 0, 'message', 'content')
|
39
|
+
|
40
|
+
@messages << { role: :assistant, content: bot_output }
|
41
|
+
|
42
|
+
if tools.map(&:name).include?(bot_output)
|
43
|
+
return tools.find { |t| t.name == bot_output }.response(input)
|
44
|
+
end
|
45
|
+
|
46
|
+
input = yield bot_output
|
47
|
+
|
48
|
+
@messages << { role: :user, content: input }
|
49
|
+
end
|
50
|
+
|
51
|
+
bot_output
|
52
|
+
end
|
53
|
+
|
54
|
+
def params
|
55
|
+
{
|
56
|
+
messages: @messages
|
57
|
+
}.merge(default_params)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RubyBots
|
2
|
+
module Chattable
|
3
|
+
def response(input, &block)
|
4
|
+
run_input_validations(input)
|
5
|
+
|
6
|
+
raise RubyBots::InvalidInputError, { errors: @errors } if @errors.any?
|
7
|
+
|
8
|
+
run(input, &block)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def run(input, &block)
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module RubyBots
|
2
|
+
class OpenAIChatTool < OpenAITool
|
3
|
+
include RubyBots::Chattable
|
4
|
+
|
5
|
+
DEFAULT_DESCRIPTION = 'This tool will use OpenAI to set up a chat interface with the user. It will chat responses to the user to clarify their request.'.freeze
|
6
|
+
|
7
|
+
def initialize(name: 'OpenAI chat tool', description: DEFAULT_DESCRIPTION)
|
8
|
+
super(name:, description:)
|
9
|
+
end
|
10
|
+
|
11
|
+
def system_instructions
|
12
|
+
<<~PROMPT
|
13
|
+
You are a helpful assistant that is chatting with a user. You can ask the user to provide more information
|
14
|
+
if it helps you determine the correct response.
|
15
|
+
PROMPT
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def run(input, &block)
|
21
|
+
@messages = [
|
22
|
+
{ role: :system, content: system_instructions },
|
23
|
+
{ role: :user, content: input }
|
24
|
+
]
|
25
|
+
|
26
|
+
bot_output = ''
|
27
|
+
|
28
|
+
until ['exit', 'quit', 'q', 'stop', 'bye'].include?(input.chomp.downcase)
|
29
|
+
response = client.chat(parameters: params)
|
30
|
+
|
31
|
+
bot_output = response.dig('choices', 0, 'message', 'content')
|
32
|
+
|
33
|
+
@messages << { role: :assistant, content: bot_output }
|
34
|
+
|
35
|
+
input = yield(bot_output)
|
36
|
+
|
37
|
+
@messages << { role: :user, content: input }
|
38
|
+
end
|
39
|
+
|
40
|
+
bot_output
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
{
|
45
|
+
messages: @messages
|
46
|
+
}.merge(default_params)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RubyBots
|
2
|
+
class OpenAIStreamingTool < OpenAIChatTool
|
3
|
+
def run(input, &block)
|
4
|
+
@messages = [
|
5
|
+
{ role: :system, content: system_instructions },
|
6
|
+
{ role: :user, content: input }
|
7
|
+
]
|
8
|
+
|
9
|
+
client.chat(
|
10
|
+
parameters: {
|
11
|
+
messages: @messages,
|
12
|
+
model: 'gpt-4',
|
13
|
+
temperature: 0.7,
|
14
|
+
stream: stream_proc
|
15
|
+
}
|
16
|
+
).each do |response|
|
17
|
+
puts response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def stream_proc
|
22
|
+
proc do |chunk, _bytesize|
|
23
|
+
print chunk.dig('choices', 0, 'delta', 'content')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/ruby_bots/version.rb
CHANGED
data/lib/ruby_bots.rb
CHANGED
@@ -39,9 +39,15 @@ class String
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
require_relative 'ruby_bots/streamable'
|
43
|
+
require_relative 'ruby_bots/chattable'
|
44
|
+
require_relative 'ruby_bots/version'
|
45
|
+
|
42
46
|
# tools
|
43
47
|
require_relative 'ruby_bots/tool'
|
44
48
|
require_relative 'ruby_bots/tools/openai_tool'
|
49
|
+
require_relative 'ruby_bots/tools/openai_chat_tool'
|
50
|
+
require_relative 'ruby_bots/tools/openai_streaming_tool'
|
45
51
|
|
46
52
|
# bots
|
47
53
|
require_relative 'ruby_bots/bot'
|
@@ -49,6 +55,4 @@ require_relative 'ruby_bots/bots/openai_bot'
|
|
49
55
|
require_relative 'ruby_bots/bots/pipeline_bot'
|
50
56
|
require_relative 'ruby_bots/bots/router_bot'
|
51
57
|
require_relative 'ruby_bots/bots/openai_react_bot'
|
52
|
-
|
53
|
-
require_relative 'ruby_bots/streamable'
|
54
|
-
require_relative 'ruby_bots/version'
|
58
|
+
require_relative 'ruby_bots/bots/openai_chat_bot'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_bots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Paulson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: debug
|
@@ -91,11 +91,15 @@ files:
|
|
91
91
|
- lib/ruby_bots.rb
|
92
92
|
- lib/ruby_bots/bot.rb
|
93
93
|
- lib/ruby_bots/bots/openai_bot.rb
|
94
|
+
- lib/ruby_bots/bots/openai_chat_bot.rb
|
94
95
|
- lib/ruby_bots/bots/openai_react_bot.rb
|
95
96
|
- lib/ruby_bots/bots/pipeline_bot.rb
|
96
97
|
- lib/ruby_bots/bots/router_bot.rb
|
98
|
+
- lib/ruby_bots/chattable.rb
|
97
99
|
- lib/ruby_bots/streamable.rb
|
98
100
|
- lib/ruby_bots/tool.rb
|
101
|
+
- lib/ruby_bots/tools/openai_chat_tool.rb
|
102
|
+
- lib/ruby_bots/tools/openai_streaming_tool.rb
|
99
103
|
- lib/ruby_bots/tools/openai_tool.rb
|
100
104
|
- lib/ruby_bots/version.rb
|
101
105
|
homepage: https://github.com/aha-app/ruby_bots
|