cohere-ruby 0.9.9 → 0.9.11
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 +38 -23
- data/lib/cohere/client.rb +32 -4
- 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: 5f1c3798500d154b882bd6bb2e0e43904fea4bf69db4d982da4abc6265bc461c
|
4
|
+
data.tar.gz: d91ed933c24f2dacc5f2604639d307fd389f3774aaed8082df07e03ac2961704
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b989c60be670a143848367a7be84f53fc6adf97f31e1f3739113a7a7caec988450e4e889bcbc7f2733783623977dc4f6e1eb7484470259935e30e7dc6d5d1414
|
7
|
+
data.tar.gz: 00a2e3e1a19bfb1980d06ebc1fd6117262bcdf226bc95bfe0edff8ea8e1397791eaf7ef37b78ad158e0ef5a8363fa809c9215fc325d771e72abf22d7919c54c0
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Cohere
|
2
2
|
|
3
3
|
<p>
|
4
|
-
<img alt='
|
4
|
+
<img alt='Cohere logo' src='https://static.wikia.nocookie.net/logopedia/images/d/d4/Cohere_2023.svg/revision/latest?cb=20230419182227' height='50' />
|
5
5
|
+
|
6
6
|
<img alt='Ruby logo' src='https://user-images.githubusercontent.com/541665/230231593-43861278-4550-421d-a543-fd3553aac4f6.png' height='40' />
|
7
7
|
</p>
|
@@ -34,7 +34,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
34
34
|
require "cohere"
|
35
35
|
|
36
36
|
client = Cohere::Client.new(
|
37
|
-
ENV['COHERE_API_KEY']
|
37
|
+
api_key: ENV['COHERE_API_KEY']
|
38
38
|
)
|
39
39
|
```
|
40
40
|
|
@@ -42,7 +42,7 @@ client = Cohere::Client.new(
|
|
42
42
|
|
43
43
|
```ruby
|
44
44
|
client.generate(
|
45
|
-
|
45
|
+
prompt: "Once upon a time in a magical land called"
|
46
46
|
)
|
47
47
|
```
|
48
48
|
|
@@ -50,7 +50,7 @@ client.generate(
|
|
50
50
|
|
51
51
|
```ruby
|
52
52
|
client.chat(
|
53
|
-
|
53
|
+
message: "Hey! How are you?"
|
54
54
|
)
|
55
55
|
```
|
56
56
|
|
@@ -90,30 +90,45 @@ client.chat(
|
|
90
90
|
)
|
91
91
|
```
|
92
92
|
|
93
|
-
|
94
|
-
|
95
93
|
### Embed
|
96
94
|
|
97
95
|
```ruby
|
98
96
|
client.embed(
|
99
|
-
|
97
|
+
texts: ["hello!"]
|
98
|
+
)
|
99
|
+
```
|
100
|
+
|
101
|
+
### Rerank
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
docs = [
|
105
|
+
"Carson City is the capital city of the American state of Nevada.",
|
106
|
+
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
|
107
|
+
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
|
108
|
+
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
|
109
|
+
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
|
110
|
+
]
|
111
|
+
|
112
|
+
client.rerank(
|
113
|
+
texts: ["hello!"]
|
100
114
|
)
|
101
115
|
```
|
102
116
|
|
117
|
+
|
103
118
|
### Classify
|
104
119
|
|
105
120
|
```ruby
|
106
121
|
examples = [
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
122
|
+
{ text: "Dermatologists don't like her!", label: "Spam" },
|
123
|
+
{ text: "Hello, open to this?", label: "Spam" },
|
124
|
+
{ text: "I need help please wire me $1000 right now", label: "Spam" },
|
125
|
+
{ text: "Nice to know you ;)", label: "Spam" },
|
126
|
+
{ text: "Please help me?", label: "Spam" },
|
127
|
+
{ text: "Your parcel will be delivered today", label: "Not spam" },
|
128
|
+
{ text: "Review changes to our Terms and Conditions", label: "Not spam" },
|
129
|
+
{ text: "Weekly sync notes", label: "Not spam" },
|
130
|
+
{ text: "Re: Follow up from today's meeting", label: "Not spam" },
|
131
|
+
{ text: "Pre-read for tomorrow", label: "Not spam" }
|
117
132
|
]
|
118
133
|
|
119
134
|
inputs = [
|
@@ -122,8 +137,8 @@ inputs = [
|
|
122
137
|
]
|
123
138
|
|
124
139
|
client.classify(
|
125
|
-
|
126
|
-
|
140
|
+
examples: examples,
|
141
|
+
inputs: inputs
|
127
142
|
)
|
128
143
|
```
|
129
144
|
|
@@ -131,7 +146,7 @@ client.classify(
|
|
131
146
|
|
132
147
|
```ruby
|
133
148
|
client.tokenize(
|
134
|
-
|
149
|
+
text: "hello world!"
|
135
150
|
)
|
136
151
|
```
|
137
152
|
|
@@ -139,7 +154,7 @@ client.tokenize(
|
|
139
154
|
|
140
155
|
```ruby
|
141
156
|
client.detokenize(
|
142
|
-
|
157
|
+
tokens: [33555, 1114 , 34]
|
143
158
|
)
|
144
159
|
```
|
145
160
|
|
@@ -147,7 +162,7 @@ client.detokenize(
|
|
147
162
|
|
148
163
|
```ruby
|
149
164
|
client.detect_language(
|
150
|
-
|
165
|
+
texts: ["Здравствуй, Мир"]
|
151
166
|
)
|
152
167
|
```
|
153
168
|
|
@@ -155,7 +170,7 @@ client.detect_language(
|
|
155
170
|
|
156
171
|
```ruby
|
157
172
|
client.summarize(
|
158
|
-
|
173
|
+
text: "..."
|
159
174
|
)
|
160
175
|
```
|
161
176
|
|
data/lib/cohere/client.rb
CHANGED
@@ -8,12 +8,13 @@ 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,
|
19
20
|
preamble: nil,
|
@@ -29,13 +30,16 @@ module Cohere
|
|
29
30
|
max_tokens: nil,
|
30
31
|
k: nil,
|
31
32
|
p: nil,
|
33
|
+
seed: nil,
|
32
34
|
frequency_penalty: nil,
|
33
35
|
presence_penalty: nil,
|
34
36
|
tools: [],
|
35
37
|
&block
|
36
38
|
)
|
37
39
|
response = connection.post("chat") do |req|
|
38
|
-
req.body = {
|
40
|
+
req.body = {}
|
41
|
+
|
42
|
+
req.body[:message] = message if message
|
39
43
|
req.body[:model] = model if model
|
40
44
|
if stream || block
|
41
45
|
req.body[:stream] = true
|
@@ -54,6 +58,7 @@ module Cohere
|
|
54
58
|
req.body[:max_tokens] = max_tokens if max_tokens
|
55
59
|
req.body[:k] = k if k
|
56
60
|
req.body[:p] = p if p
|
61
|
+
req.body[:seed] = seed if seed
|
57
62
|
req.body[:frequency_penalty] = frequency_penalty if frequency_penalty
|
58
63
|
req.body[:presence_penalty] = presence_penalty if presence_penalty
|
59
64
|
req.body[:tools] = tools if tools
|
@@ -114,6 +119,29 @@ module Cohere
|
|
114
119
|
response.body
|
115
120
|
end
|
116
121
|
|
122
|
+
def rerank(
|
123
|
+
query:,
|
124
|
+
documents:,
|
125
|
+
model: nil,
|
126
|
+
top_n: nil,
|
127
|
+
rank_fields: nil,
|
128
|
+
return_documents: nil,
|
129
|
+
max_chunks_per_doc: nil
|
130
|
+
)
|
131
|
+
response = connection.post("rerank") do |req|
|
132
|
+
req.body = {
|
133
|
+
query: query,
|
134
|
+
documents: documents
|
135
|
+
}
|
136
|
+
req.body[:model] = model if model
|
137
|
+
req.body[:top_n] = top_n if top_n
|
138
|
+
req.body[:rank_fields] = rank_fields if rank_fields
|
139
|
+
req.body[:return_documents] = return_documents if return_documents
|
140
|
+
req.body[:max_chunks_per_doc] = max_chunks_per_doc if max_chunks_per_doc
|
141
|
+
end
|
142
|
+
response.body
|
143
|
+
end
|
144
|
+
|
117
145
|
def classify(
|
118
146
|
inputs:,
|
119
147
|
examples:,
|
@@ -179,7 +207,7 @@ module Cohere
|
|
179
207
|
|
180
208
|
# standard:disable Lint/DuplicateMethods
|
181
209
|
def connection
|
182
|
-
@connection ||= Faraday.new(url: ENDPOINT_URL) do |faraday|
|
210
|
+
@connection ||= Faraday.new(url: ENDPOINT_URL, request: {timeout: @timeout}) do |faraday|
|
183
211
|
if api_key
|
184
212
|
faraday.request :authorization, :Bearer, api_key
|
185
213
|
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.11
|
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-08-01 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.5.11
|
74
74
|
signing_key:
|
75
75
|
specification_version: 4
|
76
76
|
summary: Cohere API client for Ruby.
|