cohere-ai 1.0.0 → 1.0.1

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: 6f22e3efb3ba5824ea31a317019c161357645909902224eaba56fc14ae74a612
4
- data.tar.gz: 0bdabed7a0be20e8ab55fc5a5c931360b314734820a2606aab75f7e3596c6526
3
+ metadata.gz: a5326901fa2fcef2d71d74de376e3ca9df19d9adb65fe6cf5e1a6d0bd195b8d6
4
+ data.tar.gz: 78e162ac160c691b0060945511059a9ea185ee44105298bbf784f3a8e86fbf5a
5
5
  SHA512:
6
- metadata.gz: c68ceb466d71170a84644d68d7be0d5f40c3f9ba3cf7e45b891f2c99460967f808a85402e92a9ccb67195ac76ae2776621e8f66d9538568b4bafe128a8006ee5
7
- data.tar.gz: abccfbf233944338c738516610f398891ef76defb155142fa2e322ef22832b4817b786ba48e33f19d268ff2823822c191a04eaa21b26f60a710b3533430b9765
6
+ metadata.gz: 9cff2fb206e5daa899b440b27280c9b155b012364319da3536e28875a14cbebbb6c6f2a00e07409ec1edc501cd9c9a87154fbd31a28c084985bf8eccab627d28
7
+ data.tar.gz: 71453d884fd1bd3e76a9c78c5c9752dd6b695d887a1448aa12d3b0421a788f3da9306a6796beebbd0335537fa81a67ddb92dd68fd965e5e9b65d26169ff58640
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cohere-ai (1.0.0)
4
+ cohere-ai (1.0.1)
5
5
  faraday (~> 2.8, >= 2.8.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -9,7 +9,7 @@ A Ruby gem for interacting with [Cohere AI](https://cohere.com).
9
9
  ## TL;DR and Quick Start
10
10
 
11
11
  ```ruby
12
- gem 'cohere-ai', '~> 1.0.0'
12
+ gem 'cohere-ai', '~> 1.0.1'
13
13
  ```
14
14
 
15
15
  ```ruby
@@ -87,11 +87,11 @@ Result:
87
87
  ### Installing
88
88
 
89
89
  ```sh
90
- gem install cohere-ai -v 1.0.0
90
+ gem install cohere-ai -v 1.0.1
91
91
  ```
92
92
 
93
93
  ```sh
94
- gem 'cohere-ai', '~> 1.0.0'
94
+ gem 'cohere-ai', '~> 1.0.1'
95
95
  ```
96
96
 
97
97
  ### Credentials
@@ -694,6 +694,7 @@ CohereError
694
694
 
695
695
  MissingAPIKeyError
696
696
  BlockWithoutServerSentEventsError
697
+ IncompleteJSONReceivedError
697
698
 
698
699
  RequestError
699
700
  ```
@@ -716,7 +717,7 @@ gem build cohere-ai.gemspec
716
717
 
717
718
  gem signin
718
719
 
719
- gem push cohere-ai-1.0.0.gem
720
+ gem push cohere-ai-1.0.1.gem
720
721
  ```
721
722
 
722
723
  ### Updating the README
data/components/errors.rb CHANGED
@@ -10,6 +10,7 @@ module Cohere
10
10
 
11
11
  class MissingAPIKeyError < CohereError; end
12
12
  class BlockWithoutServerSentEventsError < CohereError; end
13
+ class IncompleteJSONReceivedError < CohereError; end
13
14
 
14
15
  class RequestError < CohereError
15
16
  attr_reader :request, :payload
@@ -88,6 +88,8 @@ module Cohere
88
88
 
89
89
  method_to_call = request_method.to_s.strip.downcase.to_sym
90
90
 
91
+ partial_json = ''
92
+
91
93
  response = Faraday.new(request: @request_options) do |faraday|
92
94
  faraday.response :raise_error
93
95
  end.send(method_to_call) do |request|
@@ -105,27 +107,43 @@ module Cohere
105
107
  raise_error.on_complete(env.merge(body: chunk))
106
108
  end
107
109
 
108
- result = { event: safe_parse_json(chunk), raw: { chunk:, bytes:, env: } }
110
+ partial_json += chunk
111
+
112
+ parsed_json = safe_parse_json(partial_json)
113
+
114
+ if parsed_json
115
+ result = { event: parsed_json, raw: { chunk:, bytes:, env: } }
109
116
 
110
- callback.call(result[:event], result[:raw]) unless callback.nil?
117
+ callback.call(result[:event], result[:raw]) unless callback.nil?
111
118
 
112
- results << result
119
+ results << result
120
+
121
+ partial_json = ''
122
+ end
113
123
  end
114
124
  end
115
125
  end
116
126
 
117
- return safe_parse_json(response.body) unless server_sent_events_enabled
127
+ return safe_parse_json_with_fallback_to_raw(response.body) unless server_sent_events_enabled
128
+
129
+ raise IncompleteJSONReceivedError, partial_json if partial_json != ''
118
130
 
119
131
  results.map { |result| result[:event] }
120
132
  rescue Faraday::ServerError => e
121
133
  raise RequestError.new(e.message, request: e, payload:)
122
134
  end
123
135
 
124
- def safe_parse_json(raw)
125
- raw.start_with?('{', '[') ? JSON.parse(raw) : raw
136
+ def safe_parse_json_with_fallback_to_raw(raw)
137
+ raw.to_s.lstrip.start_with?('{', '[') ? JSON.parse(raw) : raw
126
138
  rescue JSON::ParserError
127
139
  raw
128
140
  end
141
+
142
+ def safe_parse_json(raw)
143
+ raw.to_s.lstrip.start_with?('{', '[') ? JSON.parse(raw) : nil
144
+ rescue JSON::ParserError
145
+ nil
146
+ end
129
147
  end
130
148
  end
131
149
  end
data/static/gem.rb CHANGED
@@ -3,10 +3,10 @@
3
3
  module Cohere
4
4
  GEM = {
5
5
  name: 'cohere-ai',
6
- version: '1.0.0',
6
+ version: '1.0.1',
7
7
  author: 'gbaptista',
8
8
  summary: 'Interact with Cohere AI.',
9
- description: "A Ruby gem for interacting with Cohere AI platform.",
9
+ description: 'A Ruby gem for interacting with Cohere AI platform.',
10
10
  github: 'https://github.com/gbaptista/cohere-ai',
11
11
  gem_server: 'https://rubygems.org',
12
12
  license: 'MIT',
data/template.md CHANGED
@@ -9,7 +9,7 @@ A Ruby gem for interacting with [Cohere AI](https://cohere.com).
9
9
  ## TL;DR and Quick Start
10
10
 
11
11
  ```ruby
12
- gem 'cohere-ai', '~> 1.0.0'
12
+ gem 'cohere-ai', '~> 1.0.1'
13
13
  ```
14
14
 
15
15
  ```ruby
@@ -50,11 +50,11 @@ Result:
50
50
  ### Installing
51
51
 
52
52
  ```sh
53
- gem install cohere-ai -v 1.0.0
53
+ gem install cohere-ai -v 1.0.1
54
54
  ```
55
55
 
56
56
  ```sh
57
- gem 'cohere-ai', '~> 1.0.0'
57
+ gem 'cohere-ai', '~> 1.0.1'
58
58
  ```
59
59
 
60
60
  ### Credentials
@@ -657,6 +657,7 @@ CohereError
657
657
 
658
658
  MissingAPIKeyError
659
659
  BlockWithoutServerSentEventsError
660
+ IncompleteJSONReceivedError
660
661
 
661
662
  RequestError
662
663
  ```
@@ -679,7 +680,7 @@ gem build cohere-ai.gemspec
679
680
 
680
681
  gem signin
681
682
 
682
- gem push cohere-ai-1.0.0.gem
683
+ gem push cohere-ai-1.0.1.gem
683
684
  ```
684
685
 
685
686
  ### Updating the README
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cohere-ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gbaptista
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 3.4.22
77
+ rubygems_version: 3.3.3
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Interact with Cohere AI.