luogu 0.1.7 → 0.1.9

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: fc5e0c2faef39054b2d1b084393c5af185fa52660a46b552af6fe26def302c8b
4
- data.tar.gz: d20f25098e3fe03226375d3a0471acdbbd207523db7b101c431007b82cc49fa7
3
+ metadata.gz: 8b6f8d477025b929343fbd70605905c557d33550d3f0f3921d256b295f993a96
4
+ data.tar.gz: 49aaf6803e6a8428bf143a743dd07ad840ddd0655a3228fad59543842af65466
5
5
  SHA512:
6
- metadata.gz: 5d493d41fd71f846549921c4ee5d2ac9aa5d398f8c07010dd0d0ca4ae5b33a9c30d2625325161cf0eb4427e9297b9b103783dadef2e08d321e63aa6b0f1255cb
7
- data.tar.gz: 44c77c4a4a4be4f2eaaef0bada464279c26c3bb90c9a56017211ae1c6f656299d521328b4235a53dd28db0706c8e7fc6616b1cea1429f6e54442aab3b0e031e1
6
+ metadata.gz: 6de98f0d6be3266c9b4d873fb0e97bd1cf94f63231411961af39671a1be9b4ce0040d3efd1476fe37a309bee3722e1a7e724fdbd37a0dc949e4982fb8e8425d4
7
+ data.tar.gz: 0cc7c4f3f9fa709c7fd1c7ca1a3b10ede905e5be36b90701edd0f100b404e64144deb20de6f081d3f6b89c757932f2932651fb0b6c6f60ec938419dfde2d5855
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luogu (0.1.6)
4
+ luogu (0.1.9)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
6
  dry-cli (~> 1.0)
7
7
  ruby-openai (~> 3.7)
data/README.md CHANGED
@@ -61,4 +61,10 @@ end
61
61
  - history 查看当前上下文
62
62
  - exit 退出
63
63
 
64
+ ## 插件模式
65
+ OpenStruct.new(request_params: params)
66
+ OpenStruct.new(user_input: user_message)
67
+ OpenStruct.new(request_messages: messages)
68
+ OpenStruct.new(user_input: user_message, response_message: assistant_message)
69
+
64
70
  ## MIT 协议
data/lib/luogu/chatgpt.rb CHANGED
@@ -1,8 +1,20 @@
1
1
  module Luogu
2
2
  class ChatGPT
3
- def initialize(file, history_path='.')
3
+
4
+ attr_accessor :template, :limit_history, :prompt, :row_history, :history, :temperature, :model_name
5
+
6
+ def initialize(file, history_path='.', plugin_file_path=nil)
7
+ @plugin_file_path = plugin_file_path || file.sub(File.extname(file), ".plugin.rb")
8
+
9
+ if File.exist?(@plugin_file_path)
10
+ @plugin = Plugin.new(@plugin_file_path).load()
11
+ else
12
+ @plugin = Plugin.new(@plugin_file_path)
13
+ end
14
+
4
15
  @temperature = ENV.fetch('OPENAI_TEMPERATURE', '0.7').to_f
5
16
  @limit_history = ENV.fetch('OPENAI_LIMIT_HISTORY', '6').to_i * 2
17
+ @model_name = "gpt-3.5-turbo"
6
18
 
7
19
  @history_path = history_path
8
20
  @prompt_file = file
@@ -10,20 +22,37 @@ module Luogu
10
22
  @prompt = PromptParser.new(file)
11
23
  @row_history = []
12
24
  @history = HistoryQueue.new @limit_history
25
+
26
+ if @plugin.setup_proc
27
+ @plugin.setup_proc.call(self, OpenStruct.new)
28
+ end
13
29
  end
14
30
 
15
31
  def request(messages)
16
- response = client.chat(
17
- parameters: {
18
- model: "gpt-3.5-turbo",
19
- messages: messages,
20
- temperature: 0.7,
21
- })
32
+ params = {
33
+ model: @model_name,
34
+ messages: messages,
35
+ temperature: @temperature,
36
+ }
37
+
38
+ if @plugin.before_request_proc
39
+ params = @plugin.before_request_proc.call(self, OpenStruct.new(request_params: params)).request_params
40
+ end
41
+ response = client.chat(parameters: params)
42
+ @plugin.after_request_proc.call(self, OpenStruct.new(response: response)) if @plugin.after_request_proc
43
+
22
44
  response.dig("choices", 0, "message", "content")
23
45
  end
24
46
 
25
47
  def chat(user_message)
48
+ if @plugin.before_input_proc
49
+ user_message = @plugin.before_input_proc.call(self, OpenStruct.new(user_input: user_message)).user_input
50
+ end
26
51
  messages = (@prompt.render + @history.to_a) << {role: "user", content: user_message}
52
+ if @plugin.after_input_proc
53
+ messages = @plugin.after_input_proc.call(self, OpenStruct.new(request_messages: messages)).request_messages
54
+ end
55
+
27
56
  assistant_message = self.request(messages)
28
57
 
29
58
  self.push_row_history(user_message, assistant_message)
@@ -31,6 +60,8 @@ module Luogu
31
60
  if @prompt.ruby_code
32
61
  puts "执行文档中的callback"
