mj 0.7.0 → 0.8.0

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: 59cf5e3a74d7c41f0b5b2c3789d85987b3393eccd110dc4741a3bdff59058827
4
- data.tar.gz: c0772655f097653bbbd719e1791791e97bd30349973febf410ae54be24f470ae
3
+ metadata.gz: 8b15f4c4efd81a03bf545e20a2bebb6beff7a12612c470173073f35c5b67cab2
4
+ data.tar.gz: 6c61d4acd6cc0d8590835bfd038fe0e291df05fd3100db7fff41f189eeade3fe
5
5
  SHA512:
6
- metadata.gz: 5f385ad6f1354b967cde4c98e63b96b1e918c406bd6009e676bac8d61f30f4df1ebffe73d3b34ac35c246f383338b0224cf7bf1d3d606176fe7f3ac95d40df74
7
- data.tar.gz: 4ce923b46dbf190b9b233512fae4fc9163a7853926e29aa4e7f9f3cfad1ddfd274be89f0c1052b4dcaa4bde42deced0cf9e7cf3ce425154153b2dc2212202c74
6
+ metadata.gz: 7d1017ad9f4b623c86d864e16c5110b9ecc1e131bbe96e27f23e0782b79ce209257972837a857a6557877a484dd0de834c2bf0c5be6c38d2ff5c67866243872c
7
+ data.tar.gz: 77da1cdfb825c43e50d8ae4cf7b203f585fc7c4be33c1121ad47e46c30befe773ec9cfe345a980112e02b625e0f3a82bd4158ef5d15438e8f46f46816b416529
data/Gemfile CHANGED
@@ -16,3 +16,4 @@ gem "rubocop-rake"
16
16
  gem "rubocop-rspec"
17
17
  gem "simplecov", require: false
18
18
  gem "simplecov-lcov", require: false
19
+ gem "uri", "~> 0.13"
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mj (0.7.0)
4
+ mj (0.8.0)
5
5
  koine-rest_client
6
+ mj-hash_utils
7
+ ruby-openai (~> 6.2.0)
6
8
  thor (~> 1.2.1)
7
9
 
8
10
  GEM
@@ -13,62 +15,89 @@ GEM
13
15
  ast (2.4.2)
14
16
  awesome_print (1.9.2)
15
17
  coderay (1.1.3)
16
- diff-lcs (1.5.0)
18
+ diff-lcs (1.5.1)
17
19
  docile (1.4.0)
20
+ event_stream_parser (0.3.0)
21
+ faraday (2.9.0)
22
+ faraday-net_http (>= 2.0, < 3.2)
23
+ faraday-multipart (1.0.4)
24
+ multipart-post (~> 2)
25
+ faraday-net_http (3.1.0)
26
+ net-http
18
27
  httparty (0.21.0)
19
28
  mini_mime (>= 1.0.0)
20
29
  multi_xml (>= 0.5.2)
30
+ json (2.7.1)
21
31
  koine-rest_client (1.1.1)
22
32
  addressable (>= 2.3)
23
33
  httparty (>= 0.17.0)
24
34
  koine-test_runner (0.4.0)
35
+ language_server-protocol (3.17.0.3)
25
36
  method_source (1.0.0)
26
37
  mini_mime (1.1.5)
38
+ mj-hash_utils (0.1.0)
27
39
  multi_xml (0.6.0)
28
- parallel (1.21.0)
29
- parser (3.1.1.0)
40
+ multipart-post (2.4.0)
41
+ net-http (0.4.1)
42
+ uri
43
+ parallel (1.24.0)
44
+ parser (3.3.0.5)
30
45
  ast (~> 2.4.1)
46
+ racc
31
47
  pry (0.14.2)
32
48
  coderay (~> 1.1)
33
49
  method_source (~> 1.0)
34
50
  public_suffix (5.0.4)
51
+ racc (1.7.3)
35
52
  rainbow (3.1.1)
36
- rake (13.0.6)
37
- regexp_parser (2.2.1)
38
- rexml (3.2.5)
39
- rspec (3.11.0)
40
- rspec-core (~> 3.11.0)
41
- rspec-expectations (~> 3.11.0)
42
- rspec-mocks (~> 3.11.0)
43
- rspec-core (3.11.0)
44
- rspec-support (~> 3.11.0)
45
- rspec-expectations (3.11.0)
53
+ rake (13.1.0)
54
+ regexp_parser (2.9.0)
55
+ rexml (3.2.6)
56
+ rspec (3.13.0)
57
+ rspec-core (~> 3.13.0)
58
+ rspec-expectations (~> 3.13.0)
59
+ rspec-mocks (~> 3.13.0)
60
+ rspec-core (3.13.0)
61
+ rspec-support (~> 3.13.0)
62
+ rspec-expectations (3.13.0)
46
63
  diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.11.0)
48
- rspec-mocks (3.11.0)
64
+ rspec-support (~> 3.13.0)
65
+ rspec-mocks (3.13.0)
49
66
  diff-lcs (>= 1.2.0, < 2.0)
50
- rspec-support (~> 3.11.0)
51
- rspec-support (3.11.0)
52
- rubocop (1.25.1)
67
+ rspec-support (~> 3.13.0)
68
+ rspec-support (3.13.1)
69
+ rubocop (1.62.0)
70
+ json (~> 2.3)
71
+ language_server-protocol (>= 3.17.0)
53
72
  parallel (~> 1.10)
54
- parser (>= 3.1.0.0)
73
+ parser (>= 3.3.0.2)
55
74
  rainbow (>= 2.2.2, < 4.0)
56
75
  regexp_parser (>= 1.8, < 3.0)
57
- rexml
58
- rubocop-ast (>= 1.15.1, < 2.0)
76
+ rexml (>= 3.2.5, < 4.0)
77
+ rubocop-ast (>= 1.31.1, < 2.0)
59
78
  ruby-progressbar (~> 1.7)
60
- unicode-display_width (>= 1.4.0, < 3.0)
61
- rubocop-ast (1.16.0)
62
- parser (>= 3.1.1.0)
63
- rubocop-performance (1.13.2)
64
- rubocop (>= 1.7.0, < 2.0)
65
- rubocop-ast (>= 0.4.0)
79
+ unicode-display_width (>= 2.4.0, < 3.0)
80
+ rubocop-ast (1.31.2)
81
+ parser (>= 3.3.0.4)
82
+ rubocop-capybara (2.20.0)
83
+ rubocop (~> 1.41)
84
+ rubocop-factory_bot (2.25.1)
85
+ rubocop (~> 1.41)
86
+ rubocop-performance (1.20.2)
87
+ rubocop (>= 1.48.1, < 2.0)
88
+ rubocop-ast (>= 1.30.0, < 2.0)
66
89
  rubocop-rake (0.6.0)
67
90
  rubocop (~> 1.0)
68
- rubocop-rspec (2.9.0)
69
- rubocop (~> 1.19)
70
- ruby-progressbar (1.11.0)
71
- simplecov (0.21.2)
91
+ rubocop-rspec (2.27.1)
92
+ rubocop (~> 1.40)
93
+ rubocop-capybara (~> 2.17)
94
+ rubocop-factory_bot (~> 2.22)
95
+ ruby-openai (6.2.0)
96
+ event_stream_parser (>= 0.3.0, < 1.0.0)
97
+ faraday (>= 1)
98
+ faraday-multipart (>= 1)
99
+ ruby-progressbar (1.13.0)
100
+ simplecov (0.22.0)
72
101
  docile (~> 1.1)
73
102
  simplecov-html (~> 0.11)
74
103
  simplecov_json_formatter (~> 0.1)
@@ -76,7 +105,8 @@ GEM
76
105
  simplecov-lcov (0.8.0)
77
106
  simplecov_json_formatter (0.1.4)
78
107
  thor (1.2.2)
79
- unicode-display_width (2.1.0)
108
+ unicode-display_width (2.5.0)
109
+ uri (0.13.0)
80
110
 
81
111
  PLATFORMS
82
112
  ruby
@@ -94,6 +124,7 @@ DEPENDENCIES
94
124
  rubocop-rspec
95
125
  simplecov
96
126
  simplecov-lcov
127
+ uri (~> 0.13)
97
128
 
98
129
  BUNDLED WITH
99
130
  2.3.16
data/README.md CHANGED
@@ -28,6 +28,61 @@ Or install it yourself as:
28
28
  mj help
