用途
Laravel AI SDK(laravel/ai)用于在 Laravel 应用中构建 AI 驱动的功能。该资料是一份围绕该 SDK 的综合开发指南,包含覆盖 7 个类别的 17 条规则,涉及智能体、工具、媒体生成、嵌入、向量存储和测试。
适用时机
在以下场景中可参考这些指南:
- 创建带指令、工具和结构化输出的 AI 智能体
- 结合对话上下文提示智能体
- 流式传输或排队智能体响应
- 生成图像、音频或转录文本
- 创建和查询向量嵌入
- 构建 RAG(检索增强生成)功能
- 使用 fake 和断言测试 AI 功能
规则分类(按优先级)
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | Agents | CRITICAL | agent- |
| 2 | Tools | HIGH | tool- |
| 3 | Embeddings & Search | HIGH | embed- |
| 4 | Media Generation | MEDIUM | media- |
| 5 | Files & Storage | MEDIUM | files- |
| 6 | Infrastructure | MEDIUM | infra- |
| 7 | Testing | HIGH | test- |
快速参考
1. Agents(CRITICAL)
- agent-create-configure:使用 artisan、PHP 属性配置创建智能体
- agent-prompting:提示智能体、对话上下文、RemembersConversations
- agent-structured-output:使用 JSON schema 的结构化输出
- agent-streaming-async:流式传输、广播和排队响应
- agent-middleware:智能体中间件管道
- agent-anonymous:用于快速交互的匿名智能体
2. Tools(HIGH)
- tool-create:使用 schema 和 handle 方法创建自定义工具
- tool-provider:提供方工具:WebSearch、WebFetch、FileSearch、SimilaritySearch
3. Embeddings & Search(HIGH)
- embed-generate-cache:生成、存储和缓存向量嵌入
- embed-rerank:按相关性对文档和集合重新排序
4. Media Generation(MEDIUM)
- media-images:生成、存储和排队图像
- media-audio-transcription:文本转语音和语音转文本
5. Files & Storage(MEDIUM)
- files-vector-stores:用于 RAG 的文件存储和向量存储
6. Infrastructure(MEDIUM)
- infra-failover:用于提升韧性的自动提供方故障转移
7. Testing(HIGH)
- test-agents:伪造智能体、断言提示词、防止杂散提示词
- test-media:伪造图像、音频和转录文本
- test-data:伪造嵌入、重排序、文件和向量存储
基础代码模式
创建智能体
<?php
namespace App\Ai\Agents;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Promptable;
class SalesCoach implements Agent
{
use Promptable;
public function instructions(): string
{
return 'You are a sales coach, analyzing transcripts and providing feedback.';
}
}
提示调用
use App\Ai\Agents\SalesCoach;
$response = SalesCoach::make()->prompt('Analyze this sales transcript...');
return (string) $response;
生成图像
use Laravel\Ai\Image;
$image = Image::of('A donut sitting on the kitchen counter')
->landscape()
->generate();
$path = $image->store();
生成嵌入
use Illuminate\Support\Str;
$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();
测试
use App\Ai\Agents\SalesCoach;
SalesCoach::fake(['First response', 'Second response']);
SalesCoach::make()->prompt('Analyze this...');
SalesCoach::assertPrompted('Analyze this...');
使用方式
阅读各个规则文件以获取详细说明和代码示例。每个规则文件包含:
- 带有元数据(标题、影响、标签)的 YAML frontmatter
- 关于其重要性的简要说明
- 带有解释的反例
- 带有解释的正例
- Laravel 13 和 PHP 8.3 特定的上下文与参考
完整编译文档(含全部规则展开)见 AGENTS.md。
限制
该资料未提供价格、收费、性能指标等具体信息。