jpskill.com
🎨 画像AI コミュニティ

openai-sdk

OpenAIのAPIを活用し、GPTやChatGPTをアプリに組み込んだり、テキスト生成、チャットボット構築、DALL-Eによる画像生成、Whisperによる音声認識など、様々なAI機能をアプリケーションに統合するSkill。

📜 元の英語説明(参考)

Integrate OpenAI APIs into applications. Use when a user asks to add GPT or ChatGPT to an app, generate text with OpenAI, build a chatbot, use GPT-4 or o1 models, generate embeddings, use function calling, stream chat completions, build AI features, moderate content, generate images with DALL-E, transcribe audio with Whisper API, or integrate any OpenAI model. Covers Chat Completions, Assistants API, function calling, embeddings, streaming, vision, DALL-E, Whisper, and moderation.

🇯🇵 日本人クリエイター向け解説

一言でいうと

OpenAIのAPIを活用し、GPTやChatGPTをアプリに組み込んだり、テキスト生成、チャットボット構築、DALL-Eによる画像生成、Whisperによる音声認識など、様々なAI機能をアプリケーションに統合するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o openai-sdk.zip https://jpskill.com/download/15203.zip && unzip -o openai-sdk.zip && rm openai-sdk.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/15203.zip -OutFile "$d\openai-sdk.zip"; Expand-Archive "$d\openai-sdk.zip" -DestinationPath $d -Force; ri "$d\openai-sdk.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して openai-sdk.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → openai-sdk フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

OpenAI SDK

概要

OpenAI SDK は、GPT-4o、o1、DALL-E 3、Whisper、および埋め込みモデルへのアクセスを提供します。このスキルでは、Chat Completions API (テキスト生成、会話、関数呼び出し)、ストリーミング応答、vision (画像理解)、埋め込み、Assistants API (ステートフルエージェント)、DALL-E 画像生成、Whisper 文字起こし、およびコンテンツモデレーションについて説明します。TypeScript/Node.js と Python の両方の例があります。

手順

ステップ 1: インストールとセットアップ

# Node.js
npm install openai

# Python
pip install openai
// lib/openai.ts — クライアントの初期化
import OpenAI from 'openai'

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,    // または OPENAI_API_KEY 環境変数を設定
})

ステップ 2: Chat Completions

// chat.ts — 基本的なチャット補完と会話
import OpenAI from 'openai'

const openai = new OpenAI()

// 単一のメッセージ
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'あなたは役立つコーディングアシスタントです。' },
    { role: 'user', content: '2つのソートされたリストをマージする Python 関数を記述してください。' },
  ],
  temperature: 0.7,       // 0 = 決定的、2 = 創造的
  max_tokens: 1000,
})

console.log(response.choices[0].message.content)

// 複数ターンの会話 (メッセージ履歴を維持)
const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: 'system', content: 'あなたはデータ分析アシスタントです。' },
]

async function chat(userMessage: string) {
  messages.push({ role: 'user', content: userMessage })

  const response = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages,
  })

  const assistantMessage = response.choices[0].message
  messages.push(assistantMessage)
  return assistantMessage.content
}

ステップ 3: ストリーミング応答

// stream.ts — リアルタイム UI のためのストリームチャット補完
const stream = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: '量子コンピューティングを簡単な言葉で説明してください。' }],
  stream: true,
})

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || ''
  process.stdout.write(content)    // ターミナル/UI にストリーム
}
// Next.js ストリーミング API ルート
// app/api/chat/route.ts — フロントエンドへのストリーミングのための Server-sent events
import { OpenAIStream, StreamingTextResponse } from 'ai'    // Vercel AI SDK ヘルパー

export async function POST(req: Request) {
  const { messages } = await req.json()

  const response = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages,
    stream: true,
  })

  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}

ステップ 4: 関数呼び出し (ツール使用)

// function_calling.ts — GPT に関数を呼び出させる
const tools: OpenAI.ChatCompletionTool[] = [
  {
    type: 'function',
    function: {
      name: 'get_weather',
      description: '都市の現在の天気を取得する',
      parameters: {
        type: 'object',
        properties: {
          city: { type: 'string', description: '都市名' },
          units: { type: 'string', enum: ['celsius', 'fahrenheit'] },
        },
        required: ['city'],
      },
    },
  },
  {
    type: 'function',
    function: {
      name: 'search_database',
      description: '製品データベースを検索する',
      parameters: {
        type: 'object',
        properties: {
          query: { type: 'string' },
          category: { type: 'string' },
          max_price: { type: 'number' },
        },
        required: ['query'],
      },
    },
  },
]

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: '東京の天気はどうですか?' }],
  tools,
  tool_choice: 'auto',
})

