luogu 0.1.9 → 0.1.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b6f8d477025b929343fbd70605905c557d33550d3f0f3921d256b295f993a96
4
- data.tar.gz: 49aaf6803e6a8428bf143a743dd07ad840ddd0655a3228fad59543842af65466
3
+ metadata.gz: 6fe743a8d25cfb2bd189727b99b889fa41d3c4a770b75350dc7da72b11915198
4
+ data.tar.gz: 951e6cb7340553cdf51a9ef74a160774f354fb66f9eebeaded7e96c326118096
5
5
  SHA512:
6
- metadata.gz: 6de98f0d6be3266c9b4d873fb0e97bd1cf94f63231411961af39671a1be9b4ce0040d3efd1476fe37a309bee3722e1a7e724fdbd37a0dc949e4982fb8e8425d4
7
- data.tar.gz: 0cc7c4f3f9fa709c7fd1c7ca1a3b10ede905e5be36b90701edd0f100b404e64144deb20de6f081d3f6b89c757932f2932651fb0b6c6f60ec938419dfde2d5855
6
+ metadata.gz: 501920c17b82fc2de7678c16c9688f95a112d61d633025af0a9fcbbf9ab52dd47a71e1b666bee0e3d3c725c41d67f1262c5a2d8f2833e94c21e58b9d38956a15
7
+ data.tar.gz: afdbe6c180a29db054c2d818ab71bcf441cd2c41e65310d6df5d6c4575fb16a9d57e89c718f7b8c41a35ac5c7ac96bbb5a8fcd0e37f67092ba135243a08dcc9f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luogu (0.1.9)
4
+ luogu (0.1.11)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
6
  dry-cli (~> 1.0)
7
7
  ruby-openai (~> 3.7)
data/README.md CHANGED
@@ -62,9 +62,69 @@ end
62
62
  - exit 退出
63
63
 
64
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)
65
+ 在 run 和 test 中可以使用,可以使用 --plugin=<file>.plugin.rb
66
+ 默认情况下你可以使用 <文件名>.plugin.rb 来实现一个prompt.md的插件
67
+ 在插件中有两个对象
68
+
69
+ ```ruby
70
+ #gpt
71
+ gpt.row_history # 访问裸的历史记录 可读写
72
+ gpt.history # 处理过的历史记录 可读写
73
+ gpt.temperature # 设置请求的 temperature
74
+ gpt.model_name # 获取模型名称
75
+
76
+ #context
77
+ OpenStruct.new(
78
+ request_params: params, # 请求参数
79
+ user_input: user_message, # 用户输入
80
+ request_messages: messages, #请求时的messages
81
+ response_message: assistant_message, # 回复的message
82
+ response: response # 回复的response
83
+ )
84
+ # 如果需要在方法中使用使用变量传递,必须使用context包括你要的变量名,比如 context.name = "luogu"
85
+ ```
86
+
87
+ 支持的回调
88
+ ```ruby
89
+ # 所有方法都必须返回context
90
+ # 可以使用
91
+ # require 'irb'
92
+ # binding.irb
93
+ # 来调试断点
94
+
95
+ setup do |gpt, context|
96
+ puts gpt
97
+ context
98
+ end
99
+
100
+ before_input do |gpt, context|
101
+ puts "用户输入了: #{context.user_input}"
102
+ context
103
+ end
104
+
105
+ after_input do |gpt, context|
106
+ context
107
+ end
108
+
109
+ before_request do |gpt, context|
110
+ puts "request params: "
111
+ puts context
112
+ context
113
+ end
114
+
115
+ after_request do |gpt, context|
116
+ context
117
+ end
118
+
119
+ before_save_history do |gpt, context|
120
+ context
121
+ end
122
+
123
+ after_save_historydo |gpt, context|
124
+ context
125
+ end
126
+
127
+
128
+ ```
69
129
 
70
130
  ## MIT 协议
data/lib/luogu/chatgpt.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Luogu
2
2
  class ChatGPT
3
3
 
4
- attr_accessor :template, :limit_history, :prompt, :row_history, :history, :temperature, :model_name
4
+ attr_accessor :limit_history, :prompt, :row_history, :history, :temperature, :model_name, :context
5
5
 
6
6
  def initialize(file, history_path='.', plugin_file_path=nil)
7
7
  @plugin_file_path = plugin_file_path || file.sub(File.extname(file), ".plugin.rb")
@@ -23,6 +23,8 @@ module Luogu
23
23
  @row_history = []