29
29
  ```
30
30
 
31
+ ### ChatGPT
32
+
33
+ ```
34
+ bundle exec mj chatgpt ask "Who won the World Cup in 1994?" --request-file samples/chatgpt/football-team.yml | jq
35
+ ```
36
+
37
+ Where the config file looks like:
38
+
39
+ ```yaml
40
+ request:
41
+ parameters:
42
+ model: gpt-3.5-turbo-16k
43
+ temperature: 0.1
44
+ messages:
45
+ - role: system
46
+ content: You re an expert in futebool history.
47
+ ```
48
+
49
+ Response:
50
+
51
+ ```json
52
+ {
53
+ "id": "chatcmpl-90a0HhlOIqYkMPRbuICPy5m798yjQ",
54
+ "object": "chat.completion",
55
+ "created": 1709925425,
56
+ "model": "gpt-3.5-turbo-16k-0613",
57
+ "choices": [
58
+ {
59
+ "index": 0,
60
+ "message": {
61
+ "role": "assistant",
62
+ "content": "The World Cup in 1994 was won by the Brazilian national team. They defeated Italy in the final match, which was held at the Rose Bowl stadium in Pasadena, California, United States. The match ended in a 0-0 draw after extra time, and Brazil won the penalty shootout 3-2 to claim their fourth World Cup title."
63
+ },
64
+ "logprobs": null,
65
+ "finish_reason": "stop"
66
+ }
67
+ ],
68
+ "usage": {
69
+ "prompt_tokens": 31,
70
+ "completion_tokens": 71,
71
+ "total_tokens": 102
72
+ },
73
+ "system_fingerprint": null
74
+ }
75
+ ```
76
+
77
+ You can also do `jq '.choices[0].message.content'`:
78
+
79
+ ```sh
80
+ mj chatgpt ask "Who won the World Cup in 1994?" \
81
+ --config-file samples/chatgpt/football-team.yml | jq '.choices[0].message.content'
82
+
83
+ # "The World Cup in 1994 was won by the Brazilian national team. They defeated Italy in the final match, which was held at the Rose Bowl stadium in Pasadena, California, United States. The match ended in a 0-0 draw after extra time, and Brazil won the penalty shootout 3-2 to claim their fourth World Cup title."
84
+ ```
85
+
31
86
  ### GraphQl
32
87
 
33
88
  ```
@@ -40,8 +95,7 @@ Your graqphql fiel is a regular file with variables at the top:
40
95
  # gql-endpoint: http://api.myapi.me/graphql
41
96
  # gql-header: Authorization: Bearer cb923f33617877578961a19cf4566ec2
42
97
  # gql-header: Content-Type: application/json
43
- # gql-header: Cookie: ajs_user_id=ae3948ee-345e-47f2-8e2f-54277858e2c2; ajs_anonymous_id=30880a34-276e-4a86-a1d3-8869d1669199; _ga=GA1.1.649149230.1706868690; browser_id=14af87a406ec54cbe9804ea25c8f0a84; _ga_TZKSP5TS45=GS1.1.1708606615.26.1.1708606911.0.0.0
44
- # gql-variables: { "input": { "id": "yTXuIgoU88LKUNDLSVPFFjOSkgxlyyuSrNki3GY=" } }
98
+ # gql-variables: { "input": { "id": "some-id" } }
45
99
 