// 関数呼び出しを処理する
const toolCall = response.choices[0].message.tool_calls?.[0]
if (toolCall) {
  const args = JSON.parse(toolCall.function.arguments)
  // 実際の関数を実行する
  const result = await getWeather(args.city, args.units)

  // 結果を GPT に送り返す
  const finalResponse = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      { role: 'user', content: '東京の天気はどうですか?' },
      response.choices[0].message,
      { role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) },
    ],
    tools,
  })
}

ステップ 5: 埋め込み

// embeddings.ts — セマンティック検索のための埋め込みを生成する
const response = await openai.embeddings.create({
  model: 'text-embedding-3-small',    // 1536 次元、最も安価
  input: 'Node.js アプリを本番環境にデプロイする方法',
})

const embedding = response.data[0].embedding    // 長さ 1536 の number[]

// バッチ埋め込み
const batchResponse = await openai.embeddings.create({
  model: 'text-embedding-3-small',
  input: [
    '最初のドキュメントテキスト',
    '2番目のドキュメントテキスト',
    '3番目のドキュメントテキスト',
  ],
})
// batchResponse.data[0].embedding, batchResponse.data[1].embedding, etc.

ステップ 6: Vision (画像理解)

// vision.ts — GPT-4o で画像を分析する
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'この画像には何が写っていますか?詳細に説明してください。' },
      { type: 'image_url', image_url: { url: 'https://example.com/photo.jpg' } },
    ],
  }],
})

// Base64 画像 (ファイルアップロードから)
const base64Image = fs.readFileSync('photo.jpg').toString('base64')
const response2 = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'この領収書からすべてのテキストを抽出してください。' },
      { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
    ],
  }],
})

ステップ 7: 構造化された出力

// structured.ts — スキーマに一致する JSON 出力を取得する
const response = await open

(原文がここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

OpenAI SDK

Overview

The OpenAI SDK provides access to GPT-4o, o1, DALL-E 3, Whisper, and embedding models. This skill covers the Chat Completions API (text generation, conversation, function calling), streaming responses, vision (image understanding), embeddings, the Assistants API (stateful agents), DALL-E image generation, Whisper transcription, and content moderation. Examples in both TypeScript/Node.js and Python.

Instructions

Step 1: Installation and Setup

# Node.js
npm install openai

# Python
pip install openai
// lib/openai.ts — Client initialization
import OpenAI from 'openai'

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,    // or set OPENAI_API_KEY env var
})

Step 2: Chat Completions

// chat.ts — Basic chat completion and conversation
import OpenAI from 'openai'

const openai = new OpenAI()

// Single message
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful coding assistant.' },
    { role: 'user', content: 'Write a Python function to merge two sorted lists.' },
  ],
  temperature: 0.7,       // 0 = deterministic, 2 = creative
  max_tokens: 1000,
})

console.log(response.choices[0].message.content)

// Multi-turn conversation (maintain message history)
const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: 'system', content: 'You are a data analysis assistant.' },
]

async function chat(userMessage: string) {
  messages.push({ role: 'user', content: userMessage })

  const response = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages,
  })

  const assistantMessage = response.choices[0].message
  messages.push(assistantMessage)
  return assistantMessage.content
}

Step 3: Streaming Responses

// stream.ts — Stream chat completions for real-time UI
const stream = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Explain quantum computing in simple terms.' }],
  stream: true,
})

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || ''
  process.stdout.write(content)    // stream to terminal/UI
}
// Next.js streaming API route
// app/api/chat/route.ts — Server-sent events for streaming to frontend
import { OpenAIStream, StreamingTextResponse } from 'ai'    // Vercel AI SDK helper

export async function POST(req: Request) {
  const { messages } = await req.json()

  const response = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages,
    stream: true,
  })

  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}

Step 4: Function Calling (Tool Use)

