luogu 0.1.6 → 0.1.7

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: 5dc8912fdf448f5a9cb2f27d32125bf5a82b5231a8c74f36c03c2269d3345a14
4
- data.tar.gz: 9039e6a2ebf32390d96625e734e63c49a9fda770945d8240a4a11dc3d8af114b
3
+ metadata.gz: fc5e0c2faef39054b2d1b084393c5af185fa52660a46b552af6fe26def302c8b
4
+ data.tar.gz: d20f25098e3fe03226375d3a0471acdbbd207523db7b101c431007b82cc49fa7
5
5
  SHA512:
6
- metadata.gz: bfc85f6ebf5dc6970243291b277654a86adc93d3a917e4c571f7e2a7b08bbc74fd7d20ee5fbacd4981ba8b983f9b1da05fe97a60da9b8a3b607a4fe5bf8b6b7b
7
- data.tar.gz: e5b8db6cb68a539ec09f706fed396dfafccc352817f1072616b66db77843d95fea9c67374fda515407c75cb71273523d0c8ede5793387a4663ab8c8ca9848480
6
+ metadata.gz: 5d493d41fd71f846549921c4ee5d2ac9aa5d398f8c07010dd0d0ca4ae5b33a9c30d2625325161cf0eb4427e9297b9b103783dadef2e08d321e63aa6b0f1255cb
7
+ data.tar.gz: 44c77c4a4a4be4f2eaaef0bada464279c26c3bb90c9a56017211ae1c6f656299d521328b4235a53dd28db0706c8e7fc6616b1cea1429f6e54442aab3b0e031e1
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luogu (0.1.1)
4
+ luogu (0.1.6)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
+ dry-cli (~> 1.0)
6
7
  ruby-openai (~> 3.7)
7
8
  tty-prompt (~> 0.23.1)
8
9
 
@@ -10,6 +11,7 @@ GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
12
13
  dotenv (2.8.1)
14
+ dry-cli (1.0.0)
13
15
  httparty (0.21.0)
14
16
  mini_mime (>= 1.0.0)
15
17
  multi_xml (>= 0.5.2)
