cohere-ai 1.0.1 → 1.1.0

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: a5326901fa2fcef2d71d74de376e3ca9df19d9adb65fe6cf5e1a6d0bd195b8d6
4
- data.tar.gz: 78e162ac160c691b0060945511059a9ea185ee44105298bbf784f3a8e86fbf5a
3
+ metadata.gz: 3ea877fce85c65fa6b22f580d335c1e4d2445f60b85df8db0025cd4e1acb93d6
4
+ data.tar.gz: 435e4efc2cf75f18fd6ed283e915975ffda1561c868bbfd9cc7b691295fa0ea2
5
5
  SHA512:
6
- metadata.gz: 9cff2fb206e5daa899b440b27280c9b155b012364319da3536e28875a14cbebbb6c6f2a00e07409ec1edc501cd9c9a87154fbd31a28c084985bf8eccab627d28
7
- data.tar.gz: 71453d884fd1bd3e76a9c78c5c9752dd6b695d887a1448aa12d3b0421a788f3da9306a6796beebbd0335537fa81a67ddb92dd68fd965e5e9b65d26169ff58640
6
+ metadata.gz: 74d7b9593bae09f94e6d0c9c60f060a5118f4772be5a8fb8125a0bd96161ba0ac578f8d094a52c9f9312e2fa4c7cb4a35307b078ad7a339bb30309f37a45fae2
7
+ data.tar.gz: e23ee1214c25cf3cde1005a7847dd5a6d752c2e1b3a9e1bb1746ba6c6049c8784a46d8f27c96156c6900f20c91255b01fa5c569779dcb3693650338105b3b717
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
- gem 'rubocop', '~> 1.58'
10
+ gem 'rubocop', '~> 1.60', '>= 1.60.2'
10
11
  end
data/Gemfile.lock CHANGED
@@ -1,26 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cohere-ai (1.0.1)
5
- faraday (~> 2.8, >= 2.8.1)
4
+ cohere-ai (1.1.0)
5
+ faraday (~> 2.9)
6
+ faraday-typhoeus (~> 1.1)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
11
  ast (2.4.2)
11
- base64 (0.2.0)
12
12
  byebug (11.1.3)
13
13
  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)
14
+ dotenv (2.8.1)
15
+ ethon (0.16.0)
16
+ ffi (>= 1.15.0)
17
+ faraday (2.9.0)
18
+ faraday-net_http (>= 2.0, < 3.2)
19
+ faraday-net_http (3.1.0)
20
+ net-http
21
+ faraday-typhoeus (1.1.0)
22
+ faraday (~> 2.0)
23
+ typhoeus (~> 1.4)
24
+ ffi (1.16.3)
19
25
  json (2.7.1)
20
26
  language_server-protocol (3.17.0.3)
21
27
  method_source (1.0.0)
28
+ net-http (0.4.1)
29
+ uri
22
30
  parallel (1.24.0)
23
- parser (3.2.2.4)
31
+ parser (3.3.0.5)
24
32
  ast (~> 2.4.1)
25
33
  racc
26
34
  pry (0.14.2)
@@ -31,13 +39,13 @@ GEM
31
39
  pry (>= 0.13, < 0.15)
32
40
  racc (1.7.3)
33
41
  rainbow (3.1.1)
34
- regexp_parser (2.8.3)
42
+ regexp_parser (2.9.0)
35
43
  rexml (3.2.6)
36
- rubocop (1.59.0)
44
+ rubocop (1.60.2)
37
45
  json (~> 2.3)
38
46
  language_server-protocol (>= 3.17.0)
39
47
  parallel (~> 1.10)
40
- parser (>= 3.2.2.4)
48
+ parser (>= 3.3.0.2)
41
49
  rainbow (>= 2.2.2, < 4.0)
42
50
  regexp_parser (>= 1.8, < 3.0)
43
51
  rexml (>= 3.2.5, < 4.0)
@@ -47,16 +55,19 @@ GEM
47
55
  rubocop-ast (1.30.0)
48
56
  parser (>= 3.2.1.0)
49
57
  ruby-progressbar (1.13.0)
50
- ruby2_keywords (0.0.5)
58
+ typhoeus (1.4.1)
59
+ ethon (>= 0.9.0)
51
60
  unicode-display_width (2.5.0)
61
+ uri (0.13.0)
52
62
 
53
63
  PLATFORMS
54
64
  x86_64-linux
55
65
 
56
66
  DEPENDENCIES
57
67
  cohere-ai!
68
+ dotenv (~> 2.8, >= 2.8.1)
58
69
  pry-byebug (~> 3.10, >= 3.10.1)
59
- rubocop (~> 1.58)
70
+ rubocop (~> 1.60, >= 1.60.2)
60
71
 
61
72
  BUNDLED WITH
62
73
  2.4.22
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.1'
12
+ gem 'cohere-ai', '~> 1.1.0'
13
13
  ```
14
14
 
15
15
  ```ruby
@@ -46,39 +46,40 @@ Result:
46
46
  - [TL;DR and Quick Start](#tldr-and-quick-start)
47
47
  - [Index](#index)
48
48
  - [Setup](#setup)
49
- - [Installing](#installing)
50
- - [Credentials](#credentials)
49
+ - [Installing](#installing)
50
+ - [Credentials](#credentials)
51
51
  - [Usage](#usage)
52
- - [Client](#client)
53
- - [Custom Address](#custom-address)
54
- - [Methods](#methods)
55
- - [chat](#chat)
56
- - [Without Streaming Events](#without-streaming-events)
57
- - [Receiving Stream Events](#receiving-stream-events)
58
- - [generate](#generate)
59
- - [embed](#embed)
60
- - [rerank](#rerank)
61
- - [classify](#classify)
62
- - [detect_language](#detect_language)
63
- - [summarize](#summarize)
64
- - [tokenize](#tokenize)
65
- - [detokenize](#detokenize)
66
- - [Datasets](#datasets)
67
- - [Connectors](#connectors)
68
- - [Streaming and Server-Sent Events (SSE)](#streaming-and-server-sent-events-sse)
69
- - [Server-Sent Events (SSE) Hang](#server-sent-events-sse-hang)
70
- - [Back-and-Forth Conversations](#back-and-forth-conversations)
71
- - [New Functionalities and APIs](#new-functionalities-and-apis)
72
- - [Request Options](#request-options)
73
- - [Timeout](#timeout)
74
- - [Error Handling](#error-handling)
75
- - [Rescuing](#rescuing)
76
- - [For Short](#for-short)
77
- - [Errors](#errors)
52
+ - [Client](#client)
53
+ - [Custom Address](#custom-address)
54
+ - [Methods](#methods)
55
+ - [chat](#chat)
56
+ - [Without Streaming Events](#without-streaming-events)
57
+ - [Receiving Stream Events](#receiving-stream-events)
58
+ - [generate](#generate)
59
+ - [embed](#embed)
60
+ - [rerank](#rerank)
61
+ - [classify](#classify)
62
+ - [detect_language](#detect_language)
63
+ - [summarize](#summarize)
64
+ - [tokenize](#tokenize)
65
+ - [detokenize](#detokenize)
66
+ - [Datasets](#datasets)
67
+ - [Connectors](#connectors)
68
+ - [Streaming and Server-Sent Events (SSE)](#streaming-and-server-sent-events-sse)
69
+ - [Server-Sent Events (SSE) Hang](#server-sent-events-sse-hang)
70
+ - [Back-and-Forth Conversations](#back-and-forth-conversations)
71
+ - [New Functionalities and APIs](#new-functionalities-and-apis)
72
+ - [Request Options](#request-options)
73
+ - [Adapter](#adapter)
74
+ - [Timeout](#timeout)
75
+ - [Error Handling](#error-handling)
76
+ - [Rescuing](#rescuing)
77
+ - [For Short](#for-short)
78
+ - [Errors](#errors)
78
79
  - [Development](#development)
79
- - [Purpose](#purpose)
80
- - [Publish to RubyGems](#publish-to-rubygems)
81
- - [Updating the README](#updating-the-readme)
80
+ - [Purpose](#purpose)
81
+ - [Publish to RubyGems](#publish-to-rubygems)
82
+ - [Updating the README](#updating-the-readme)
82
83
  - [Resources and References](#resources-and-references)
83
84
  - [Disclaimer](#disclaimer)
84
85
 
@@ -87,11 +88,11 @@ Result:
87
88
  ### Installing
88
89
 
89
90
  ```sh
90
- gem install cohere-ai -v 1.0.1
91
+ gem install cohere-ai -v 1.1.0
91
92
  ```
92
93
 
93
94
  ```sh
94
- gem 'cohere-ai', '~> 1.0.1'
95
+ gem 'cohere-ai', '~> 1.1.0'
95
96
  ```
96
97
 
97
98
  ### Credentials
@@ -617,6 +618,21 @@ result = client.request(
617
618
 
618
619
  ### Request Options
619
620
 
621
+ #### Adapter
622
+
623
+ The gem uses [Faraday](https://github.com/lostisland/faraday) with the [Typhoeus](https://github.com/typhoeus/typhoeus) adapter by default.
624
+
625
+ You can use a different adapter if you want:
626
+
627
+ ```ruby
628
+ require 'faraday/net_http'
629
+
630
+ client = Cohere.new(
631
+ credentials: { api_key: ENV['COHERE_API_KEY'] },
632
+ options: { connection: { adapter: :net_http } }
633
+ )
634
+ ```
635
+
620
636
  #### Timeout
621
637
 
622
638
  You can set the maximum number of seconds to wait for the request to complete with the `timeout` option:
@@ -704,6 +720,8 @@ RequestError
704
720
  ```bash
705
721
  bundle
706
722
  rubocop -A
723
+
724
+ bundle exec ruby spec/tasks/run-client.rb
707
725
  ```
708
726
 
709
727
  ### Purpose
@@ -717,7 +735,7 @@ gem build cohere-ai.gemspec
717
735
 
718
736
  gem signin
719
737
 
720
- gem push cohere-ai-1.0.1.gem
738
+ gem push cohere-ai-1.1.0.gem
721
739
  ```
722
740
 
723
741
  ### Updating the README
data/cohere-ai.gemspec CHANGED
@@ -29,7 +29,8 @@ 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
+ spec.add_dependency 'faraday-typhoeus', '~> 1.1'
33
34
 
34
35
  spec.metadata['rubygems_mfa_required'] = 'true'
35
36
  end
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'faraday'
4
+ require 'faraday/typhoeus'
4
5
  require 'json'
5
6
 
6
- require_relative '../ports/dsl/cohere-ai/errors'
7
+ require_relative '../components/errors'
7
8
 
8
9
  module Cohere
9
10
  module Controllers
@@ -12,6 +13,8 @@ module Cohere
12
13
 
13
14
  ALLOWED_REQUEST_OPTIONS = %i[timeout open_timeout read_timeout write_timeout].freeze
14
15
 
16
+ DEFAULT_FARADAY_ADAPTER = :typhoeus
17
+
15
18
  def initialize(config)
16
19
  @api_key = config.dig(:credentials, :api_key)
17
20
  @server_sent_events = config.dig(:options, :server_sent_events)
@@ -23,7 +26,7 @@ module Cohere
23
26
  end
24
27
 
25
28
  if @api_key.nil? && @address == "#{DEFAULT_ADDRESS}/"
26
- raise MissingAPIKeyError, 'Missing API Key, which is required.'
29
+ raise Errors::MissingAPIKeyError, 'Missing API Key, which is required.'
27
30
  end
28
31
 
29
32
  @request_options = config.dig(:options, :connection, :request)
@@ -35,6 +38,8 @@ module Cohere
35
38
  else
36
39
  {}
37
40
  end
41
+
42
+ @faraday_adapter = config.dig(:options, :connection, :adapter) || DEFAULT_FARADAY_ADAPTER
38
43
  end
39
44
 
40
45
  def chat(payload, server_sent_events: nil, &callback)
@@ -80,7 +85,7 @@ module Cohere
80
85
  url = "#{@address}#{path}"
81
86
 
82
87
  if !callback.nil? && !server_sent_events_enabled
83
- raise BlockWithoutServerSentEventsError,
88
+ raise Errors::BlockWithoutServerSentEventsError,
84
89
  'You are trying to use a block without Server Sent Events (SSE) enabled.'
85
90
  end
86
91
 
@@ -91,6 +96,7 @@ module Cohere
91
96
  partial_json = ''
92
97
 
93
98
  response = Faraday.new(request: @request_options) do |faraday|
99
+ faraday.adapter @faraday_adapter
94
100
  faraday.response :raise_error
95
101
  end.send(method_to_call) do |request|
96
102
  request.url url
@@ -109,14 +115,16 @@ module Cohere
109
115
 
110
116
  partial_json += chunk
111
117
 
112
- parsed_json = safe_parse_json(partial_json)
118
+ parsed_jsons = safe_parse_jsonl(partial_json)
113
119
 
114
- if parsed_json
115
- result = { event: parsed_json, raw: { chunk:, bytes:, env: } }
120
+ if parsed_jsons
121
+ parsed_jsons.each do |parsed_json|
122
+ result = { event: parsed_json, raw: { chunk:, bytes:, env: } }
116
123
 
117
- callback.call(result[:event], result[:raw]) unless callback.nil?
124
+ callback.call(result[:event], result[:raw]) unless callback.nil?
118
125
 
119
- results << result
126
+ results << result
127
+ end
120
128
 
121
129
  partial_json = ''
122
130
  end
@@ -126,11 +134,11 @@ module Cohere
126
134
 
127
135
  return safe_parse_json_with_fallback_to_raw(response.body) unless server_sent_events_enabled
128
136
 
129
- raise IncompleteJSONReceivedError, partial_json if partial_json != ''
137
+ raise Errors::IncompleteJSONReceivedError, partial_json if partial_json != ''
130
138
 
131
139
  results.map { |result| result[:event] }
132
140
  rescue Faraday::ServerError => e
133
- raise RequestError.new(e.message, request: e, payload:)
141
+ raise Errors::RequestError.new(e.message, request: e, payload:)
134
142
  end
135
143
 
136
144
  def safe_parse_json_with_fallback_to_raw(raw)
@@ -139,8 +147,8 @@ module Cohere
139
147
  raw
140
148
  end
141
149
 
142
- def safe_parse_json(raw)
143
- raw.to_s.lstrip.start_with?('{', '[') ? JSON.parse(raw) : nil
150
+ def safe_parse_jsonl(raw)
151
+ raw.to_s.lstrip.start_with?('{', '[') ? raw.split("\n").map { |line| JSON.parse(line) } : raw
144
152
  rescue JSON::ParserError
145
153
  nil
146
154
  end
data/static/gem.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Cohere
4
4
  GEM = {
5
5
  name: 'cohere-ai',
6
- version: '1.0.1',
6
+ version: '1.1.0',
7
7
  author: 'gbaptista',
8
8
  summary: 'Interact with Cohere AI.',
9
9
  description: 'A Ruby gem for interacting with Cohere AI platform.',
@@ -23,7 +23,7 @@
23
23
  (remove nil?))]
24
24
  (->> processed-lines
25
25
  (map (fn [{:keys [level title link]}]
26
- (str (apply str (repeat (* 4 (- level 2)) " "))
26
+ (str (apply str (repeat (* 2 (- level 2)) " "))
27
27
  "- ["
28
28
  title
29
29
  "](#"
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.1'
12
+ gem 'cohere-ai', '~> 1.1.0'
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.1
53
+ gem install cohere-ai -v 1.1.0
54
54
  ```
55
55
 
56
56
  ```sh
57
- gem 'cohere-ai', '~> 1.0.1'
57
+ gem 'cohere-ai', '~> 1.1.0'
58
58
  ```
59
59
 
60
60
  ### Credentials
@@ -580,6 +580,21 @@ result = client.request(
580
580
 
581
581
  ### Request Options
582
582
 
583
+ #### Adapter
584
+
585
+ The gem uses [Faraday](https://github.com/lostisland/faraday) with the [Typhoeus](https://github.com/typhoeus/typhoeus) adapter by default.
586
+
587
+ You can use a different adapter if you want:
588
+
589
+ ```ruby
590
+ require 'faraday/net_http'
591
+
592
+ client = Cohere.new(
593
+ credentials: { api_key: ENV['COHERE_API_KEY'] },
594
+ options: { connection: { adapter: :net_http } }
595
+ )
596
+ ```
597
+
583
598
  #### Timeout
584
599
 
585
600
  You can set the maximum number of seconds to wait for the request to complete with the `timeout` option:
@@ -667,6 +682,8 @@ RequestError
667
682
  ```bash
668
683
  bundle
669
684
  rubocop -A
685
+
686
+ bundle exec ruby spec/tasks/run-client.rb
670
687
  ```
671
688
 
672
689
  ### Purpose
@@ -680,7 +697,7 @@ gem build cohere-ai.gemspec
680
697
 
681
698
  gem signin
682
699
 
683
- gem push cohere-ai-1.0.1.gem
700
+ gem push cohere-ai-1.1.0.gem
684
701
  ```
685
702
 
686
703
  ### 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.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gbaptista
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-29 00:00:00.000000000 Z
11
+ date: 2024-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,22 +16,30 @@ 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
- - - ">="
26
+ version: '2.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-typhoeus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
31
39
  - !ruby/object:Gem::Version
32
- version: 2.8.1
40
+ version: '1.1'
33
41
  description: A Ruby gem for interacting with Cohere AI platform.
34
- email:
42
+ email:
35
43
  executables: []
36
44
  extensions: []
37
45
  extra_rdoc_files: []
@@ -59,7 +67,7 @@ metadata:
59
67
  homepage_uri: https://github.com/gbaptista/cohere-ai
60
68
  source_code_uri: https://github.com/gbaptista/cohere-ai
61
69
  rubygems_mfa_required: 'true'
62
- post_install_message:
70
+ post_install_message:
63
71
  rdoc_options: []
64
72
  require_paths:
65
73
  - ports/dsl
@@ -75,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
83
  version: '0'
76
84
  requirements: []
77
85
  rubygems_version: 3.3.3
78
- signing_key:
86
+ signing_key:
79
87
  specification_version: 4
80
88
  summary: Interact with Cohere AI.
81
89
  test_files: []