spectre_ai 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/README.md +3 -3
- data/lib/generators/spectre/templates/spectre_initializer.rb +2 -0
- data/lib/spectre/prompt.rb +63 -3
- data/lib/spectre/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a1b57e957fd8d44c84db209bc46d0c86a7be77a37d5f0f9ca726c2f2622a539
|
4
|
+
data.tar.gz: b09d880f7f80e918c229caba2d5754e2dcf96ca9afeb1ca3091e7db0232420c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91d020445d05ca703d78b6f0fda842a9f92c55b4c67d8b201accdad8fe352ccec2e4e28e36b157ae2a82f86f574d0873ddf23ff4ebbf7c15f707a2c467c47532
|
7
|
+
data.tar.gz: 48eb3d8f6339d999ff7386e32de81cb01008ceb3245fb9fc2e150311845779e1f5c788198c35710d20584100085974035fd1730f1c093d8634202a45d9b3756f
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Changelog for Version 1.0.1
|
2
|
+
|
3
|
+
**Release Date:** [19th Sep 2024]
|
4
|
+
|
5
|
+
**Improvements to Prompt Class**
|
6
|
+
|
7
|
+
1. Escaping Special Characters in YAML/ERB:
|
8
|
+
|
9
|
+
• Implemented automatic escaping of special characters in YAML/ERB templates, such as &, <, >, ", and ' to prevent parsing issues.
|
10
|
+
|
11
|
+
• Special characters are escaped before rendering the YAML file to ensure compatibility with YAML parsers.
|
12
|
+
|
13
|
+
• After rendering, the escaped characters are converted back to their original form, ensuring the final output is clean and readable.
|
14
|
+
|
15
|
+
• This update prevents errors that occur when rendering templates with complex queries or strings containing special characters.
|
16
|
+
|
17
|
+
Example:
|
18
|
+
|
19
|
+
```yaml
|
20
|
+
user: |
|
21
|
+
User's query: <%= @query %>
|
22
|
+
Context: <%= @responses.join(", ") %>
|
23
|
+
```
|
24
|
+
|
25
|
+
Before this change, queries or responses containing special characters might have caused YAML parsing errors. This update ensures that even complex strings are handled safely and returned in their original form.
|
26
|
+
|
27
|
+
To upgrade, update your Gemfile to version 1.0.1 and run bundle install. Make sure your YAML/ERB templates do not manually escape special characters anymore, as the Prompt class will handle it automatically.
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Spectre
|
1
|
+
# Spectre [![Gem Version](https://badge.fury.io/rb/spectre_ai.svg)](https://badge.fury.io/rb/spectre_ai)
|
2
2
|
|
3
3
|
**Spectre** is a Ruby gem that makes it easy to AI-enable your Ruby on Rails application. Currently, Spectre focuses on helping developers create embeddings, perform vector-based searches, create chat completions, and manage dynamic prompts — ideal for applications that are featuring RAG (Retrieval-Augmented Generation), chatbots and dynamic prompts.
|
4
4
|
|
@@ -18,7 +18,7 @@
|
|
18
18
|
Add this line to your application's Gemfile:
|
19
19
|
|
20
20
|
```ruby
|
21
|
-
gem '
|
21
|
+
gem 'spectre_ai'
|
22
22
|
```
|
23
23
|
|
24
24
|
And then execute:
|
@@ -30,7 +30,7 @@ bundle install
|
|
30
30
|
Or install it yourself as:
|
31
31
|
|
32
32
|
```bash
|
33
|
-
gem install
|
33
|
+
gem install spectre_ai
|
34
34
|
```
|
35
35
|
|
36
36
|
## Usage
|
data/lib/spectre/prompt.rb
CHANGED
@@ -19,13 +19,26 @@ module Spectre
|
|
19
19
|
|
20
20
|
raise "Prompt file not found: #{file_path}" unless File.exist?(file_path)
|
21
21
|
|
22
|
+
# Preprocess the locals before rendering the YAML file
|
23
|
+
preprocessed_locals = preprocess_locals(locals)
|
24
|
+
|
22
25
|
template_content = File.read(file_path)
|
23
26
|
erb_template = ERB.new(template_content)
|
24
27
|
|
25
|
-
context = Context.new(
|
28
|
+
context = Context.new(preprocessed_locals)
|
26
29
|
rendered_prompt = erb_template.result(context.get_binding)
|
27
30
|
|
28
|
-
YAML.safe_load
|
31
|
+
# YAML.safe_load returns a hash, so fetch the correct part based on the prompt
|
32
|
+
parsed_yaml = YAML.safe_load(rendered_prompt)[prompt]
|
33
|
+
|
34
|
+
# Convert special characters back after YAML processing
|
35
|
+
convert_special_chars_back(parsed_yaml)
|
36
|
+
rescue Errno::ENOENT
|
37
|
+
raise "Template file not found at path: #{file_path}"
|
38
|
+
rescue Psych::SyntaxError => e
|
39
|
+
raise "YAML Syntax Error in file #{file_path}: #{e.message}"
|
40
|
+
rescue StandardError => e
|
41
|
+
raise "Error rendering prompt for template '#{template}': #{e.message}"
|
29
42
|
end
|
30
43
|
|
31
44
|
private
|
@@ -48,7 +61,54 @@ module Spectre
|
|
48
61
|
"#{PROMPTS_PATH}/#{type}/#{prompt}.yml.erb"
|
49
62
|
end
|
50
63
|
|
51
|
-
#
|
64
|
+
# Preprocess locals recursively to escape special characters in strings
|
65
|
+
#
|
66
|
+
# @param value [Object] The value to process (string, array, hash, etc.)
|
67
|
+
# @return [Object] Processed value with special characters escaped
|
68
|
+
def self.preprocess_locals(value)
|
69
|
+
case value
|
70
|
+
when String
|
71
|
+
escape_special_chars(value)
|
72
|
+
when Hash
|
73
|
+
value.transform_values { |v| preprocess_locals(v) } # Recurse into hash values
|
74
|
+
when Array
|
75
|
+
value.map { |item| preprocess_locals(item) } # Recurse into array items
|
76
|
+
else
|
77
|
+
value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Escape special characters in strings to avoid YAML parsing issues
|
82
|
+
#
|
83
|
+
# @param value [String] The string to process
|
84
|
+
# @return [String] The processed string with special characters escaped
|
85
|
+
def self.escape_special_chars(value)
|
86
|
+
value.gsub('&', '&')
|
87
|
+
.gsub('<', '<')
|
88
|
+
.gsub('>', '>')
|
89
|
+
.gsub('"', '"')
|
90
|
+
.gsub("'", ''')
|
91
|
+
.gsub("\n", '\\n')
|
92
|
+
.gsub("\r", '\\r')
|
93
|
+
.gsub("\t", '\\t')
|
94
|
+
end
|
95
|
+
|
96
|
+
# Convert special characters back to their original form after YAML processing
|
97
|
+
#
|
98
|
+
# @param value [String] The string to process
|
99
|
+
# @return [String] The processed string with original special characters restored
|
100
|
+
def self.convert_special_chars_back(value)
|
101
|
+
value.gsub('&', '&')
|
102
|
+
.gsub('<', '<')
|
103
|
+
.gsub('>', '>')
|
104
|
+
.gsub('"', '"')
|
105
|
+
.gsub(''', "'")
|
106
|
+
.gsub('\\n', "\n")
|
107
|
+
.gsub('\\r', "\r")
|
108
|
+
.gsub('\\t', "\t")
|
109
|
+
end
|
110
|
+
|
111
|
+
# Helper class to handle the binding for ERB template rendering
|
52
112
|
class Context
|
53
113
|
def initialize(locals)
|
54
114
|
locals.each do |key, value|
|
data/lib/spectre/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spectre_ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Klapatok
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-09-
|
12
|
+
date: 2024-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec-rails
|