卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章76259本站已运行4320

如何用 PHP 访问人工智能模型?

在 php 中,使用 google cloud platform (gcp) php 客户库访问 ai 模型:安装 php 7.1 及以上版本。设置 google cloud sdk。通过 gcp 控制台启用 ai platform predictions api。使用 composer 安装 php 库。发送预测请求,包括项目 id、地区、端点 id 和 json 编码的预测实例。解析响应,获取预测结果和置信度分数。

如何用 PHP 访问人工智能模型?

如何用 PHP 访问人工智能模型

在 PHP 中,可以使用 Google Cloud Platform(GCP)提供的 PHP 客户库来访问 AI Platform Predictions 服务。该服务允许您向事先训练好的 AI 模型发送预测请求。

先决条件

  • 安装 [PHP](https://www.php.net/) 7.1 或更高版本
  • 配置 [Google Cloud SDK](https://cloud.google.com/sdk/)
  • 启用 AI Platform Predictions API:[https://console.cloud.google.com/apis/dashboard](https://console.cloud.google.com/apis/dashboard)

安装 PHP 库

composer require google/cloud-aiplatform

运行一个预测实例

以下是一个用 PHP 向已部署的 AI 模型发送预测请求的示例:

<?php

use GoogleCloudAIPlatformV1EndpointName;
use GoogleCloudAIPlatformV1PredictSchemataPrediction;
use GoogleCloudAIPlatformV1PredictionServiceClient;
use GoogleProtobufAny;

/**
 * Deploys the Prediction service using the AI Platform endpoint.
 *
 * @param string $projectId  Your GCP project ID.
 * @param string $location  Your GCP region (e.g. 'us-central1').
 * @param string $endpointId  The ID of your AI Platform Endpoint (e.g. '12345').
 * @param array $instance  Your JSON-encoded prediction instance array or array of arrays.
 */
function predict(
    string $projectId,
    string $location,
    string $endpointId,
    array $instance
): void {
    $endpoint = new EndpointName($projectId, $location, $endpointId);

    // Convert JSON-encoded instance array(s) to PHP array(s).
    $convertedInstance = [];
    foreach ($instance as $instanceRow) {
        $convertedInstanceRow = json_decode($instanceRow);
        if ($convertedInstanceRow === null) {
            throw new Exception('Invalid JSON in $instance.');
        }
        $convertedInstance[] = $convertedInstanceRow;
    }

    // Instantiation of a client.
    $clientOptions = ['apiEndpoint' => 'us-central1-aiplatform.googleapis.com:443'];
    $client = new PredictionServiceClient($clientOptions);

    // Set the parameters for the predict call.
    $parameters = [];
    $encodedInstance = new Any();
    $encodedInstance->pack(
        (new Prediction())
            ->setInstances($convertedInstance)
            ->setSerialize()
    );
    $parameters['instances'] = $encodedInstance;

    // Send the prediction request.
    $response = $client->predict($endpoint, $parameters);
    printf('Raw response: %s' . PHP_EOL, $response->serializeToJsonString());

    $predictionsResult = $response->getPredictions();
    printf(
        'Predicted class name(s): %s' . PHP_EOL,
        implode(', ', $predictionsResult[0]->getDisplayNames())
    );
    printf(
        'Predicted class score(s): %s' . PHP_EOL,
        implode(', ', $predictionsResult[0]->getConfidences())
    );
}

实战

要运行此示例,请将以下信息作为参数传递给 predict() 函数:

立即学习“PHP免费学习笔记(深入)”;

点击下载“修复打印机驱动工具”;

  • $projectId: 你的 GCP 项目 ID
  • $location: 你的 GCP 地区(例如“us-central1”)
  • $endpointId: 你的 AI Platform 端点的 ID(例如“12345”)
  • $instance: 一个包含一个或多个 JSON-编码预测实例的 PHP 数组

例如,以下命令将向部署在“us-central1”的“my-endpoint”中名为“my-model”的模型发送预测请求:

php predict my-project us-central1 my-endpoint '[{ "petal_length": 5.1, "petal_width": 3.5, "sepal_length": 1.4, "sepal_width": 0.2 }]'

此命令将打印出模型对给定实例的预测结果,包括预测的类别名称和置信度分数。

卓越飞翔博客
上一篇: golang框架在高并发场景下的扩展和弹性设计
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