NAME

LLM::SimpleClient - Perl module for making single API requests to LLM providers

SYNOPSIS

use LLM::SimpleClient;
use Log::Log4perl;

# Initialize logging
Log::Log4perl->init("log4perl.conf");

# Create LLM client
my $llm = LLM::SimpleClient->new(
    api_key    => 'your-api-key',
    model      => 'mistral-small-latest',
    provider   => 'mistral',
    role       => 'You are a helpful assistant.',
);

# Send single query and get response (no history stored)
my $result = $llm->ask("What is Perl?");

if ($result->{success}) {
    print "Response: $result->{content}\n";
} else {
    print "Error: $result->{error}\n";
}

DESCRIPTION

LLM::SimpleClient provides a simple interface for sending single queries to LLM (Language Model) providers through their REST APIs. Each call to ask() is independent and does not maintain conversation history.

Supported providers: - Mistral AI (api.mistral.ai) - HuggingFace Router (router.huggingface.co) - OpenRouter (openrouter.ai)

The module handles API authentication, request formatting, response parsing, and automatic failover to backup providers when errors occur.

METHODS

new(%params)

Creates a new LLM client instance.

Required parameters: - api_key: API key for authentication - model: Model identifier (e.g., 'mistral-small-latest') - provider: Provider name ('mistral', 'huggingface', or 'openrouter')

Optional parameters: - temperature: Sampling temperature (0.0-2.0, default: 0.7) - max_tokens: Maximum tokens to generate (default: 2048) - role: System prompt/role (default: 'You are a helpful assistant.') - top_p: Nucleus sampling parameter (default: 1.0) - timeout: Request timeout in seconds (default: 60) - fallback: Arrayref of fallback provider configurations. Each element must be a hashref with required keys: provider, api_key, model. Optional keys: temperature, top_p, timeout.

Example:
  fallback => [
      { provider => 'huggingface', api_key => '...', model => '...' },
      { provider => 'openrouter', api_key => '...', model => '...', temperature => 0.5 },
  ]

ask($text)

Sends a single text query to the LLM and returns the response. This is the only public method - each call is independent with no conversation history.

Returns a hashref with keys: - success: Boolean indicating success - content: Generated text response (on success) - error: Error message if failed - provider: Provider that handled the request

AUTHOR

Konstantin Pristine <kpristine@cpan.org>

LICENSE AND COPYRIGHT

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.