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 3 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
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"
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
8 changes: 8 additions & 0 deletions lib/langchain/chunker/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Langchain
module Chunker
class Base
end
end
end
27 changes: 27 additions & 0 deletions lib/langchain/chunker/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "baran"

module Langchain
module Chunker
class Text < Base
attr_reader :text, :chunk_size, :chunk_overlap, :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

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