Leanpub Header

Skip to main content

使用 PHP 构建 AI 应用程序

一本使用 PapiAI、Laravel 和 Symfony 的实用指南

用 PHP 构建真正的 AI 智能体——无需 Python。两个完整项目,十家 LLM 提供商,覆盖生产环境中的所有关键问题。

This book is a translation into Chinese (Simplified) of Building AI Applications with PHP which was originally written in English

Minimum price

$9.00

$15.00

You pay

$15.00

Author earns

$12.00
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
About

About

About the Book

PHP 开发者每天都在使用 Laravel 和 Symfony 构建雄心勃勃的应用程序——但一旦涉及 AI,人们就会告诉他们应该转向 Python。本书证明,这并没有必要。

使用 PHP 构建 AI 应用程序 将一步步带你从零开始构建两个完整、可用于生产环境的 AI 应用:一个基于 Laravel 的智能客服代理,以及一个基于 Symfony 的 AI 内容生成流水线。两者都使用 PapiAI,这是一个面向 PHP 的开源库,零运行时依赖,支持 10 家 LLM 提供商,并具备完整的智能体运行时。

你将学习生产环境中真正重要的 AI 应用开发要点:实时流式响应、为智能体提供可查询数据库的工具、跨请求管理对话记忆、使用模式校验结构化输出、通过嵌入和向量检索构建 RAG 流水线、使用队列异步处理任务、防御提示注入、控制成本,以及自信地完成部署。

这不是一本 API 端点导览手册,而是一份关于架构、模式与实战经验的指南,讲清楚什么才能让一个演示原型成长为真正可部署的产品——并且全部使用你已经熟悉的语言来实现。

This book is a translation into Chinese (Simplified) of Building AI Applications with PHP which was originally written in English

Author

About the Author

Marcello Duarte

Marcello Duarte was awarded Best UK Agile Coach in 2014. He has helped teams across Europe adopt Agile ways of working and improve engineering practices such as Scrum, TDD, refactoring, collaborative product ownership, and software quality. He is also the co-creator of PhpSpec, a well-known tool for TDD/SpecBDD in PHP, and brings many years of experience as a software engineering leader, consultant, trainer, and open-source author.

Translations

Translations

Contents

Table of Contents

使用 PHP 构建 AI 应用程序

  1. 基于 PapiAI、Laravel 和 Symfony 的实战指南

第一部分:基础

第1章:AI 原生 PHP 应用

  1. 你将构建什么
  2. 本书适合谁
  3. 你需要准备什么
  4. AI 应用的架构剖析
  5. 安装 PapiAI

第2章:你的第一个代理

  1. 提供商
  2. 代理
  3. 响应
  4. 流式传输
  5. 切换提供商
  6. 故障转移

第3章:工具 —— 赋予 AI 行动能力

  1. 工具调用的工作原理
  2. 使用闭包创建工具
  3. 使用 PHP 属性创建工具
  4. 工具设计原则
  5. 代理循环和 maxTurns

第4章:对话与记忆

  1. Conversation 对象
  2. 消息
  3. 为什么上下文管理很重要
  4. 上下文窗口策略
  5. 持久化对话

第5章:结构化输出

  1. Schema 系统
  2. Schema 类型
  3. 约束与修饰符
  4. 验证

第6章:检索增强生成(RAG)

  1. RAG 的工作原理
  2. PapiAI 中的嵌入
  3. 向量存储
  4. 构建 RAG 管道
  5. 将 RAG 作为工具
  6. 调优 RAG 质量
  7. 生产环境注意事项

第7章:中间件

  1. 中间件接口
  2. 内置中间件
  3. 堆叠中间件
  4. 自定义中间件

第二部分:构建 Laravel 项目

第8章:项目搭建 —— 客服代理

  1. 项目脚手架
  2. 配置
  3. 数据库设置
  4. 系统提示

第9章:构建工具

  1. OrderLookup
  2. KnowledgeBase
  3. InventoryCheck
  4. 在服务提供者中注册工具
  5. 领域模型

第10章:聊天控制器与流式传输

  1. 控制器
  2. 路由
  3. 前端
  4. 测试流程

第11章:安全性

  1. 提示词注入
  2. 工具安全
  3. API 密钥管理
  4. 速率限制
  5. 成本控制

第三部分:构建 Symfony 项目

第12章:项目搭建 — 内容管道

  1. 脚手架
  2. Bundle 配置
  3. 数据库 Schema
  4. 文章 Schema

第13章:内容生成管道

  1. 管道编排器
  2. 使用 Messenger 进行异步处理
  3. 控制器
  4. Twig 模板
  5. 运行 Worker

第14章:性能

  1. 流式传输
  2. 缓存
  3. 模型选择
  4. 异步处理
  5. Token 优化
  6. 连接复用

第15章:架构模式

  1. 智能体即服务
  2. 关注点分离
  3. 提示词管理
  4. 路由器模式
  5. 可观测性

第16章:测试 AI 应用程序

  1. 测试工具
  2. 使用 Mock 提供者测试
  3. 测试 Schema
  4. 集成测试

第17章:部署与生产注意事项

  1. 环境配置
  2. 健康检查
  3. 监控
  4. 超时
  5. 扩展

附录 A:提供者参考

  1. 提供者能力矩阵
  2. 提供者构造函数参数

附录 B:Schema 参考

  1. 类型
  2. 约束
  3. 验证方法

附录 C:中间件参考

  1. RetryMiddleware
  2. RateLimitMiddleware
  3. CacheMiddleware
  4. LoggingMiddleware
  5. 自定义中间件

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub