mpstenson / cloudflare-ai
This package allows you to interact with Cloudflare AI web services.
Fund package maintenance!
mpstenson
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
The cloudflare-ai package provides easy access to the cloudflare ai rest web services in Laravel.
Installation
You can install the package via composer:
composer require mpstenson/cloudflare-ai
This package relies on two .env var settings.
CLOUDFLARE_ACCOUNT_ID
CLOUDFLARE_API_TOKEN
The API token used needs to have access to the cloudflare ai tools. You can get details on generating the cloudflare token here https://developers.cloudflare.com/workers-ai/get-started/rest-api/. You can publish the config file with:
php artisan vendor:publish --tag="cloudflare-ai-config"
This is the contents of the published config file:
<?php // config for mpstenson/CloudflareAI return [ 'api_url' => 'https://api.cloudflare.com/client/v4', 'account_id' => env('CLOUDFLARE_ACCOUNT_ID', ''), 'api_token' => env('CLOUDFLARE_API_TOKEN', ''), 'default_model' => env('CLOUDFLARE_DEFAULT_MODEL', 'meta/llama-3-8b-instruct'), 'default_speech_to_text_model' => env('CLOUDFLARE_DEFAULT_SPEECH_TO_TEXT_MODEL', 'openai/whisper'), 'default_image_classification_model' => env('CLOUDFLARE_DEFAULT_IMAGE_CLASSIFICATION_MODEL', 'microsoft/resnet-50'), ];
Default models can be optionally specified in the .env file. If default models are specified they can still be overwritten on specific method calls.
CLOUDFLARE_DEFAULT_MODEL
CLOUDFLARE_DEFAULT_SPEECH_TO_TEXT_MODEL
CLOUDFLARE_DEFAULT_IMAGE_CLASSIFICATION_MODEL
Usage
Run a completion
use mpstenson\CloudflareAI\CloudflareAI; $response = CloudflareAI::runModel([ 'messages' => [ ['role' => 'system', 'content' => 'You are a friendly assistant'], ['role' => 'user', 'content' => 'Why is pizza so good'], ] ],'meta/llama-2-7b-chat-int8');
Transcribe Audio
use mpstenson\CloudflareAI\CloudflareAI; $whisper = CloudflareAI::runSpeechToText(fopen(storage_path().'/app/public/test.mp3', 'r'),'openai/whisper');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.