luogu 0.1.8 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -0
- data/lib/luogu/chatgpt.rb +28 -8
- data/lib/luogu/init.rb +1 -0
- data/lib/luogu/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf66040bab414493d1be24b2ed42780d2cefae0c79814b48b363bcc2ed094415
|
4
|
+
data.tar.gz: 4e1c53cb018a14ed2ca607d364717fd5f84a8ff7a7246c641e79cd6f17806ae7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9670b5c6bd270e449b2916313474c4a6ac0eafff47b550a9853e8a6b0a032375eb7d74bc29d68c431fbb8ffe2ba946cc458316aa0046fde514e710b7dc642d06
|
7
|
+
data.tar.gz: 30556b308a94306863f8f2f7e6b1ebf9d53214a3832644cb013dc99fb332e781e519e2dd6f52a99d6da367b9f9973c1b7123b5a5e524aaa4d1d4eb5015f3d0e4
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -61,4 +61,11 @@ 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
|
+
- OpenStruct.new(response: response)
|
70
|
+
|
64
71
|
## 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 :template, :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,7 +23,11 @@ module Luogu
|
|
23
23
|
@row_history = []
|
24
24
|
@history = HistoryQueue.new @limit_history
|
25
25
|
|
26
|
-
@
|
26
|
+
@context = OpenStruct.new
|
27
|
+
|
28
|
+
if @plugin.setup_proc
|
29
|
+
@plugin.setup_proc.call(self, OpenStruct.new)
|
30
|
+
end
|
27
31
|
end
|
28
32
|
|
29
33
|
def request(messages)
|
@@ -33,17 +37,27 @@ module Luogu
|
|
33
37
|
temperature: @temperature,
|
34
38
|
}
|
35
39
|
|
36
|
-
|
40
|
+
if @plugin.before_request_proc
|
41
|
+
@context.request_params = params
|
42
|
+
params = @plugin.before_request_proc.call(self, @context).request_params
|
43
|
+
end
|
37
44
|
response = client.chat(parameters: params)
|
38
|
-
@
|
45
|
+
@context.response = response
|
46
|
+
@plugin.after_request_proc.call(self, @context) if @plugin.after_request_proc
|
39
47
|
|
40
48
|
response.dig("choices", 0, "message", "content")
|
41
49
|
end
|
42
50
|
|
43
51
|
def chat(user_message)
|
44
|
-
|
52
|
+
if @plugin.before_input_proc
|
53
|
+
@context.user_input = user_message
|
54
|
+
user_message = @plugin.before_input_proc.call(self, @context).user_input
|
55
|
+
end
|
45
56
|
messages = (@prompt.render + @history.to_a) << {role: "user", content: user_message}
|
46
|
-
|
57
|
+
if @plugin.after_input_proc
|
58
|
+
@context.request_messages = messages
|
59
|
+
messages = @plugin.after_input_proc.call(self, @context).request_messages
|
60
|
+
end
|
47
61
|
|
48
62
|
assistant_message = self.request(messages)
|
49
63
|
|
@@ -53,7 +67,9 @@ module Luogu
|
|
53
67
|
puts "执行文档中的callback"
|
54
68
|
instance_eval @prompt.ruby_code, @prompt.file_path, @prompt.ruby_code_line
|
55
69
|
elsif @plugin.before_save_history_proc
|
56
|
-
@
|
70
|
+
@context.user_input = user_message
|
71
|
+
@context.response_message = assistant_message
|
72
|
+
@plugin.before_save_history_proc.call(self, @context)
|
57
73
|
else
|
58
74
|
puts "执行默认的历史记录"
|
59
75
|
self.push_history(user_message, assistant_message)
|
@@ -70,7 +86,11 @@ module Luogu
|
|
70
86
|
def push_history(user_message, assistant_message)
|
71
87
|
@history.enqueue({role: "user", content: user_message})
|
72
88
|
@history.enqueue({role: "assistant", content: assistant_message})
|
73
|
-
|
89
|
+
if @plugin.after_save_history_proc
|
90
|
+
@context.user_input = user_message
|
91
|
+
@context.response_message = response_message
|
92
|
+
@plugin.after_save_history_proc.call(self, @context)
|
93
|
+
end
|
74
94
|
end
|
75
95
|
|
76
96
|
def ask(message)
|
data/lib/luogu/init.rb
CHANGED
data/lib/luogu/version.rb
CHANGED