24
24
  @history = HistoryQueue.new @limit_history
25
25
 
26
+ @context = OpenStruct.new
27
+
26
28
  if @plugin.setup_proc
27
29
  @plugin.setup_proc.call(self, OpenStruct.new)
28
30
  end
@@ -36,21 +38,25 @@ module Luogu
36
38
  }
37
39
 
38
40
  if @plugin.before_request_proc
39
- params = @plugin.before_request_proc.call(self, OpenStruct.new(request_params: params)).request_params
41
+ @context.request_params = params
42
+ params = @plugin.before_request_proc.call(self, @context).request_params
40
43
  end
41
44
  response = client.chat(parameters: params)
42
- @plugin.after_request_proc.call(self, OpenStruct.new(response: response)) if @plugin.after_request_proc
45
+ @context.response = response
46
+ @plugin.after_request_proc.call(self, @context) if @plugin.after_request_proc
43
47
 
44
48
  response.dig("choices", 0, "message", "content")
45
49
  end
46
50
 
47
51
  def chat(user_message)
48
52
  if @plugin.before_input_proc
49
- user_message = @plugin.before_input_proc.call(self, OpenStruct.new(user_input: user_message)).user_input
53
+ @context.user_input = user_message
54
+ user_message = @plugin.before_input_proc.call(self, @context).user_input
50
55
  end
51
56
  messages = (@prompt.render + @history.to_a) << {role: "user", content: user_message}
52
57
  if @plugin.after_input_proc
53
- messages = @plugin.after_input_proc.call(self, OpenStruct.new(request_messages: messages)).request_messages
58
+ @context.request_messages = messages
59
+ messages = @plugin.after_input_proc.call(self, @context).request_messages
54
60
  end
55
61
 
56
62
  assistant_message = self.request(messages)
@@ -61,7 +67,9 @@ module Luogu
61
67
  puts "执行文档中的callback"
62
68
  instance_eval @prompt.ruby_code, @prompt.file_path, @prompt.ruby_code_line
63
69
  elsif @plugin.before_save_history_proc
64
- @plugin.before_save_history_proc.call(self, OpenStruct.new(user_input: user_message, response_message: assistant_message))
70
+ @context.user_input = user_message
71
+ @context.response_message = assistant_message
72
+ @plugin.before_save_history_proc.call(self, @context)
65
73
  else
66
74
  puts "执行默认的历史记录"
67
75
  self.push_history(user_message, assistant_message)
@@ -79,7 +87,9 @@ module Luogu
79
87
  @history.enqueue({role: "user", content: user_message})
80
88
  @history.enqueue({role: "assistant", content: assistant_message})
81
89
  if @plugin.after_save_history_proc
82
- @plugin.after_save_history_proc.call(self, OpenStruct.new(user_input: user_message, response_message: assistant_message))
90
+ @context.user_input = user_message
91
+ @context.response_message = response_message
92
+ @plugin.after_save_history_proc.call(self, @context)
83
93
  end
84
94
  end
85
95
 
@@ -118,15 +128,21 @@ module Luogu
118
128
  puts "再见!"
119
129
  break
120
130
  else
121
- self.puts self.chat(input)
131
+ time = Benchmark.measure do
132
+ self.puts self.chat(input)
133
+ end
134
+ puts "input: #{input} 执行时间为 #{time.real} 秒"
122
135
  end
123
136
  end
124
137
  end
125
138
 
126
139
  def playload(messages)
127
140
  messages.each do |message|
128
- puts "test: #{message}"
129
- self.puts self.chat(message)
141
+ time = Benchmark.measure do
142
+ puts "test: #{message}"
143
+ self.puts self.chat(message)
144
+ end
145
+ puts "test: #{message} 执行时间为 #{time.real} 秒"
130
146
  end
131
147
  now = Time.now.to_i
132
148
  file_name = File.basename(@prompt_file, ".*")
data/lib/luogu/init.rb CHANGED
@@ -6,6 +6,7 @@ require 'yaml'
6
6
  require "dry/cli"
7
7
  require 'fileutils'
8
8
  require 'ostruct'
9
+ require 'benchmark'
9
10
 
10
11
  require_relative 'plugin'
11
12
  require_relative 'history_queue'
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.9"
4
+ VERSION = "0.1.11"
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.9
4
+ version: 0.1.11
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-11 00:00:00.000000000 Z
11
+ date: 2023-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai