laravel-ai-sdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLaravel AI SDK
Laravel AI SDK
Comprehensive guide for building AI-powered features with the Laravel AI SDK (). Contains 17 rules across 7 categories covering agents, tools, media generation, embeddings, vector stores, and testing.
laravel/ai使用Laravel AI SDK()构建AI驱动功能的综合指南。包含7个类别下的17条规则,涵盖Agent、工具、媒体生成、嵌入向量、向量存储和测试等内容。
laravel/aiWhen to Apply
适用场景
Reference these guidelines when:
- Creating AI agents with instructions, tools, and structured output
- Prompting agents with conversation context
- Streaming or queueing agent responses
- Generating images, audio, or transcriptions
- Creating and querying vector embeddings
- Building RAG (retrieval-augmented generation) features
- Testing AI features with fakes and assertions
在以下场景中参考本指南:
- 创建包含指令、工具和结构化输出的AI Agent
- 结合对话上下文向Agent发送提示词
- 流式传输或异步队列处理Agent响应
- 生成图像、音频或转录内容
- 创建并查询向量嵌入
- 构建RAG(检索增强生成)功能
- 使用模拟(fake)和断言测试AI功能
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Agents | CRITICAL | |
| 2 | Tools | HIGH | |
| 3 | Embeddings & Search | HIGH | |
| 4 | Media Generation | MEDIUM | |
| 5 | Files & Storage | MEDIUM | |
| 6 | Infrastructure | MEDIUM | |
| 7 | Testing | HIGH | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | Agent | 关键(CRITICAL) | |
| 2 | 工具 | 高(HIGH) | |
| 3 | 嵌入向量与搜索 | 高(HIGH) | |
| 4 | 媒体生成 | 中(MEDIUM) | |
| 5 | 文件与存储 | 中(MEDIUM) | |
| 6 | 基础设施 | 中(MEDIUM) | |
| 7 | 测试 | 高(HIGH) | |
Quick Reference
快速参考
1. Agents (CRITICAL)
1. Agent(关键)
- - Create agents with artisan, PHP attribute configuration
agent-create-configure - - Prompt agents, conversation context, RemembersConversations
agent-prompting - - Structured output with JSON schema
agent-structured-output - - Streaming, broadcasting, and queueing responses
agent-streaming-async - - Agent middleware pipeline
agent-middleware - - Anonymous agents for quick interactions
agent-anonymous
- - 使用Artisan命令、PHP属性配置创建Agent
agent-create-configure - - 向Agent发送提示词、对话上下文、RemembersConversations特性
agent-prompting - - 基于JSON Schema的结构化输出
agent-structured-output - - 流式传输、广播和队列处理响应
agent-streaming-async - - Agent中间件管道
agent-middleware - - 用于快速交互的匿名Agent
agent-anonymous
2. Tools (HIGH)
2. 工具(高)
- - Create custom tools with schema and handle method
tool-create - - Provider tools: WebSearch, WebFetch, FileSearch, SimilaritySearch
tool-provider
- - 创建带有Schema和处理方法的自定义工具
tool-create - - 内置工具:WebSearch、WebFetch、FileSearch、SimilaritySearch
tool-provider
3. Embeddings & Search (HIGH)
3. 嵌入向量与搜索(高)
- - Generate, store, and cache vector embeddings
embed-generate-cache - - Rerank documents and collections by relevance
embed-rerank
- - 生成、存储和缓存向量嵌入
embed-generate-cache - - 按相关性对文档和集合进行重排序
embed-rerank
4. Media Generation (MEDIUM)
4. 媒体生成(中)
- - Generate, store, and queue images
media-images - - Text-to-speech and speech-to-text
media-audio-transcription
- - 生成、存储和队列处理图像
media-images - - 文本转语音和语音转文本
media-audio-transcription
5. Files & Storage (MEDIUM)
5. 文件与存储(中)
- - File storage and vector stores for RAG
files-vector-stores
- - 用于RAG的文件存储和向量存储
files-vector-stores
6. Infrastructure (MEDIUM)
6. 基础设施(中)
- - Automatic provider failover for resilience
infra-failover
- - 自动服务商故障转移以提升韧性
infra-failover
7. Testing (HIGH)
7. 测试(高)
- - Fake agents, assert prompts, prevent stray prompts
test-agents - - Fake images, audio, and transcriptions
test-media - - Fake embeddings, reranking, files, and vector stores
test-data
- - 模拟Agent、断言提示词、防止无效提示词
test-agents - - 模拟图像、音频和转录内容
test-media - - 模拟嵌入向量、重排序、文件和向量存储
test-data
Essential Patterns
核心模式
Creating an Agent
创建Agent
php
<?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.';
}
}php
<?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.';
}
}Prompting
发送提示词
php
use App\Ai\Agents\SalesCoach;
$response = SalesCoach::make()->prompt('Analyze this sales transcript...');
return (string) $response;php
use App\Ai\Agents\SalesCoach;
$response = SalesCoach::make()->prompt('Analyze this sales transcript...');
return (string) $response;Generating Images
生成图像
php
use Laravel\Ai\Image;
$image = Image::of('A donut sitting on the kitchen counter')
->landscape()
->generate();
$path = $image->store();php
use Laravel\Ai\Image;
$image = Image::of('A donut sitting on the kitchen counter')
->landscape()
->generate();
$path = $image->store();Generating Embeddings
生成嵌入向量
php
use Illuminate\Support\Str;
$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();php
use Illuminate\Support\Str;
$embeddings = Str::of('Napa Valley has great wine.')->toEmbeddings();Testing
测试
php
use App\Ai\Agents\SalesCoach;
SalesCoach::fake(['First response', 'Second response']);
SalesCoach::make()->prompt('Analyze this...');
SalesCoach::assertPrompted('Analyze this...');php
use App\Ai\Agents\SalesCoach;
SalesCoach::fake(['First response', 'Second response']);
SalesCoach::make()->prompt('Analyze this...');
SalesCoach::assertPrompted('Analyze this...');How to Use
使用方法
Read individual rule files for detailed explanations and code examples.
Each rule file contains:
- YAML frontmatter with metadata (title, impact, tags)
- Brief explanation of why it matters
- Bad Example with explanation
- Good Example with explanation
- Laravel 13 and PHP 8.3 specific context and references
阅读单个规则文件获取详细说明和代码示例。
每个规则文件包含:
- 带有元数据(标题、影响程度、标签)的YAML前置内容
- 简要说明该规则的重要性
- 反面示例及解释
- 正面示例及解释
- Laravel 13和PHP 8.3的特定上下文和参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需包含所有扩展规则的完整指南,请查看:
AGENTS.md