Skip to content

Commit 1e10b14

Browse files
Adding specs
1 parent af29521 commit 1e10b14

15 files changed

+348
-15
lines changed

Gemfile.lock

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cohere-ruby (0.9.0)
5+
faraday (~> 2.7.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
diff-lcs (1.5.0)
11+
faraday (2.7.4)
12+
faraday-net_http (>= 2.0, < 3.1)
13+
ruby2_keywords (>= 0.0.4)
14+
faraday-net_http (3.0.2)
15+
rake (13.0.6)
16+
rspec (3.12.0)
17+
rspec-core (~> 3.12.0)
18+
rspec-expectations (~> 3.12.0)
19+
rspec-mocks (~> 3.12.0)
20+
rspec-core (3.12.1)
21+
rspec-support (~> 3.12.0)
22+
rspec-expectations (3.12.2)
23+
diff-lcs (>= 1.2.0, < 2.0)
24+
rspec-support (~> 3.12.0)
25+
rspec-mocks (3.12.5)
26+
diff-lcs (>= 1.2.0, < 2.0)
27+
rspec-support (~> 3.12.0)
28+
rspec-support (3.12.0)
29+
ruby2_keywords (0.0.5)
30+
31+
PLATFORMS
32+
x86_64-darwin-19
33+
34+
DEPENDENCIES
35+
cohere-ruby!
36+
rake (~> 13.0)
37+
rspec (~> 3.0)
38+
39+
BUNDLED WITH
40+
2.4.0

README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,70 @@ client = Cohere::Client.new(
3030
```
3131
### Generate
3232
```ruby
33-
client.generate()
33+
client.generate(
34+
prompt: "Once upon a time in a magical land called"
35+
)
3436
```
3537

3638
### Embed
3739
```ruby
38-
client.embed()
40+
client.embed(
41+
texts: ["hello!"]
42+
)
3943
```
4044

4145
### Classify
4246
```ruby
43-
client.classify()
47+
examples = [
48+
{ text: "Dermatologists don't like her!", label: "Spam" },
49+
{ text: "Hello, open to this?", label: "Spam" },
50+
{ text: "I need help please wire me $1000 right now", label: "Spam" },
51+
{ text: "Nice to know you ;)", label: "Spam" },
52+
{ text: "Please help me?", label: "Spam" },
53+
{ text: "Your parcel will be delivered today", label: "Not spam" },
54+
{ text: "Review changes to our Terms and Conditions", label: "Not spam" },
55+
{ text: "Weekly sync notes", label: "Not spam" },
56+
{ text: "Re: Follow up from today's meeting", label: "Not spam" },
57+
{ text: "Pre-read for tomorrow", label: "Not spam" }
58+
]
59+
60+
inputs = [
61+
"Confirm your email address",
62+
"hey i need u to send some $",
63+
]
64+
65+
client.classify(
66+
examples: examples,
67+
inputs: inputs
68+
)
4469
```
4570

4671
### Tokenize
4772
```ruby
48-
client.tokenize()
73+
client.tokenize(
74+
text: "hello world!"
75+
)
4976
```
5077

5178
### Detokenize
5279
```ruby
53-
client.detokenize()
80+
client.detokenize(
81+
tokens: [33555, 1114 , 34]
82+
)
5483
```
5584

5685
### Detect language
5786
```ruby
58-
client.detect_language()
87+
client.detect_language(
88+
texts: ["Здравствуй, Мир"]
89+
)
5990
```
6091

6192
### Summarize
6293
```ruby
63-
client.summarize()
94+
client.summarize(
95+
text: "..."
96+
)
6497
```
6598

6699
## Development

bin/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require "cohere"
1212
# Pry.start
1313

1414
client = Cohere::Client.new(
15-
api_key: ENV['COHERE_API_KEY']
15+
api_key: "8wobyic1avYNGy4QV8Nm5sUyM2API8UgCUOZNwNP" #ENV['COHERE_API_KEY']
1616
)
1717

1818
require "irb"

lib/cohere/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "faraday"
44

5-
module Weaviate
5+
module Cohere
66
class Client
77
attr_reader :api_key, :connection
88

@@ -102,7 +102,6 @@ def detect_language(texts:)
102102
end
103103
response.body
104104
end
105-
end
106105

107106
def summarize(
108107
text:,
@@ -122,6 +121,7 @@ def summarize(
122121
req.body[:temperature] = temperature if temperature
123122
req.body[:additional_command] = additional_command if additional_command
124123
end
124+
response.body
125125
end
126126

127127
private

lib/cohere/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Cohere
4-
VERSION = "0.1.0"
4+
VERSION = "0.9.0"
55
end

spec/cohere/client_spec.rb

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,156 @@
44

55
RSpec.describe Cohere::Client do
66
let(:instance) { described_class.new(api_key: "123") }
7+
8+
describe "#generate" do
9+
let(:generate_result) { JSON.parse(File.read("spec/fixtures/generate_result.json")) }
10+
let(:response) { OpenStruct.new(body: generate_result) }
11+
12+
before do
13+
allow_any_instance_of(Faraday::Connection).to receive(:post)
14+
.with("generate")
15+
.and_return(response)
16+
end
17+
18+
it "returns a response" do
19+
expect(instance.generate(
20+
prompt: "Once upon a time in a magical land called"
21+
).dig('generations').first.dig('text')).to eq(" The Past there was a Game called Warhammer Fantasy Battle.")
22+
end
23+
end
24+
25+
describe "#embed" do
26+
let(:embed_result) { JSON.parse(File.read("spec/fixtures/embed_result.json")) }
27+
let(:response) { OpenStruct.new(body: embed_result) }
28+
29+
before do
30+
allow_any_instance_of(Faraday::Connection).to receive(:post)
31+
.with("embed")
32+
.and_return(response)
33+
end
34+
35+
it "returns a response" do
36+
expect(instance.embed(
37+
texts: ["hello!"]
38+
).dig('embeddings')).to eq([[ 1.2177734, 0.67529297, 2.0742188 ]])
39+
end
40+
end
41+
42+
describe "#classify" do
43+
let(:classify_result) { JSON.parse(File.read("spec/fixtures/classify_result.json")) }
44+
let(:response) { OpenStruct.new(body: classify_result) }
45+
46+
let(:inputs) {[
47+
{ text: "Dermatologists don't like her!", label: "Spam" },
48+
{ text: "Hello, open to this?", label: "Spam" }
49+
]}
50+
51+
let(:examples) {[
52+
"Confirm your email address",
53+
"hey i need u to send some $"
54+
]}
55+
56+
before do
57+
allow_any_instance_of(Faraday::Connection).to receive(:post)
58+
.with("classify")
59+
.and_return(response)
60+
end
61+
62+
it "returns a response" do
63+
res = instance.classify(
64+
inputs: inputs,
65+
examples: examples
66+
).dig('classifications')
67+
68+
expect(res.first.dig('prediction')).to eq("Not spam")
69+
expect(res.last.dig('prediction')).to eq("Spam")
70+
end
71+
end
72+
73+
describe "#tokenize" do
74+
let(:tokenize_result) { JSON.parse(File.read("spec/fixtures/tokenize_result.json")) }
75+
let(:response) { OpenStruct.new(body: tokenize_result) }
76+
77+
before do
78+
allow_any_instance_of(Faraday::Connection).to receive(:post)
79+
.with("tokenize")
80+
.and_return(response)
81+
end
82+
83+
it "returns a response" do
84+
expect(instance.tokenize(
85+
text: "Hello, world!"
86+
).dig('tokens')).to eq([33555, 1114 , 34])
87+
end
88+
end
89+
90+
describe "#detokenize" do
91+
let(:detokenize_result) { JSON.parse(File.read("spec/fixtures/detokenize_result.json")) }
92+
let(:response) { OpenStruct.new(body: detokenize_result) }
93+
94+
before do
95+
allow_any_instance_of(Faraday::Connection).to receive(:post)
96+
.with("detokenize")
97+
.and_return(response)
98+
end
99+
100+
it "returns a response" do
101+
expect(instance.detokenize(
102+
tokens: [33555, 1114 , 34]
103+
).dig('text')).to eq("hello world!")
104+
end
105+
end
106+
107+
describe "#detect_language" do
108+
let(:detect_language_result) { JSON.parse(File.read("spec/fixtures/detect_language_result.json")) }
109+
let(:response) { OpenStruct.new(body: detect_language_result) }
110+
111+
before do
112+
allow_any_instance_of(Faraday::Connection).to receive(:post)
113+
.with("detect-language")
114+
.and_return(response)
115+
end
116+
117+
it "returns a response" do
118+
expect(instance.detect_language(
119+
texts: ["Здравствуй, Мир"]
120+
).dig('results').first.dig('language_code')).to eq("ru")
121+
end
122+
end
123+
124+
describe "#summarize" do
125+
let(:summarize_result) { JSON.parse(File.read("spec/fixtures/summarize_result.json")) }
126+
let(:response) { OpenStruct.new(body: summarize_result) }
127+
128+
before do
129+
allow_any_instance_of(Faraday::Connection).to receive(:post)
130+
.with("summarize")
131+
.and_return(response)
132+
end
133+
134+
it "returns a response" do
135+
expect(instance.summarize(
136+
text: "Ice cream is a sweetened frozen food typically eaten as a snack or dessert. " +
137+
"It may be made from milk or cream and is flavoured with a sweetener, " +
138+
"either sugar or an alternative, and a spice, such as cocoa or vanilla, " +
139+
"or with fruit such as strawberries or peaches. " +
140+
"It can also be made by whisking a flavored cream base and liquid nitrogen together. " +
141+
"Food coloring is sometimes added, in addition to stabilizers. " +
142+
"The mixture is cooled below the freezing point of water and stirred to incorporate air spaces " +
143+
"and to prevent detectable ice crystals from forming. The result is a smooth, " +
144+
"semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). " +
145+
"It becomes more malleable as its temperature increases.\n\n" +
146+
"The meaning of the name \"ice cream\" varies from one country to another. " +
147+
"In some countries, such as the United States, \"ice cream\" applies only to a specific variety, " +
148+
"and most governments regulate the commercial use of the various terms according to the " +
149+
"relative quantities of the main ingredients, notably the amount of cream. " +
150+
"Products that do not meet the criteria to be called ice cream are sometimes labelled " +
151+
"\"frozen dairy dessert\" instead. In other countries, such as Italy and Argentina, " +
152+
"one word is used fo\r all variants. Analogues made from dairy alternatives, " +
153+
"such as goat's or sheep's milk, or milk substitutes " +
154+
"(e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are " +
155+
"lactose intolerant, allergic to dairy protein or vegan."
156+
).dig('summary')).to eq("Ice cream is a frozen dessert made from dairy products or non-dairy substitutes. It is flavoured with a sweetener and a spice or with fruit. It is smooth and semi-solid at low temperatures.")
157+
end
158+
end
7159
end

spec/cohere_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44
it "has a version number" do
55
expect(Cohere::VERSION).not_to be nil
66
end
7-
8-
it "does something useful" do
9-
expect(false).to eq(true)
10-
end
117
end

spec/fixtures/classify_result.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"id": "7b8be981-cb89-4d16-9a2c-dec3a2b0f71d",
3+
"classifications": [
4+
{
5+
"id": "c33e7cf7-1d5f-41f8-916b-20107b73fca8",
6+
"input": "Confirm your email address",
7+
"prediction": "Not spam",
8+
"confidence": 0.80833024,
9+
"labels": {
10+
"Not spam": {
11+
"confidence": 0.80833024
12+
},
13+
"Spam": {
14+
"confidence": 0.19166975
15+
}
16+
}
17+
},
18+
{
19+
"id": "18384c67-9cab-4960-9fc3-ca577586701b",
20+
"input": "hey i need u to send some $",
21+
"prediction": "Spam",
22+
"confidence": 0.9893047,
23+
"labels": {
24+
"Not spam": {
25+
"confidence": 0.010695281
26+
},
27+
"Spam": {
28+
"confidence": 0.9893047
29+
}
30+
}
31+
}
32+
],
33+
"meta": {
34+
"api_version": {
35+
"version": "1"
36+
}
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "a3fda54a-303f-4bfe-9649-d677fedf8d0d",
3+
"results": [
4+
{
5+
"language_code": "ru",
6+
"language_name": "Russian"
7+
}
8+
],
9+
"meta": {
10+
"api_version": {
11+
"version": "1"
12+
}
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"text": "hello world!",
3+
"meta": {
4+
"api_version": {
5+
"version": "1"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)