33
62
  instance_eval @prompt.ruby_code, @prompt.file_path, @prompt.ruby_code_line
63
+ elsif @plugin.before_save_history_proc
64
+ @plugin.before_save_history_proc.call(self, OpenStruct.new(user_input: user_message, response_message: assistant_message))
34
65
  else
35
66
  puts "执行默认的历史记录"
36
67
  self.push_history(user_message, assistant_message)
@@ -47,6 +78,9 @@ module Luogu
47
78
  def push_history(user_message, assistant_message)
48
79
  @history.enqueue({role: "user", content: user_message})
49
80
  @history.enqueue({role: "assistant", content: assistant_message})
81
+ if @plugin.after_save_history_proc
82
+ @plugin.after_save_history_proc.call(self, OpenStruct.new(user_input: user_message, response_message: assistant_message))
83
+ end
50
84
  end
51
85
 
52
86
  def ask(message)
data/lib/luogu/cli.rb CHANGED
@@ -32,9 +32,10 @@ module Luogu
32
32
  desc "编译 Prompt.md 成能够提交给 ChatGPT API 的 messages. 默认输出为 <同文件名>.json"
33
33
  argument :prompt_file, type: :string, required: true, desc: "Prompt文件, 使用markdown书写"
34
34
  option :out, type: :string, default: ".", desc: "保存历史时存放的目录,默认为当前目录"
35
+ option :plugin, type: :string, desc: "运行的时候载入对应的插件"
35
36
 
36
37
  def call(prompt_file: nil, **options)
37
- chatgpt = ChatGPT.new(prompt_file, options.fetch(:out))
38
+ chatgpt = ChatGPT.new(prompt_file, options.fetch(:out), options.fetch(:plugin, nil))
38
39
  chatgpt.run
39
40
  end
40
41
 
@@ -61,11 +62,12 @@ module Luogu
61
62
  argument :prompt_file, type: :string, require: true, desc: "输出 Prompt 文件"
62
63
  argument :test_file, type: :string, require: false, desc: "测试文件, 使用 YAML 文件, 一个字符串数组。默认为 同名.test.yml"
63
64
  option :out, type: :string, default: ".", desc: "保存测试历史时存放的目录,默认为当前目录"
65
+ option :plugin, type: :string, desc: "运行的时候载入对应的插件"
64
66
 
65
67
  def call(prompt_file: nil, test_file:nil, **options)
66
68
  test_file ||= prompt_file.sub(File.extname(prompt_file), ".test.yml")
67
69
 
68
- chatgpt = ChatGPT.new(prompt_file, options.fetch(:out))
70
+ chatgpt = ChatGPT.new(prompt_file, options.fetch(:out), options.fetch(:plugin, nil))
69
71
  messages = YAML.load_file(test_file)
70
72
  chatgpt.playload messages
71
73
  end
data/lib/luogu/init.rb CHANGED
@@ -5,7 +5,9 @@ require 'json'
5
5
  require 'yaml'
6
6
  require "dry/cli"
7
7
  require 'fileutils'
8
+ require 'ostruct'
8
9
 
10
+ require_relative 'plugin'
9
11
  require_relative 'history_queue'
10
12
  require_relative "prompt_parser"
11
13
  require_relative "chatgpt"
@@ -0,0 +1,52 @@
1
+ module Luogu
2
+ class Plugin
3
+ attr_reader :before_input_proc, :before_save_history_proc, :after_input_proc, :after_save_history_proc,
4
+ :setup_proc, :before_request_proc, :after_request_proc
5
+
6
+ def initialize(plugin_file_path)
7
+ @plugin_file_path = plugin_file_path
8
+
9
+ @before_input_proc = nil
10
+ @before_save_history_proc = nil
11
+
12
+ @after_input_proc = nil
13
+ @after_save_history_proc = nil
14
+
15
+ @setup_proc = nil
16
+ end
17
+
18
+ def before_input(&block)
19
+ @before_input_proc = block
20
+ end
21
+
22
+ def before_save_history(&block)
23
+ @before_save_history_proc = block
24
+ end
25
+
26
+ def after_input(&block)
27
+ @after_input_proc = block
28
+ end
29
+
30
+ def after_save_history(&block)
31
+ @after_save_history_proc = block
32
+ end
33
+
34
+ def setup(&block)
35
+ @setup_proc = block
36
+ end
37
+
38
+ def before_request(&block)
39
+ @before_request_proc = block
40
+ end
41
+
42
+ def after_request(&block)
43
+ @after_request_proc = block
44
+ end
45
+
46
+ def load()
47
+ self.instance_eval File.read(@plugin_file_path)
48
+ self
49
+ end
50
+
51
+ end
52
+ end
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.7"
4
+ VERSION = "0.1.9"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luogu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - MJ
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-09 00:00:00.000000000 Z
11
+ date: 2023-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -90,6 +90,7 @@ files:
90
90
  - lib/luogu/cli.rb
91
91
  - lib/luogu/history_queue.rb
92
92
  - lib/luogu/init.rb
93
+ - lib/luogu/plugin.rb
93
94
  - lib/luogu/prompt_parser.rb
94
95
  - lib/luogu/version.rb
95
96
  - luogu.gemspec