Skip to content

Commit 710f5bd

Browse files
authored
Merge branch 'main' into extract_uri_parser
2 parents 6627612 + e1b9f39 commit 710f5bd

File tree

186 files changed

+1620
-719
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+1620
-719
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@
1616
.idea
1717
test.ann
1818
Gemfile.lock
19+
20+
/log/*.log
21+
test/dummy/db/*.sqlite3
22+
test/dummy/db/*.sqlite3-*
23+
test/dummy/log/*.log
24+
test/dummy/storage/
25+
test/dummy/tmp/
26+
*.keep
27+
28+
tags

.rspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
--format documentation
2-
--color
31
--require spec_helper
2+
--require rails_helper
3+
--format documentation
4+
--color

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby 3.4
1+
ruby 3.4.4

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
## [Unreleased]
1313
- [COMPAT] [https://github.com/patterns-ai-core/langchainrb/pull/980] Suppress a Ruby 3.4 warning for URI parser.
14+
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/997] Remove `Langchain::Vectorsearch::Epsilla` class
15+
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/1003] Response classes are now namespaced under `Langchain::LLM::Response`, converted to Rails engine
1416

1517
## [0.19.5]
1618
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/859] Add metadata support to PgVector storage
@@ -237,7 +239,7 @@
237239

238240
## [0.8.0]
239241
- [BREAKING] Updated llama_cpp.rb to 0.9.4. The model file format used by the underlying llama.cpp library has changed to GGUF. llama.cpp ships with scripts to convert existing files and GGUF format models can be downloaded from HuggingFace.
240-
- Introducing Langchain::LLM::GoogleVertexAi LLM provider
242+
- Introducing Langchain::LLM::GoogleVertexAI LLM provider
241243

242244
## [0.7.5] - 2023-11-13
243245
- Fixes

Gemfile

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

33
source "https://rubygems.org"
44

5-
# Specify your gem's dependencies in langchain.gemspec
5+
# Specify your gem's dependencies in langchain.gemspec.
66
gemspec
77

8-
gem "rake", "~> 13.0"
8+
group :test do
9+
gem "sqlite3"
10+
end
911

10-
gem "rspec", "~> 3.0"
11-
12-
gem "standard", ">= 1.35.1"
13-
# Lets add rubocop explicitly here, we are using only standardrb rules in .rubocop.yml
14-
gem "rubocop"
12+
# Development and test dependencies
13+
group :development, :test do
14+
gem "rspec", "~> 3.0"
15+
gem "rspec-rails"
16+
gem "standard", ">= 1.35.1"
17+
gem "rubocop"
18+
gem "rubocop-rails-omakase", require: false
19+
gem "rake", "~> 13.0"
20+
end

Rakefile

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
# frozen_string_literal: true
22

3-
require "bundler/gem_tasks"
4-
require "rspec/core/rake_task"
5-
require "standard/rake"
6-
require "yard"
7-
8-
RSpec::Core::RakeTask.new(:spec)
3+
require "bundler/setup"
94

10-
task default: :spec
5+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
116

12-
Rake::Task["spec"].enhance do
13-
Rake::Task["standard:fix"].invoke
14-
end
15-
16-
YARD::Rake::YardocTask.new do |t|
17-
end
7+
require "bundler/gem_tasks"

bin/console

Lines changed: 0 additions & 51 deletions
This file was deleted.

bin/rails

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env ruby
2+
# This command will automatically be run when you run "rails" with Rails gems
3+
# installed from the root of your application.
4+
5+
ENGINE_ROOT = File.expand_path("..", __dir__)
6+
ENGINE_PATH = File.expand_path("../lib/langchain/engine", __dir__)
7+
APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)
8+
9+
# Set up gems listed in the Gemfile.
10+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
11+
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
12+
13+
require "rails"
14+
# Pick the frameworks you want:
15+
# require "active_model/railtie"
16+
# require "active_job/railtie"
17+
# require "active_record/railtie"
18+
# require "active_storage/engine"
19+
# require "action_controller/railtie"
20+
# require "action_mailer/railtie"
21+
# require "action_mailbox/engine"
22+
# require "action_text/engine"
23+
# require "action_view/railtie"
24+
# require "action_cable/engine"
25+
# require "rails/test_unit/railtie"
26+
require "rails/engine/commands"

bin/rubocop

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
require "rubygems"
3+
require "bundler/setup"
4+
5+
# explicit rubocop config increases performance slightly while avoiding config confusion.
6+
ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__))
7+
8+
load Gem.bin_path("rubocop", "rubocop")

bin/setup

Lines changed: 0 additions & 8 deletions
This file was deleted.

config/initializers/inflections.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ActiveSupport::Inflector.inflections(:en) do |inflect|
2+
inflect.acronym "AI21"
3+
inflect.acronym "CSV"
4+
inflect.acronym "HTML"
5+
inflect.acronym "JSON"
6+
inflect.acronym "JSONL"
7+
inflect.acronym "LLM"
8+
inflect.acronym "PDF"
9+
inflect.acronym "OpenAI"
10+
inflect.acronym "OpenAIResponse"
11+
inflect.acronym "OpenAIMessage"
12+
inflect.acronym "MistralAI"
13+
inflect.acronym "MistralAIResponse"
14+
inflect.acronym "MistralAIMessage"
15+
inflect.acronym "GoogleVertexAI"
16+
inflect.acronym "VertexAI"
17+
inflect.acronym "AI21Response"
18+
inflect.acronym "AI21Validator"
19+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Langchain::Engine.routes.draw do
2+
end

langchain.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
1919
spec.metadata["changelog_uri"] = "https://github.com/patterns-ai-core/langchainrb/blob/main/CHANGELOG.md"
2020
spec.metadata["documentation_uri"] = "https://rubydoc.info/gems/langchainrb"
2121

22-
# Specify which files should be added to the gem when it is released.
23-
spec.files = Dir["LICENSE.txt", "README.md", "CHANGELOG.md", "lib/**/*"]
24-
spec.bindir = "exe"
25-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26-
spec.require_paths = ["lib"]
22+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
23+
Dir["{app,config,db,lib}/**/*", "LICENSE.txt", "Rakefile", "README.md"]
24+
end
25+
26+
spec.add_dependency "rails", ">= 6.1.0"
2727

