Skip to content

Commit 91e00eb

Browse files
Prompt template integration (#101)
* core functionality * prompt spec * renamed Prompt to Prompting took it out of the model folder due to naming collision with the actual AR model * copy change * rubocop & standardrb fixes * Update README.md * fixes * Fix spec * Code comments --------- Co-authored-by: Andrei Bondarev <[email protected]> Co-authored-by: Andrei Bondarev <[email protected]>
1 parent 37e8133 commit 91e00eb

File tree

12 files changed

+193
-2
lines changed

12 files changed

+193
-2
lines changed

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ gem "rake", "~> 13.2"
99

1010
gem "rspec", "~> 3.13"
1111

12-
gem "bundler-audit", require: false
1312
gem "brakeman", require: false
13+
gem "bundler-audit", require: false
1414
gem "rubocop", require: false
1515

1616
gem "standardrb"
1717

18+
gem "generator_spec"
1819
gem "langchainrb"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ GEM
103103
diff-lcs (1.5.1)
104104
drb (2.2.1)
105105
erubi (1.12.0)
106+
generator_spec (0.10.0)
107+
activesupport (>= 3.0.0)
108+
railties (>= 3.0.0)
106109
globalid (1.2.1)
107110
activesupport (>= 6.1)
108111
i18n (1.14.5)
@@ -294,6 +297,7 @@ PLATFORMS
294297
DEPENDENCIES
295298
brakeman
296299
bundler-audit
300+
generator_spec
297301
langchainrb
298302
langchainrb_rails!
299303
pry-byebug (~> 3.10.0)

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,20 @@ Pinecone Generator does the following:
129129
2. Adds necessary code to the ActiveRecord model to enable vectorsearch
130130
3. Adds `pinecone` gem to the Gemfile
131131

132+
### Prompt Generator - adds prompt templating capabilities to your ActiveRecord model
133+
```bash
134+
rails generate langchainrb_rails:prompt
135+
```
136+
137+
This generator adds the following files to your Rails project:
138+
1. An ActiveRecord `Prompt` model at `app/models/prompt.rb`
139+
2. A rails migration to create the `prompts` table
140+
141+
You can then use the `Prompt` model to create and manage prompts for your model.
142+
143+
Example usage:
144+
```ruby
145+
prompt = Prompt.create!(template: "Tell me a {adjective} joke about {subject}.")
146+
prompt.render(adjective: "funny", subject: "elephants")
147+
# => "Tell me a funny joke about elephants."
148+
```

lib/langchainrb_rails.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative "langchainrb_rails/version"
77
require "langchainrb_rails/railtie"
88
require "langchainrb_rails/config"
9+
require "langchainrb_rails/prompting"
910
require_relative "langchainrb_overrides/vectorsearch/pgvector"
1011

1112
module LangchainrbRails
@@ -20,6 +21,7 @@ module Generators
2021
autoload :ChromaGenerator, "langchainrb_rails/generators/langchainrb_rails/chroma_generator"
2122
autoload :PgvectorGenerator, "langchainrb_rails/generators/langchainrb_rails/pgvector_generator"
2223
autoload :QdrantGenerator, "langchainrb_rails/generators/langchainrb_rails/qdrant_generator"
24+
autoload :PromptGenerator, "langchainrb_rails/generators/langchainrb_rails/prompt_generator"
2325
end
2426

2527
class << self

lib/langchainrb_rails/generators/langchainrb_rails/base_generator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "rails/generators"
24
require "rails/generators/active_record"
35

@@ -18,7 +20,7 @@ class BaseGenerator < Rails::Generators::Base
1820
"ollama" => "Langchain::LLM::Ollama",
1921
"openai" => "Langchain::LLM::OpenAI",
2022
"replicate" => "Langchain::LLM::Replicate"
21-
}
23+
}.freeze
2224

2325
def post_install_message
2426
say "Please do the following to start Q&A with your #{model_name} records:", :green
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/generators"
4+
require "rails/generators/active_record"
5+
6+
module LangchainrbRails
7+
module Generators
8+
# Adds prompt templating capabilities to your ActiveRecord model
9+
#
10+
# Usage:
11+
# rails generate langchainrb_rails:prompt
12+
#
13+
class PromptGenerator < Rails::Generators::Base
14+
include ::ActiveRecord::Generators::Migration
15+
16+
source_root File.join(__dir__, "templates")
17+
18+
def create_prompt_model
19+
template "prompt_model.rb", "app/models/prompt.rb"
20+
migration_template "create_prompts.rb", "db/migrate/create_prompts.rb"
21+
end
22+
23+
def migration_version
24+
"[#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}]"
25+
end
26+
end
27+
end
28+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreatePrompts < ActiveRecord::Migration<%= migration_version %>
2+
def change
3+
create_table :prompts do |t|
4+
t.text :template, null: false
5+
6+
t.timestamps
7+
end
8+
end
9+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Prompt < ApplicationRecord
2+
include LangchainrbRails::Prompting
3+
end

lib/langchainrb_rails/prompting.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module LangchainrbRails
4+
module Prompting
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
validates :template, presence: true
9+
10+
def render(variables = {})
11+
template.gsub(/\{(\w+)\}/) do |match|
12+
variable = ::Regexp.last_match(1).to_sym
13+
variables.fetch(variable, match)
14+
end
15+
end
16+
17+
def template_variables
18+
template.scan(/\{(\w+)\}/).flatten.uniq
19+
end
20+
end
21+
end
22+
end

lib/langchainrb_rails/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Railtie < Rails::Railtie
1313
require_relative "generators/langchainrb_rails/pinecone_generator"
1414
require_relative "generators/langchainrb_rails/pgvector_generator"
1515
require_relative "generators/langchainrb_rails/qdrant_generator"
16+
require_relative "generators/langchainrb_rails/prompt_generator"
1617
end
1718
end
1819
end

0 commit comments

Comments
 (0)