// function_calling.ts — Let GPT call your functions
const tools: OpenAI.ChatCompletionTool[] = [
  {
    type: 'function',
    function: {
      name: 'get_weather',
      description: 'Get current weather for a city',
      parameters: {
        type: 'object',
        properties: {
          city: { type: 'string', description: 'City name' },
          units: { type: 'string', enum: ['celsius', 'fahrenheit'] },
        },
        required: ['city'],
      },
    },
  },
  {
    type: 'function',
    function: {
      name: 'search_database',
      description: 'Search the product database',
      parameters: {
        type: 'object',
        properties: {
          query: { type: 'string' },
          category: { type: 'string' },
          max_price: { type: 'number' },
        },
        required: ['query'],
      },
    },
  },
]

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
  tools,
  tool_choice: 'auto',
})

// Handle function call
const toolCall = response.choices[0].message.tool_calls?.[0]
if (toolCall) {
  const args = JSON.parse(toolCall.function.arguments)
  // Execute the actual function
  const result = await getWeather(args.city, args.units)

  // Send result back to GPT
  const finalResponse = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      { role: 'user', content: 'What is the weather in Tokyo?' },
      response.choices[0].message,
      { role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) },
    ],
    tools,
  })
}

Step 5: Embeddings

// embeddings.ts — Generate embeddings for semantic search
const response = await openai.embeddings.create({
  model: 'text-embedding-3-small',    // 1536 dimensions, cheapest
  input: 'How to deploy a Node.js app to production',
})

const embedding = response.data[0].embedding    // number[] of length 1536

// Batch embeddings
const batchResponse = await openai.embeddings.create({
  model: 'text-embedding-3-small',
  input: [
    'First document text',
    'Second document text',
    'Third document text',
  ],
})
// batchResponse.data[0].embedding, batchResponse.data[1].embedding, etc.

Step 6: Vision (Image Understanding)

// vision.ts — Analyze images with GPT-4o
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'What is in this image? Describe in detail.' },
      { type: 'image_url', image_url: { url: 'https://example.com/photo.jpg' } },
    ],
  }],
})

// Base64 image (from file upload)
const base64Image = fs.readFileSync('photo.jpg').toString('base64')
const response2 = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'Extract all text from this receipt.' },
      { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
    ],
  }],
})

Step 7: Structured Outputs

// structured.ts — Get JSON output matching a schema
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Extract info from: John Smith, 35, Software Engineer at Google, lives in SF' }],
  response_format: {
    type: 'json_schema',
    json_schema: {
      name: 'person_info',
      schema: {
        type: 'object',
        properties: {
          name: { type: 'string' },
          age: { type: 'number' },
          job_title: { type: 'string' },
          company: { type: 'string' },
          city: { type: 'string' },
        },
        required: ['name', 'age', 'job_title', 'company', 'city'],
      },
    },
  },
})
// Guaranteed to match the schema
const person = JSON.parse(response.choices[0].message.content!)

Examples

Example 1: Build a customer support chatbot with function calling

User prompt: "Build a chatbot for our e-commerce site that can check order status, search products, and answer FAQs using our knowledge base."

The agent will:

  1. Define tools for check_order_status, search_products, and search_knowledge_base.
  2. Create a chat endpoint with streaming for real-time responses.
  3. Implement the tool execution loop (GPT calls tool → execute → send result back).
  4. Add conversation history management for multi-turn interactions.

Example 2: Build a document analysis pipeline

User prompt: "Users upload contracts (PDF/images). Extract key terms, dates, and parties, then generate a structured summary."

The agent will:

  1. Use vision API to extract text from document images.
  2. Use structured outputs to extract entities into a typed JSON schema.
  3. Generate a plain-language summary with a system prompt tuned for legal documents.

Guidelines

  • Use gpt-4o for most tasks — it's the best balance of quality, speed, and cost. Use gpt-4o-mini for simple tasks where cost matters.
  • Always set a system message to define the assistant's behavior, constraints, and output format.
  • Use structured outputs (response_format: json_schema) when you need reliable JSON — it guarantees schema compliance, unlike asking for JSON in the prompt.
  • For function calling, define clear description fields — GPT uses these to decide when to call each function.
  • Use streaming for any user-facing chat interface — the perceived latency drops dramatically when users see tokens appear in real-time.
  • Set temperature: 0 for deterministic tasks (classification, extraction, code generation) and 0.7-1.0 for creative tasks (writing, brainstorming).
  • Track token usage via response.usage for cost monitoring. Embedding and completion costs differ significantly.