Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple text chunking #188

Merged
merged 7 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
langchainrb (0.5.5)
baran (~> 0.1.6)
colorize (~> 0.8.1)
tiktoken_ruby (~> 0.0.5)

Expand Down Expand Up @@ -32,6 +33,7 @@ GEM
afm (0.2.2)
ai21 (0.2.0)
ast (2.4.2)
baran (0.1.6)
builder (3.2.4)
byebug (11.1.3)
childprocess (4.1.0)
Expand Down
3 changes: 2 additions & 1 deletion langchain.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ Gem::Specification.new do |spec|

# dependencies
# Not sure if we should require this as it only applies to OpenAI usecase.
spec.add_dependency "tiktoken_ruby", "~> 0.0.5"
spec.add_dependency "baran", "~> 0.1.6"
spec.add_dependency "colorize", "~> 0.8.1"
spec.add_dependency "tiktoken_ruby", "~> 0.0.5"

# development dependencies
spec.add_development_dependency "dotenv-rails", "~> 2.7.6"
Expand Down
5 changes: 5 additions & 0 deletions lib/langchain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ module Agent
autoload :SQLQueryAgent, "langchain/agent/sql_query_agent/sql_query_agent.rb"
end

module Chunker
autoload :Base, "langchain/chunker/base"
autoload :Text, "langchain/chunker/text"
end

module Tool
autoload :Base, "langchain/tool/base"
autoload :Calculator, "langchain/tool/calculator"
Expand Down
15 changes: 15 additions & 0 deletions lib/langchain/chunker/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Langchain
module Chunker
# = Chunkers
# Chunkers are used to split documents into smaller chunks before indexing into vector search databases.
# Otherwise large documents, when retrieved and passed to LLMs, may hit the context window limits.
#
# == Available chunkers
#
# - {Langchain::Chunker::Text}
class Base
end
end
end
38 changes: 38 additions & 0 deletions lib/langchain/chunker/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "baran"

module Langchain
module Chunker
#
# Simple text chunker
#
# Usage:
# Langchain::Chunker::Text.new(text).chunks
#
class Text < Base
attr_reader :text, :chunk_size, :chunk_overlap, :separator

# @param [String] text
# @param [Integer] chunk_size
# @param [Integer] chunk_overlap
# @param [String] separator
def initialize(text, chunk_size: 1000, chunk_overlap: 200, separator: "\n\n")
@text = text
@chunk_size = chunk_size
@chunk_overlap = chunk_overlap
@separator = separator
end

# @return [Array<String>]
def chunks
splitter = Baran::CharacterTextSplitter.new(
chunk_size: chunk_size,
chunk_overlap: chunk_overlap,
separator: separator
)
splitter.chunks(text)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/langchain/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ def initialize(data, options = {})
def value
@data
end

def chunks(opts = {})
Langchain::Chunker::Text.new(@data, **opts).chunks
end
end
end
4 changes: 4 additions & 0 deletions spec/langchain/chunker/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

RSpec.describe Langchain::Chunker::Base do
end
29 changes: 29 additions & 0 deletions spec/langchain/chunker/text_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

RSpec.describe Langchain::Chunker::Text do
let(:source) { "spec/fixtures/loaders/example.txt" }
let(:text) { File.read(source) }

subject {
described_class.new(text,
chunk_size: 1000,
chunk_overlap: 200,
separator: "\n\n")
}

describe "#chunks" do
it "returns an array of chunks" do
expect(Baran::CharacterTextSplitter).to receive(:new).with(
chunk_size: 1000,
chunk_overlap: 200,
separator: "\n\n"
).and_call_original

allow_any_instance_of(Baran::CharacterTextSplitter).to receive(:chunks)
.with(text)
.and_call_original

subject.chunks
end
end
end
17 changes: 17 additions & 0 deletions spec/langchain/data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe Langchain::Data do
let(:source) { "spec/fixtures/loaders/example.txt" }
let(:data) { File.read(source) }

subject { described_class.new(data, source: source) }

describe "#chunks" do
it "returns an array of chunks" do
chunks = subject.chunks
expect(chunks[0]).to have_key(:text)
expect(chunks[1]).to have_key(:text)
expect(chunks[2]).to have_key(:text)
end
end
end