cohere-ai 1.0.0 → 1.0.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: 6f22e3efb3ba5824ea31a317019c161357645909902224eaba56fc14ae74a612
4
- data.tar.gz: 0bdabed7a0be20e8ab55fc5a5c931360b314734820a2606aab75f7e3596c6526
3
+ metadata.gz: 28430515ffef2963f9fa1ae10b23bb3563bdea2e8c8ec7fdb7c711b783249312
4
+ data.tar.gz: eb0127adde3a17d90f30bed12a4b61cd27b99b5a4a956329429020d07a3cd658
5
5
  SHA512:
6
- metadata.gz: c68ceb466d71170a84644d68d7be0d5f40c3f9ba3cf7e45b891f2c99460967f808a85402e92a9ccb67195ac76ae2776621e8f66d9538568b4bafe128a8006ee5
7
- data.tar.gz: abccfbf233944338c738516610f398891ef76defb155142fa2e322ef22832b4817b786ba48e33f19d268ff2823822c191a04eaa21b26f60a710b3533430b9765
6
+ metadata.gz: a02810e106da158ebaf4b0cbd2aef712d3fc8c29ecb9472a8346cd7e0e905c928f28d698137a0adc57956dfcb0eb7d512767a61c764e7fbee5e82e7f982ab719
7
+ data.tar.gz: e1ed417932a6c1be52d1ee733f685d3e676e0d5d6d0c0c2ccfa25ebb0cf576897124be3413650ff4185c62672a461112f285ac35c17fd3595db8c20ef9eed576
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  *.gem
2
2
  .devcontainer
3
+ .env
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  gemspec
6
6
 
7
7
  group :test, :development do
8
+ gem 'dotenv', '~> 2.8', '>= 2.8.1'
8
9
  gem 'pry-byebug', '~> 3.10', '>= 3.10.1'
9
10
  gem 'rubocop', '~> 1.58'
10
11
  end
data/Gemfile.lock CHANGED
@@ -1,26 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cohere-ai (1.0.0)
5
- faraday (~> 2.8, >= 2.8.1)
4
+ cohere-ai (1.0.2)
5
+ faraday (~> 2.9)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
- base64 (0.2.0)
12
11
  byebug (11.1.3)
13
12
  coderay (1.1.3)
14
- faraday (2.8.1)
15
- base64
16
- faraday-net_http (>= 2.0, < 3.1)
17
- ruby2_keywords (>= 0.0.4)
18
- faraday-net_http (3.0.2)
13
+ dotenv (2.8.1)
14
+ faraday (2.9.0)
15
+ faraday-net_http (>= 2.0, < 3.2)
16
+ faraday-net_http (3.1.0)
17
+ net-http
19
18
  json (2.7.1)
20
19
  language_server-protocol (3.17.0.3)
21
20
  method_source (1.0.0)
21
+ net-http (0.4.1)
22
+ uri
22
23
  parallel (1.24.0)
23
- parser (3.2.2.4)
24
+ parser (3.3.0.3)
24
25
  ast (~> 2.4.1)
25
26
  racc
26
27
  pry (0.14.2)
@@ -31,7 +32,7 @@ GEM
31
32
  pry (>= 0.13, < 0.15)
32
33
  racc (1.7.3)
33
34
  rainbow (3.1.1)
34
- regexp_parser (2.8.3)
35
+ regexp_parser (2.9.0)
35
36
  rexml (3.2.6)
36
37
  rubocop (1.59.0)
37
38
  json (~> 2.3)
@@ -47,14 +48,15 @@ GEM
47
48
  rubocop-ast (1.30.0)
48
49
  parser (>= 3.2.1.0)
49
50
  ruby-progressbar (1.13.0)
50
- ruby2_keywords (0.0.5)
51
51
  unicode-display_width (2.5.0)
52
+ uri (0.13.0)
52
53
 
53
54
  PLATFORMS
54
55
  x86_64-linux
55
56
 
56
57
  DEPENDENCIES
57
58
  cohere-ai!
59
+ dotenv (~> 2.8, >= 2.8.1)
58
60
  pry-byebug (~> 3.10, >= 3.10.1)
59
61
  rubocop (~> 1.58)
60
62
 
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.2'
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.2
91
91
  ```
92
92
 
93
93
  ```sh
94
- gem 'cohere-ai', '~> 1.0.0'
94
+ gem 'cohere-ai', '~> 1.0.2'
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
  ```
@@ -703,6 +704,8 @@ RequestError
703
704
  ```bash
704
705
  bundle
705
706
  rubocop -A
707
+
708
+ bundle exec ruby spec/tasks/run-client.rb
706
709
  ```
707
710
 
708
711
  ### Purpose
@@ -716,7 +719,7 @@ gem build cohere-ai.gemspec
716
719
 
717
720
  gem signin
718
721
 
719
- gem push cohere-ai-1.0.0.gem
722
+ gem push cohere-ai-1.0.2.gem
720
723
  ```
721
724
 
722
725
  ### Updating the README
data/cohere-ai.gemspec CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.require_paths = ['ports/dsl']
31
31
 
32
- spec.add_dependency 'faraday', '~> 2.8', '>= 2.8.1'
32
+ spec.add_dependency 'faraday', '~> 2.9'
33
33
 
34
34
  spec.metadata['rubygems_mfa_required'] = 'true'
35
35
  end
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
@@ -3,7 +3,7 @@
3
3
  require 'faraday'
4
4
  require 'json'
5
5
 
6
- require_relative '../ports/dsl/cohere-ai/errors'
6
+ require_relative '../components/errors'
7
7
 
8
8
  module Cohere
9
9
  module Controllers
@@ -23,7 +23,7 @@ module Cohere
23
23
  end
24
24
 
25
25
  if @api_key.nil? && @address == "#{DEFAULT_ADDRESS}/"
26
- raise MissingAPIKeyError, 'Missing API Key, which is required.'
26
+ raise Errors::MissingAPIKeyError, 'Missing API Key, which is required.'
27
27
  end
28
28
 
29
29
  @request_options = config.dig(:options, :connection, :request)
@@ -80,7 +80,7 @@ module Cohere
80
80
  url = "#{@address}#{path}"
81
81
 
82
82
  if !callback.nil? && !server_sent_events_enabled
83
- raise BlockWithoutServerSentEventsError,
83
+ raise Errors::BlockWithoutServerSentEventsError,
84
84
  'You are trying to use a block without Server Sent Events (SSE) enabled.'
85
85
  end
86
86
 
@@ -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,45 @@ 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_jsons = safe_parse_jsonl(partial_json)
113
+
114
+ if parsed_jsons
115
+ parsed_jsons.each do |parsed_json|
116
+ result = { event: parsed_json, raw: { chunk:, bytes:, env: } }
109
117
 
110
- callback.call(result[:event], result[:raw]) unless callback.nil?
118
+ callback.call(result[:event], result[:raw]) unless callback.nil?
111
119
 
112
- results << result
120
+ results << result
121
+ end
122
+
123
+ partial_json = ''
124
+ end
113
125
  end
114
126
  end
115
127
  end
116
128
 
117
- return safe_parse_json(response.body) unless server_sent_events_enabled
129
+ return safe_parse_json_with_fallback_to_raw(response.body) unless server_sent_events_enabled
130
+
131
+ raise Errors::IncompleteJSONReceivedError, partial_json if partial_json != ''
118
132
 
119
133
  results.map { |result| result[:event] }
120
134
  rescue Faraday::ServerError => e
121
- raise RequestError.new(e.message, request: e, payload:)
135
+ raise Errors::RequestError.new(e.message, request: e, payload:)
122
136
  end
123
137
 
124
- def safe_parse_json(raw)
125
- raw.start_with?('{', '[') ? JSON.parse(raw) : raw
138
+ def safe_parse_json_with_fallback_to_raw(raw)
139
+ raw.to_s.lstrip.start_with?('{', '[') ? JSON.parse(raw) : raw
126
140
  rescue JSON::ParserError
127
141
  raw
128
142
  end
143
+
144
+ def safe_parse_jsonl(raw)
145
+ raw.to_s.lstrip.start_with?('{', '[') ? raw.split("\n").map { |line| JSON.parse(line) } : raw
146
+ rescue JSON::ParserError
147
+ nil
148
+ end
129
149
  end
130
150
  end
131
151
  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.2',
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.2'
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.2
54
54
  ```
55
55
 
56
56
  ```sh
57
- gem 'cohere-ai', '~> 1.0.0'
57
+ gem 'cohere-ai', '~> 1.0.2'
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
  ```
@@ -666,6 +667,8 @@ RequestError
666
667
  ```bash
667
668
  bundle
668
669
  rubocop -A
670
+
671
+ bundle exec ruby spec/tasks/run-client.rb
669
672
  ```
670
673
 
671
674
  ### Purpose
@@ -679,7 +682,7 @@ gem build cohere-ai.gemspec
679
682
 
680
683
  gem signin
681
684
 
682
- gem push cohere-ai-1.0.0.gem
685
+ gem push cohere-ai-1.0.2.gem
683
686
  ```
684
687
 
685
688
  ### Updating the README
metadata CHANGED
@@ -1,14 +1,14 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gbaptista
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-29 00:00:00.000000000 Z
11
+ date: 2024-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.8'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 2.8.1
19
+ version: '2.9'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '2.8'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 2.8.1
26
+ version: '2.9'
33
27
  description: A Ruby gem for interacting with Cohere AI platform.
34
28
  email:
35
29
  executables: []
@@ -74,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
68
  - !ruby/object:Gem::Version
75
69
  version: '0'
76
70
  requirements: []
77
- rubygems_version: 3.4.22
71
+ rubygems_version: 3.3.3
78
72
  signing_key:
79
73
  specification_version: 4
80
74
  summary: Interact with Cohere AI.