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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +49 -1
- data/lib/cohere/client.rb +19 -6
- data/lib/cohere/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c5f0625529c315bd0a953bb8e0acf36fbebe685b13decde1fe26d93b5d97ab8
|
4
|
+
data.tar.gz: 504ce83377f6f2175b2d7ab9f899c307a7a02563c75b8c630b249d0e7487c746
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dc8c2c85e551438f597346679c3bb198808e906eb9317dd05c26511556e34f512707cbc4bb3046487de9e498ca7b92c6405d2f9c3e4175f466e49618c7215b9
|
7
|
+
data.tar.gz: 0024a4c8c6091fa78fe83dcd7879ea8a511436e4ddc16929dd3e88e517901ba1ac569e16c519f47abccbba08a0cdd43fe795c4e867b4e252f0d5003a9ffb5b3e
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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 = {
|
40
|
+
req.body = {}
|
41
|
+
|
42
|
+
req.body[:message] = message if message
|
36
43
|
req.body[:model] = model if model
|
37
|
-
|
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
|
data/lib/cohere/version.rb
CHANGED
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.
|
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-
|
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.
|
73
|
+
rubygems_version: 3.4.1
|
74
74
|
signing_key:
|
75
75
|
specification_version: 4
|
76
76
|
summary: Cohere API client for Ruby.
|