-
Notifications
You must be signed in to change notification settings - Fork 0
erb
The --erb
options allows the aia
to run embedded Ruby code within the prompt text file.
The --erb
option turns the prompt text file into a fully functioning ERB template. The Embedded Ruby (ERB) template syntax (2024) provides a good overview of the syntax and power of ERB.
Ruby ERB (Embedded Ruby) is a system designed for interweaving Ruby code with text documents. This is especially useful for content that requires dynamic generation such as text files that undergo computation or data incorporation.
ERB boasts numerous powerful features:
-
Ruby Code Embedding: Insert and execute any valid Ruby code inside a text document. Whether it's performing calculations or invoking methods, ERB's capabilities are far-reaching.
-
Safe Evaluation: ERB templates can be evaluated within a
binding
that exposes only select variables and methods. This precaution is crucial when dealing with untrusted template content. -
Control Structures: Adopt Ruby control structures such as loops and conditionals within ERB, allowing conditional content inclusion.
-
User-Friendly: The ERB syntax is straightforward and blends seamlessly with usual text, providing a smooth experience for those familiar with HTML and similar languages.
-
Included in Ruby's Standard Library: ERB comes bundled with Ruby, negating the need for extra installations where Ruby is present.
-
Industry Standard: As the default engine for many Ruby frameworks, including Rails for views, ERB's reliability and support are well-established.
-
Error Reporting: ERB pinpoints errors with detailed stack traces, pointing out the exact line in the template to facilitate debugging.
-
Commentary: Integrate comments into an ERB template that remain hidden from the final output, parallel to Ruby comment syntax and the line comments used by
ai
which aids in documentation.
In Ruby — and ERB by extension — the 'binding' refers to the execution context, the sphere where Ruby objects bind to values. It's the scope within which Ruby objects are accessible; objects outside the binding remain out of reach.
With the --erb
option in aia
, the binding for Ruby code in a prompt text file is confined to the file itself. You can define anything from scalar values to arrays and hashes, as well as methods or any other Ruby object type you might need. There is great freedom, yet the binding keeps a firm boundary — if an object is undefined within the prompt text, it remains inaccessible.
For a detailed exploration of the ERB syntax, refer to this comprehensive guide.
Ruby code embedding with ERB offers two main patterns:
-
<% ruby code %>
for code execution only. -
<%= ruby code %>
for code execution with output.
Both patterns support multi-line Ruby code; you're not constrained to a single line.
Using the first pattern, <% ... %>
, you define Ruby objects within the binding — setting variable values, declaring methods, and so forth — without generating any output to the prompt text. After execution the ERB block is removed from the prompt text.
The second pattern, <%= ... %>
, includes an equals sign '=' that signals the output of the ERB block to be inserted into the prompt text. Like the first, the ERB block is removed but its output remains as part of the prompt text.
<$ name = "Ruby" %>
Say hello to <%= name %> in three different languages.
In the first line the Ruby variable name
was added to the binding as a Sring type with the value of "Ruby" so that in the second line the value of that variable could be accessed as used within the prompt text.
ERB empowers you to create conditional prompts based on the available binding.
<% "Ruby" == name %>
Review these files: $(ls -1 *.rb) for duplicate code blocks.
M% else %>
Review the grammar and spelling in these files $(ls -1 *.md)
<% end %>
By nature, large language models (LLMs) have a historical cut-off for training data. Only recently, generative AI tools granting real-time data access have been introduced. With ERB, you can complement LLM data by integrating up-to-date information directly into your prompts.
<%
require 'alphavantagerb'
stock_info = Alphavantage::Stock.new(
symbol: 'AAPL',
key: "your api key").quote
current_price = stock_info.price
change = stock_info.change
change_percent = stock_info.change_percent
%>
The latest stock information for Apple Inc. (AAPL) is as follows:
- Current Price: <%= current_price %>
- Change: <%= change %>
- Change (%): <%= change_percent %>
Generate a brief analysis of the stock performance based on this information.
This script illustrates how the ERB binding can extend to include external Ruby objects, though external libraries must be explicitly required within the ERB's binding.
Both the --shell
option and the --erb
option offer flexibility and great power in your prompt engineering.
With great power comes great responsibility -- Attributed to Voltaire; popularized by Spider-Man comics (Stan Lee).