Download the PHP package aimatchfun/laravel-ai without Composer
On this page you can find all versions of the php package aimatchfun/laravel-ai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-ai
Laravel AI
A Laravel package that provides a fluent interface for interacting with AI providers like Ollama, OpenAI, and Anthropic.
Installation
You can install the package via composer:
The package will automatically register itself.
You can publish the configuration file with:
This will publish a config/ai.php
file where you can configure your AI providers.
Conversation History Configuration
By default, conversation history is disabled. If you want to enable conversation history (persisting messages in the database), set the following in your .env
file:
Or directly in config/ai.php
:
If conversation history is disabled, you do not need to run the migrations and no data will be stored in the database, even if you use the conversationHistory
method.
Run the migrations
If you want to use conversation history, you need to run the migration to create the necessary table and enable the option as described above:
This will create the laravelai_conversation_histories
table in your database.
Configuration
After publishing the configuration file, you can configure your AI providers in the config/ai.php
file:
You can also set these values in your .env
file:
Timeout: You can set the timeout (in seconds) for each provider. If a request takes longer than this value, it will fail with a timeout error. The default is 30 seconds for all providers.
Usage
The package provides a fluent interface through the AI
facade:
About the AIResponse object
The run()
method returns an instance of AIResponse
:
conversation_id
: The unique identifier for the conversation. Use this to continue a conversation or fetch its history.answer
: The AI's response to your prompt(s).
Conversation History
Note: Conversation history will only be persisted if
conversation_history_enabled
is set totrue
in your configuration or.env
file.
The conversationHistory
method uses the database connection specified in your configuration file (config/ai.php
). For example, if you use conversationHistory('mysql')
, the conversation will be persisted using the mysql
connection defined in your Laravel project.
- If you do not use the
conversationHistory
method, each call to therun()
method will start a new conversation and a newconversation_id
will be returned. - If you use the
conversationHistory
method, the conversation will be continued and messages will be grouped by the sameconversation_id
.
You can use the returned conversation_id
to continue the conversation in future calls by passing it to the conversationHistory($conversationId)
method.
Example:
Extending
You can add your own AI providers by extending the AIService
class in a service provider:
Your custom provider needs to implement the AIMatchFun\LaravelAI\Contracts\AIProvider
interface or extend the AIMatchFun\LaravelAI\Services\Providers\AbstractProvider
class.
Contributing
Contributions are welcome! If you would like to improve this package, please follow these steps:
- Fork the repository.
- Create a branch for your feature or bugfix (
git checkout -b my-feature
). - Make your changes and add tests if necessary.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to your branch (
git push origin my-feature
). - Open a Pull Request describing your changes.
Please follow the project's code style and write tests whenever possible.
License
This package is open-sourced software licensed under the MIT license.