Langertha-Knarr

 view release on metacpan or  search on metacpan

lib/Langertha/Knarr/Handler.pm  view on Meta::CPAN

package Langertha::Knarr::Handler;
# ABSTRACT: Role for Knarr backend handlers (Raider, Engine, Code, ...)
our $VERSION = '1.100';
use Moose::Role;
use Future::AsyncAwait;
use Langertha::Knarr::Stream;
use Langertha::Knarr::Response;


requires 'handle_chat_f';
requires 'list_models';

# Default streaming = run handle_chat_f and emit one chunk.
# Handlers that natively stream should override.
async sub handle_stream_f {
  my ($self, $session, $request) = @_;
  my $r = Langertha::Knarr::Response->coerce( await $self->handle_chat_f($session, $request) );
  return Langertha::Knarr::Stream->from_list( $r->content );
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Langertha::Knarr::Handler - Role for Knarr backend handlers (Raider, Engine, Code, ...)

=head1 VERSION

version 1.100

=head1 SYNOPSIS

    package My::Handler;
    use Moose;
    use Future;
    with 'Langertha::Knarr::Handler';

    sub handle_chat_f {
        my ($self, $session, $request) = @_;
        return Future->done(
            Langertha::Knarr::Response->new( content => 'hello', model => 'my-model' )
        );
    }

    sub list_models { [ { id => 'my-model', object => 'model' } ] }

    1;

=head1 DESCRIPTION

The role every Knarr backend handler must consume. A handler is the
"what answers the request" half of Knarr — it receives a normalized
L<Langertha::Knarr::Request> and returns either a sync result hash via
L</handle_chat_f> or an async chunk iterator via L</handle_stream_f>.

Knarr ships with concrete handlers for the common cases:

=over

=item * L<Langertha::Knarr::Handler::Code> — coderef-backed, for tests/fakes

=item * L<Langertha::Knarr::Handler::Engine> — passthrough to a Langertha engine

=item * L<Langertha::Knarr::Handler::Raider> — per-session L<Langertha::Raider>

=item * L<Langertha::Knarr::Handler::Router> — model-name routing via L<Langertha::Knarr::Router>



( run in 0.746 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )