Skip to content

A Laravel Package Wrapping the Cloudflare AI Rest API

License

Notifications You must be signed in to change notification settings

mpstenson/cloudflare-ai

Repository files navigation

This package allows you to interact with Cloudflare AI web services.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

About

A Laravel Package Wrapping the Cloudflare AI Rest API

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages