Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/1085] Add `Langchain::Tool::SearchApi`, a wrapper around [SearchApi](https://www.searchapi.io) to search Google, Bing and other search engines.
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/1083] `Langchain::LLM::Response::AnthropicResponse#prompt_tokens` (and `#total_tokens`) include the prompt-cache read and creation tokens that Anthropic reports apart from `input_tokens`; they were left out before.
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/1084] `Langchain::LLM::Response::GoogleGeminiResponse#completion_tokens` includes the thinking tokens (`thoughtsTokenCount`), so prompt plus completion tokens match `total_tokens` for thinking models.
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/1080] `require "langchain"` works outside Rails again: without `Rails::Engine` the gem sets up Zeitwerk itself. Since the move to a Rails engine it raised `NameError` (uninitialized constant Rails) in a plain Ruby script.
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/1079] `Langchain::DependencyHelper#depends_on` accepts a gem that's in the bundle through another gem's gemspec instead of the `Gemfile`, such as `aws-sdk-bedrockruntime` declared by a gem that depends on langchainrb; it raised `LoadError` before.
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/1078] Add `Langchain::Vectorsearch::Milvus#update_texts`; with the default `auto_id` schema Milvus gives each updated text a new id, returned under `"data" => "upsertIds"`.
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/1074] Add `Langchain::Vectorsearch::Pinecone#remove_texts`.
Expand Down
29 changes: 28 additions & 1 deletion lib/langchain.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
require "logger"
require "langchain/version"
require "langchain/engine"
if defined?(Rails::Engine)
require "langchain/engine"
else
# Outside a Rails app there's no engine to autoload the gem, so set up Zeitwerk with the
# same acronyms as config/initializers/inflections.rb
require "zeitwerk"

loader = Zeitwerk::Loader.for_gem
loader.ignore("#{__dir__}/langchainrb.rb", "#{__dir__}/langchain/engine.rb", "#{__dir__}/langchain/application_record.rb")
loader.inflector.inflect(
"ai21" => "AI21",
"ai21_response" => "AI21Response",
"csv" => "CSV",
"google_vertexai" => "GoogleVertexAI",
"html" => "HTML",
"json" => "JSON",
"jsonl" => "JSONL",
"llm" => "LLM",
"mistralai" => "MistralAI",
"mistralai_message" => "MistralAIMessage",
"mistralai_response" => "MistralAIResponse",
"openai" => "OpenAI",
"openai_message" => "OpenAIMessage",
"openai_response" => "OpenAIResponse",
"pdf" => "PDF"
)
loader.setup
end

module Langchain
class << self
Expand Down
15 changes: 15 additions & 0 deletions spec/langchain_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# frozen_string_literal: true

require "open3"

RSpec.describe Langchain do
it "has a version number" do
expect(Langchain::VERSION).not_to be nil
end

it "loads every class without Rails" do
script = <<~RUBY
require "langchain"
abort "Rails was loaded" if defined?(Rails)
Zeitwerk::Loader.eager_load_all
print Langchain::LLM::OpenAI.name, " ", Langchain::LLM::Response::OpenAIResponse.name
RUBY
output, status = Open3.capture2e(RbConfig.ruby, "-I", Langchain.root.to_s, "-e", script)

expect(status).to be_success, output
expect(output).to eq("Langchain::LLM::OpenAI Langchain::LLM::Response::OpenAIResponse")
end
end
Loading