openai_ruby 0.2.1 → 0.3.2

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: 9b5c8133ce4d085c9c54fc61ce8d9dea0432f9bdae4b8ac0edd4d6fb4dc00e12
4
- data.tar.gz: 8a228ca58c6e52ca44896e647312dd0dcf3de7a35aea6cbf2fafe42ce1121bc7
3
+ metadata.gz: 973ae690504a531c6b3b95415d622346a7fedca7e1fe7d2b509fe689fa733f7a
4
+ data.tar.gz: ec6b40954dccffbed531aad98b79458061eb1cba7e88b370d30a639c891352c2
5
5
  SHA512:
6
- metadata.gz: 214ffe83d487d1121ed9055cf0ffd8b62eddd68fc963f2fa613f932a0e2c04b47b4bf8bedbf1047b0b6c453845f0b1229102eb61252e07ce52a14295c376cf07
7
- data.tar.gz: c845dd68da6d20878b439c6451d59ee681eb53ad3a2b5ef8a421c500c7532fb7c42552eeb872e333358d3d7b36eedf5961e6acb8cbf65121b003e39c5d65fa7d
6
+ metadata.gz: 379578031e02141ee8ce4b7949cd08c84a98806cfdeb277a12e1a46f4a0394d59a402007b566ab4247bafda0daa646869866b6fc1bd29acebf338d312bbce76d
7
+ data.tar.gz: e8a1fc7ee1822493e11b1e1b4eb318491af75252b2873040379f102ce997961d0afa23e1a9abff40459d89a7af6a81c29418f22487287c379bcf23aae8b83d3a
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # OpenAI Ruby library
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/openai_ruby.svg)](https://badge.fury.io/rb/openai_ruby)
4
+ [![Build Status](https://travis-ci.org/renny-ren/openai_ruby.svg?branch=main)](https://travis-ci.org/renny-ren/openai_ruby)
5
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/renny-ren/openai_ruby/blob/main/LICENSE)
4
6
 
5
- This is a Ruby wrapper for [OpenAI API](https://openai.com/api/), which provides convenient access to the OpenAI API from applications written in Ruby, helps us to build next-gen apps with OpenAI’s powerful models.
7
+ This is a Ruby wrapper for [OpenAI API](https://openai.com/api/), which provides convenient access to the OpenAI API from applications written in Ruby.
8
+
9
+ Let's build next-gen apps with OpenAI’s powerful models.
6
10
 
7
11
  ## Installation
8
12
 
@@ -18,24 +22,90 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
22
 
19
23
  ## Usage
20
24
 
25
+ Before starting, you need to have an API key, if not, create one [here](https://platform.openai.com/account/api-keys)
26
+
27
+ create a client like this:
28
+
29
+ ```ruby
30
+ client = OpenAI::Client.new("your OpenAI key here")
31
+ ```
32
+
33
+ ### Completion
34
+ https://platform.openai.com/docs/api-reference/completions
35
+
36
+ ```ruby
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)
46
+ response = JSON.parse(res.body)
47
+
48
+ p response.dig("choices", 0, "text") # "I am an AI created by OpenAI."
49
+ p response.dig('usage', 'total_tokens') # 18
50
+ ```
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
+
21
57
  ```ruby
22
- client = OpenAI::Client.new("your openai key here")
23
- client.create_completion(
24
- model: "text-davinci-003",
25
- prompt: "What is the date today?",
26
- max_tokens: 100
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
+
77
+ ### Edit
78
+ https://platform.openai.com/docs/api-reference/edits
79
+
80
+ ```ruby
81
+ res = client.create_edit(
82
+ model: "text-davinci-edit-001",
83
+ input: "What are the date today?",
84
+ instruction: "Fix the grammer."
27
85
  )
86
+ response = JSON.parse(res.body)
87
+ p response.dig("choices", 0, "text") # "What is the date today?\n"
88
+ ```
89
+
90
+ ### Image
91
+
92
+ ```ruby
93
+
28
94
  ```
29
95
 
30
96
  ## Development
31
97
 
32
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+ After checking out the repo, run `bin/setup` to install dependencies.
99
+
100
+ You can run `rake spec` to run the tests.
101
+
102
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
103
 
34
104
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
105
 
36
106
  ## Contributing
37
107
 
38
- Bug reports and pull requests are welcome on GitHub at https://github.com/renny-ren/openai-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/renny-ren/openai-ruby/blob/main/CODE_OF_CONDUCT.md).
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/renny-ren/openai_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/renny-ren/openai_ruby/blob/main/CODE_OF_CONDUCT.md).
39
109
 
40
110
  ## License
41
111
 
@@ -43,4 +113,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
43
113
 
44
114
  ## Code of Conduct
45
115
 
46
- Everyone interacting in the Openai::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/renny-ren/openai-ruby/blob/main/CODE_OF_CONDUCT.md).
116
+ Everyone interacting in the Openai::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/renny-ren/openai_ruby/blob/main/CODE_OF_CONDUCT.md).
@@ -10,7 +10,7 @@ module OpenAI
10
10
  @api_key = api_key
11
11
  end
12
12
 
13
- def create_completion(params)
13
+ def create_completion(params = {})
14
14
  Faraday.post(
15
15
  "#{BASE_URL}/v1/completions",
16
16
  params.to_json,
@@ -18,8 +18,37 @@ 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
+
38
+ def create_edit(params = {})
39
+ Faraday.post(
40
+ "#{BASE_URL}/v1/edits",
41
+ params.to_json,
42
+ headers
43
+ )
44
+ end
45
+
21
46
  private
22
47
 
48
+ def connection
49
+ Faraday.new(url: BASE_URL, headers: headers)
50
+ end
51
+
23
52
  def headers
24
53
  {
25
54
  "Content-Type" => "application/json",
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenAI
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.2"
5
5
  end
data/lib/openai_ruby.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "faraday"
3
4
  require "openai_ruby/client"
4
5
  require "openai_ruby/version"
5
6
 
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.2.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
@@ -32,12 +32,10 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - ".rspec"
35
- - ".rspec_status"
36
35
  - ".rubocop.yml"
37
36
  - CHANGELOG.md
38
37
  - CODE_OF_CONDUCT.md
39
38
  - Gemfile
40
- - Gemfile.lock
41
39
  - LICENSE
42
40
  - README.md
43
41
  - Rakefile
data/.rspec_status DELETED
@@ -1,3 +0,0 @@
1
- example_id | status | run_time |
2
- ------------------------------------ | ------ | --------------- |
3
- ./spec/openai_ruby/ruby_spec.rb[1:1] | passed | 0.00026 seconds |
data/Gemfile.lock DELETED
@@ -1,63 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- oepnai_ruby (0.2.1)
5
- faraday (~> 2.7)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- ast (2.4.2)
11
- diff-lcs (1.5.0)
12
- faraday (2.7.4)
13
- faraday-net_http (>= 2.0, < 3.1)
14
- ruby2_keywords (>= 0.0.4)
15
- faraday-net_http (3.0.2)
16
- json (2.6.3)
17
- parallel (1.22.1)
18
- parser (3.2.1.0)
19
- ast (~> 2.4.1)
20
- rainbow (3.1.1)
21
- rake (13.0.6)
22
- regexp_parser (2.7.0)
23
- rexml (3.2.5)
24
- rspec (3.12.0)
25
- rspec-core (~> 3.12.0)
26
- rspec-expectations (~> 3.12.0)
27
- rspec-mocks (~> 3.12.0)
28
- rspec-core (3.12.1)
29
- rspec-support (~> 3.12.0)
30
- rspec-expectations (3.12.2)
31
- diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.12.0)
33
- rspec-mocks (3.12.3)
34
- diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.12.0)
36
- rspec-support (3.12.0)
37
- rubocop (1.45.1)
38
- json (~> 2.3)
39
- parallel (~> 1.10)
40
- parser (>= 3.2.0.0)
41
- rainbow (>= 2.2.2, < 4.0)
42
- regexp_parser (>= 1.8, < 3.0)
43
- rexml (>= 3.2.5, < 4.0)
44
- rubocop-ast (>= 1.24.1, < 2.0)
45
- ruby-progressbar (~> 1.7)
46
- unicode-display_width (>= 2.4.0, < 3.0)
47
- rubocop-ast (1.26.0)
48
- parser (>= 3.2.1.0)
49
- ruby-progressbar (1.11.0)
50
- ruby2_keywords (0.0.5)
51
- unicode-display_width (2.4.2)
52
-
53
- PLATFORMS
54
- arm64-darwin-22
55
-
56
- DEPENDENCIES
57
- oepnai_ruby!
58
- rake (~> 13.0)
59
- rspec (~> 3.12)
60
- rubocop (~> 1.45)
61
-
62
- BUNDLED WITH
63
- 2.3.16