如何用 PHP 访问人工智能模型?
时间:2024-08-29 14:04:46 305浏览 收藏
学习文章要努力,但是不要急!今天的这篇文章《如何用 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 中,可以使用 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 Google\Cloud\AIPlatform\V1\EndpointName;
use Google\Cloud\AIPlatform\V1\PredictSchemata\Prediction;
use Google\Cloud\AIPlatform\V1\PredictionServiceClient;
use Google\Protobuf\Any;
/**
* 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() 函数:
$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 }]'此命令将打印出模型对给定实例的预测结果,包括预测的类别名称和置信度分数。
今天关于《如何用 PHP 访问人工智能模型?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
175 收藏
-
222 收藏
-
145 收藏
-
451 收藏
-
225 收藏
-
369 收藏
-
183 收藏
-
336 收藏
-
178 收藏
-
296 收藏
-
229 收藏
-
274 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习