ruby_bots 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_bots/bots/openai_chat_bot.rb +60 -0
- data/lib/ruby_bots/chattable.rb +17 -0
- data/lib/ruby_bots/streamable.rb +11 -0
- data/lib/ruby_bots/tools/openai_chat_tool.rb +49 -0
- data/lib/ruby_bots/version.rb +1 -1
- data/lib/ruby_bots.rb +6 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df9f251c6730b2873d3ceb1584d2a82fa9e6a563d2b760706343a60f69de0017
|
4
|
+
data.tar.gz: a9f2522f666a0185019fafd8aa757d2c61a2565d8b2f2bd6c0bfc75577dcd36c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3bdb00120855fefd228f0fc1c35b9aa4f148fdfa8704f66f3828ef188ef8c67247224be1758aa0241da6c771d755284ad30acba283a4909913c9b075bdd3d46
|
7
|
+
data.tar.gz: 3e07cdb73ee2fb5ad46af11512ce588c4013f2d4fc7ec55bcbcebcccae1f4bfbd43cf60821a68686a4ef1d796e3cb1921cd60b75fd6b89fb42666fa15ad6f998
|
@@ -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
|
data/lib/ruby_bots/version.rb
CHANGED
data/lib/ruby_bots.rb
CHANGED
@@ -39,9 +39,14 @@ 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'
|
45
50
|
|
46
51
|
# bots
|
47
52
|
require_relative 'ruby_bots/bot'
|
@@ -49,5 +54,4 @@ require_relative 'ruby_bots/bots/openai_bot'
|
|
49
54
|
require_relative 'ruby_bots/bots/pipeline_bot'
|
50
55
|
require_relative 'ruby_bots/bots/router_bot'
|
51
56
|
require_relative 'ruby_bots/bots/openai_react_bot'
|
52
|
-
|
53
|
-
require_relative 'ruby_bots/version'
|
57
|
+
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.2
|
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-04-
|
11
|
+
date: 2023-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: debug
|
@@ -91,10 +91,14 @@ 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
|
99
|
+
- lib/ruby_bots/streamable.rb
|
97
100
|
- lib/ruby_bots/tool.rb
|
101
|
+
- lib/ruby_bots/tools/openai_chat_tool.rb
|
98
102
|
- lib/ruby_bots/tools/openai_tool.rb
|
99
103
|
- lib/ruby_bots/version.rb
|
100
104
|
homepage: https://github.com/aha-app/ruby_bots
|