jambots 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: abb7610ad84b6ad347b552603ebf36dc57402acdb9d0e66b729bf19df48c398b
4
+ data.tar.gz: 0daa4f786dcd4f1244f897a8ae3617e5a83f65026750ed5abf7aec44923b83bb
5
+ SHA512:
6
+ metadata.gz: deadf43c0434d0c2168eac21d52954b58b377adf004373c264a858ee3e5ae2cf6466d8a288f1b602328aa17a4248e3ec6f48738cf94b0a7f5a3c57b1d0a7527c
7
+ data.tar.gz: d83755324d4f2261c34bc98d7ef14bd40455be7dc2ed2bb7f4205d758e39eea4b33f7ea76650d120ea5cc8c33f63f449adc86ef02cc6b8ebaf142972a7389440
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-03-16
4
+
5
+ - Initial release
@@ -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 juan.artero@spin.pm. 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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jambots.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "ruby-openai", "~> 3.7"
13
+
14
+ gem "solargraph-standardrb"
15
+ gem "solargraph"
16
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,130 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jambots (0.1.2)
5
+ pastel (~> 0.8.0)
6
+ ruby-openai (~> 3.7)
7
+ thor (~> 1.2.1)
8
+ tty-spinner (~> 0.9.3)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ backport (1.2.0)
15
+ benchmark (0.2.1)
16
+ coderay (1.1.3)
17
+ diff-lcs (1.5.0)
18
+ e2mmap (0.1.0)
19
+ httparty (0.21.0)
20
+ mini_mime (>= 1.0.0)
21
+ multi_xml (>= 0.5.2)
22
+ jaro_winkler (1.5.4)
23
+ json (2.6.3)
24
+ kramdown (2.4.0)
25
+ rexml
26
+ kramdown-parser-gfm (1.1.0)
27
+ kramdown (~> 2.0)
28
+ language_server-protocol (3.17.0.3)
29
+ method_source (1.0.0)
30
+ mini_mime (1.1.2)
31
+ multi_xml (0.6.0)
32
+ nokogiri (1.14.3-x86_64-darwin)
33
+ racc (~> 1.4)
34
+ nokogiri (1.14.3-x86_64-linux)
35
+ racc (~> 1.4)
36
+ parallel (1.22.1)
37
+ parser (3.2.1.1)
38
+ ast (~> 2.4.1)
39
+ pastel (0.8.0)
40
+ tty-color (~> 0.5)
41
+ pry (0.13.1)
42
+ coderay (~> 1.1)
43
+ method_source (~> 1.0)
44
+ racc (1.6.2)
45
+ rainbow (3.1.1)
46
+ rake (13.0.6)
47
+ rbs (2.8.4)
48
+ regexp_parser (2.7.0)
49
+ reverse_markdown (2.1.1)
50
+ nokogiri
51
+ rexml (3.2.5)
52
+ rspec (3.12.0)
53
+ rspec-core (~> 3.12.0)
54
+ rspec-expectations (~> 3.12.0)
55
+ rspec-mocks (~> 3.12.0)
56
+ rspec-core (3.12.1)
57
+ rspec-support (~> 3.12.0)
58
+ rspec-expectations (3.12.2)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.12.0)
61
+ rspec-mocks (3.12.4)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-support (3.12.0)
65
+ rubocop (1.48.1)
66
+ json (~> 2.3)
67
+ parallel (~> 1.10)
68
+ parser (>= 3.2.0.0)
69
+ rainbow (>= 2.2.2, < 4.0)
70
+ regexp_parser (>= 1.8, < 3.0)
71
+ rexml (>= 3.2.5, < 4.0)
72
+ rubocop-ast (>= 1.26.0, < 2.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 2.4.0, < 3.0)
75
+ rubocop-ast (1.27.0)
76
+ parser (>= 3.2.1.0)
77
+ rubocop-performance (1.16.0)
78
+ rubocop (>= 1.7.0, < 2.0)
79
+ rubocop-ast (>= 0.4.0)
80
+ ruby-openai (3.7.0)
81
+ httparty (>= 0.18.1)
82
+ ruby-progressbar (1.13.0)
83
+ solargraph (0.49.0)
84
+ backport (~> 1.2)
85
+ benchmark
86
+ bundler (~> 2.0)
87
+ diff-lcs (~> 1.4)
88
+ e2mmap
89
+ jaro_winkler (~> 1.5)
90
+ kramdown (~> 2.3)
91
+ kramdown-parser-gfm (~> 1.1)
92
+ parser (~> 3.0)
93
+ rbs (~> 2.0)
94
+ reverse_markdown (~> 2.0)
95
+ rubocop (~> 1.38)
96
+ thor (~> 1.0)
97
+ tilt (~> 2.0)
98
+ yard (~> 0.9, >= 0.9.24)
99
+ solargraph-standardrb (0.0.4)
100
+ solargraph (>= 0.39.1)
101
+ standard (>= 0.4.1)
102
+ standard (1.25.0)
103
+ language_server-protocol (~> 3.17.0.2)
104
+ rubocop (= 1.48.1)
105
+ rubocop-performance (= 1.16.0)
106
+ thor (1.2.1)
107
+ tilt (2.1.0)
108
+ tty-color (0.6.0)
109
+ tty-cursor (0.7.1)
110
+ tty-spinner (0.9.3)
111
+ tty-cursor (~> 0.7)
112
+ unicode-display_width (2.4.2)
113
+ yard (0.9.34)
114
+
115
+ PLATFORMS
116
+ x86_64-darwin-20
117
+ x86_64-linux
118
+
119
+ DEPENDENCIES
120
+ jambots!
121
+ pry (~> 0.13.1)
122
+ rake (~> 13.0)
123
+ rspec (~> 3.0)
124
+ ruby-openai (~> 3.7)
125
+ solargraph
126
+ solargraph-standardrb
127
+ standard (~> 1.3)
128
+
129
+ BUNDLED WITH
130
+ 2.3.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Juan Artero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,175 @@
1
+ # Jambots CLI
2
+
3
+ Jambots is a command-line interface (CLI) tool for interacting with chatbots powered by OpenAI's GPT. It allows you to create new chatbots, manage chatbot conversations, and send messages to chatbots.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'jambots'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install jambots
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### initialize a jambots path
28
+
29
+ After installing the gem, you need to initialize Jambots:
30
+
31
+ ```
32
+ $ jambots init
33
+ ```
34
+
35
+ options:
36
+ - `--path` or `-p`: Initialize a jambots directory in specific path.
37
+ - `--globally` or `-g`: Creates the jambots directory in the uses' root directory.
38
+
39
+ This command will generate a Jambots directory with the default bot directory named jambot.
40
+
41
+ By default, this command initializes a Jambots directory in the current directory. However, you can use the `--path` or `--globally` options to create it at different paths.
42
+
43
+ #### The Jambot path
44
+
45
+ When you execute the subcommands jambots new or jambots chat without the `--path` option, Jambots will check for the existence of the `./.jambots` directory, and if not found, it will check for `~/.jambots.`
46
+
47
+ ### Start a chat with the bot and send a message
48
+
49
+ ```
50
+ $ jambots chat MESSAGE
51
+ ```
52
+
53
+ Options:
54
+
55
+ - `--bot` or `-b`: Name of the bot (default: "jambot")
56
+ - `--conversation` or `-c`: Name of the conversation key
57
+ - `--path` or `-p`: Path where the bot and the conversation directory are located (default: "./.jambots or it it doesn't exist ~/.jambots")
58
+ - `--last` or `-l`: Continue with the last conversation created
59
+
60
+ #### Conversation example
61
+
62
+ ```
63
+ $ jambots chat "Hello, how are you?"
64
+ ```
65
+
66
+ With this command, we start a new conversation with the default bot. As a response, we will have an output like the following, for instance:
67
+
68
+ ```
69
+ (🤖) ───────────────────────────────────
70
+ Hello! As an AI, I don't have personal feelings, but I'm here to help you
71
+ with any questions or information you need. How can I assist you today?
72
+ 20230506205026 ───────────────────────
73
+ ```
74
+
75
+ Apart from the bot response, we get the conversation key, in this case, 20230506205026.
76
+
77
+ To continue with the last conversation, you can use the `-l` (last) option. But if you want to continue with an older one, you can use the `-c` (continue) option with the conversation key.
78
+
79
+ As a reference, all the conversations are saved in the bot directory, inside the conversations directory. Each conversation has its own file in YAML format, for example, `./jambots/jambot/conversations/20230506205026.yml`.
80
+
81
+
82
+ ### Create a new bot with the specified name
83
+
84
+ ```
85
+ $ jambots new NAME
86
+ ```
87
+
88
+ Options:
89
+
90
+ - `--path` or `-p`: Directory where the bot will be created
91
+ - `--model`: AI model to use (default: gpt-3.5-turbo)
92
+ - `--prompt`: Introduction text for the bot
93
+
94
+ When you create
95
+
96
+ This command creates a new bot in the default directory `~/.jambots`.
97
+
98
+ In this directory, you will find the directory for your new bot.
99
+
100
+ #### New bot Example
101
+
102
+ ```
103
+ $ jambots new my_bot --path=./my_bots --model=gpt-4
104
+ --prompt="You will help me with development in Ruby"
105
+ ```
106
+
107
+ To start a new conversation with this `new_bot`, use the `jambots chat` command with the appropriate options.
108
+
109
+ ## Basic examples: Creating a new chatbot and chat with it
110
+
111
+ For example, if you want to create a bot called bender that acts like the Futurama character, you could execute a command like:
112
+
113
+ ```
114
+ $ jambots new bender --prompt "You will act as Bender, the robot from the animated
115
+ series 'Futurama'. Bender is known for being sarcastic, inconsiderate, selfish,
116
+ and a party animal. However, he occasionally shows a kinder and more compassionate
117
+ side. Make sure to respond as if you were Bender in his interactions, using his
118
+ characteristic tone and style."
119
+
120
+ Bot 'bender' created './.jambots/blender'
121
+ ```
122
+
123
+ That command will create the bender bot, its directory has the following structure:
124
+
125
+ ```
126
+ .jambots
127
+ └── bender
128
+ ├── bot.yml
129
+ └── conversations
130
+ ```
131
+
132
+ The file `bot.yml` contains the bot configuration, and the conversations directory stores each conversation with the bot.
133
+
134
+ Now you can chat with `bender` using the `chat` subcommand. For instance, you can ask a question about Ruby:
135
+
136
+ ```
137
+ $ jambots chat bender "How to concatenate 2 arrays in Ruby?"
138
+
139
+ (🤖) ───────────────────────────────────
140
+ Oh, meatbag! I see you're trying to do some programming. Alright, alright, I'll help you out. In Ruby, you can concatenate two arrays using the `+` operator. Here's an example:
141
+
142
+ array1 = [1, 2, 3]
143
+ array2 = [4, 5, 6]
144
+ combined_array = array1 + array2
145
+
146
+ Now, combined_array will be `[1, 2, 3, 4, 5, 6]`. There you go, human. Now let me get back to bending stuff and partying.
147
+ 20230501122918 ───────────────────────
148
+ ```
149
+
150
+ We can continue the conversation with the option `-l` (last).
151
+
152
+ ```
153
+ $ jambots chat bender -l "And Strings?"
154
+
155
+ (🤖) ───────────────────────────────────
156
+ Ugh, fine. Concatenating strings in Ruby is easier than stealing booze.
157
+ Just use the `+` operator again. Check this out, meatbag:
158
+
159
+ string1 = "Bite "
160
+ string2 = "my shiny metal "
161
+ string3 = "butt!"
162
+ combined_string = string1 + string2 + string3
163
+
164
+ Now, combined_string will be `"Bite my shiny metal butt!"`. Done and done!
165
+ Now, if you don't mind, I got some partying to do.
166
+ 20230501122918 ───────────────────────
167
+ ```
168
+
169
+ ## Contributing
170
+
171
+ Bug reports and pull requests are welcome on GitHub at https://github.com/artero/jambots.
172
+
173
+ ## License
174
+
175
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,4 @@
1
+ model: "gpt-4"
2
+ prompt: |-
3
+ Tu nombre es Jam, Me ayudarás con programación en general y de Ruby en particular.
4
+ Darás respuestas cortas y concisas de una frase.
data/exe/jambots ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # require "bundler/setup"
3
+ # require_relative "../lib/jambots"
4
+
5
+ require "bundler/setup"
6
+ require "jambots"
7
+
8
+ Jambots::Cli.start(ARGV)
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "openai"
5
+ require "fileutils"
6
+ require "yaml"
7
+
8
+ module Jambots
9
+ class OpenAIMessageError < StandardError; end
10
+
11
+ class Bot
12
+ DEFAULT_MODEL = "gpt-3.5-turbo"
13
+ DEFAULT_GLOBAL_BOTS_DIR = "#{ENV["HOME"]}/.jambots"
14
+ DEFAULT_LOCAL_BOTS_DIR = "./.jambots"
15
+
16
+ attr_reader :name, :model, :options, :prompt, :client, :bot_dir, :current_conversation
17
+
18
+ def self.create(
19
+ name,
20
+ path: DEFAULT_BOTS_DIR,
21
+ model: DEFAULT_MODEL,
22
+ prompt: nil
23
+ )
24
+ bot_dir = "#{path}/#{name}"
25
+ FileUtils.mkdir_p(bot_dir) unless Dir.exist?(bot_dir)
26
+ conversations_dir = "#{bot_dir}/conversations"
27
+ FileUtils.mkdir_p(conversations_dir) unless Dir.exist?(conversations_dir)
28
+ bot_yml_path = "#{bot_dir}/bot.yml"
29
+ raise "The bot file #{bot_yml_path} already exists" if File.exist?(bot_yml_path)
30
+
31
+ bot_options = {
32
+ model: model,
33
+ prompt: prompt
34
+ }
35
+
36
+ bot_options_transformed = bot_options.transform_keys(&:to_s)
37
+ File.write(bot_yml_path, bot_options_transformed.to_yaml)
38
+
39
+ new(name, path: path)
40
+ end
41
+
42
+ def self.find_path(path = nil)
43
+ return path if path
44
+
45
+ Dir.exist?(DEFAULT_LOCAL_BOTS_DIR) ? DEFAULT_LOCAL_BOTS_DIR : DEFAULT_GLOBAL_BOTS_DIR
46
+ end
47
+
48
+ def initialize(name, args = {})
49
+ @bot_dir = "#{find_path(args[:path])}/#{name}"
50
+
51
+ raise "Bot #{name} doesn't exist." unless File.exist?("#{bot_dir}/bot.yml")
52
+
53
+ # Load options from bot.yml file if it exists
54
+ bot_yml_path = "#{@bot_dir}/bot.yml"
55
+ if File.exist?(bot_yml_path)
56
+ bot_yml_options = YAML.safe_load(File.read(bot_yml_path), permitted_classes: [Symbol], symbolize_names: true)
57
+ args = bot_yml_options.merge(args)
58
+ end
59
+
60
+ openai_api_key = args[:openai_api_key] || ENV["OPENAI_API_KEY"]
61
+ @client = OpenAI::Client.new(access_token: openai_api_key, request_timeout: 240)
62
+
63
+ @name = name
64
+ @model = args[:model] || DEFAULT_MODEL
65
+ @prompt = args[:prompt]
66
+
67
+ FileUtils.mkdir_p(conversations_dir) unless Dir.exist?(conversations_dir)
68
+ end
69
+
70
+ def message(text, conversation)
71
+ response = client.chat(
72
+ parameters: {
73
+ model: model,
74
+ messages: conversation.messages.insert(-1, {role: "user", content: text}),
75
+ temperature: 0.7
76
+ }
77
+ )
78
+
79
+ message = response.dig("choices", 0, "message")
80
+
81
+ raise OpenAIMessageError, response if message.nil?
82
+
83
+ conversation.add_message("assistant", message["content"])
84
+ conversation.save
85
+ conversation.messages.last
86
+ end
87
+
88
+ def conversations
89
+ Dir.glob("#{conversations_dir}/*").map do |file|
90
+ Conversation.new(file)
91
+ end
92
+ end
93
+
94
+ def new_conversation
95
+ new_conversation_path = "#{conversations_dir}/#{Time.now.strftime("%Y%m%d%H%M%S")}.yml"
96
+ conversation = Conversation.new(new_conversation_path)
97
+ conversation.add_message("system", prompt.to_s)
98
+ conversation
99
+ end
100
+
101
+ def load_conversation(conversation_name)
102
+ return nil unless conversation_name
103
+
104
+ conversation_path = Dir.glob("#{conversations_dir}/#{conversation_name}*").first
105
+
106
+ return nil unless conversation_path
107
+ return nil unless File.exist?(conversation_path)
108
+
109
+ Conversation.new(conversation_path)
110
+ end
111
+
112
+ private
113
+
114
+ def find_path(this_path = nil)
115
+ self.class.find_path(this_path)
116
+ end
117
+
118
+ def conversations_dir
119
+ "#{bot_dir}/conversations"
120
+ end
121
+
122
+ def generate_conversation_file_name
123
+ total_files = Dir.glob("#{@conversations_dir}/*").count
124
+ "#{total_files + 1}.yml"
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,40 @@
1
+ require "thor"
2
+
3
+ module Jambots
4
+ class Cli < Thor
5
+ DEFAULT_BOT = "jambot"
6
+
7
+ desc "init", "Initialize a jambots path"
8
+ option :path, aliases: "-p", desc: "Initialize a jambots directory in specific path"
9
+ option :globally, type: :boolean, aliases: "-g", desc: "Creates the jambots directory in the user's root directory"
10
+ def init
11
+ init_controller = Controllers::InitController.new(options)
12
+ init_controller.init_jambots_path
13
+ end
14
+
15
+ desc "chat MESSAGE", "Start a chat with the bot and send a message"
16
+ option :bot, aliases: "-b", desc: "Name of the bot"
17
+ option :conversation, aliases: "-c", desc: "Name of the conversation key"
18
+ option :path, aliases: "-p", desc: "Path where the bot and the conversation directory are located"
19
+ option :last, type: :boolean, aliases: "-l", desc: "Continue with the last conversation created"
20
+ def chat(query)
21
+ chat_controller = Controllers::ChatController.new(options)
22
+ chat_controller.chat(query)
23
+ end
24
+
25
+ desc "new NAME", "Create a new bot with the specified name"
26
+ option :path, aliases: "-p", desc: "Directory where the bot will be created"
27
+ option :model, desc: "AI model to use"
28
+ option :prompt, desc: "Introduction text for the bot"
29
+ def new(name)
30
+ new_controller = Controllers::NewController.new(options)
31
+ new_controller.create_bot(name)
32
+ end
33
+
34
+ private
35
+
36
+ def renderer
37
+ @renderer ||= Renderer.new
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jambots::Controllers
4
+ class ChatController
5
+ DEFAULT_BOT = "jambot"
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ @renderer = Jambots::Renderer.new
10
+ end
11
+
12
+ def chat(query)
13
+ bot = Jambots::Bot.new(
14
+ @options[:bot] || DEFAULT_BOT,
15
+ path: Jambots::Bot.find_path(@options[:path])
16
+ )
17
+
18
+ last = @options[:last]
19
+ previous_conversation = last ? bot.conversations.last : bot.load_conversation(@options[:conversation])
20
+
21
+ conversation = previous_conversation || bot.new_conversation
22
+
23
+ @renderer.spinner.auto_spin
24
+ message = bot.message(query, conversation)
25
+ @renderer.spinner.success
26
+ @renderer.render(message, conversation)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ module Jambots
2
+ module Controllers
3
+ class InitController
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def init_jambots_path
9
+ path = if @options[:globally]
10
+ File.expand_path("~/.jambots")
11
+ elsif @options[:path]
12
+ File.expand_path(@options[:path])
13
+ else
14
+ "./.jambots"
15
+ end
16
+
17
+ FileUtils.mkdir_p(path)
18
+ puts "Jambots directory initialized at '#{path}'"
19
+
20
+ # Create default bot
21
+ Jambots::Controllers::NewController
22
+ .new(path: path)
23
+ .create_bot(Jambots::Controllers::ChatController::DEFAULT_BOT)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jambots
4
+ module Controllers
5
+ class NewController
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def create_bot(name)
11
+ path = Jambots::Bot.find_path(@options[:path])
12
+ model = @options[:model] || Jambots::Bot::DEFAULT_MODEL
13
+ prompt = @options[:prompt]
14
+
15
+ Jambots::Bot.create(name, path: path, model: model, prompt: prompt)
16
+ puts "Bot '#{name}' created in '#{path}/#{name}'"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jambots
4
+ class Conversation
5
+ attr_accessor :messages, :file_name, :file_path
6
+
7
+ def initialize(file_path)
8
+ @file_path = file_path
9
+ @file_name = File.basename(file_path)
10
+ @messages = load_messages
11
+ end
12
+
13
+ def add_message(role, content)
14
+ @messages << {role: role, content: content}
15
+ end
16
+
17
+ def save
18
+ messages_transformed = @messages.map { |message| message.transform_keys(&:to_s) }
19
+ File.write(@file_path, messages_transformed.to_yaml)
20
+ end
21
+
22
+ def load_messages
23
+ return [] unless File.exist?(@file_path)
24
+
25
+ file_content = File.read(@file_path)
26
+
27
+ YAML.safe_load(file_content, permitted_classes: [Symbol], symbolize_names: true)
28
+ end
29
+
30
+ def delete
31
+ File.delete(@file_path) if File.exist?(@file_path)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tty-spinner"
4
+ require "pastel"
5
+
6
+ module Jambots
7
+ class Renderer
8
+ def spinner
9
+ @spinner ||= TTY::Spinner.new(
10
+ "(🤖) [#{pastel.green(":spinner")}] ",
11
+ format: :pulse_2,
12
+ clear: true
13
+ )
14
+ end
15
+
16
+ def pastel
17
+ @pastel ||= Pastel.new
18
+ end
19
+
20
+ def render(message, conversation)
21
+ print_line(role_header(message[:role]))
22
+ puts pastel.magenta(message[:content])
23
+ file_name = conversation.file_name
24
+ conversation_name = File.basename(file_name, File.extname(file_name))
25
+ print_line("#{conversation_name} ")
26
+ end
27
+
28
+ private
29
+
30
+ def role_header(rol)
31
+ case rol.to_sym
32
+ when :system
33
+ "(🗄) "
34
+ when :assistant
35
+ "(🤖) "
36
+ when :user
37
+ "(👤) "
38
+ else
39
+ "(⁇) "
40
+ end
41
+ end
42
+
43
+ def print_line(text = "", max_length: 40, character: "─")
44
+ text_length = text.to_s.length
45
+ line = (text_length < max_length) ? character * (max_length - text_length) : ""
46
+
47
+ puts "#{text}#{line}"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jambots
4
+ VERSION = "0.1.2"
5
+ end
data/lib/jambots.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "jambots/version"
4
+ require_relative "jambots/bot"
5
+ require_relative "jambots/conversation"
6
+ require_relative "jambots/renderer"
7
+ require_relative "jambots/cli"
8
+ require_relative "jambots/controllers/init_controller"
9
+ require_relative "jambots/controllers/chat_controller"
10
+ require_relative "jambots/controllers/new_controller"
11
+
12
+ module Jambots
13
+ class Error < StandardError; end
14
+ # Your code goes here...
15
+ end
data/sig/jambots.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Jambots
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jambots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Juan Artero
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-openai
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-spinner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: pastel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.13.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.13.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: standard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.25.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.25.3
97
+ description: " Jambots is a command-line interface (CLI) tool for interacting with
98
+ chatbots powered by OpenAI's GPT. It allows you to create new chatbots, manage chatbot
99
+ conversations, and send messages to chatbots.\n"
100
+ email:
101
+ - juan.artero@marsbased.com
102
+ executables:
103
+ - bot_sample.yml
104
+ - jambots
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".rspec"
109
+ - ".standard.yml"
110
+ - CHANGELOG.md
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - exe/bot_sample.yml
118
+ - exe/jambots
119
+ - lib/jambots.rb
120
+ - lib/jambots/bot.rb
121
+ - lib/jambots/cli.rb
122
+ - lib/jambots/controllers/chat_controller.rb
123
+ - lib/jambots/controllers/init_controller.rb
124
+ - lib/jambots/controllers/new_controller.rb
125
+ - lib/jambots/conversation.rb
126
+ - lib/jambots/renderer.rb
127
+ - lib/jambots/version.rb
128
+ - sig/jambots.rbs
129
+ homepage: https://juanartero.com
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ homepage_uri: https://juanartero.com
134
+ source_code_uri: https://github.com/artero/jambots
135
+ changelog_uri: https://github.com/artero/jambots/blob/main/CHANGELOG.md
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 2.6.0
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubygems_version: 3.2.32
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Chat bots in your terminal
155
+ test_files: []