Communitygithub.com

YepAPI 技能集

GPT API 模式、函数调用、使用 Zod 的结构化输出、流式传输和令牌管理。

YepAPI 技能集 是什么?

YepAPI/skills 技能集为开发者提供了与 OpenAI GPT API 集成的最佳实践和标准模式。它涵盖了从基础的 API 初始化(通过环境变量管理密钥)到高级特性,包括使用 `stream: true` 进行流式聊天补全(支持异步迭代,无需等待完整响应)、基于 JSON Schema 的函数调用(处理 `tool_calls` 并返回工具消息)、以及使用 `response_format` 或 Zod 实现结构化输出。该技能还强调令牌管理(估算与修剪上下文)、模型选择(复杂任务使用 `gpt-4o`,简单任务使用 `gpt-4o-mini` 以节省成本)、系统提示词设计、温度参数设置(0 用于确定性输出,0.7-1.0 用于创造性任务)以及速率限制处理(实现指数退避)。该技能以 TypeScript 代码片段形式提供了流式补全和函数调用的直接使用模式,并明确列出了需要避免的常见错误,如硬编码密钥、忽略令牌限制、误用模型以及缺乏错误处理。它适用于 Claude Code、Cursor、Codex 等 AI 代理工具,是构建可靠、高效 OpenAI 集成应用的实用参考。

兼容平台~Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/openai-integration

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

OpenAI 集成

规则

  • 使用官方的 openai 包 — new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
  • 聊天流式传输:使用 stream: true 配合异步迭代器 — 不要等待完整响应再面向用户
  • 函数调用:使用 JSON Schema 定义函数,在响应中处理 tool_calls,并将结果作为工具消息返回
  • 结构化输出:使用 response_format: { type: "json_schema", json_schema: {...} } 或与 AI SDK 结合使用 Zod
  • 令牌管理:发送前预估令牌数(大约每 4 个字符一个令牌),主动修剪上下文以保持在限制内
  • 模型选择:复杂推理使用 gpt-4o,简单任务使用 gpt-4o-mini — 使其可配置
  • 系统提示词作为第一条消息 — 设定语气、约束和输出格式
  • 温度:确定性输出(数据提取)设为 0,创意任务设为 0.7-1.0
  • 速率限制:尊重 x-ratelimit-* 头部,对 429 错误实现指数退避

模式

// 流式补全
const stream = await openai.chat.completions.create({
  model: "gpt-4o",
  messages,
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || "";
  process.stdout.write(content);
}
// 函数调用
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages,
  tools: [{ type: "function", function: { name: "get_weather", parameters: schema } }],
});

避免

  • 硬编码 API 密钥 — 始终使用环境变量
  • 忽略令牌限制 — 主动计数令牌并修剪消息
  • 对琐碎任务使用 gpt-4o — 使用 gpt-4o-mini 以节省成本
  • API 调用时缺少错误处理 — 优雅地处理 429、500、超时错误

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能