luogu 0.1.13 → 0.1.14

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: b16f3080badfbe3a83427b1e7e2c13f60fdeebaa758a3009beb6fb88d81995b1
4
- data.tar.gz: b270f3adb7cebb3f5391ef88aa1fd331a355465da5a861071a7b060f8c989955
3
+ metadata.gz: e5932a14c78478ce16a36f5cfd141834e9f677c90ddd7c398e8cdcbb66bd1ea4
4
+ data.tar.gz: a870afd3b6808cf89f01695a47b57c03ea906bf7b59e6b66bb28246a93215fac
5
5
  SHA512:
6
- metadata.gz: 850096cb88ea2764db10802b13047443aaa81f9e4343d671a02d386abcebcfab1581a1a90453566eef7bb3eae5a49198d966029d7fc59143b62d1bc4e96f8b58
7
- data.tar.gz: f9847779776839a7dff96e75cf7e7d932510c4700d01c8c77e8e91f1d3b4321afa01af3bac943aa7bf2f2580a05c77779818b0a5d9df965ed5280f1197ef9f64
6
+ metadata.gz: c5e59722ac54f8a247853bd67161297eae0976a6d402819ea4a774460f3ab4d2a5758c5511b9a4758747e5a1df93f760e7ad3f5084c395c5efe496f4d4abf17f
7
+ data.tar.gz: 5f69526dfc2f0b56f91daac355575703b0f6389ed889584663510598bf0f0fb5b6a53f95ccd7344656f621e0ec36b629c2d7c8009ffe9e07aa89b5a0b678a59d
data/Gemfile.lock CHANGED
@@ -1,27 +1,40 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luogu (0.1.11)
4
+ luogu (0.1.14)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
6
  dry-cli (~> 1.0)
7
- ruby-openai (~> 3.7)
7
+ http (~> 5.1, >= 5.1.1)
8
8
  tty-prompt (~> 0.23.1)
9
9
 
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
+ addressable (2.8.4)
14
+ public_suffix (>= 2.0.2, < 6.0)
15
+ domain_name (0.5.20190701)
16
+ unf (>= 0.0.5, < 1.0.0)
13
17
  dotenv (2.8.1)
14
18
  dry-cli (1.0.0)
15
- httparty (0.21.0)
16
- mini_mime (>= 1.0.0)
17
- multi_xml (>= 0.5.2)
18
- mini_mime (1.1.2)
19
- multi_xml (0.6.0)
19
+ ffi (1.15.5)
20
+ ffi-compiler (1.0.1)
21
+ ffi (>= 1.0.0)
22
+ rake
23
+ http (5.1.1)
24
+ addressable (~> 2.8)
25
+ http-cookie (~> 1.0)
26
+ http-form_data (~> 2.2)
27
+ llhttp-ffi (~> 0.4.0)
28
+ http-cookie (1.0.5)
29
+ domain_name (~> 0.5)
30
+ http-form_data (2.3.0)
31
+ llhttp-ffi (0.4.0)
32
+ ffi-compiler (~> 1.0)
33
+ rake (~> 13.0)
20
34
  pastel (0.8.0)
21
35
  tty-color (~> 0.5)
36
+ public_suffix (5.0.1)
22
37
  rake (13.0.6)
23
- ruby-openai (3.7.0)
24
- httparty (>= 0.18.1)
25
38
  tty-color (0.6.0)
26
39
  tty-cursor (0.7.1)
27
40
  tty-prompt (0.23.1)
@@ -32,6 +45,9 @@ GEM
32
45
  tty-screen (~> 0.8)
33
46
  wisper (~> 2.0)
34
47
  tty-screen (0.8.1)
48
+ unf (0.1.4)
49
+ unf_ext
50
+ unf_ext (0.0.8.2)
35
51
  wisper (2.0.1)
36
52
 
37
53
  PLATFORMS
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
 
4
4
  用来开发 prompt 工程的工具
5
5
 
6
+ ### 更新记录
7
+ - 0.1.14 讲http库替换成HTTP.rb并且加入了重试机制,默认为3次,可通过设置环境变量 OPENAI_REQUEST_RETRIES 来设置次数
8
+
6
9
  ### 安装
7
10
  - 安装ruby,要求2.6以上,Mac自带不需要再安装
8
11
  - gem install luogu
data/lib/luogu/chatgpt.rb CHANGED
@@ -44,8 +44,7 @@ module Luogu
44
44
  response = client.chat(parameters: params)
45
45
  @context.response = response
46
46
  @plugin.after_request_proc.call(self, @context) if @plugin.after_request_proc
47
-
48
- response.dig("choices", 0, "message", "content")
47
+ response.parse.dig("choices", 0, "message", "content")
49
48
  end
50
49
 
51
50
  def chat(user_message)
data/lib/luogu/init.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "openai"
1
+ require "http"
2
2
  require 'dotenv/load'
3
3
  require "tty-prompt"
4
4
  require 'json'
@@ -8,6 +8,7 @@ require 'fileutils'
8
8
  require 'ostruct'
9
9
  require 'benchmark'
10
10
 
11
+ require_relative 'openai'
11
12
  require_relative 'plugin'
12
13
  require_relative 'history_queue'
13
14
  require_relative "prompt_parser"
@@ -15,7 +16,7 @@ require_relative "chatgpt"
15
16
  require_relative "cli"
16
17
 
17
18
  def client
18
- $client ||= OpenAI::Client.new
19
+ $client ||= Luogu::OpenAI::Client.new
19
20
  end
20
21
 
21
22
  class String
@@ -0,0 +1,26 @@
1
+ module Luogu::OpenAI
2
+ class Client
3
+ def initialize
4
+ @access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
5
+ @client = HTTP.auth("Bearer #{@access_token}").persistent "https://api.openai.com"
6
+ end
7
+
8
+ def chat(parameters: _params, params: nil, retries: 3)
9
+ params ||= parameters
10
+ retries_left = ENV.fetch('OPENAI_REQUEST_RETRIES', retries)
11
+ begin
12
+ @client.post('/v1/chat/completions', json: params)
13
+ rescue HTTP::Error => e
14
+ if retries_left > 0
15
+ puts "retrying ..."
16
+ retries_left -= 1
17
+ sleep(1)
18
+ retry
19
+ else
20
+ puts "Connection error #{e}"
21
+ return nil
22
+ end
23
+ end
24
+ end
25
+ end
26
+ 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.13"
4
+ VERSION = "0.1.14"
5
5
  end
data/lib/luogu.rb CHANGED
@@ -9,9 +9,5 @@ module Luogu
9
9
 
10
10
  module_eval do
11
11
  Dotenv.load
12
-
13
- OpenAI.configure do |config|
14
- config.access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
15
- end
16
12
  end
17
13
  end
data/luogu.gemspec CHANGED
@@ -32,10 +32,10 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  # Uncomment to register a new dependency of your gem
34
34
  # spec.add_dependency "example-gem", "~> 1.0"
35
- spec.add_dependency 'ruby-openai', '~> 3.7'
36
35
  spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1'
37
36
  spec.add_dependency 'tty-prompt', '~> 0.23.1'
38
37
  spec.add_dependency 'dry-cli', '~> 1.0'
38
+ spec.add_dependency 'http', '~> 5.1', '>= 5.1.1'
39
39
 
40
40
  # For more information and examples about making a new gem, check out our
41
41
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luogu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
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-13 00:00:00.000000000 Z
11
+ date: 2023-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: ruby-openai
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3.7'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3.7'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: dotenv
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +58,26 @@ dependencies:
72
58
  - - "~>"
73
59
  - !ruby/object:Gem::Version
74
60
  version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: http
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.1'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 5.1.1
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '5.1'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 5.1.1
75
81
  description: 使用markdown来快速实现 Prompt工程研发
76
82
  email:
77
83
  - tywf91@gmail.com
@@ -90,6 +96,7 @@ files:
90
96
  - lib/luogu/cli.rb
91
97
  - lib/luogu/history_queue.rb
92
98
  - lib/luogu/init.rb
99
+ - lib/luogu/openai.rb
93
100
  - lib/luogu/plugin.rb
94
101
  - lib/luogu/prompt_parser.rb
95
102
  - lib/luogu/version.rb