1. Go to this page and download the library: Download aimatchfun/laravel-ai 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/ */
aimatchfun / laravel-ai example snippets
$messages = [
['role' => 'user', 'content' => 'Hello, how are you?'],
['role' => 'assistant', 'content' => 'I am doing well, thank you!'],
['role' => 'user', 'content' => 'What can you help me with?']
];
use AIMatchFun\LaravelAI\Services\Message;
$messages = [
Message::user('Hello, how are you?'),
Message::assistant('I am doing well, thank you!'),
Message::user('What can you help me with?')
];
use AIMatchFun\LaravelAI\Facades\AI;
use AIMatchFun\LaravelAI\Services\AICreativity;
// Basic usage with default provider
$response = AI::prompt('What is Laravel?')
->run();
// $response is an object with:
// $response->answer (string)
// Specify a provider
$response = AI::provider('ollama')
->prompt('What is Laravel?')
->run();
// Specify a model
$response = AI::provider('ollama')
->model('llama3')
->prompt('What is Laravel?')
->run();
// With system instruction
$response = AI::provider('ollama')
->model('llama3')
->systemInstruction('You are a helpful AI assistant.')
->prompt('What is Laravel?')
->run();
// Using preview messages for context
$messages = [
['role' => 'user', 'content' => 'Hello, how are you?'],
['role' => 'assistant', 'content' => 'I am doing well, thank you!']
];
$response = AI::provider('ollama')
->model('llama3')
->previewMessages($messages)
->prompt('What is Laravel?')
->run();
// Adjust creativity level (temperature)
$response = AI::provider('ollama')
->model('llama3')
->prompt('Write a poem about Laravel.')
->creativityLevel(AICreativity::HIGH)
->run();
// Continue a conversation using preview messages
$response = AI::prompt('Hello, who are you?')->run();
$messages = [
['role' => 'user', 'content' => 'Hello, who are you?'],
['role' => 'assistant', 'content' => $response->answer]
];
$response = AI::prompt('And what can you do?')
->previewMessages($messages)
->run();
$response = AI::prompt('What is Laravel?')->run();
$answer = $response->answer; // string
// Using array format
$messages = [
['role' => 'user', 'content' => 'Hello, who are you?'],
['role' => 'assistant', 'content' => 'I am an AI assistant.']
];
$response = AI::previewMessages($messages)
->prompt('What can you help me with?')
->run();
// Using Message objects
use AIMatchFun\LaravelAI\Services\Message;
$messages = [
Message::user('Hello, who are you?'),
Message::assistant('I am an AI assistant.')
];
$response = AI::previewMessages($messages)
->prompt('What can you help me with?')
->run();
use AIMatchFun\LaravelAI\Enums\NovitaModel;
// Use a specific model
$response = AI::provider('novita')
->model(NovitaModel::ERNIE_4_5_0_3B->value)
->prompt('What is Laravel?')
->run();
// Get all available models
$allModels = NovitaModel::getValues();
// Find a model by value
$model = NovitaModel::fromValue('baidu/ernie-4.5-0.3b');
use AIMatchFun\LaravelAI\Services\AIService;
use App\Services\AI\CustomProvider;
public function boot()
{
$this->app->extend('ai', function (AIService $service, $app) {
$service->extend('custom', function () {
return new CustomProvider(
config('ai.providers.custom.api_key'),
config('ai.providers.custom.default_model')
);
});
return $service;
});
}