关于 openai-api
openai-api 是面向开发者的 OpenAI API 集成技能,涵盖 SDK 与 HTTP 请求。指南和模板涉及聊天、流式输出、函数调用、嵌入、图像生成及音频任务。
主要功能
- 01聊天补全与流式输出
- 02函数调用和结构化输出
- 03文本嵌入
- 04图像生成与编辑
- 05音频转录与语音合成
适用场景
- 01为应用添加流式聊天
- 02将模型响应连接到函数
- 03为文本生成嵌入
- 04转录音频或生成语音
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ secondsky/ claude-skills --skill openai-api安装后在 Claude Code 中输入/openai-api确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/audio-guide.mdcost-optimization.mdembeddings-guide.mderror-catalog.mdfunction-calling-patterns.mdimages-guide.mdmodels-guide.mdstructured-output-guide.mdtop-errors.mdscripts/check-versions.shtemplates/audio-transcription.tsbasic-usage.tschat-completion-basic.tschat-completion-nodejs.tscloudflare-worker.tsembeddings.tsfunction-calling.tsimage-editing.tsimage-generation.tsmoderation.tspackage.jsonrate-limit-handling.tsstreaming-chat.tsstreaming-fetch.tsstructured-output.tstext-to-speech.tsvision-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'
});
常见错误
- API 密钥无效(401):验证 OPENAI_API_KEY
- 速率限制(429):实现指数退避
- 找不到模型(404):使用正确的模型名称
- 上下文长度(400):缩减输入大小
- 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 转录 + TTSreferences/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- 使用不同音色进行 TTStemplates/moderation.ts- 内容审核templates/rate-limit-handling.ts- 指数退避templates/cloudflare-worker.ts- Cloudflare Workers 集成
评论
支持 Markdown