active_prompt_rails 0.1.17 → 0.1.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -11
- data/lib/active_prompt/base.rb +1 -1
- data/lib/active_prompt/version.rb +1 -1
- data/lib/generators/active_prompt/prompt_generator.rb +5 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9b0ed834ff87bf6e5e43d1a513cebece5021afc6b4306dda7c4bd7b89038066
|
4
|
+
data.tar.gz: 5febc8f1d53ef6309da8d579c78c58bd6ab5b302f7dd1304e999005dd5e89caa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3b645c825c321dad775b564d1e2d7001b66fd8c94114f10dd1570ea3498f9e03af62b42f6009f9b73f7b4dad59cc8e457b7c4e238d7a5135dd631c480c4be4a
|
7
|
+
data.tar.gz: d4d0f0a9b498f3ff84ecb65e79a5ef3a8f2f3b674fff8b722c7b9442985c8aa715294e70454d9a420d59fd2fed0ef1efd4b7787e01c5d5236b502c58e61ca537
|
data/README.md
CHANGED
@@ -1,24 +1,79 @@
|
|
1
1
|
# ActivePrompt
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_prompt`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
ActivePrompt is a gem that helps you structure and manage LLM prompts in your rails apps.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
Add the following line to your Gemfile:
|
8
|
+
|
9
|
+
```Gemfile
|
10
|
+
gem "active_prompt_rails", "~> 0.1.18", require: "active_prompt"
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
and run `bundle install`
|
12
14
|
|
13
|
-
|
15
|
+
## Usage
|
14
16
|
|
15
|
-
|
17
|
+
When you want to create a new LLM prompt, you can generate one using the following command:
|
16
18
|
|
17
|
-
|
19
|
+
```bash
|
20
|
+
rails g active_prompt:prompt moderate_user_comment
|
21
|
+
```
|
18
22
|
|
19
|
-
|
23
|
+
This will create the following files in your rails project:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
app/prompts/moderate_user_comment_prompt.rb
|
27
|
+
app/prompts/templates/moderate_user_comment_prompt/system.liquid
|
28
|
+
app/prompts/templates/moderate_user_comment_prompt/user.liquid
|
29
|
+
```
|
30
|
+
|
31
|
+
You can add your system message and user message to the corresponding Liquid template files and include any dynamic variables you'd like, e.g.:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# /app/prompts/moderate_user_comment_prompt.rb
|
35
|
+
|
36
|
+
class ModerateUserCommentPrompt < ActivePrompt::Base
|
37
|
+
variable :comment
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
```liquid
|
42
|
+
# /app/prompts/templates/moderate_user_comment_prompt/system.liquid
|
43
|
+
|
44
|
+
You are a comment moderator agent that is responsible for reviewing and moderating user comments on a social media platform. You are assigned to reviewing comments and returning your decision on whether to approve or flag them for further review. Check for bullying, hate speech, and threatening language. Swearing is allowed as long as it's not harrassing. You return your decision in JSON format with the following structure:
|
45
|
+
{
|
46
|
+
"result": "approve" (or "flag"),
|
47
|
+
"reason": "optional reason for flagging the comment"
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
```liquid
|
52
|
+
# /app/prompts/templates/moderate_user_comment_prompt/user.liquid
|
53
|
+
|
54
|
+
Review this comment and return your decision in the specified JSON format:
|
55
|
+
|
56
|
+
{{ comment }}
|
57
|
+
```
|
58
|
+
|
59
|
+
Then within your application, you can use this prompt when calling an LLM as such:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
prompt = ModerateUserCommentPrompt.new(comment: user_comment)
|
20
63
|
|
21
|
-
|
64
|
+
client = OpenAI::Client.new
|
65
|
+
response = client.chat(
|
66
|
+
parameters: {
|
67
|
+
model: "gpt-4o",
|
68
|
+
response_format: { type: "json_object" },
|
69
|
+
messages: prompt.render_messages, # ActivePrompt::Base.render_messages returns OpenAI formatted message data
|
70
|
+
temperature: 1.0
|
71
|
+
}
|
72
|
+
)
|
73
|
+
content = response.dig("choices", 0, "message", "content")
|
74
|
+
data = JSON.parse(content)
|
75
|
+
puts data
|
76
|
+
```
|
22
77
|
|
23
78
|
## Development
|
24
79
|
|
@@ -28,4 +83,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
28
83
|
|
29
84
|
## Contributing
|
30
85
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shaneprrlt/active_prompt.
|
data/lib/active_prompt/base.rb
CHANGED
@@ -43,7 +43,7 @@ module ActivePrompt
|
|
43
43
|
private
|
44
44
|
|
45
45
|
def render_template(template_name)
|
46
|
-
template_path = File.join("app", "prompts", "#{self.class.name.underscore}", template_name)
|
46
|
+
template_path = File.join("app", "prompts", "templates", "#{self.class.name.underscore}", template_name)
|
47
47
|
template = File.read(template_path)
|
48
48
|
context = # Dynamic method call to get the variable value
|
49
49
|
self.class.variables.map do |var|
|
@@ -10,16 +10,14 @@ module ActivePrompt
|
|
10
10
|
source_root File.expand_path("templates", __dir__)
|
11
11
|
|
12
12
|
def create_prompt_files
|
13
|
-
empty_directory "app/prompts/#{name.underscore}_prompt"
|
14
|
-
template "system.liquid", "app/prompts/#{name.underscore}_prompt/system.liquid"
|
15
|
-
template "user.liquid", "app/prompts/#{name.underscore}_prompt/user.liquid"
|
13
|
+
empty_directory "app/prompts/templates/#{name.underscore}_prompt"
|
14
|
+
template "system.liquid", "app/prompts/templates/#{name.underscore}_prompt/system.liquid"
|
15
|
+
template "user.liquid", "app/prompts/templates/#{name.underscore}_prompt/user.liquid"
|
16
16
|
create_file "app/prompts/#{name.underscore}_prompt.rb", <<~RUBY
|
17
|
-
#
|
17
|
+
# frozen_string_literal: true
|
18
|
+
|
18
19
|
class #{name.camelize}Prompt < ActivePrompt::Base
|
19
|
-
# Define your required template variables here, e.g.
|
20
20
|
# variable :name
|
21
|
-
# variable :age
|
22
|
-
# variable :gender
|
23
21
|
end
|
24
22
|
RUBY
|
25
23
|
end
|