承接 clouddark/cloudflare-ai-worker-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

clouddark/cloudflare-ai-worker-php

Composer 安装命令:

composer require clouddark/cloudflare-ai-worker-php

包简介

A PHP library to interact with Cloudflare AI Workers or other AI models.

README 文档

README

This is a simple PHP wrapper to interact with Cloudflare's AI Worker API. The wrapper allows you to send messages to the Cloudflare AI model and get responses in both streaming and non-streaming modes.

Requirements

  • PHP 7.4 or higher
  • Composer

Installation

To get started, you can install the package via Composer.

  1. Clone the repository or initialize it in your project:

    composer require clouddark/cloudflare-ai-worker-php:dev-main
  2. Upgrade dependencies using Composer:

    composer upgrade && composer update

    This will install the necessary dependencies and set up the autoloader.

Usage

Once installed, you can use the CloudflareAiWorker class to send requests to the Cloudflare AI API.

Example PHP Code

1. Non-Streaming (Standard Request)

<?php

// Ensure Composer autoloader is loaded
require 'vendor/autoload.php';  // No need for full path when using Composer autoload

use CloudDark\CloudflareAiWorker;  // Use the appropriate namespace

// Create an instance of CloudflareAiWorker for non-streaming
$worker = new CloudflareAiWorker(
    'https://api.cloudflare.com',   // Cloudflare API URL
    'XXXX', // Account ID
    'XXXX', // API Token
    '@cf/meta/llama-3.1-70b-instruct', // Model
    false // Non-streaming
);

// Prepare the message to send
$messages = [
    [
        'role' => 'system',
        'content' => 'You are an AI assistant.'
    ],
    [
        'role' => 'user',
        'content' => 'Hello, AI!'
    ]
];

// Send the request and get the response
$response = json_encode($worker->requestAiWorker($messages));

// Output the response
print_r($response);
?>

2. Streaming (Real-time Request)

<?php

// Ensure Composer autoloader is loaded
require 'vendor/autoload.php';  // No need for full path when using Composer autoload

use CloudDark\CloudflareAiWorker;  // Use the appropriate namespace

// Create an instance of CloudflareAiWorker for streaming
$worker = new CloudflareAiWorker(
    'https://api.cloudflare.com',   // Cloudflare API URL
    'XXXX', // Account ID
    'XXXX', // API Token
    '@cf/meta/llama-2-7b-chat-int8', // Model
    true // Streaming enabled
);

// Prepare the message to send (only prompt required for streaming)
$messages = 'where is new york?';

// Send the request and get the response
$response = $worker->requestAiWorker($messages);

// Streaming directly displayed to browser
?>

Expected Response

The expected response from the Cloudflare AI Worker can vary based on the mode.

Non-Streaming Response:

The response for non-streaming will be a complete JSON object, similar to:

{
    "result": {
        "response": "Hello! It's nice to meet you. I'm here to help answer any questions, provide information, or just chat if you'd like. How can I assist you today?"
    },
    "success": true,
    "errors": [],
    "messages": []
}

Streaming Response:

In the case of streaming, the data will be received and output in real-time. The response will be directly displayed as it is processed, which could look like:

"Hello! It's nice to meet you. I'm here to help answer any questions..."

The output will be displayed as chunks of data in the browser as they are received from the API.

Customization

You can customize the CloudflareAiWorker class by adjusting the URL, token, or model that you're using to interact with the Cloudflare AI API.

  • Set URL: setUrl('your-api-url')
  • Set API Token: setToken('your-api-token')
  • Set Model: setModel('your-model-id')
  • Enable/Disable Streaming: setStream(true) or setStream(false)

License

This project is open-source and available under the MIT License.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-01-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固