data/README.md CHANGED
@@ -10,10 +10,14 @@
10
10
  - 如果需要在终端显示markdown,需要 [glow](https://github.com/charmbracelet/glow)
11
11
 
12
12
  ### 使用
13
- - luogu build <file> 编译成对应的json
14
- - luogu run <file> 测试prompt
15
- - luogu gen <prompt.json file> <target.md> 用来逆向生成md文件的命令
16
- - luogu test <file> <yml> 用来跑自动化测试 yaml 一行一句话
13
+ ```Bash
14
+ Commands:
15
+ luogu build PROMPT_FILE [TARGET_FILE] # 编译 Prompt.md 成能够提交给 ChatGPT API 的 messages. 默认输出为 <同文件名>.json
16
+ luogu generate JSON_FILE [PROMPT_FILE] # 根据 ChatGPT messages JSON 来生成 Prompt.md
17
+ luogu run PROMPT_FILE # 编译 Prompt.md 成能够提交给 ChatGPT API 的 messages. 默认输出为 <同文件名>.json
18
+ luogu test [PROMPT_FILE] [TEST_FILE] # 测试 Prompt 文件
19
+ luogu version # 打印版本
20
+ ```
17
21
 
18
22
  你可以在项目目录的.env中设置下面的环境变量,或者直接系统设置
19
23
  ```
data/lib/luogu/chatgpt.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  module Luogu
2
2
  class ChatGPT
3
- def initialize(file)
3
+ def initialize(file, history_path='.')
4
4
  @temperature = ENV.fetch('OPENAI_TEMPERATURE', '0.7').to_f
5
5
  @limit_history = ENV.fetch('OPENAI_LIMIT_HISTORY', '6').to_i * 2
6
6
 
7
+ @history_path = history_path
8
+ @prompt_file = file
9
+
7
10
  @prompt = PromptParser.new(file)
8
11
  @row_history = []
9
12
  @history = HistoryQueue.new @limit_history
@@ -70,8 +73,9 @@ module Luogu
70
73
  # 根据用户输入执行相应的操作
71
74
  case input
72
75
  when "save"
73
- self.class.save @row_history, "./prompt.row_history.md"
74
- self.class.save @history.to_a, "./prompt.history.md"
76
+ file_name = File.basename(@prompt_file, ".*")
77
+ self.class.save @row_history, File.join(@history_path, "#{file_name}.row_history.md")
78
+ self.class.save @history.to_a, File.join(@history_path, "#{file_name}.history.md")
75
79
  when "row history"
76
80
  p @row_history
77
81
  when "history"
@@ -91,8 +95,10 @@ module Luogu
91
95
  self.puts self.chat(message)
92
96
  end
93
97
  now = Time.now.to_i
94
- self.class.save @row_history, "./prompt.row_history.test-#{now}.md"
95
- self.class.save @history.to_a, "./prompt.history.test-#{now}.md"
98
+ file_name = File.basename(@prompt_file, ".*")
99
+
100
+ self.class.save @row_history, File.join(@history_path, "#{file_name}-#{now}.row_history.md")
101
+ self.class.save @history.to_a, File.join(@history_path, "#{file_name}-#{now}.history.md")
96
102
  end
97
103
 
98
104
  class << self
@@ -105,6 +111,7 @@ module Luogu
105
111
  text += item[:content]
106
112
  text += "\n\n"
107
113
  end
114
+ FileUtils.mkdir_p(File.dirname(file_path))
108
115
  File.open(file_path, 'w') do |f|
109
116
  f.write(text)
110
117
  end
data/lib/luogu/cli.rb CHANGED
@@ -1,58 +1,88 @@
1
1
  module Luogu
2
- module_function
3
- def cli
4
- options = {}
5
- subcommands = {}
6
-
7
- OptionParser.new do |opts|
8
- opts.banner = "Usage: luogu [command]"
9
-
10
- opts.on("-h", "--help", "Prints help") do
11
- puts """
12
- luogu build <file> -> 编译prompt
13
- luogu run <file> -> 测试 prompt
14
- luogu gen <file> <target> -> 根据 json 生成 prompt 文件
15
- luogu test <file> <test_file.yml> -> 根据 yaml 来对 prompt 进行测试
16
- """
17
- exit
2
+ module CLI
3
+ module Commands
4
+ extend Dry::CLI::Registry
5
+
6
+ class Version < Dry::CLI::Command
7
+ desc "打印版本"
8
+
9
+ def call(*)
10
+ puts Luogu::VERSION
11
+ end
18
12
  end
19
13
 
20
- end.parse!
14
+ class Build < Dry::CLI::Command
15
+
16
+ desc "编译 Prompt.md 成能够提交给 ChatGPT API 的 messages. 默认输出为 <同文件名>.json"
17
+ argument :prompt_file, type: :string, required: true, deec: "Prompt文件, 使用markdown书写"
18
+ argument :target_file, type: :string, required: false, deec: "输出 JSON 文件"
19
+
20
+ def call(prompt_file: nil, target_file: nil, **)
21
+ target_file ||= prompt_file.sub(File.extname(prompt_file), ".json")
22
+ data = PromptParser.new(prompt_file).to_json
23
+ File.open(target_file, 'w') do |f|
24
+ f.write(data)
25
+ end
26
+ end
21
27
 
22
-
23
- subcommands['build'] = Proc.new do |args|
24
- data = PromptParser.new(args.first).to_json
25
- target_path = args[1] || "./prompt.json"
26
- File.open(target_path, 'w') do |f|
27
- f.write(data)
28
28
  end
29
- end
30
29
 
31
- subcommands['run'] = Proc.new do |args|
32
- chatgpt = ChatGPT.new(args.first)
33
- chatgpt.run
34
- end
30
+ class Run < Dry::CLI::Command
35
31
 
36
- subcommands['gen'] = Proc.new do |args|
37
- json = JSON.parse File.read(args.first), symbolize_names: true
38
- chatgpt = ChatGPT.save(json, args.last)
39
- end
32
+ desc "编译 Prompt.md 成能够提交给 ChatGPT API 的 messages. 默认输出为 <同文件名>.json"
33
+ argument :prompt_file, type: :string, required: true, desc: "Prompt文件, 使用markdown书写"
34
+ option :out, type: :string, default: ".", desc: "保存历史时存放的目录,默认为当前目录"
40
35
 
41
- subcommands['test'] = Proc.new do |args|
42
- promtpt_file = args.first
43
- promtpt_test_file = args.last
36
+ def call(prompt_file: nil, **options)
37
+ chatgpt = ChatGPT.new(prompt_file, options.fetch(:out))
38
+ chatgpt.run
39
+ end
44
40
 
45
- chatgpt = ChatGPT.new(args.first)
41
+ end
46
42
 
47
- messages = YAML.load_file(promtpt_test_file)
48
- chatgpt.playload messages
49
- end
43
+ class Generate < Dry::CLI::Command
44
+
45
+ desc "根据 ChatGPT messages JSON 来生成 Prompt.md"
46
+ argument :json_file, type: :string, required: true, deec: "ChatGPT 生成的 messages json 文件"
47
+ argument :prompt_file, type: :string, required: false, deec: "要输出的Prompt文件路径, 默认生成 <同名>.md"
48
+
49
+ def call(json_file: nil, prompt_file: nil, **)
50
+ json = JSON.parse(File.read(json_file), symbolize_names: true)
51
+ prompt_file ||= json_file.sub(File.extname(json_file), ".md")
52
+
53
+ chatgpt = ChatGPT.save(json, prompt_file)
54
+ end
55
+
56
+ end
57
+
58
+ class Test < Dry::CLI::Command
59
+
60
+ desc "测试 Prompt 文件"
61
+ argument :prompt_file, type: :string, require: true, desc: "输出 Prompt 文件"
62
+ argument :test_file, type: :string, require: false, desc: "测试文件, 使用 YAML 文件, 一个字符串数组。默认为 同名.test.yml"
63
+ option :out, type: :string, default: ".", desc: "保存测试历史时存放的目录,默认为当前目录"
64
+
65
+ def call(prompt_file: nil, test_file:nil, **options)
66
+ test_file ||= prompt_file.sub(File.extname(prompt_file), ".test.yml")
67
+
68
+ chatgpt = ChatGPT.new(prompt_file, options.fetch(:out))
69
+ messages = YAML.load_file(test_file)
70
+ chatgpt.playload messages
71
+ end
72
+
73
+ end
74
+
75
+ register "version", Version, aliases: ["v", "-v", "--version"]
76
+ register "build", Build, aliases: ["b"]
77
+ register "run", Run, aliases: ["r"]
78
+ register "generate", Generate, aliases: ["g"]
79
+ register "test", Test, aliases: ["t"]
50
80
 
51
- if subcommands.key?(ARGV.first)
52
- subcommands[ARGV.first].call(ARGV[1..-1])
53
- else
54
- puts "Invalid command. Use -h or --help for usage information."
55
81
  end
82
+ end
56
83
 
84
+ module_function
85
+ def cli
86
+ Dry::CLI.new(Luogu::CLI::Commands).call
57
87
  end
58
- end
88
+ end
data/lib/luogu/init.rb CHANGED
@@ -3,7 +3,8 @@ require 'dotenv/load'
3
3
  require "tty-prompt"
4
4
  require 'json'
5
5
  require 'yaml'
6
- require 'optparse'
6
+ require "dry/cli"
7
+ require 'fileutils'
7
8
 
8
9
  require_relative 'history_queue'
9
10
  require_relative "prompt_parser"
data/lib/luogu/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Luogu
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
data/luogu.gemspec CHANGED
@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency 'ruby-openai', '~> 3.7'
36
36
  spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1'
37
37
  spec.add_dependency 'tty-prompt', '~> 0.23.1'
38
+ spec.add_dependency 'dry-cli', '~> 1.0'
38
39
 
39
40
  # For more information and examples about making a new gem, check out our
40
41
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luogu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - MJ
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: 0.23.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: dry-cli
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.0'
61
75
  description: 使用markdown来快速实现 Prompt工程研发
62
76
  email:
63
77
  - tywf91@gmail.com