openai_ruby 0.3.1 → 0.3.2

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: a4c166c6a904ae2e24c863aa5fc906f92ae6be0a01b239524c5f6ceb9f1db125
4
- data.tar.gz: ce98d485c2a40eda5dd2139721bd4507e7eb9b7e947757cfe790e6061c4534a6
3
+ metadata.gz: 973ae690504a531c6b3b95415d622346a7fedca7e1fe7d2b509fe689fa733f7a
4
+ data.tar.gz: ec6b40954dccffbed531aad98b79458061eb1cba7e88b370d30a639c891352c2
5
5
  SHA512:
6
- metadata.gz: e3e938b3e14930e9c12f5b4aa68e1f56e528e298a48089a6a0e96a2ae0dddff6ce6a54b66cfeec0f09512dc7d62a65117f889606bfe9570d133c629e022853c0
7
- data.tar.gz: acad979520949c2d763f3834f97411b44a0532795451af51f63e72737957cae79bc219b421ed41187f3352232600914cf52e9e79fbc7f1dc1bc9234a741ce276
6
+ metadata.gz: 379578031e02141ee8ce4b7949cd08c84a98806cfdeb277a12e1a46f4a0394d59a402007b566ab4247bafda0daa646869866b6fc1bd29acebf338d312bbce76d
7
+ data.tar.gz: e8a1fc7ee1822493e11b1e1b4eb318491af75252b2873040379f102ce997961d0afa23e1a9abff40459d89a7af6a81c29418f22487287c379bcf23aae8b83d3a
data/README.md CHANGED
@@ -30,27 +30,52 @@ create a client like this:
30
30
  client = OpenAI::Client.new("your OpenAI key here")
31
31
  ```
32
32
 
33
- ### Completion
33
+ ### Completion
34
+ https://platform.openai.com/docs/api-reference/completions
34
35
 
35
36
  ```ruby
36
- res = client.create_completion(
37
- model: "text-davinci-003", # The model which will generate the completion
38
- prompt: "Hello, who are you?",
39
- max_tokens: 100 # The maximum number of tokens to generate
40
- temperature: 0.5, # Control randomness
41
- top_p: 1,
42
- frequency_penalty: 0,
43
- presence_penalty: 0,
44
- stop: "\n"
45
- )
46
- p res.status # 200
37
+ # params = {
38
+ # model: "text-davinci-003",
39
+ # prompt: "Hello, who are you?",
40
+ # max_tokens: 100,
41
+ # temperature: 0.5,
42
+ # frequency_penalty: 0,
43
+ # presence_penalty: 0,
44
+ # }
45
+ res = client.create_completion(params)
47
46
  response = JSON.parse(res.body)
48
47
 
49
48
  p response.dig("choices", 0, "text") # "I am an AI created by OpenAI."
50
49
  p response.dig('usage', 'total_tokens') # 18
51
50
  ```
52
51
 
52
+ ### Chat Completion
53
+ https://platform.openai.com/docs/api-reference/chat/create
54
+
55
+ If you set `stream` param to `true`, a block will be called with the chunk data:
56
+
57
+ ```ruby
58
+ # params = {
59
+ # model: "gpt-3.5-turbo",
60
+ # prompt: "Hello",
61
+ # max_tokens: 100,
62
+ # temperature: 1,
63
+ # stream: true,
64
+ # }
65
+ res = client.create_chat_completion(params) do |chunk, overall_received_bytes, env|
66
+ data = chunk[/data: (.*)\n\n$/, 1]
67
+ p data # {"id":"chatcmpl-6xcOWMQcilJUwJiosi7Rht6Fvuu3D","object":"chat.completion.chunk","created":1679666960,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Hello"},"index":0,"finish_reason":null}]}
68
+ if data == "[DONE]"
69
+ # the stream is end
70
+ else
71
+ response = JSON.parse(data)
72
+ p response.dig("choices", 0, "text") # "Hello"
73
+ end
74
+ end
75
+ ```
76
+
53
77
  ### Edit
78
+ https://platform.openai.com/docs/api-reference/edits
54
79
 
55
80
  ```ruby
56
81
  res = client.create_edit(
@@ -18,6 +18,23 @@ module OpenAI
18
18
  )
19
19
  end
20
20
 
21
+ def create_chat_completion(params = {}, &block)
22
+ if params[:stream]
23
+ connection.post("/v1/chat/completions") do |req|
24
+ req.body = params.to_json
25
+ req.options.on_data = Proc.new do |chunk, overall_received_bytes, env|
26
+ block.call(chunk, overall_received_bytes, env)
27
+ end
28
+ end
29
+ else
30
+ connection.post(
31
+ "/v1/chat/completions",
32
+ params.to_json,
33
+ headers
34
+ )
35
+ end
36
+ end
37
+
21
38
  def create_edit(params = {})
22
39
  Faraday.post(
23
40
  "#{BASE_URL}/v1/edits",
@@ -28,6 +45,10 @@ module OpenAI
28
45
 
29
46
  private
30
47
 
48
+ def connection
49
+ Faraday.new(url: BASE_URL, headers: headers)
50
+ end
51
+
31
52
  def headers
32
53
  {
33
54
  "Content-Type" => "application/json",
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenAI
4
- VERSION = "0.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openai_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renny Ren
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-15 00:00:00.000000000 Z
11
+ date: 2023-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -42,7 +42,6 @@ files:
42
42
  - lib/openai_ruby.rb
43
43
  - lib/openai_ruby/client.rb
44
44
  - lib/openai_ruby/version.rb
45
- - openai_ruby.gemspec
46
45
  - sig/openai_ruby/ruby.rbs
47
46
  homepage: https://github.com/renny-ren/openai_ruby
48
47
  licenses:
data/openai_ruby.gemspec DELETED
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/openai_ruby/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "openai_ruby"
7
- spec.version = OpenAI::VERSION
8
- spec.authors = ["Renny Ren"]
9
- spec.email = ["rennyrjh@gmail.com"]
10
-
11
- spec.summary = "A Ruby wrapper for OpenAI API"
12
- spec.homepage = "https://github.com/renny-ren/openai_ruby"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/renny-ren/openai_ruby"
18
- spec.metadata["changelog_uri"] = "https://github.com/renny-ren/openai_ruby/blob/main/CHANGELOG.md"
19
-
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(__dir__) do
23
- `git ls-files -z`.split("\x0").reject do |f|
24
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
- end
26
- end
27
-
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- spec.add_dependency "faraday", "~> 2.7"
33
-
34
- # For more information and examples about making a new gem, check out our
35
- # guide at: https://bundler.io/guides/creating_gem.html
36
- end