46
100
  query QueryAccountUsers(
47
101
  $input: AccountInput!
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module ChatGpt
5
+ module Commands
6
+ class AskCommand
7
+ attr_reader :question
8
+ attr_reader :options
9
+
10
+ def initialize(question, options: {})
11
+ @question = question
12
+ @options = options
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openai"
4
+ require_relative "../request"
5
+
6
+ module Mj
7
+ module ChatGpt
8
+ module Commands
9
+ class AskCommandHandler
10
+ def initialize(client:)
11
+ @client = client
12
+ end
13
+
14
+ def handle(command)
15
+ request = Request.from_config_file(command.options.fetch("config_file"))
16
+ @client.chat(**request.asking(command.question).to_h)
17
+ rescue Faraday::Error => exception
18
+ {
19
+ error: exception.message,
20
+ body: exception.response&.dig(:body, "error") || exception.response
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module ChatGpt
5
+ class Request
6
+ def initialize(data = {})
7
+ @data = HashUtils.new.deep_symbolize_keys(data)
8
+ end
9
+
10
+ def asking(question)
11
+ data = @data.dup
12
+ data[:parameters] ||= {}
13
+ data[:parameters][:messages] ||= []
14
+ data[:parameters][:messages] << {
15
+ role: :user,
16
+ content: question
17
+ }
18
+ self.class.new(data)
19
+ end
20
+
21
+ def to_h
22
+ @data
23
+ end
24
+
25
+ def self.from_config_file(file)
26
+ data = YAML.load_file(file)
27
+ new(data.fetch("request"))
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "commands/ask_command_handler"
4
+ require_relative "commands/ask_command"
5
+
6
+ module Mj
7
+ module ChatGpt
8
+ class ThorCommand < Thor
9
+ desc "ask <question>", "Ask ChatGPT a question"
10
+ option :config_file, type: :string, desc: "Config file"
11
+ def ask(question)
12
+ client_config = {
13
+ access_token: ENV.fetch("OPENAI_ACCESS_TOKEN")
14
+ }
15
+ client = OpenAI::Client.new(**client_config.compact)
16
+ handler = Commands::AskCommandHandler.new(client: client)
17
+ data = handler.handle(Commands::AskCommand.new(question, options: options))
18
+ $stdout.puts JSON.generate(data)
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/mj/cli.rb CHANGED
@@ -4,6 +4,7 @@ require "thor"
4
4
  require_relative "version"
5
5
  require_relative "alternative_file/thor_command"
6
6
  require_relative "graphql/thor_command"
7
+ require_relative "chatgpt/thor_command"
7
8
 
8
9
  module Mj
9
10
  class Cli < Thor
@@ -21,5 +22,8 @@ module Mj
21
22
 
22
23
  desc "graphql", "CLI client for GraphQL"
23
24
  subcommand "graphql", GraphQL::ThorCommand
25
+
26
+ desc "chatgpt", "CLI client for ChatGPT"
27
+ subcommand "chatgpt", ChatGpt::ThorCommand
24
28
  end
25
29
  end
data/lib/mj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mj
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.0"
5
5
  end
data/lib/mj.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "mj/version"
4
4
  require_relative "mj/cli"
5
+ require "mj/hash_utils"
5
6
 
6
7
  module Mj
7
8
  class Error < StandardError; end
data/mj.gemspec CHANGED
@@ -32,6 +32,8 @@ Gem::Specification.new do |spec|
32
32
  spec.require_paths = ["lib"]
33
33
 
34
34
  spec.add_dependency "koine-rest_client"
35
+ spec.add_dependency "mj-hash_utils"
36
+ spec.add_dependency "ruby-openai", "~> 6.2.0"
35
37
  spec.add_dependency "thor", "~> 1.2.1"
36
38
 
37
39
  # For more information and examples about making a new gem, check out our
@@ -0,0 +1,10 @@
1
+ # samples/chatgpt/football-team.yml
2
+ # usage: bundle exec mj chatgpt ask "Who won the World Cup in 1994?" --config-file samples/chatgpt/football-team.yml | jq
3
+ request:
4
+ parameters:
5
+ model: gpt-3.5-turbo-16k
6
+ temperature: 0.1
7
+ max_tokens: 8192
8
+ messages:
9
+ - role: system
10
+ content: You re an expert in futebool history.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-22 00:00:00.000000000 Z
11
+ date: 2024-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: koine-rest_client
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mj-hash_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-openai
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 6.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 6.2.0
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: thor
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +100,10 @@ files:
72
100
  - lib/mj/alternative_file/resolvers/ruby/vendored_gems_resolver.rb
73
101
  - lib/mj/alternative_file/resolvers/ruby/view_component_resolver.rb
74
102
  - lib/mj/alternative_file/thor_command.rb
103
+ - lib/mj/chatgpt/commands/ask_command.rb
104
+ - lib/mj/chatgpt/commands/ask_command_handler.rb
105
+ - lib/mj/chatgpt/request.rb
106
+ - lib/mj/chatgpt/thor_command.rb
75
107
  - lib/mj/cli.rb
76
108
  - lib/mj/graphql/client.rb
77
109
  - lib/mj/graphql/commands/query_file_command.rb
@@ -81,6 +113,7 @@ files:
81
113
  - lib/mj/graphql/thor_command.rb
82
114
  - lib/mj/version.rb
83
115
  - mj.gemspec
116
+ - samples/chatgpt/football-team.yml
84
117
  - sig/mj.rbs
85
118
  homepage: https://github.com/mjacobus/mj
86
119
  licenses: