Skip to content

Commit 8579092

Browse files
Add Qdrant generator (#21)
1 parent 28ea2c9 commit 8579092

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ rails generate langchainrb_rails:pgvector --model=Product --llm=openai
115115
rails generate langchainrb_rails:pinecone --model=Product --llm=openai
116116
```
117117

118+
### Qdrant Generator - adds vectorsearch to your ActiveRecord model
119+
```bash
120+
rails generate langchainrb_rails:qdrant --model=Product --llm=openai
121+
```
122+
118123
Available `--llm` options: `cohere`, `google_palm`, `hugging_face`, `llama_cpp`, `ollama`, `openai`, and `replicate`. The selected LLM will be used to generate embeddings and completions.
119124

120125
The `--model` option is used to specify which ActiveRecord model vectorsearch capabilities will be added to.

lib/langchainrb_rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Generators
1919
autoload :BaseGenerator, "langchainrb_rails/generators/langchainrb_rails/base_generator"
2020
autoload :ChromaGenerator, "langchainrb_rails/generators/langchainrb_rails/chroma_generator"
2121
autoload :PgvectorGenerator, "langchainrb_rails/generators/langchainrb_rails/pgvector_generator"
22+
autoload :QdrantGenerator, "langchainrb_rails/generators/langchainrb_rails/qdrant_generator"
2223
end
2324

2425
class << self

lib/langchainrb_rails/active_record/hooks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def similarity_search(query, k: 1)
112112
# @return [String] The answer to the question
113113
def ask(question, k: 4, &block)
114114
class_variable_get(:@@provider).ask(
115-
question,
115+
question: question,
116116
k: k,
117117
&block
118118
).completion
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
module LangchainrbRails
4+
module Generators
5+
#
6+
# PineconeGenerator does the following:
7+
# 1. Creates the `langchainrb_rails.rb` initializer file
8+
# 2. Adds necessary code to the ActiveRecord model to enable vectorsearch
9+
# 3. Adds `qdrant-ruby` gem to the Gemfile
10+
#
11+
# Usage:
12+
# rails generate langchainrb_rails:qdrant --model=Product --llm=openai
13+
#
14+
class QdrantGenerator < LangchainrbRails::Generators::BaseGenerator
15+
desc "This generator adds Qdrant vectorsearch integration to your ActiveRecord model"
16+
source_root File.join(__dir__, "templates")
17+
18+
# Creates the `langchainrb_rails.rb` initializer file
19+
def create_initializer_file
20+
template "qdrant_initializer.rb", "config/initializers/langchainrb_rails.rb"
21+
end
22+
23+
# Adds `vectorsearch` class method to the model and `after_save` callback that calls `upsert_to_vectorsearch()`
24+
def add_to_model
25+
inject_into_class "app/models/#{model_name.downcase}.rb", model_name do
26+
" vectorsearch\n\n after_save :upsert_to_vectorsearch\n\n"
27+
end
28+
end
29+
30+
# Adds `qdrant-ruby` gem to the Gemfile
31+
# TODO: Can we automatically run `bundle install`?
32+
def add_to_gemfile
33+
gem "qdrant-ruby"
34+
end
35+
36+
private
37+
38+
# @return [String] Name of the model
39+
def model_name
40+
options["model"]
41+
end
42+
43+
# @return [String] LLM provider to use
44+
def llm
45+
options["llm"]
46+
end
47+
48+
# @return [Langchain::LLM::*] LLM class
49+
def llm_class
50+
Langchain::LLM.const_get(LLMS[llm])
51+
end
52+
end
53+
end
54+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
LangchainrbRails.configure do |config|
4+
config.vectorsearch = Langchain::Vectorsearch::Qdrant.new(
5+
llm: <%= llm_class %>.new(api_key: ENV["<%= llm.upcase %>_API_KEY"]),
6+
url: ENV["QDRANT_URL"],
7+
api_key: ENV["QDRANT_API_KEY"],
8+
index_name: ""
9+
)
10+
end

lib/langchainrb_rails/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Railtie < Rails::Railtie
1212
require_relative "generators/langchainrb_rails/chroma_generator"
1313
require_relative "generators/langchainrb_rails/pinecone_generator"
1414
require_relative "generators/langchainrb_rails/pgvector_generator"
15+
require_relative "generators/langchainrb_rails/qdrant_generator"
1516
end
1617
end
1718
end

0 commit comments

Comments
 (0)