ruboty-openai_chat 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.example +4 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +89 -0
- data/HOW_TO_DEVELOPMENT.md +21 -0
- data/README.md +23 -0
- data/Rakefile +12 -0
- data/lib/ruboty/handlers/openai_chat.rb +26 -0
- data/lib/ruboty/openai_chat/actions/base.rb +33 -0
- data/lib/ruboty/openai_chat/actions/chat.rb +118 -0
- data/lib/ruboty/openai_chat/dialog.rb +53 -0
- data/lib/ruboty/openai_chat/memory.rb +35 -0
- data/lib/ruboty/openai_chat/version.rb +7 -0
- data/lib/ruboty/openai_chat.rb +19 -0
- data/ruboty-openai_chat.gemspec +37 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cdfa3a76cfc5835ec767fcb4b901b82e9dfdf0d38e745de8436f992483efc174
|
4
|
+
data.tar.gz: f89ad7497aa22c7fa4d28544badae1e98d523a66900c85e949dc3ab4f2fad5ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9572cd9c1a59a56fc4beaa6f97ea0e0a6c4993bc0545ed02bcaadfe94faf9f5acb878f3658413eca24e0fa33e687d54e345ec1e929af8ac5e120d6515e449d51
|
7
|
+
data.tar.gz: 9bf943c328b980051170064f7f53051b6dcca4a519dc4003c7e156f40a86b37641d1716ff8a93ef383fe781bb6bb6f467d5b6e6d0080ea05207371cfecff7d4c
|
data/.env.example
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Metrics:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/StringLiterals:
|
8
|
+
Enabled: true
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Style/StringLiteralsInInterpolation:
|
12
|
+
Enabled: true
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Style/Documentation:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Layout/LineLength:
|
19
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tomo.asleep@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruboty-openai_chat (0.1.0)
|
5
|
+
ruboty
|
6
|
+
ruby-openai (~> 2.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (7.0.4)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
ast (2.4.2)
|
17
|
+
concurrent-ruby (1.1.10)
|
18
|
+
diff-lcs (1.5.0)
|
19
|
+
dotenv (2.8.1)
|
20
|
+
httparty (0.20.0)
|
21
|
+
mime-types (~> 3.0)
|
22
|
+
multi_xml (>= 0.5.2)
|
23
|
+
i18n (1.12.0)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
json (2.6.3)
|
26
|
+
mem (0.1.5)
|
27
|
+
mime-types (3.4.1)
|
28
|
+
mime-types-data (~> 3.2015)
|
29
|
+
mime-types-data (3.2022.0105)
|
30
|
+
minitest (5.16.3)
|
31
|
+
multi_xml (0.6.0)
|
32
|
+
parallel (1.22.1)
|
33
|
+
parser (3.1.3.0)
|
34
|
+
ast (~> 2.4.1)
|
35
|
+
rainbow (3.1.1)
|
36
|
+
rake (13.0.6)
|
37
|
+
regexp_parser (2.6.1)
|
38
|
+
rexml (3.2.5)
|
39
|
+
rspec (3.12.0)
|
40
|
+
rspec-core (~> 3.12.0)
|
41
|
+
rspec-expectations (~> 3.12.0)
|
42
|
+
rspec-mocks (~> 3.12.0)
|
43
|
+
rspec-core (3.12.0)
|
44
|
+
rspec-support (~> 3.12.0)
|
45
|
+
rspec-expectations (3.12.1)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.12.0)
|
48
|
+
rspec-mocks (3.12.1)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.12.0)
|
51
|
+
rspec-support (3.12.0)
|
52
|
+
rubocop (1.40.0)
|
53
|
+
json (~> 2.3)
|
54
|
+
parallel (~> 1.10)
|
55
|
+
parser (>= 3.1.2.1)
|
56
|
+
rainbow (>= 2.2.2, < 4.0)
|
57
|
+
regexp_parser (>= 1.8, < 3.0)
|
58
|
+
rexml (>= 3.2.5, < 4.0)
|
59
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
60
|
+
ruby-progressbar (~> 1.7)
|
61
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
62
|
+
rubocop-ast (1.24.0)
|
63
|
+
parser (>= 3.1.1.0)
|
64
|
+
ruboty (1.3.1)
|
65
|
+
activesupport
|
66
|
+
bundler
|
67
|
+
dotenv
|
68
|
+
mem
|
69
|
+
slop
|
70
|
+
ruby-openai (2.2.0)
|
71
|
+
dotenv (>= 2.7.6, < 2.9.0)
|
72
|
+
httparty (>= 0.18.1, < 0.21.0)
|
73
|
+
ruby-progressbar (1.11.0)
|
74
|
+
slop (4.9.3)
|
75
|
+
tzinfo (2.0.5)
|
76
|
+
concurrent-ruby (~> 1.0)
|
77
|
+
unicode-display_width (2.3.0)
|
78
|
+
|
79
|
+
PLATFORMS
|
80
|
+
arm64-darwin-21
|
81
|
+
|
82
|
+
DEPENDENCIES
|
83
|
+
rake (~> 13.0)
|
84
|
+
rspec (~> 3.0)
|
85
|
+
rubocop (~> 1.21)
|
86
|
+
ruboty-openai_chat!
|
87
|
+
|
88
|
+
BUNDLED WITH
|
89
|
+
2.3.18
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# How to development
|
2
|
+
|
3
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
4
|
+
|
5
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
6
|
+
|
7
|
+
# Contributing
|
8
|
+
|
9
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tomoasleep/ruboty-openai_chat. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruboty-openai-chat/blob/main/CODE_OF_CONDUCT.md).
|
10
|
+
|
11
|
+
# Code of Conduct
|
12
|
+
|
13
|
+
Everyone interacting in the Ruboty::OpenAI::Chat project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tomoasleep/ruboty-openai_chat/blob/main/CODE_OF_CONDUCT.md).
|
14
|
+
|
15
|
+
# Tips
|
16
|
+
|
17
|
+
## Run Ruboty::OpenAIChat locally
|
18
|
+
|
19
|
+
```
|
20
|
+
./bin/ruboty --dotenv
|
21
|
+
```
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Ruboty::OpenAIChat
|
2
|
+
|
3
|
+
[OpenAI](https://openai.com/) responds to given message if any other handler does not matches.
|
4
|
+
|
5
|
+
This gem uses conversation with an AI assistant like https://beta.openai.com/examples/default-chat as prompt for OpenAI.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add ruboty-openai-chat
|
12
|
+
|
13
|
+
### ENV
|
14
|
+
|
15
|
+
- `OPENAI_ACCESS_TOKEN` - Pass OpenAI ACCESS TOKEN
|
16
|
+
- `OPENAI_ORGANIZATION_ID` - Pass OpenAI Organization ID"
|
17
|
+
- `OPENAI_CHAT_PRETEXT` -
|
18
|
+
- `OPENAI_CHAT_LANGUAGE` - Pass your primary language", optional: true
|
19
|
+
- `OPENAI_CHAT_MEMORIZE_SECONDS` - AI
|
20
|
+
|
21
|
+
## Development
|
22
|
+
|
23
|
+
See: [HOW_TO_DEVELOPMENT.md](./HOW_TO_DEVELOPMENT.md)
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ruboty"
|
4
|
+
|
5
|
+
module Ruboty
|
6
|
+
module Handlers
|
7
|
+
class OpenAIChat < Base
|
8
|
+
env :OPENAI_ACCESS_TOKEN, "Pass OpenAI ACCESS TOKEN"
|
9
|
+
env :OPENAI_ORGANIZATION_ID, "Pass OpenAI Organization ID"
|
10
|
+
env :OPENAI_CHAT_PRETEXT, "Pretext of OpenAI prompt"
|
11
|
+
env :OPENAI_CHAT_LANGUAGE, "Pass your primary language", optional: true
|
12
|
+
env :OPENAI_CHAT_MEMORIZE_SECONDS, "AI remembers the past dialogs in the specified seconds", optional: true
|
13
|
+
|
14
|
+
on(
|
15
|
+
/(?<body>.+)/m,
|
16
|
+
description: "OpenAI responds to your message if given message did not match any other handlers",
|
17
|
+
missing: true,
|
18
|
+
name: "chat"
|
19
|
+
)
|
20
|
+
|
21
|
+
def chat(message)
|
22
|
+
Ruboty::OpenAIChat::Actions::Chat.new(message).call
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module OpenAIChat
|
5
|
+
module Actions
|
6
|
+
# @abstract
|
7
|
+
class Base
|
8
|
+
attr_reader :message
|
9
|
+
|
10
|
+
# @param message [Ruboty::Message]
|
11
|
+
def initialize(message)
|
12
|
+
@message = message
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [OpenAI::Client]
|
16
|
+
def client
|
17
|
+
@client ||= OpenAI::Client.new(access_token: ENV.fetch("OPENAI_ACCESS_TOKEN"),
|
18
|
+
organization_id: ENV.fetch("OPENAI_ORGANIZATION_ID"))
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Ruboty::Robot]
|
22
|
+
def robot
|
23
|
+
message.robot
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Memory]
|
27
|
+
def memory
|
28
|
+
@memory ||= Memory.new(robot)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
|
5
|
+
module Ruboty
|
6
|
+
module OpenAIChat
|
7
|
+
module Actions
|
8
|
+
class Chat < Base
|
9
|
+
NAMESPACE = "openai-chat-actions-chat"
|
10
|
+
|
11
|
+
# @return [String]
|
12
|
+
attr_reader :human_comment, :ai_comment
|
13
|
+
|
14
|
+
def call
|
15
|
+
human_comment = message[:body]
|
16
|
+
response = complete(human_comment)
|
17
|
+
p response if ENV["OPENAI_CHAT_DEBUG"]
|
18
|
+
raise response.body if response.code >= 400
|
19
|
+
|
20
|
+
ai_comment = response.dig("choices", 0, "text").gsub(/\A\s+/, "") || ""
|
21
|
+
|
22
|
+
remember_dialog(Dialog.new(human_comment: human_comment, ai_comment: ai_comment, expire_at: expire_at))
|
23
|
+
message.reply(ai_comment)
|
24
|
+
rescue StandardError => e
|
25
|
+
forget
|
26
|
+
message.reply(e.message, code: true)
|
27
|
+
raise e if ENV["OPENAI_CHAT_DEBUG"]
|
28
|
+
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def complete(human_comment)
|
35
|
+
# https://beta.openai.com/examples/default-chat
|
36
|
+
client.completions(
|
37
|
+
parameters: {
|
38
|
+
model: "text-davinci-003",
|
39
|
+
temperature: 0.9,
|
40
|
+
max_tokens: 512,
|
41
|
+
top_p: 1,
|
42
|
+
frequency_penalty: 0,
|
43
|
+
presence_penalty: 0.6,
|
44
|
+
stop: Dialog::STOP_SEQUENCES,
|
45
|
+
prompt: build_prompt(human_comment)
|
46
|
+
}
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_prompt(human_comment)
|
51
|
+
prefix = [prompt_prefix]
|
52
|
+
prefix += [ENV["OPENAI_CHAT_PRETEXT"]&.gsub(/\R/, " ")].compact
|
53
|
+
|
54
|
+
dialogs = [example_dialog, *dialogs_from_memory,
|
55
|
+
Dialog.new(human_comment: human_comment, ai_comment: "")].map do |dialog|
|
56
|
+
dialog.to_prompt.chomp
|
57
|
+
end.join("\n")
|
58
|
+
|
59
|
+
<<~STRING.chomp
|
60
|
+
#{prefix.join(" ")}
|
61
|
+
|
62
|
+
#{dialogs}
|
63
|
+
STRING
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [Array<Dialog>]
|
67
|
+
def dialogs_from_memory
|
68
|
+
raw_dialogs.reject! { |hash| Dialog.from_hash(hash).expired? }
|
69
|
+
raw_dialogs.map { |hash| Dialog.from_hash(hash) }
|
70
|
+
end
|
71
|
+
|
72
|
+
# @param dialog [Dialog]
|
73
|
+
def remember_dialog(dialog)
|
74
|
+
raw_dialogs << dialog.to_h
|
75
|
+
end
|
76
|
+
|
77
|
+
# @return [Array<Hash>]
|
78
|
+
def raw_dialogs
|
79
|
+
memory.namespace(NAMESPACE, message.from || "general")[:dialogs] ||= []
|
80
|
+
end
|
81
|
+
|
82
|
+
def forget
|
83
|
+
memory.delete(NAMESPACE, message.from || "general")
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [Time]
|
87
|
+
def expire_at
|
88
|
+
Time.now + ENV.fetch("OPENAI_CHAT_MEMORIZE_SECONDS") { 5 * 60 }.to_i
|
89
|
+
end
|
90
|
+
|
91
|
+
# @return [String]
|
92
|
+
def prompt_prefix
|
93
|
+
"The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. The AI assistant's name is #{robot.name}."
|
94
|
+
end
|
95
|
+
|
96
|
+
def example_dialog
|
97
|
+
case language
|
98
|
+
when :ja
|
99
|
+
Dialog.new(
|
100
|
+
human_comment: "こんにちは。あなたは誰ですか?",
|
101
|
+
ai_comment: "私は OpenAI 製の AI アシスタントの #{robot.name} です。なにかお手伝いできることはありますか?"
|
102
|
+
)
|
103
|
+
else
|
104
|
+
Dialog.new(
|
105
|
+
human_comment: "Hello, who are you?",
|
106
|
+
ai_comment: "I'm #{robot.name}, an AI assistant created by OpenAI. How can I help you today?"
|
107
|
+
)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# @return [Symbol]
|
112
|
+
def language
|
113
|
+
(ENV["OPENAI_CHAT_LANGUAGE"] || :en).to_sym
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module OpenAIChat
|
5
|
+
class Dialog
|
6
|
+
STOP_SEQUENCES = [">> Human: ", ">> AI: "].freeze
|
7
|
+
STOP_SEQUENCE_PATTERN = />> (Human|AI): /.freeze
|
8
|
+
|
9
|
+
# @return [String]
|
10
|
+
attr_reader :human_comment, :ai_comment
|
11
|
+
|
12
|
+
# @return [Time]
|
13
|
+
attr_reader :expire_at
|
14
|
+
|
15
|
+
def self.from_hash(hash)
|
16
|
+
new(**hash.transform_keys(&:to_sym))
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param human_comment [String]
|
20
|
+
# @param ai_comment [String]
|
21
|
+
# @param expire_at [Time, Integer, nil]
|
22
|
+
def initialize(human_comment:, ai_comment:, expire_at: nil)
|
23
|
+
@human_comment = human_comment
|
24
|
+
@ai_comment = ai_comment
|
25
|
+
@expire_at = expire_at&.yield_self { |t| Time.at(t) }
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [String]
|
29
|
+
def to_prompt
|
30
|
+
<<~STRING
|
31
|
+
>> Human: #{escape(human_comment).chomp}
|
32
|
+
>> AI: #{escape(ai_comment).chomp}
|
33
|
+
STRING
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Hash]
|
37
|
+
def to_h
|
38
|
+
{ human_comment: human_comment, ai_comment: ai_comment, expire_at: expire_at&.to_i }
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [Boolean]
|
42
|
+
def expired?
|
43
|
+
expire_at && (expire_at <= Time.now)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def escape(text)
|
49
|
+
text.gsub(STOP_SEQUENCE_PATTERN, &:downcase)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module OpenAIChat
|
5
|
+
class Memory
|
6
|
+
# @return [Ruboty::Robot]
|
7
|
+
attr_reader :robot
|
8
|
+
|
9
|
+
# @param robot [Ruboty::Robot]
|
10
|
+
def initialize(robot)
|
11
|
+
@robot = robot
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param keys [Array<String, Symbol>]
|
15
|
+
# @return [Hash]
|
16
|
+
def namespace(*keys)
|
17
|
+
keys.reduce(robot.brain.data) do |data, key|
|
18
|
+
data[key] ||= {}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param prefix [Array<String, Symbol>]
|
23
|
+
# @param key [String, Symbol]
|
24
|
+
def delete(*prefix, key)
|
25
|
+
dig(*prefix)&.delete(key)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param keys [Array<String, Symbol>]
|
29
|
+
# @return [Object, nil]
|
30
|
+
def dig(*keys)
|
31
|
+
prefix.empty? ? robot.brain.data : robot.brain.data.dig(*prefix)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ruboty"
|
4
|
+
require "ruby/openai"
|
5
|
+
|
6
|
+
require_relative "openai_chat/version"
|
7
|
+
require_relative "openai_chat/actions/base"
|
8
|
+
require_relative "openai_chat/actions/chat"
|
9
|
+
require_relative "openai_chat/dialog"
|
10
|
+
require_relative "openai_chat/memory"
|
11
|
+
|
12
|
+
require_relative "handlers/openai_chat"
|
13
|
+
|
14
|
+
module Ruboty
|
15
|
+
module OpenAIChat
|
16
|
+
class Error < StandardError; end
|
17
|
+
# Your code goes here...
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ruboty/openai_chat/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ruboty-openai_chat"
|
7
|
+
spec.version = Ruboty::OpenAIChat::VERSION
|
8
|
+
spec.authors = ["Tomoya Chiba"]
|
9
|
+
spec.email = ["tomo.asleep@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "OpenAI responds to given message if any other handler does not matches."
|
12
|
+
spec.homepage = "https://github.com/tomoasleep/ruboty-openai_chat"
|
13
|
+
spec.required_ruby_version = ">= 2.6.0"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = File.join(spec.homepage, "blob/main/CHANGELOG.md")
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(__dir__) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
# Uncomment to register a new dependency of your gem
|
31
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
32
|
+
spec.add_dependency "ruboty"
|
33
|
+
spec.add_dependency "ruby-openai", "~> 2.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruboty-openai_chat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomoya Chiba
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruboty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ruby-openai
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- tomo.asleep@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".env.example"
|
49
|
+
- ".rspec"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- CHANGELOG.md
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- HOW_TO_DEVELOPMENT.md
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/ruboty/handlers/openai_chat.rb
|
59
|
+
- lib/ruboty/openai_chat.rb
|
60
|
+
- lib/ruboty/openai_chat/actions/base.rb
|
61
|
+
- lib/ruboty/openai_chat/actions/chat.rb
|
62
|
+
- lib/ruboty/openai_chat/dialog.rb
|
63
|
+
- lib/ruboty/openai_chat/memory.rb
|
64
|
+
- lib/ruboty/openai_chat/version.rb
|
65
|
+
- ruboty-openai_chat.gemspec
|
66
|
+
homepage: https://github.com/tomoasleep/ruboty-openai_chat
|
67
|
+
licenses: []
|
68
|
+
metadata:
|
69
|
+
homepage_uri: https://github.com/tomoasleep/ruboty-openai_chat
|
70
|
+
source_code_uri: https://github.com/tomoasleep/ruboty-openai_chat
|
71
|
+
changelog_uri: https://github.com/tomoasleep/ruboty-openai_chat/blob/main/CHANGELOG.md
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 2.6.0
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.1.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: OpenAI responds to given message if any other handler does not matches.
|
91
|
+
test_files: []
|