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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d415e44469be65939709764b50b5283da141d5e68663acf5e3c376044407970
4
- data.tar.gz: 18846bbcd392e9581f6d9becff711b2e551983759460464b169824e14f3c05a1
3
+ metadata.gz: bf66040bab414493d1be24b2ed42780d2cefae0c79814b48b363bcc2ed094415
4
+ data.tar.gz: 4e1c53cb018a14ed2ca607d364717fd5f84a8ff7a7246c641e79cd6f17806ae7
5
5
  SHA512:
6
- metadata.gz: 5eb13f0a6b90843366879a773f816d55dbcdba55f2cc6f06915f9e7f7dbc4a73ceb1916505d482a670755d8197519792d649f0731c12f53071fa87b131bcd62a
7
- data.tar.gz: b25c5d0dfa8a2b4d4df66ec9695d226521551ddb77f674722d122ba8e9252546abcfa916d2d379a794f520fedf32247e1dcbeef575739675bdd84ddd705e78f5
6
+ metadata.gz: 9670b5c6bd270e449b2916313474c4a6ac0eafff47b550a9853e8a6b0a032375eb7d74bc29d68c431fbb8ffe2ba946cc458316aa0046fde514e710b7dc642d06
7
+ data.tar.gz: 30556b308a94306863f8f2f7e6b1ebf9d53214a3832644cb013dc99fb332e781e519e2dd6f52a99d6da367b9f9973c1b7123b5a5e524aaa4d1d4eb5015f3d0e4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luogu (0.1.7)
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,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
- @plugin.setup_proc.call(self) if @plugin.setup_proc
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
- params = @plugin.before_request_proc.call(self, params) if @plugin.before_request_proc
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
- @plugin.after_request_proc.call(self, response) if @plugin.after_request_proc
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
- user_message = @plugin.before_input_proc.call(self, user_message) if @plugin.before_input_proc
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
- @plugin.after_input_proc.call(self, messages) if @plugin.after_input_proc
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
- @plugin.before_save_history_proc.call(self, user_message, assistant_message)
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
- @plugin.after_save_history_proc.call(self, user_message, assistant_message) if @plugin.after_save_history_proc
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
@@ -5,6 +5,7 @@ require 'json'
5
5
  require 'yaml'
6
6
  require "dry/cli"
7
7
  require 'fileutils'
8
+ require 'ostruct'
8
9
 
9
10
  require_relative 'plugin'
10
11
  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.8"
4
+ VERSION = "0.1.10"
5
5
  end
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.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - MJ