laravel-ai-sdk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Laravel AI SDK

Laravel AI SDK

Comprehensive guide for building AI-powered features with the Laravel AI SDK (
laravel/ai
). Contains 17 rules across 7 categories covering agents, tools, media generation, embeddings, vector stores, and testing.
使用Laravel AI SDK(
laravel/ai
)构建AI驱动功能的综合指南。包含7个类别下的17条规则,涵盖Agent、工具、媒体生成、嵌入向量、向量存储和测试等内容。

When 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

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1AgentsCRITICAL
agent-
2ToolsHIGH
tool-
3Embeddings & SearchHIGH
embed-
4Media GenerationMEDIUM
media-
5Files & StorageMEDIUM
files-
6InfrastructureMEDIUM
infra-
7TestingHIGH
test-
优先级类别影响程度前缀
1Agent关键(CRITICAL)
agent-
2工具高(HIGH)
tool-
3嵌入向量与搜索高(HIGH)
embed-
4媒体生成中(MEDIUM)
media-
5文件与存储中(MEDIUM)
files-
6基础设施中(MEDIUM)
infra-
7测试高(HIGH)
test-

Quick Reference

快速参考

1. Agents (CRITICAL)

1. Agent(关键)

  • agent-create-configure
    - Create agents with artisan, PHP attribute configuration
  • agent-prompting
    - Prompt agents, conversation context, RemembersConversations
  • agent-structured-output
    - Structured output with JSON schema
  • agent-streaming-async
    - Streaming, broadcasting, and queueing responses
  • agent-middleware
    - Agent middleware pipeline
  • agent-anonymous
    - Anonymous agents for quick interactions
  • agent-create-configure
    - 使用Artisan命令、PHP属性配置创建Agent
  • agent-prompting
    - 向Agent发送提示词、对话上下文、RemembersConversations特性
  • agent-structured-output
    - 基于JSON Schema的结构化输出
  • agent-streaming-async
    - 流式传输、广播和队列处理响应
  • agent-middleware
    - Agent中间件管道
  • agent-anonymous
    - 用于快速交互的匿名Agent

2. Tools (HIGH)

2. 工具(高)

  • tool-create
    - Create custom tools with schema and handle method
  • tool-provider
    - Provider tools: WebSearch, WebFetch, FileSearch, SimilaritySearch
  • tool-create
    - 创建带有Schema和处理方法的自定义工具
  • tool-provider
    - 内置工具:WebSearch、WebFetch、FileSearch、SimilaritySearch

3. Embeddings & Search (HIGH)

3. 嵌入向量与搜索(高)

  • embed-generate-cache
    - Generate, store, and cache vector embeddings
  • embed-rerank
    - Rerank documents and collections by relevance
  • embed-generate-cache
    - 生成、存储和缓存向量嵌入
  • embed-rerank
    - 按相关性对文档和集合进行重排序

4. Media Generation (MEDIUM)

4. 媒体生成(中)

  • media-images
    - Generate, store, and queue images
  • media-audio-transcription
    - Text-to-speech and speech-to-text
  • media-images
    - 生成、存储和队列处理图像
  • media-audio-transcription
    - 文本转语音和语音转文本

5. Files & Storage (MEDIUM)

5. 文件与存储(中)

  • files-vector-stores
    - File storage and vector stores for RAG
  • files-vector-stores
    - 用于RAG的文件存储和向量存储

6. Infrastructure (MEDIUM)

6. 基础设施(中)

  • infra-failover
    - Automatic provider failover for resilience
  • infra-failover
    - 自动服务商故障转移以提升韧性

7. Testing (HIGH)

7. 测试(高)

  • test-agents
    - Fake agents, assert prompts, prevent stray prompts
  • test-media
    - Fake images, audio, and transcriptions
  • test-data
    - Fake embeddings, reranking, files, and vector stores
  • test-agents
    - 模拟Agent、断言提示词、防止无效提示词
  • 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