Communitygithub.com

YepAPI/技能

GPT API 模式、函式呼叫、使用 Zod 的結構化輸出、串流與 Token 管理。

技能 是什麼?

此技能提供 OpenAI API 的全面整合指南,涵蓋核心模式如串流完成、函式呼叫與結構化輸出。開發者能學習使用 `openai` 官方套件建立串流回應,透過非同步迭代器即時處理內容,無需等待完整回應。技能強調使用 JSON Schema 定義函式並處理 `tool_calls`,同時支援以 `response_format` 或 Zod 產出結構化資料。Token 管理上,建議預估字元數(約 4 字元/Token)並主動裁剪上下文以符合限制。模型選擇方面,複雜推理任務建議使用 `gpt-4o`,簡單任務則用 `gpt-4o-mini` 節省成本。此外,技能包含溫度參數設定(0 用於確定性輸出、0.7-1.0 用於創意任務)、速率限制處理(遵循 `x-ratelimit-*` 標頭、429 錯誤指數退避),以及最佳實務如避免硬編碼 API 金鑰、處理 429/500 逾時錯誤。支援 Node.js 生態,適用於 Claude Code、Cursor 等 AI 代理開發平台。

相容平台~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
  • Token 管理:發送前預估 Token(約 4 字元等於 1 個 Token),主動裁剪上下文以保持在限制內
  • 模型選擇:複雜推理使用 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 金鑰 — 永遠使用環境變數
  • 忽略 Token 限制 — 計算 Token 並主動裁剪訊息
  • 瑣碎任務使用 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.

相關技能