Skip to content

Commit f7b6850

Browse files
Merge pull request #4 from jinshen1983/main
Add optional parameter model to tokenize/detokenize
2 parents e4f54a2 + 3fbfc1a commit f7b6850

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ GEM
7171

7272
PLATFORMS
7373
x86_64-darwin-19
74+
x86_64-darwin-21
7475
x86_64-linux
7576

7677
DEPENDENCIES

lib/cohere/client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ def classify(
8383
response.body
8484
end
8585

86-
def tokenize(text:)
86+
def tokenize(text:, model: nil)
8787
response = connection.post("tokenize") do |req|
88-
req.body = {text: text}
88+
req.body = model.nil? ? {text: text} : {text: text, model: model}
8989
end
9090
response.body
9191
end
9292

93-
def detokenize(tokens:)
93+
def detokenize(tokens:, model: nil)
9494
response = connection.post("detokenize") do |req|
95-
req.body = {tokens: tokens}
95+
req.body = model.nil? ? {tokens: tokens} : {tokens: tokens, model: model}
9696
end
9797
response.body
9898
end

spec/cohere/client_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@
9191
end
9292
end
9393

94+
describe "#tokenize_with_model" do
95+
let(:tokenize_result) { JSON.parse(File.read("spec/fixtures/tokenize_result.json")) }
96+
let(:response) { OpenStruct.new(body: tokenize_result) }
97+
98+
before do
99+
allow_any_instance_of(Faraday::Connection).to receive(:post)
100+
.with("tokenize")
101+
.and_return(response)
102+
end
103+
104+
it "returns a response" do
105+
expect(instance.tokenize(
106+
text: "Hello, world!",
107+
model: "base"
108+
).dig("tokens")).to eq([33555, 1114, 34])
109+
end
110+
end
111+
94112
describe "#detokenize" do
95113
let(:detokenize_result) { JSON.parse(File.read("spec/fixtures/detokenize_result.json")) }
96114
let(:response) { OpenStruct.new(body: detokenize_result) }
@@ -108,6 +126,24 @@
108126
end
109127
end
110128

129+
describe "#detokenize_with_model" do
130+
let(:detokenize_result) { JSON.parse(File.read("spec/fixtures/detokenize_result.json")) }
131+
let(:response) { OpenStruct.new(body: detokenize_result) }
132+
133+
before do
134+
allow_any_instance_of(Faraday::Connection).to receive(:post)
135+
.with("detokenize")
136+
.and_return(response)
137+
end
138+
139+
it "returns a response" do
140+
expect(instance.detokenize(
141+
tokens: [33555, 1114, 34],
142+
model: "base"
143+
).dig("text")).to eq("hello world!")
144+
end
145+
end
146+
111147
describe "#detect_language" do
112148
let(:detect_language_result) { JSON.parse(File.read("spec/fixtures/detect_language_result.json")) }
113149
let(:response) { OpenStruct.new(body: detect_language_result) }

0 commit comments

Comments
 (0)