gpt-function 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +3 -0
- data/.rubocop.yml +16 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +74 -0
- data/LICENSE.txt +21 -0
- data/Rakefile +12 -0
- data/gpt-function.gemspec +37 -0
- data/lib/gpt/function/version.rb +7 -0
- data/lib/gpt/function.rb +90 -0
- data/lib/gpt-function.rb +1 -0
- data/workflows/main.yml +27 -0
- metadata +12 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8048ade9bee9c6ba03f8eb97810a0ecee19b49f5cded5b0b157d556c20b570ec
|
4
|
+
data.tar.gz: 38952aeef662639339175aeaab6661010f0a168121c217b003d9aba6abb27450
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31cdb81fcce80026a3b2bc351f4c65a118f9377bb39c05d5c2b14b2ee8590ad95870fbe910d46bb9e04f0f2e4559e22e60e6d9afa1540d1443f3f67cfa40350d
|
7
|
+
data.tar.gz: 732de393247c3271a7fb9c51b1a6ef01685c1001e40c9604c8fcbcdb2e2ffb01746ec4d86e6f744aad3adf1f39d63ccecf71090d8a845269176a800e4a300cb8
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 30
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in gpt-function.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem "byebug", "~> 11.1"
|
15
|
+
|
16
|
+
gem "webmock", "~> 3.19"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gpt-function (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.8.5)
|
10
|
+
public_suffix (>= 2.0.2, < 6.0)
|
11
|
+
ast (2.4.2)
|
12
|
+
byebug (11.1.3)
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
diff-lcs (1.5.0)
|
16
|
+
hashdiff (1.0.1)
|
17
|
+
json (2.6.3)
|
18
|
+
language_server-protocol (3.17.0.3)
|
19
|
+
parallel (1.23.0)
|
20
|
+
parser (3.2.2.4)
|
21
|
+
ast (~> 2.4.1)
|
22
|
+
racc
|
23
|
+
public_suffix (5.0.4)
|
24
|
+
racc (1.7.3)
|
25
|
+
rainbow (3.1.1)
|
26
|
+
rake (13.1.0)
|
27
|
+
regexp_parser (2.8.2)
|
28
|
+
rexml (3.2.6)
|
29
|
+
rspec (3.12.0)
|
30
|
+
rspec-core (~> 3.12.0)
|
31
|
+
rspec-expectations (~> 3.12.0)
|
32
|
+
rspec-mocks (~> 3.12.0)
|
33
|
+
rspec-core (3.12.2)
|
34
|
+
rspec-support (~> 3.12.0)
|
35
|
+
rspec-expectations (3.12.3)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-mocks (3.12.6)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.12.0)
|
41
|
+
rspec-support (3.12.1)
|
42
|
+
rubocop (1.57.2)
|
43
|
+
json (~> 2.3)
|
44
|
+
language_server-protocol (>= 3.17.0)
|
45
|
+
parallel (~> 1.10)
|
46
|
+
parser (>= 3.2.2.4)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
regexp_parser (>= 1.8, < 3.0)
|
49
|
+
rexml (>= 3.2.5, < 4.0)
|
50
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
51
|
+
ruby-progressbar (~> 1.7)
|
52
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
53
|
+
rubocop-ast (1.30.0)
|
54
|
+
parser (>= 3.2.1.0)
|
55
|
+
ruby-progressbar (1.13.0)
|
56
|
+
unicode-display_width (2.5.0)
|
57
|
+
webmock (3.19.1)
|
58
|
+
addressable (>= 2.8.0)
|
59
|
+
crack (>= 0.3.2)
|
60
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
arm64-darwin-21
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
byebug (~> 11.1)
|
67
|
+
gpt-function!
|
68
|
+
rake (~> 13.0)
|
69
|
+
rspec (~> 3.0)
|
70
|
+
rubocop (~> 1.21)
|
71
|
+
webmock (~> 3.19)
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
2.4.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 etrex kuo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/gpt/function/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "gpt-function"
|
7
|
+
spec.version = Gpt::Function::VERSION
|
8
|
+
spec.authors = ["etrex kuo"]
|
9
|
+
spec.email = ["et284vu065k3@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A Ruby gem for creating simple GPT-based functions."
|
12
|
+
spec.description = "This gem allows users to create simple and complex GPT functions " \
|
13
|
+
"for various applications such as translation and keyword extraction."
|
14
|
+
spec.homepage = "https://github.com/etrex/gpt-function"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.required_ruby_version = ">= 2.6.0"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
data/lib/gpt/function.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "json"
|
5
|
+
require_relative "function/version"
|
6
|
+
|
7
|
+
module Gpt
|
8
|
+
# 這是一個簡單的 GPT 函數類別
|
9
|
+
class Function
|
10
|
+
@api_key = nil
|
11
|
+
@model = nil
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :api_key, :model
|
15
|
+
|
16
|
+
def configure(api_key:, model:)
|
17
|
+
@api_key = api_key
|
18
|
+
@model = model
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(prompt, examples = [], temperature = 0)
|
23
|
+
@temperature = temperature
|
24
|
+
@messages = [
|
25
|
+
{
|
26
|
+
role: "system",
|
27
|
+
content: "#{prompt}\n Note: The response format is always a JSON with the key output like this:{output: ...}"
|
28
|
+
},
|
29
|
+
*examples.flat_map do |example|
|
30
|
+
[
|
31
|
+
{
|
32
|
+
role: "user",
|
33
|
+
content: example[0]
|
34
|
+
},
|
35
|
+
{
|
36
|
+
role: "assistant",
|
37
|
+
content: { output: example[1] }.to_json
|
38
|
+
}
|
39
|
+
]
|
40
|
+
end
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
44
|
+
def call(input)
|
45
|
+
# 使用類別級別的變量來發送請求
|
46
|
+
response = send_request(input)
|
47
|
+
body = response.body.force_encoding("UTF-8")
|
48
|
+
json = JSON.parse(body)
|
49
|
+
# 處理可能的錯誤回應
|
50
|
+
raise StandardError, json.dig("error", "message") if json.dig("error", "code")
|
51
|
+
|
52
|
+
# 處理正常的回應
|
53
|
+
JSON.parse(json.dig("choices", 0, "message", "content"))["output"]
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def send_request(input)
|
59
|
+
uri = URI.parse("https://api.openai.com/v1/chat/completions")
|
60
|
+
request = Net::HTTP::Post.new(uri)
|
61
|
+
request.content_type = "application/json"
|
62
|
+
request["Authorization"] = "Bearer #{Function.api_key}"
|
63
|
+
request.body = {
|
64
|
+
model: Function.model,
|
65
|
+
response_format: {
|
66
|
+
type: "json_object"
|
67
|
+
},
|
68
|
+
seed: 0,
|
69
|
+
messages: [
|
70
|
+
*@messages,
|
71
|
+
{
|
72
|
+
"role": "user",
|
73
|
+
"content": input
|
74
|
+
}
|
75
|
+
],
|
76
|
+
temperature: @temperature
|
77
|
+
}.to_json
|
78
|
+
|
79
|
+
req_options = {
|
80
|
+
use_ssl: uri.scheme == "https",
|
81
|
+
open_timeout: 60, # opening a connection timeout
|
82
|
+
read_timeout: 300 # reading one block of response timeout
|
83
|
+
}
|
84
|
+
|
85
|
+
Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
86
|
+
http.request(request)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/gpt-function.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative "gpt/function"
|
data/workflows/main.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
build:
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
name: Ruby ${{ matrix.ruby }}
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby:
|
17
|
+
- '3.2.1'
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v3
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
25
|
+
bundler-cache: true
|
26
|
+
- name: Run the default task
|
27
|
+
run: bundle exec rake
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gpt-function
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- etrex kuo
|
@@ -18,7 +18,18 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
21
26
|
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- gpt-function.gemspec
|
29
|
+
- lib/gpt-function.rb
|
30
|
+
- lib/gpt/function.rb
|
31
|
+
- lib/gpt/function/version.rb
|
32
|
+
- workflows/main.yml
|
22
33
|
homepage: https://github.com/etrex/gpt-function
|
23
34
|
licenses:
|
24
35
|
- MIT
|