Langertha

 view release on metacpan or  search on metacpan

lib/Langertha/Engine/LMStudio.pm  view on Meta::CPAN

package Langertha::Engine::LMStudio;
# ABSTRACT: LM Studio native REST API
our $VERSION = '0.500';
use Moose;
use Carp qw( croak );
use JSON::MaybeXS;
use File::ShareDir::ProjectDistDir qw( :all );
use Module::Runtime qw( use_module );

extends 'Langertha::Engine::Remote';

with map { 'Langertha::Role::'.$_ } qw(
  OpenAPI
  Models
  Temperature
  ResponseSize
  ContextSize
  SystemPrompt
  Streaming
  Chat
);


has '+url' => (
  lazy => 1,
  default => sub { 'http://localhost:1234' },
);

has api_key => (
  is => 'ro',
  lazy_build => 1,
);

sub _build_api_key {
  return $ENV{LANGERTHA_LMSTUDIO_API_KEY};
}


sub update_request {
  my ( $self, $request ) = @_;
  my $key = $self->api_key;
  $request->header('Authorization', 'Bearer '.$key) if defined $key;
}

sub default_model { 'default' }
sub default_response_size { 1024 }

sub openapi_file { yaml => dist_file('Langertha','lmstudio.yaml') };


sub _build_openapi_operations {
  return use_module('Langertha::Spec::LMStudio')->data;
}

sub _build_supported_operations {[qw(
  chat
  listModels
)]}

sub openai {
  my ( $self, %args ) = @_;

  require Langertha::Engine::LMStudioOpenAI;

  my $url = $self->url;
  $url =~ s{/\z}{};
  my $api_key = defined $self->api_key ? $self->api_key : 'lmstudio';

  return Langertha::Engine::LMStudioOpenAI->new(
    url => $url.'/v1',
    model => $self->model,
    api_key => $api_key,
    $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (),
    $self->has_temperature ? ( temperature => $self->temperature ) : (),
    %args,
  );
}


sub anthropic {
  my ( $self, %args ) = @_;

  require Langertha::Engine::LMStudioAnthropic;

  my $api_key = defined $self->api_key ? $self->api_key : 'lmstudio';

  return Langertha::Engine::LMStudioAnthropic->new(
    url => $self->url,
    model => $self->model,
    api_key => $api_key,
    $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (),
    $self->has_temperature ? ( temperature => $self->temperature ) : (),
    %args,
  );
}


sub _normalize_usage {
  my ( $usage ) = @_;



( run in 0.776 second using v1.01-cache-2.11-cpan-39bf76dae61 )