2828
# dependencies
2929
# Not sure if we should require this as it only applies to OpenAI usecase.

lib/langchain.rb

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,7 @@
1-
# frozen_string_literal: true
2-
31
require "logger"
4-
require "pathname"
5-
require "zeitwerk"
6-
require "uri"
7-
require "json"
8-
9-
loader = Zeitwerk::Loader.for_gem
10-
loader.ignore("#{__dir__}/langchainrb.rb")
11-
loader.inflector.inflect(
12-
"ai21" => "AI21",
13-
"ai21_response" => "AI21Response",
14-
"ai21_validator" => "AI21Validator",
15-
"csv" => "CSV",
16-
"google_vertex_ai" => "GoogleVertexAI",
17-
"html" => "HTML",
18-
"json" => "JSON",
19-
"jsonl" => "JSONL",
20-
"llm" => "LLM",
21-
"mistral_ai" => "MistralAI",
22-
"mistral_ai_response" => "MistralAIResponse",
23-
"mistral_ai_message" => "MistralAIMessage",
24-
"openai" => "OpenAI",
25-
"openai_response" => "OpenAIResponse",
26-
"openai_message" => "OpenAIMessage",
27-
"pdf" => "PDF"
28-
)
29-
30-
loader.collapse("#{__dir__}/langchain/llm/response")
2+
require "langchain/version"
3+
require "langchain/engine"
314

32-
loader.setup
33-
34-
# Langchain.rb a is library for building LLM-backed Ruby applications. It is an abstraction layer that sits on top of the emerging AI-related tools that makes it easy for developers to consume and string those services together.
35-
#
36-
# = Installation
37-
# Install the gem and add to the application's Gemfile by executing:
38-
#
39-
# $ bundle add langchainrb
40-
#
41-
# If bundler is not being used to manage dependencies, install the gem by executing:
42-
#
43-
# $ gem install langchainrb
44-
#
45-
# Require the gem to start using it:
46-
#
47-
# require "langchain"
48-
#
49-
# = Concepts
50-
#
51-
# == Processors
52-
# Processors load and parse/process various data types such as CSVs, PDFs, Word documents, HTML pages, and others.
53-
#
54-
# == Chunkers
55-
# Chunkers split data based on various available options such as delimeters, chunk sizes or custom-defined functions. Chunkers are used when data needs to be split up before being imported in vector databases.
56-
#
57-
# == Prompts
58-
# Prompts are structured inputs to the LLMs. Prompts provide instructions, context and other user input that LLMs use to generate responses.
59-
#
60-
# == Large Language Models (LLMs)
61-
# LLM is a language model consisting of a neural network with many parameters (typically billions of weights or more), trained on large quantities of unlabeled text using self-supervised learning or semi-supervised learning.
62-
#
63-
# == Vectorsearch Databases
64-
# Vector database is a type of database that stores data as high-dimensional vectors, which are mathematical representations of features or attributes. Each vector has a certain number of dimensions, which can range from tens to thousands, depending on the complexity and granularity of the data.
65-
#
66-
# == Embedding
67-
# Word embedding or word vector is an approach with which we represent documents and words. It is defined as a numeric vector input that allows words with similar meanings to have the same representation. It can approximate meaning and represent a word in a lower dimensional space.
68-
#
69-
#
70-
# = Logging
71-
#
72-
# Langchain.rb uses standard logging mechanisms and defaults to :debug level. Most messages are at info level, but we will add debug or warn statements as needed. To show all log messages:
73-
#
74-
# Langchain.logger.level = :info
755
module Langchain
766
class << self
777
# @return [Logger]
@@ -126,6 +56,5 @@ def colorize_logger_msg(msg, severity)
12656
}.freeze
12757

12858
self.logger ||= ::Logger.new($stdout, **LOGGER_OPTIONS)
129-
13059
@root = Pathname.new(__dir__)
13160
end

lib/langchain/application_record.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Langchain
2+
class ApplicationRecord < ActiveRecord::Base
3+
self.abstract_class = true
4+
end
5+
end

lib/langchain/engine.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Langchain
2+
class Engine < ::Rails::Engine
3+
isolate_namespace Langchain
4+
5+
config.autoload_paths << root.join("lib")
6+
config.eager_load_paths << root.join("lib")
7+
end
8+
end

lib/langchain/llm/ai21.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def complete(prompt:, **params)
3737
parameters = complete_parameters params
3838

3939
response = client.complete(prompt, parameters)
40-
Langchain::LLM::AI21Response.new response, model: parameters[:model]
40+
Langchain::LLM::Response::AI21Response.new response, model: parameters[:model]
4141
end
4242

4343
#

lib/langchain/llm/anthropic.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def initialize(api_key:, llm_options: {}, default_options: {})
5757
# @param top_k [Integer] The top k value to use
5858
# @param metadata [Hash] The metadata to use
5959
# @param stream [Boolean] Whether to stream the response
60-
# @return [Langchain::LLM::AnthropicResponse] The completion
60+
# @return [Langchain::LLM::Response::AnthropicResponse] The completion
6161
def complete(
6262
prompt:,
6363
model: @defaults[:completion_model],
@@ -88,7 +88,7 @@ def complete(
8888
client.complete(parameters: parameters)
8989
end
9090

91-
Langchain::LLM::AnthropicResponse.new(response)
91+
Langchain::LLM::Response::AnthropicResponse.new(response)
9292
end
9393

9494
# Generate a chat completion for given messages
@@ -106,7 +106,7 @@ def complete(
106106
# @option params [Hash] :thinking Enable extended thinking mode, e.g. { type: "enabled", budget_tokens: 4000 }
107107
# @option params [Integer] :top_k Only sample from the top K options for each subsequent token
108108
# @option params [Float] :top_p Use nucleus sampling.
109-
# @return [Langchain::LLM::AnthropicResponse] The chat completion
109+
# @return [Langchain::LLM::Response::AnthropicResponse] The chat completion
110110
def chat(params = {}, &block)
111111
set_extra_headers! if params[:tools]
112112

@@ -129,7 +129,7 @@ def chat(params = {}, &block)
129129
response = response_from_chunks if block
130130
reset_response_chunks
131131

132-
Langchain::LLM::AnthropicResponse.new(response)
132+
Langchain::LLM::Response::AnthropicResponse.new(response)
133133
end
134134

135135
def with_api_error_handling

0 commit comments

Comments
 (0)