1
0
mirror of synced 2026-08-05 15:26:55 +00:00
Files

260 lines
8.7 KiB
Ruby
Raw Permalink Normal View History

2023-04-19 11:55:59 -03:00
# frozen_string_literal: true
require "rails_helper"
describe DiscourseAi::Tokenizer::BertTokenizer do
2023-04-19 11:55:59 -03:00
describe "#size" do
describe "returns a token count" do
it "for a single word" do
expect(described_class.size("hello")).to eq(3)
2023-04-19 11:55:59 -03:00
end
it "for a sentence" do
expect(described_class.size("hello world")).to eq(4)
2023-04-19 11:55:59 -03:00
end
it "for a sentence with punctuation" do
expect(described_class.size("hello, world!")).to eq(6)
2023-04-19 11:55:59 -03:00
end
it "for a sentence with punctuation and capitalization" do
expect(described_class.size("Hello, World!")).to eq(6)
2023-04-19 11:55:59 -03:00
end
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
2023-04-19 11:55:59 -03:00
end
end
end
describe "#tokenizer" do
it "returns a tokenizer" do
expect(described_class.tokenizer).to be_a(Tokenizers::Tokenizer)
2023-04-19 11:55:59 -03:00
end
it "returns the same tokenizer" do
expect(described_class.tokenizer).to eq(described_class.tokenizer)
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar")
end
end
end
describe DiscourseAi::Tokenizer::AnthropicTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(5)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar baz")
end
end
end
describe DiscourseAi::Tokenizer::OpenAiTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(6)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar baz")
2023-04-19 11:55:59 -03:00
end
it "truncates a sentence successfully at a multibyte unicode character" do
sentence = "foo bar 👨🏿‍👩🏿‍👧🏿‍👧🏿 baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 7)).to eq("foo bar 👨🏿")
end
2024-03-14 17:33:30 -03:00
it "truncates unicode characters properly when they use more than one token per char" do
sentence = "我喜欢吃比萨"
original_size = described_class.size(sentence)
expect(described_class.size(described_class.truncate(sentence, original_size - 1))).to be <
original_size
end
end
2024-10-25 11:51:17 -03:00
describe "#below_limit?" do
2024-03-14 17:33:30 -03:00
it "returns true when the tokens can be expanded" do
2024-10-25 11:51:17 -03:00
expect(described_class.below_limit?("foo bar baz qux", 6)).to eq(true)
2024-03-14 17:33:30 -03:00
end
it "returns false when the tokens cannot be expanded" do
2024-10-25 11:51:17 -03:00
expect(described_class.below_limit?("foo bar baz qux", 3)).to eq(false)
2024-03-14 17:33:30 -03:00
end
it "returns false when the tokens cannot be expanded due to multibyte unicode characters" do
2024-10-25 11:51:17 -03:00
expect(described_class.below_limit?("foo bar 👨🏿 baz qux", 6)).to eq(false)
2024-03-14 17:33:30 -03:00
end
it "handles unicode characters properly when they use more than one token per char" do
2024-10-25 11:51:17 -03:00
expect(described_class.below_limit?("我喜欢吃比萨萨", 10)).to eq(false)
2024-03-14 17:33:30 -03:00
end
2023-04-19 11:55:59 -03:00
end
end
2023-07-14 11:37:21 -03:00
2024-07-22 15:26:14 -03:00
describe DiscourseAi::Tokenizer::OpenAiGpt4oTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(6)
end
end
end
end
2023-07-14 11:37:21 -03:00
describe DiscourseAi::Tokenizer::AllMpnetBaseV2Tokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar")
end
end
end
2023-07-27 13:55:32 -03:00
describe DiscourseAi::Tokenizer::MultilingualE5LargeTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo")
end
end
end
describe DiscourseAi::Tokenizer::BgeLargeEnTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar")
end
end
end
describe DiscourseAi::Tokenizer::BgeM3Tokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo")
end
it "truncates a sentence successfully at a multibyte unicode character" do
sentence = "foo bar 👨🏿‍👩🏿‍👧🏿‍👧🏿 baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 7)).to eq("foo bar 👨🏿")
end
it "truncates unicode characters properly when they use more than one token per char" do
sentence = "我喜欢吃比萨"
original_size = described_class.size(sentence)
expect(described_class.size(described_class.truncate(sentence, original_size - 2))).to be <
original_size
end
end
end
2024-05-13 12:45:52 -03:00
describe DiscourseAi::Tokenizer::Llama3Tokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(7)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar")
end
# Llama3 fails here
# it "truncates a sentence successfully at a multibyte unicode character" do
# sentence = "foo bar 👨🏿‍👩🏿‍👧🏿‍👧🏿 baz qux quux corge grault garply waldo fred plugh xyzzy thud"
# expect(described_class.truncate(sentence, 8)).to eq("foo bar 👨🏿")
# end
it "truncates unicode characters properly when they use more than one token per char" do
sentence = "我喜欢吃比萨"
original_size = described_class.size(sentence)
expect(described_class.size(described_class.truncate(sentence, original_size - 2))).to be <
original_size
end
end
end
2025-01-23 18:20:35 -03:00
describe DiscourseAi::Tokenizer::GeminiTokenizer do
describe "#size" do
describe "returns a token count" do
it "for a sentence with punctuation and capitalization and numbers" do
expect(described_class.size("Hello, World! 123")).to eq(9)
end
end
end
describe "#truncate" do
it "truncates a sentence" do
sentence = "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 3)).to eq("foo bar")
end
it "truncates a sentence successfully at a multibyte unicode character" do
sentence = "foo bar 👨🏿‍👩🏿‍👧🏿‍👧🏿 baz qux quux corge grault garply waldo fred plugh xyzzy thud"
expect(described_class.truncate(sentence, 8)).to eq("foo bar 👨🏿‍👩")
end
it "truncates unicode characters properly when they use more than one token per char" do
sentence = "我喜欢吃比萨"
original_size = described_class.size(sentence)
expect(described_class.size(described_class.truncate(sentence, original_size - 2))).to be <
original_size
end
end
end