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
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If applicable, add screenshots to help explain your problem.

- OS: [e.g. OS X, Linux, Ubuntu, Windows]
- Ruby version [e.g. 3.1, 3.2, 3.3]
- Langchain.rb version [e.g. 0.13.0]
- LangChain.rb version [e.g. 0.13.0]

**Additional context**
Add any other context about the problem here.
Add any other context about the problem here.
2 changes: 1 addition & 1 deletion .standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ ignore:
- "**/*":
- Style/ArgumentsForwarding

# Specify the minimum supported Ruby version supported by Langchain.rb.
# Specify the minimum supported Ruby version supported by LangChain.rb.
ruby_version: 3.1
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [SECURITY]: A change which fixes a security vulnerability.

## [Unreleased]
- [COMPAT] [https://github.com/patterns-ai-core/langchainrb/pull/1027] Rename `Langchain` to `LangChain`.
- [COMPAT] [https://github.com/patterns-ai-core/langchainrb/pull/980] Suppress a Ruby 3.4 warning for URI parser.
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/997] Remove `Langchain::Vectorsearch::Epsilla` class
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/1003] Response classes are now namespaced under `Langchain::LLM::Response`, converted to Rails engine
Expand Down
122 changes: 61 additions & 61 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Langchain::Engine.routes.draw do
LangChain::Engine.routes.draw do
end
6 changes: 3 additions & 3 deletions examples/assistant_chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# gem install reline
# or add `gem "reline"` to your Gemfile

openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
assistant = Langchain::Assistant.new(
openai = LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
assistant = LangChain::Assistant.new(
llm: openai,
instructions: "You are a Meteorologist Assistant that is able to pull the weather for any location",
tools: [
Langchain::Tool::Weather.new(api_key: ENV["OPEN_WEATHER_API_KEY"])
LangChain::Tool::Weather.new(api_key: ENV["OPEN_WEATHER_API_KEY"])
]
)

Expand Down
6 changes: 3 additions & 3 deletions examples/create_and_manage_few_shot_prompt_templates.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "langchain"

# Create a prompt with a few shot examples
prompt = Langchain::Prompt::FewShotPromptTemplate.new(
prompt = LangChain::Prompt::FewShotPromptTemplate.new(
prefix: "Write antonyms for the following words.",
suffix: "Input: {adjective}\nOutput:",
example_prompt: Langchain::Prompt::PromptTemplate.new(
example_prompt: LangChain::Prompt::PromptTemplate.new(
input_variables: ["input", "output"],
template: "Input: {input}\nOutput: {output}"
),
Expand Down Expand Up @@ -32,5 +32,5 @@
prompt.save(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json")

# Loading a new prompt template using a JSON file
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json")
prompt = LangChain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json")
prompt.prefix # "Write antonyms for the following words."
10 changes: 5 additions & 5 deletions examples/create_and_manage_prompt_templates.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
require "langchain"

# Create a prompt with one input variable
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke.", input_variables: ["adjective"])
prompt = LangChain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke.", input_variables: ["adjective"])
prompt.format(adjective: "funny") # "Tell me a funny joke."

# Create a prompt with multiple input variables
prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}.", input_variables: ["adjective", "content"])
prompt = LangChain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}.", input_variables: ["adjective", "content"])
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."

# Creating a PromptTemplate using just a prompt and no input_variables
prompt = Langchain::Prompt::PromptTemplate.from_template("Tell me a {adjective} joke about {content}.")
prompt = LangChain::Prompt::PromptTemplate.from_template("Tell me a {adjective} joke about {content}.")
prompt.input_variables # ["adjective", "content"]
prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens."

# Save prompt template to JSON file
prompt.save(file_path: "spec/fixtures/prompt/prompt_template.json")

# Loading a new prompt template using a JSON file
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json")
prompt = LangChain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json")
prompt.input_variables # ["adjective", "content"]

# Loading a new prompt template using a YAML file
prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml")
prompt = LangChain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml")
prompt.input_variables # ["adjective", "content"]
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
required: ["name", "age", "interests"],
additionalProperties: false
}
parser = Langchain::OutputParsers::StructuredOutputParser.from_json_schema(json_schema)
prompt = Langchain::Prompt::PromptTemplate.new(template: "Generate details of a fictional character.\n{format_instructions}\nCharacter description: {description}", input_variables: ["description", "format_instructions"])
parser = LangChain::OutputParsers::StructuredOutputParser.from_json_schema(json_schema)
prompt = LangChain::Prompt::PromptTemplate.new(template: "Generate details of a fictional character.\n{format_instructions}\nCharacter description: {description}", input_variables: ["description", "format_instructions"])
prompt.format(description: "Korean chemistry student", format_instructions: parser.get_format_instructions)
# Generate details of a fictional character.
# You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
Expand All @@ -58,7 +58,7 @@

# Character description: Korean chemistry student

llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm = LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm_response = llm.chat(
messages: [{
role: "user",
Expand Down Expand Up @@ -91,7 +91,7 @@
# ```
# RESPONSE

fix_parser = Langchain::OutputParsers::OutputFixingParser.from_llm(
fix_parser = LangChain::OutputParsers::OutputFixingParser.from_llm(
llm: llm,
parser: parser
)
Expand Down
4 changes: 2 additions & 2 deletions examples/ollama_inquire_about_image.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative "../lib/langchain"
require "faraday"

llm = Langchain::LLM::Ollama.new(default_options: {chat_model: "llava"})
llm = LangChain::LLM::Ollama.new(default_options: {chat_model: "llava"})

assistant = Langchain::Assistant.new(llm: llm)
assistant = LangChain::Assistant.new(llm: llm)

response = assistant.add_message_and_run(
image_url: "https://gist.githubusercontent.com/andreibondarev/b6f444194d0ee7ab7302a4d83184e53e/raw/099e10af2d84638211e25866f71afa7308226365/sf-cable-car.jpg",
Expand Down
4 changes: 2 additions & 2 deletions examples/openai_qdrant_function_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
}
]

openai = Langchain::LLM::OpenAI.new(
openai = LangChain::LLM::OpenAI.new(
api_key: ENV["OPENAI_API_KEY"],
default_options: {
chat_model: "gpt-3.5-turbo-16k"
}
)

client = Langchain::Vectorsearch::Qdrant.new(
client = LangChain::Vectorsearch::Qdrant.new(
url: ENV["QDRANT_URL"],
api_key: ENV["QDRANT_API_KEY"],
index_name: ENV["QDRANT_INDEX"],
Expand Down
10 changes: 5 additions & 5 deletions examples/pdf_store_and_query_with_chroma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# or add `gem "chroma-db", "~> 0.6.0"` to your Gemfile

# Instantiate the Chroma client
chroma = Langchain::Vectorsearch::Chroma.new(
chroma = LangChain::Vectorsearch::Chroma.new(
url: ENV["CHROMA_URL"],
index_name: "documents",
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm: LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
)

# Create the default schema.
Expand All @@ -20,9 +20,9 @@

# Set up an array of PDF and TXT documents
docs = [
Langchain.root.join("/docs/document.pdf"),
Langchain.root.join("/docs/document.txt"),
Langchain.root.join("/docs/document.docx")
LangChain.root.join("/docs/document.pdf"),
LangChain.root.join("/docs/document.txt"),
LangChain.root.join("/docs/document.docx")
]

# Add data to the index. Weaviate will use OpenAI to generate embeddings behind the scene.
Expand Down
4 changes: 2 additions & 2 deletions examples/store_and_query_with_milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# or add `gem "milvus"` to your Gemfile

# Instantiate the OpenAI client
openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
openai = LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])

# Instantiate the Milvus client
milvus = Langchain::Vectorsearch::Milvus.new(
milvus = LangChain::Vectorsearch::Milvus.new(
url: ENV["MILVUS_URL"],
index_name: "recipes",
llm: openai
Expand Down
4 changes: 2 additions & 2 deletions examples/store_and_query_with_pgvector_using_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
require "ruby/openai"

# Initialize the Pgvector client
pgvector = Langchain::Vectorsearch::Pgvector.new(
pgvector = LangChain::Vectorsearch::Pgvector.new(
url: ENV["POSTGRES_URL"],
index_name: "documents",
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm: LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
)

# Create the default schema if it doesn't exist
Expand Down
6 changes: 3 additions & 3 deletions examples/store_and_query_with_pinecone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# or add `gem "pinecone"` to your Gemfile

# Instantiate the Pinecone client
pinecone = Langchain::Vectorsearch::Pinecone.new(
pinecone = LangChain::Vectorsearch::Pinecone.new(
environment: ENV["PINECONE_ENVIRONMENT"],
api_key: ENV["PINECONE_API_KEY"],
index_name: "recipes",
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm: LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
)

# Create the default schema.
Expand Down Expand Up @@ -39,7 +39,7 @@
)

# Generate an embedding and search by it
openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
openai = LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
embedding = openai.embed(text: "veggie").embedding

pinecone.similarity_search_by_vector(
Expand Down
4 changes: 2 additions & 2 deletions examples/store_and_query_with_qdrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# or add `gem "qdrant-ruby"` to your Gemfile

# Instantiate the Qdrant client
qdrant = Langchain::Vectorsearch::Qdrant.new(
qdrant = LangChain::Vectorsearch::Qdrant.new(
url: ENV["QDRANT_URL"],
api_key: ENV["QDRANT_API_KEY"],
index_name: "recipes",
llm: Langchain::LLM::Cohere.new(api_key: ENV["COHERE_API_KEY"])
llm: LangChain::LLM::Cohere.new(api_key: ENV["COHERE_API_KEY"])
)

# Create the default schema.
Expand Down
4 changes: 2 additions & 2 deletions examples/store_and_query_with_weaviate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# or add `gem "weaviate-ruby"` to your Gemfile

# Instantiate the Weaviate client
weaviate = Langchain::Vectorsearch::Weaviate.new(
weaviate = LangChain::Vectorsearch::Weaviate.new(
url: ENV["WEAVIATE_URL"],
api_key: ENV["WEAVIATE_API_KEY"],
index_name: "Recipes",
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
llm: LangChain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
)

# Create the default schema. A text field `content` will be used.
Expand Down
6 changes: 3 additions & 3 deletions langchain.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ require_relative "lib/langchain/version"

Gem::Specification.new do |spec|
spec.name = "langchainrb"
spec.version = Langchain::VERSION
spec.version = LangChain::VERSION
spec.authors = ["Andrei Bondarev"]
spec.email = ["[email protected]"]

spec.summary = "Build LLM-backed Ruby applications with Ruby's Langchain.rb"
spec.description = "Build LLM-backed Ruby applications with Ruby's Langchain.rb"
spec.summary = "Build LLM-backed Ruby applications with Ruby's LangChain.rb"
spec.description = "Build LLM-backed Ruby applications with Ruby's LangChain.rb"
spec.homepage = "https://rubygems.org/gems/langchainrb"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.1.0"
Expand Down
13 changes: 11 additions & 2 deletions lib/langchain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "langchain/version"
require "langchain/engine"

module Langchain
module LangChain
class << self
# @return [Logger]
attr_accessor :logger
Expand Down Expand Up @@ -43,7 +43,7 @@ def colorize_logger_msg(msg, severity)
end

LOGGER_OPTIONS = {
progname: "Langchain.rb",
progname: "LangChain.rb",

formatter: ->(severity, time, progname, msg) do
Logger::Formatter.new.call(
Expand All @@ -58,3 +58,12 @@ def colorize_logger_msg(msg, severity)
self.logger ||= ::Logger.new($stdout, **LOGGER_OPTIONS)
@root = Pathname.new(__dir__)
end

module Langchain
def self.const_missing(name)
message = LangChain::Colorizer.yellow("`Langchain` is deprecated. Use `LangChain` instead.")
warn(message, uplevel: 1)

LangChain.const_get(name)
end
end
2 changes: 1 addition & 1 deletion lib/langchain/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Langchain
module LangChain
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Expand Down
Loading
Loading