演示在边缘节点利用Stateful Actors缓存AI摘要结果的架构——首次请求AI生成并存储,后续请求直接返回缓存,零额外基础设施开销。
我一直在一个对小规模 AI 工具很有用的模式上实践:
将结果缓存在请求运行的地方附近
在真正需要之前避免构建额外的基础设施
这个示例使用 Telnyx Edge Compute Stateful Actors 将该模式应用于 URL 摘要生成。
代码仓库:https://github.com/team-telnyx/telnyx-code-examples/tree/main/edge-url-summarizer
这是一个 Node.js / TypeScript Edge Compute 示例。
curl -X POST https://edge-url-summarizer-<id>.telnyxcompute.com/summarize \
-H "Content-Type: application/json" \
-d '{"url":"https://telnyx.com/blog"}'
首次请求时,它会:
从 HTML 中提取文本
将文本发送到 Telnyx AI Inference
请求恰好三个要点
将结果存储在 Stateful Actor 存储中
对同一 URL 的重复请求,直接返回缓存的摘要。
该示例包含:
POST /summarize - 摘要一个 URL
GET /summarize/cached?url=... - 读取缓存的摘要
POST /summarize/refresh - 使缓存的 URL 失效
GET /stats - 查看缓存命中/未命中统计
GET /cached - 列出缓存的 URL
GET /health/liveness - 存活检查
GET /health/readiness - 就绪检查
{
"url": "https://example.com/article",
"title": "Example Article",
"bullets": [
"The article explains the main problem.",
"The implementation uses an edge function and AI model.",
"Cached results make repeat requests faster."
],
"word_count": 1234,
"generated_at": "2026-08-04T12:00:00Z",
"cached": false
}
再次调用同一 URL,响应中会包含:
{
"cached": true
}
POST /v2/ai/chat/completions
当前示例使用的模型:
zai-org/GLM-5.2
对模型的提示要求只返回 JSON:
{
"bullets": ["point 1", "point 2", "point 3"]
}
这样应用响应便于从仪表盘、工作流或内部工具中消费。
Stateful Actor 部分
缓存存储在 Telnyx Stateful Actor 中。
这意味着应用可以记住之前的摘要,而无需仅为演示添加 Redis 或数据库。
git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/edge-url-summarizer
设置你的 Telnyx API key:
telnyx-edge auth api-key set <YOUR_API_KEY>
telnyx-edge secrets add TELNYX_API_KEY "KEY0123..."
npm install
telnyx-edge ship
curl -sS --retry 30 --retry-delay 5 \
https://edge-url-summarizer-<id>.telnyxcompute.com/health/liveness
curl -X POST https://edge-url-summarizer-<id>.telnyxcompute.com/summarize \
-H "Content-Type: application/json" \
-d '{"url":"https://telnyx.com/blog"}'
curl https://edge-url-summarizer-<id>.telnyxcompute.com/stats
在上生产之前我会添加的内容
对于生产版本,我会添加:
URL 白名单或 SSRF 防护
基于 TTL 的缓存过期
更好的 HTML 提取
重试和可观测性
但作为一个可运行的示例,这是展示 Edge Compute、Stateful Actors 和 Telnyx AI Inference 协同工作的简洁方式。
代码仓库:https://github.com/team-telnyx/telnyx-code-examples/tree/main/edge-url-summarizer
Stateful Actors 快速入门:https://developers.telnyx.com/docs/edge-compute/stateful-actors/quick-start
Telnyx AI Inference 文档:https://developers.telnyx.com/docs/inference
Chat Completions API:https://developers.telnyx.com/api/inference/chat-completions
Telnyx AI 技能和工具包:https://github.com/team-telnyx/ai