cohere-ruby 0.9.8 → 0.9.10

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: 83208ee6cbdf496180ac2e6ed258388a74a266d86188b81ceb011bf5f2b61f44
4
- data.tar.gz: 9f0ff2d20ea3c988663bc1c104f46f0c94a6d8d833d4a0ebf1cb81f765d8502d
3
+ metadata.gz: 6c5f0625529c315bd0a953bb8e0acf36fbebe685b13decde1fe26d93b5d97ab8
4
+ data.tar.gz: 504ce83377f6f2175b2d7ab9f899c307a7a02563c75b8c630b249d0e7487c746
5
5
  SHA512:
6
- metadata.gz: a0425fe338748ba829c6d15b3fe319c23cf6fe47ab18c885476dcccffe969888c215162d9139768e53ed569b5e6799c9abc17ecb028551f3cd48c2f92cec49e6
7
- data.tar.gz: 48add01bf4d9ee6a43ee27809efe2eb352eee73bceac26b6fcfff29ae060063b8e3f4dc5803e317d80a612d0f6890ac18ca1d90cf5fd62254e3d1bc6f1dce8de
6
+ metadata.gz: 1dc8c2c85e551438f597346679c3bb198808e906eb9317dd05c26511556e34f512707cbc4bb3046487de9e498ca7b92c6405d2f9c3e4175f466e49618c7215b9
7
+ data.tar.gz: 0024a4c8c6091fa78fe83dcd7879ea8a511436e4ddc16929dd3e88e517901ba1ac569e16c519f47abccbba08a0cdd43fe795c4e867b4e252f0d5003a9ffb5b3e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.9.10] - 2024-05-10
4
+ - /chat endpoint does not require `message:` parameter anymore
5
+
6
+ ## [0.9.9] - 2024-04-05
7
+ - Adding missing parameters to endpoints
8
+
3
9
  ## [0.9.8] - 2024-02-10
4
10
  - Adding method for /chat endpoint
