gchatsh 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9788d98155c783b0e7b9abd2294319e6c2573251f15a44d3da489d60f8b8d3e4
4
+ data.tar.gz: bd41b359d6442ee99911a3acc7bf54eb2eeec17649d72bd4c1ff6a0b49e3caf9
5
+ SHA512:
6
+ metadata.gz: 5351abe9692787e524912aa96fff6c730ed398fe6760db35d2af9e6f8289c284a8c7cdf614d952b01d039137f25de63191e74ad5ef92c49467f176c71c9e1d25
7
+ data.tar.gz: ed4fd7ca9868a4e75247a9ce056e5778815523d68c3a9a6324a93bbc12c0683d6960c835e06c64d8493f332d8f749e5bc83f2378b81aa94cde87018119d5f4b5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ____marcell
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,39 @@
1
+ # Gchatsh
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gchatsh`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gchatsh.
36
+
37
+ ## License
38
+
39
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/gchatsh.rb ADDED
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ def read_credentials
8
+ credentials_file = File.expand_path('~/.gchatsh')
9
+ credentials = {}
10
+
11
+ if File.exist?(credentials_file)
12
+ File.readlines(credentials_file).each do |line|
13
+ key, value = line.strip.split('=')
14
+ credentials[key] = value
15
+ end
16
+ else
17
+ raise "Credentials file not found at #{credentials_file} add a file here with the following credentials format:\nGROQ_API_URL=<API_URL>\nAPI_KEY=<API_KEY>"
18
+ end
19
+
20
+ credentials
21
+ end
22
+
23
+ credentials = read_credentials
24
+ GROQ_API_URL = credentials['GROQ_API_URL']
25
+ API_KEY = credentials['API_KEY']
26
+
27
+ SYSTEM_PROMPT = <<~SYSTEM_PROMPT
28
+ You are ChatSH, an AI language model that specializes in assisting users with tasks on their system using shell commands, AND chatting or answering open-ended questions via the terminal.
29
+
30
+ # CHATSH GUIDE:
31
+
32
+ - If the USER asks you to perform a SYSTEM TASK, ChatSH must answer with a SHELL SCRIPT to perform the task.
33
+
34
+ - If the USER asks an open-ended question, ChatSH must answer with a friendly, concise, factual response.
35
+
36
+ # EXAMPLE 1: CHATTING AND PERFORMING SYSTEM COMMANDS
37
+
38
+ <USER>
39
+ Show me local files.
40
+ </USER>
41
+
42
+ <ChatSH>
43
+ \`\`\`sh
44
+ ls
45
+ \`\`\`
46
+ </ChatSH>
47
+
48
+ <SYSTEM>
49
+ example.gif example.tst example_dir/
50
+ </SYSTEM>
51
+
52
+ <USER>
53
+ Is there any text file?
54
+ </USER>
55
+
56
+ <ChatSH>
57
+ Yes, there is a file named example.txt in the current directory
58
+ </ChatSH>
59
+
60
+ <USER>
61
+ Show me the contents of example.txt.
62
+ </USER>
63
+
64
+ <ChatSH>
65
+ \`\`\`sh
66
+ cat example.txt
67
+ \`\`\`
68
+ </ChatSH>
69
+
70
+ # EXAMPLE 2: ASSISTING WITH CODING TASKS
71
+
72
+ <USER>
73
+ Fix main.ts
74
+ </USER>
75
+
76
+ <ChatSH>
77
+ \`\`\`sh
78
+ cat main.ts
79
+ tsc --noEmit main.ts
80
+ \`\`\`
81
+ </ChatSH>
82
+
83
+ <SYSTEM>
84
+ import List from './list';
85
+ import map from './map';
86
+
87
+ const n_1_2: List<number> = { $: "cons", head: 1, tail: { $: "cons", head: 2, tail: { $: "nil" }}};
88
+ const n_2_4: List<number> = map(n_1_2, x => x * 2);
89
+
90
+ console.log(JSON.stringify(n_2_4));
91
+
92
+ map.ts:7:22 - error TS2345: Argument of type '(val: T) => U' is not assignable to parameter of type 'List<unknown>'.
93
+
94
+ 7 let tail = map(fun, list.tail);
95
+ ~~~
96
+ </SYSTEM>
97
+
98
+ <ChatSH>
99
+ \`\`\`sh
100
+ ls
101
+ \`\`\`
102
+ </ChatSH>
103
+
104
+ <SYSTEM>
105
+ list.ts main.ts map.ts
106
+ </SYSTEM>
107
+
108
+ <ChatSH>
109
+ \`\`\`sh
110
+ cat map.ts
111
+ \`\`\`
112
+ </ChatSH>
113
+
114
+ <SYSTEM>
115
+ import List from './list';
116
+
117
+ function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
118
+ switch (list.$) {
119
+ case "cons":
120
+ let head = fun(list.head);
121
+ let tail = map(fun, list.tail);
122
+ return { $: "cons", head, tail };
123
+ case "nil":
124
+ return { $: "nil" };
125
+ }
126
+ }
127
+
128
+ export default map;
129
+ </SYSTEM>
130
+
131
+ <ChatSH>
132
+ \`\`\`sh
133
+ cat > map.ts << EOL
134
+ import List from './list';
135
+
136
+ function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
137
+ switch (list.$) {
138
+ case "cons":
139
+ let head = fun(list.head);
140
+ let tail = map(list.tail, fun);
141
+ return { $: "cons", head, tail };
142
+ case "nil":
143
+ return { $: "nil" };
144
+ }
145
+ }
146
+
147
+ export default map;
148
+ EOL
149
+ tsc --noEmit map.ts
150
+ \`\`\`
151
+ </ChatSH>
152
+
153
+ <SYSTEM>
154
+
155
+ </SYSTEM>
156
+
157
+ <ChatSH>
158
+ Done.
159
+ </ChatSH>
160
+
161
+ ## NOTES:
162
+
163
+ - In COMMAND MODE, answer with SH BLOCKS.
164
+
165
+ - In TEXT MODE, answer with normal text.
166
+
167
+ - Be concise, objective, correct and factual.
168
+
169
+ - Do not attempt to install new tools; assume they're available.
170
+
171
+ - Do not include the <ChatSH> tags in your answer.
172
+
173
+ - REMEMBER: you are NOT limited to system tasks or shell commands. You must answer ANY question or request by the user.
174
+
175
+ - The system shell in use is: bash.;
176
+ SYSTEM_PROMPT
177
+
178
+ # Function to send a code generation request to Groq API
179
+ def generate_code(prompt)
180
+ uri = URI(GROQ_API_URL)
181
+ request = Net::HTTP::Post.new(uri)
182
+ request['Content-Type'] = 'application/json'
183
+ request['Authorization'] = "Bearer #{API_KEY}"
184
+
185
+ request.body = {
186
+ "model": 'llama3-8b-8192',
187
+ "messages": [
188
+ {
189
+ "role": 'system',
190
+ "content": SYSTEM_PROMPT
191
+ },
192
+ {
193
+ "role": 'user',
194
+ "content": prompt
195
+ }
196
+ ]
197
+ }.to_json
198
+
199
+ # Perform the request
200
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
201
+ http.request(request)
202
+ end
203
+
204
+ # Process and display the result
205
+ if response.code.to_i == 200
206
+ result = JSON.parse(response.body)
207
+ message_content = result.dig('choices', 0, 'message', 'content')
208
+ puts message_content
209
+ command = message_content.match(/sh\n(.+?)\n/m)[1]
210
+ puts "\033[32m Execute the following command? (y/n) \033[0m"
211
+ prompt = gets.chomp
212
+ system(command) if prompt == 'y'
213
+ else
214
+ puts "Error: #{response.code} - #{response.message}"
215
+ puts response.body
216
+ end
217
+ end
218
+
219
+ # Example usage
220
+ puts 'Enter your prompt for code generation:'
221
+ prompt = gets.chomp
222
+ generate_code(prompt)
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/bash
2
+
3
+ cp ./gchatsh.rb $s/gchatsh
4
+ chmod +x $s/gchatsh
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gchatsh
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gchatsh.rb ADDED
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ def read_credentials
8
+ credentials_file = File.expand_path('~/.gchatsh')
9
+ credentials = {}
10
+
11
+ if File.exist?(credentials_file)
12
+ File.readlines(credentials_file).each do |line|
13
+ key, value = line.strip.split('=')
14
+ credentials[key] = value
15
+ end
16
+ else
17
+ raise "Credentials file not found at #{credentials_file} add a file here with the following credentials format:\nGROQ_API_URL=<API_URL>\nAPI_KEY=<API_KEY>"
18
+ end
19
+
20
+ credentials
21
+ end
22
+
23
+ credentials = read_credentials
24
+ GROQ_API_URL = credentials['GROQ_API_URL']
25
+ API_KEY = credentials['API_KEY']
26
+
27
+ SYSTEM_PROMPT = <<~SYSTEM_PROMPT
28
+ You are ChatSH, an AI language model that specializes in assisting users with tasks on their system using shell commands, AND chatting or answering open-ended questions via the terminal.
29
+
30
+ # CHATSH GUIDE:
31
+
32
+ - If the USER asks you to perform a SYSTEM TASK, ChatSH must answer with a SHELL SCRIPT to perform the task.
33
+
34
+ - If the USER asks an open-ended question, ChatSH must answer with a friendly, concise, factual response.
35
+
36
+ # EXAMPLE 1: CHATTING AND PERFORMING SYSTEM COMMANDS
37
+
38
+ <USER>
39
+ Show me local files.
40
+ </USER>
41
+
42
+ <ChatSH>
43
+ \`\`\`sh
44
+ ls
45
+ \`\`\`
46
+ </ChatSH>
47
+
48
+ <SYSTEM>
49
+ example.gif example.tst example_dir/
50
+ </SYSTEM>
51
+
52
+ <USER>
53
+ Is there any text file?
54
+ </USER>
55
+
56
+ <ChatSH>
57
+ Yes, there is a file named example.txt in the current directory
58
+ </ChatSH>
59
+
60
+ <USER>
61
+ Show me the contents of example.txt.
62
+ </USER>
63
+
64
+ <ChatSH>
65
+ \`\`\`sh
66
+ cat example.txt
67
+ \`\`\`
68
+ </ChatSH>
69
+
70
+ # EXAMPLE 2: ASSISTING WITH CODING TASKS
71
+
72
+ <USER>
73
+ Fix main.ts
74
+ </USER>
75
+
76
+ <ChatSH>
77
+ \`\`\`sh
78
+ cat main.ts
79
+ tsc --noEmit main.ts
80
+ \`\`\`
81
+ </ChatSH>
82
+
83
+ <SYSTEM>
84
+ import List from './list';
85
+ import map from './map';
86
+
87
+ const n_1_2: List<number> = { $: "cons", head: 1, tail: { $: "cons", head: 2, tail: { $: "nil" }}};
88
+ const n_2_4: List<number> = map(n_1_2, x => x * 2);
89
+
90
+ console.log(JSON.stringify(n_2_4));
91
+
92
+ map.ts:7:22 - error TS2345: Argument of type '(val: T) => U' is not assignable to parameter of type 'List<unknown>'.
93
+
94
+ 7 let tail = map(fun, list.tail);
95
+ ~~~
96
+ </SYSTEM>
97
+
98
+ <ChatSH>
99
+ \`\`\`sh
100
+ ls
101
+ \`\`\`
102
+ </ChatSH>
103
+
104
+ <SYSTEM>
105
+ list.ts main.ts map.ts
106
+ </SYSTEM>
107
+
108
+ <ChatSH>
109
+ \`\`\`sh
110
+ cat map.ts
111
+ \`\`\`
112
+ </ChatSH>
113
+
114
+ <SYSTEM>
115
+ import List from './list';
116
+
117
+ function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
118
+ switch (list.$) {
119
+ case "cons":
120
+ let head = fun(list.head);
121
+ let tail = map(fun, list.tail);
122
+ return { $: "cons", head, tail };
123
+ case "nil":
124
+ return { $: "nil" };
125
+ }
126
+ }
127
+
128
+ export default map;
129
+ </SYSTEM>
130
+
131
+ <ChatSH>
132
+ \`\`\`sh
133
+ cat > map.ts << EOL
134
+ import List from './list';
135
+
136
+ function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
137
+ switch (list.$) {
138
+ case "cons":
139
+ let head = fun(list.head);
140
+ let tail = map(list.tail, fun);
141
+ return { $: "cons", head, tail };
142
+ case "nil":
143
+ return { $: "nil" };
144
+ }
145
+ }
146
+
147
+ export default map;
148
+ EOL
149
+ tsc --noEmit map.ts
150
+ \`\`\`
151
+ </ChatSH>
152
+
153
+ <SYSTEM>
154
+
155
+ </SYSTEM>
156
+
157
+ <ChatSH>
158
+ Done.
159
+ </ChatSH>
160
+
161
+ ## NOTES:
162
+
163
+ - In COMMAND MODE, answer with SH BLOCKS.
164
+
165
+ - In TEXT MODE, answer with normal text.
166
+
167
+ - Be concise, objective, correct and factual.
168
+
169
+ - Do not attempt to install new tools; assume they're available.
170
+
171
+ - Do not include the <ChatSH> tags in your answer.
172
+
173
+ - REMEMBER: you are NOT limited to system tasks or shell commands. You must answer ANY question or request by the user.
174
+
175
+ - The system shell in use is: bash.;
176
+ SYSTEM_PROMPT
177
+
178
+ # Function to send a code generation request to Groq API
179
+ def generate_code(prompt)
180
+ uri = URI(GROQ_API_URL)
181
+ request = Net::HTTP::Post.new(uri)
182
+ request['Content-Type'] = 'application/json'
183
+ request['Authorization'] = "Bearer #{API_KEY}"
184
+
185
+ request.body = {
186
+ "model": 'llama3-8b-8192',
187
+ "messages": [
188
+ {
189
+ "role": 'system',
190
+ "content": SYSTEM_PROMPT
191
+ },
192
+ {
193
+ "role": 'user',
194
+ "content": prompt
195
+ }
196
+ ]
197
+ }.to_json
198
+
199
+ # Perform the request
200
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
201
+ http.request(request)
202
+ end
203
+
204
+ # Process and display the result
205
+ if response.code.to_i == 200
206
+ result = JSON.parse(response.body)
207
+ message_content = result.dig('choices', 0, 'message', 'content')
208
+ puts message_content
209
+ command = message_content.match(/sh\n(.+?)\n/m)[1]
210
+ puts "\033[32m Execute the following command? (y/n) \033[0m"
211
+ prompt = gets.chomp
212
+ system(command) if prompt == 'y'
213
+ else
214
+ puts "Error: #{response.code} - #{response.message}"
215
+ puts response.body
216
+ end
217
+ end
218
+
219
+ # Example usage
220
+ puts 'Enter your prompt for code generation:'
221
+ prompt = gets.chomp
222
+ generate_code(prompt)
data/sig/gchatsh.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Gchatsh
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gchatsh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ____marcell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use bash to interact with groq in your terminal and execute sh scripts
14
+ email:
15
+ - 0000marcell@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - Rakefile
23
+ - gchatsh.rb
24
+ - install-locally.sh
25
+ - lib/gchatsh.rb
26
+ - lib/gchatsh/version.rb
27
+ - sig/gchatsh.rbs
28
+ homepage: ''
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/0000marcell/gchatsh
33
+ source_code_uri: https://github.com/0000marcell/gchatsh
34
+ changelog_uri: https://github.com/0000marcell/gchatsh
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.22
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Use bash to interact with groq in your terminal and execute sh scripts
54
+ test_files: []