DWIM-Block
view release on metacpan or search on metacpan
lib/DWIM/Block.pm view on Meta::CPAN
C<DWIM> block containing the actual request you want to send. Like so:
sub autoinflect ($text) {
DWIM {
Please inflect the following text so that its grammar is correct
and its nouns and verbs agree in number and person.
Please return only the inflected sentence, without any commentary:
$text
}
}
This automatically adds back in all the necessary boilerplate code to drive C<Chat::AI>.
Note that the contents of the C<DWIM> block behave exactly like a double-quoted
string, which is then passed to C<AI::Chat::prompt()>. This means that you can
put Perl variables inside the block and they will be interpolated into the
double-quoted string. For instance, note the use of C<$text> in the comment
in the preceding example.
=head2 Capturing the result of the query
Unlike regular Perl blocks, you can also use a DWIM block as part of a
larger expression (as you would a C<do> block). For example:
dwim from_JSON ($JSON_data) {
return eval DWIM { Please convert the following JSON object to a Perl hashref: JSON_data };
}
Note that, if the query fails for any reason, the result of the C<DWIM> block may be C<undef>.
The module decides whether to treat a C<DWIM> as an control block or a C<do> block,
by examining its syntactic location. If a C<DWIM> is specified at the beginning of a statement,
it is treated as a control block (like an C<if> or C<for> block), and does not require
a semicolon after it. If it is not in a location where a control block could appear,
it is treated as a special form of C<do> block, and the expression containing it I<does>
require a semicolon (or a closing block delimiter) after it.
=head1 DIAGNOSTICS
=over
=item C<< Can't find API key >>
The module requires an OpenAI API key to allow it to connect with the AI server.
This key must be provided in an environment variable, which may be named
either C<DWIM_API_KEY> or C<OPENAI_API_KEY>.
You specified a C<DWIM> block, but it could not find either of those environment variables.
Set up the variable in your shell or IDE. For example, in your shell config file:
# In your .bashrc or .zshrc file:
export DWIM_API_KEY=sk-proj-y0ur0p3n41Pr0j3c74P1k3y6035H3r3TH150n35N07R34l
# In your .cshrc file:
setenv DWIM_API_KEY sk-proj-y0ur0p3n41Pr0j3c74P1k3y6035H3r3TH150n35N07R34l
# In your .env file:
DWIM_API_KEY=sk-proj-y0ur0p3n41Pr0j3c74P1k3y6035H3r3TH150n35N07R34l
=back
=head1 CONFIGURATION AND ENVIRONMENT
DWIM::Block requires no configuration files.
The module requires one of the following environment variables
to be set to a string representing a valid API key for the model
you are using:
$ENV{'DWIM_API_KEY'}
$ENV{'OPENAI_API_KEY'}
If both are set, then C<$ENV{'DWIM_API_KEY'}> is always used.
The module also looks for one of the following environment variables,
with which you can specify the specific model to be used:
$ENV{'DWIM_MODEL'}
$ENV{'OPENAI_MODEL'}
If both are set, then C<$ENV{'DWIM_MODEL'}> is always used.
If neither is set, then AI::Chat selects the model for you automatically.
=head1 DEPENDENCIES
This module requires the C<Filter::Simple>, C<PPR>, and C<AI::Chat> modules.
=head1 INCOMPATIBILITIES
None reported.
=head1 BUGS AND LIMITATIONS
No bugs have been reported.
What a C<DWIM> can be made to accomplish is limited primarily by your imagination.
Please report any bugs or feature requests to
C<bug-dwim-block@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
Damian Conway C<< <DCONWAY@CPAN.org> >>
=head1 LICENCE AND COPYRIGHT
( run in 0.727 second using v1.01-cache-2.11-cpan-39bf76dae61 )