mcpField
O
openai-api

openai-api

github.com/secondsky开发247136 次安装6 周前更新
代码仓库

关于 openai-api

openai-api 是面向开发者的 OpenAI API 集成技能,涵盖 SDK 与 HTTP 请求。指南和模板涉及聊天、流式输出、函数调用、嵌入、图像生成及音频任务。

主要功能

  1. 01聊天补全与流式输出
  2. 02函数调用和结构化输出
  3. 03文本嵌入
  4. 04图像生成与编辑
  5. 05音频转录与语音合成

适用场景

  1. 01为应用添加流式聊天
  2. 02将模型响应连接到函数
  3. 03为文本生成嵌入
  4. 04转录音频或生成语音

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/secondsky/claude-skills --skill openai-api

安装后在 Claude Code 中输入/openai-api确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
audio-guide.md
cost-optimization.md
embeddings-guide.md
error-catalog.md
function-calling-patterns.md
images-guide.md
models-guide.md
structured-output-guide.md
top-errors.md
scripts/
check-versions.sh
templates/
audio-transcription.ts
basic-usage.ts
chat-completion-basic.ts
chat-completion-nodejs.ts
cloudflare-worker.ts
embeddings.ts
function-calling.ts
image-editing.ts
image-generation.ts
moderation.ts
package.json
rate-limit-handling.ts
streaming-chat.ts
streaming-fetch.ts
structured-output.ts
text-to-speech.ts
vision-gpt4o.ts

详细介绍

OpenAI API

软件包:[email protected] | 上次更新:2025-11-21

快速入门

bun add openai
export OPENAI_API_KEY="sk-..."
import OpenAI from 'openai';

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});

当前模型(2025)

  • gpt-5.2:能力最强(128k 上下文)
  • gpt-4o:快速多模态(128k 上下文)
  • gpt-4o-mini:高性价比(128k 上下文)
  • gpt-4o-transcribe:针对音频转录优化
  • gpt-4o-mini-transcribe:高性价比转录
  • o1-preview:高级推理(128k 上下文)
  • o1-mini:快速推理(128k 上下文)

聊天补全

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant' },
    { role: 'user', content: 'Explain AI' }
  ],
  temperature: 0.7,
  max_tokens: 1000
});

流式传输

const stream = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell a story' }],
  stream: true
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

函数调用

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the weather?' }],
  tools: [{
    type: 'function',
    function: {
      name: 'getWeather',
      parameters: {
        type: 'object',
        properties: { location: { type: 'string' } },
        required: ['location']
      }
    }
  }]
});

嵌入

const response = await client.embeddings.create({
  model: 'text-embedding-3-small',
  input: 'Your text here'
});

const embedding = response.data[0].embedding; // 1536 dimensions

图像(GPT-Image-1.5)

const image = await client.images.generate({
  model: 'gpt-image-1.5',
  prompt: 'A serene landscape',
  size: '1024x1024',
  quality: 'standard'  // or 'hd'
});

音频

转录(Whisper):

const transcription = await client.audio.transcriptions.create({
  file: fs.createReadStream('audio.mp3'),
  model: 'whisper-1'
});

文本转语音:

const speech = await client.audio.speech.create({
  model: 'tts-1',
  voice: 'alloy',
  input: 'Hello world'
});

常见错误

  1. API 密钥无效(401):验证 OPENAI_API_KEY
  2. 速率限制(429):实现指数退避
  3. 找不到模型(404):使用正确的模型名称
  4. 上下文长度(400):缩减输入大小
  5. JSON 无效:修正函数调用架构

参见:references/error-catalog.md

资源

参考指南
  • references/models-guide.md - 完整的模型比较与选择指南
  • references/function-calling-patterns.md - 函数调用最佳实践
  • references/structured-output-guide.md - 使用 JSON Schema 的结构化输出
  • references/embeddings-guide.md - 文本嵌入与向量搜索
  • references/images-guide.md - GPT-Image-1.5 图像生成
  • references/audio-guide.md - Whisper 转录 + TTS
  • references/cost-optimization.md - Token 优化与定价
  • references/top-errors.md - 20 个常见错误及解决方案
  • references/error-catalog.md - 完整错误参考
模板
  • templates/basic-usage.ts - 快速入门示例
  • templates/chat-completion-basic.ts - 基本聊天补全
  • templates/chat-completion-nodejs.ts - Node.js 实现
  • templates/streaming-chat.ts - 流式响应
  • templates/streaming-fetch.ts - 使用 fetch API 进行流式传输
  • templates/function-calling.ts - 工具与函数调用
  • templates/structured-output.ts - JSON Schema 输出
  • templates/vision-gpt4o.ts - 使用 GPT-4o 进行视觉处理
  • templates/embeddings.ts - 文本嵌入
  • templates/image-generation.ts - GPT-Image-1.5 图像生成
  • templates/image-editing.ts - 图像编辑
  • templates/audio-transcription.ts - Whisper 转录
  • templates/text-to-speech.ts - 使用不同音色进行 TTS
  • templates/moderation.ts - 内容审核
  • templates/rate-limit-handling.ts - 指数退避
  • templates/cloudflare-worker.ts - Cloudflare Workers 集成

文档:https://platform.openai.com/docs

评论

支持 Markdown