PHP code example of ngandu-dev / lugha

1. Go to this page and download the library: Download ngandu-dev/lugha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ngandu-dev / lugha example snippets


$client = ClientFactory::create(Provider::GOOGLE);
$embeddings = $client->embeddings(
    prompt: 'Hello, world!', 
    config: new EmbeddingsConfig(
        model: 'text-embedding-004',
        dimensions: 512
    )
)->embeddings;

$client = ClientFactory::create(Provider::OPENAI);

$completion = $client->completion(
    input: 'Hello, world!', 
    config: new CompletionConfig(
        model: 'gpt-3.5-turbo',
        temperature: 0.5,
        maxTokens: 100,
        frequencyPenalty: 0.5,
        presencePenalty: 0.5
    )
)->completion;

$completion = $client->chat(
    input: History::fromMessages([
        new Message('You are a chatbot, expert in philosophy', Role::SYSTEM),
        new Message('what is the meaning of life ?', Role::USER)
    ]),
    config: new ChatConfig(model: 'gpt-4-turbo')
)->completion;

#[ToolDefinition(
    name: 'get_weather',
    description: 'Get the weather for a location on a specific date.',
    parameters: [
        new ToolParameter('location', 'string', 'The location to get the weather for.',   {
        return "The weather in $location on $date is sunny.";
    }
}

$completion = $client->completion(
    input: 'What is the weather in Lubumbashi on January 16th ?',
    config: new CompletionConfig(model: 'gpt-4-turbo'),
    tools: [new WeatherProvider()] 
)->completion;
  
$client = ClientFactory::create(Provider::VOYAGER);

$reRankedDocuments = $client->reranking(
    prompt: 'What is the meaning of life ?',
    documents: [
        new Document('The best way to predict the future is to create it.'),
        new Document('The only way to do great work is to love what you do.'),
        new Document('Life is short, smile while you still have teeth.'),
        new Document('The best time to plant a tree was 20 years ago. The second best time is now.')
    ],
    config: new RerankingConfig(model: 'voyager-1.0', topK: 3)
)->documents;