5
11
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cohere-ruby (0.9.8)
4
+ cohere-ruby (0.9.10)
5
5
  faraday (>= 2.0.1, < 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -29,14 +29,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
29
29
  ## Usage
30
30
 
31
31
  ### Instantiating API client
32
+
32
33
  ```ruby
33
34
  require "cohere"
34
35
 
35
36
  client = Cohere::Client.new(
36
- ENV['COHERE_API_KEY']
37
+ api_key: ENV['COHERE_API_KEY']
37
38
  )
38
39
  ```
40
+
39
41
  ### Generate
42
+
40
43
  ```ruby
41
44
  client.generate(
42
45
  prompt: "Once upon a time in a magical land called"
@@ -44,13 +47,53 @@ client.generate(
44
47
  ```
45
48
 
46
49
  ### Chat
50
+
47
51
  ```ruby
48
52
  client.chat(
49
53
  message: "Hey! How are you?"
50
54
  )
51
55
  ```
52
56
 
57
+ `chat` supports a streaming option. You can pass a block to the `chat` method and it will yield a new chunk as soon as it is received.
58
+
59
+ ```ruby
60
+ client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
61
+ puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
62
+ end
63
+ ```
64
+
65
+ `force_encoding` is preferred to avoid JSON parsing issue when Cohere returns emoticon.
66
+
67
+ `chat` supports Tool use (function calling).
68
+
69
+ ```ruby
70
+ tools = [
71
+ {
72
+ name: "query_daily_sales_report",
73
+ description: "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
74
+ parameter_definitions: {
75
+ day: {
76
+ description: "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
77
+ type: "str",
78
+ required: true
79
+ }
80
+ }
81
+ }
82
+ ]
83
+
84
+ message = "Can you provide a sales summary for 29th September 2023, and also give me some details about the products in the 'Electronics' category, for example their prices and stock levels?"
85
+
86
+ client.chat(
87
+ model: model,
88
+ message: message,
89
+ tools: tools,
90
+ )
91
+ ```
92
+
93
+
94
+
53
95
  ### Embed
96
+
54
97
  ```ruby
55
98
  client.embed(
56
99
  texts: ["hello!"]
@@ -58,6 +101,7 @@ client.embed(
58
101
  ```
59
102
 
60
103
  ### Classify
104
+
61
105
  ```ruby
62
106
  examples = [
63
107
  { text: "Dermatologists don't like her!", label: "Spam" },
@@ -84,6 +128,7 @@ client.classify(
84
128
  ```
85
129
 
86
130
  ### Tokenize
131
+
87
132
  ```ruby
88
133
  client.tokenize(
89
134
  text: "hello world!"
@@ -91,6 +136,7 @@ client.tokenize(
91
136
  ```
92
137
 
93
138
  ### Detokenize
139
+
94
140
  ```ruby
95
141
  client.detokenize(
96
142
  tokens: [33555, 1114 , 34]
@@ -98,6 +144,7 @@ client.detokenize(
98
144
  ```
99
145
 
100
146
  ### Detect language
147
+
101
148
  ```ruby
102
149
  client.detect_language(
103
150
  texts: ["Здравствуй, Мир"]
@@ -105,6 +152,7 @@ client.detect_language(
105
152
  ```
106
153
 
107
154
  ### Summarize
155
+
108
156
  ```ruby
109
157
  client.summarize(
110
158
  text: "..."
data/lib/cohere/client.rb CHANGED
@@ -8,14 +8,16 @@ module Cohere
8
8
 
9
9
  ENDPOINT_URL = "https://api.cohere.ai/v1"
10
10
 
11
- def initialize(api_key)
11
+ def initialize(api_key:, timeout: nil)
12
12
  @api_key = api_key
13
+ @timeout = timeout
13
14
  end
14
15
 
15
16
  def chat(
16
- message:,
17
+ message: nil,
17
18
  model: nil,
18
19
  stream: false,
20
+ preamble: nil,
19
21
  preamble_override: nil,
20
22
  chat_history: [],
21
23
  conversation_id: nil,
@@ -28,13 +30,22 @@ module Cohere
28
30
  max_tokens: nil,
29
31
  k: nil,
30
32
  p: nil,
33
+ seed: nil,
31
34
  frequency_penalty: nil,
32
- presence_penalty: nil
35
+ presence_penalty: nil,
36
+ tools: [],
37
+ &block
33
38
  )
34
39
  response = connection.post("chat") do |req|
35
- req.body = {message: message}
40
+ req.body = {}
41
+
42
+ req.body[:message] = message if message
36
43
  req.body[:model] = model if model
37
- req.body[:stream] = stream if stream
44
+ if stream || block
45
+ req.body[:stream] = true
46
+ req.options.on_data = block if block
47
+ end
48
+ req.body[:preamble] = preamble if preamble
38
49
  req.body[:preamble_override] = preamble_override if preamble_override
39
50
  req.body[:chat_history] = chat_history if chat_history
40
51
  req.body[:conversation_id] = conversation_id if conversation_id
@@ -47,8 +58,10 @@ module Cohere
47
58
  req.body[:max_tokens] = max_tokens if max_tokens
48
59
  req.body[:k] = k if k
49
60
  req.body[:p] = p if p
61
+ req.body[:seed] = seed if seed
50
62
  req.body[:frequency_penalty] = frequency_penalty if frequency_penalty
51
63
  req.body[:presence_penalty] = presence_penalty if presence_penalty
64
+ req.body[:tools] = tools if tools
52
65
  end
53
66
  response.body
54
67
  end
@@ -171,7 +184,7 @@ module Cohere
171
184
 
172
185
  # standard:disable Lint/DuplicateMethods
173
186
  def connection
174
- @connection ||= Faraday.new(url: ENDPOINT_URL) do |faraday|
187
+ @connection ||= Faraday.new(url: ENDPOINT_URL, request: {timeout: @timeout}) do |faraday|
175
188
  if api_key
176
189
  faraday.request :authorization, :Bearer, api_key
177
190
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cohere
4
- VERSION = "0.9.8"
4
+ VERSION = "0.9.10"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cohere-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bondarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-11 00:00:00.000000000 Z
11
+ date: 2024-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  - !ruby/object:Gem::Version
71
71
  version: '0'
72
72
  requirements: []
73
- rubygems_version: 3.5.3
73
+ rubygems_version: 3.4.1
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: Cohere API client for Ruby.