सामग्री पर जाएँ

OpenAI API ट्यूटोरियल

OpenAI API tutorial: keys and setup, text generation, structured outputs, tool calling, embeddings and retrieval, images and audio, cost, safety and projects.

Getting Started

  1. 1 What Is the OpenAI API? Learn what the OpenAI API is, how it differs from ChatGPT, what you need before you start, what this tutorial can run, and how the course is organized.
  2. 2 API Keys, Projects and Setup Create an OpenAI API key, store it safely in an environment variable, install the official SDK, and organize projects, limits and key rotation.
  3. 3 Your First Request with the Responses API Send your first Responses API request in Python or curl, read output_text and output items, check status and usage, and compare it with Chat Completions.

Generating Text

  1. 4 Instructions and Message Roles Use developer, user and assistant roles and the instructions parameter to steer OpenAI models, write clear prompts and keep untrusted text separate.
  2. 5 Conversation State: Multi-Turn Chats Build multi-turn chats with a manual history list, previous_response_id or the Conversations API, and manage storage, retention, cost and context limits.
  3. 6 Streaming Responses Stream OpenAI API output with stream=True, handle typed events such as text deltas and completion, relay them to browsers and cope with broken streams.
  4. 7 Models and Reasoning Effort Compare OpenAI API models as of December 2026, set reasoning effort, understand reasoning tokens, and choose a model by testing quality, speed and cost.

Structured Outputs

  1. 8 Structured Outputs with JSON Schema Get predictable JSON from the OpenAI API with Structured Outputs: text.format with json_schema, strict mode rules, Pydantic parsing and refusals.
  2. 9 Validating Structured Results Check the meaning of structured model output with business rules, evidence quotes, safe retries and human review before your program acts on the data.

Tool Calling and Agents

  1. 10 Function Calling with the Responses API Define function tools, handle function_call items, return function_call_output results, control tool_choice and run a safe, bounded tool-calling loop.
  2. 11 Built-In Tools and Remote MCP Use OpenAI's hosted tools such as web search and file search, connect remote MCP servers with approvals, show citations and manage cost and risk.
  3. 12 The OpenAI Agents SDK Learn the OpenAI Agents SDK: agents, the runner, function tools and handoffs, how it compares with the hosted Agents API, and when to keep things simple.

Embeddings and Retrieval

  1. 13 Embeddings and Semantic Search Create OpenAI embeddings, compare vectors with cosine similarity, shorten dimensions, store vectors in a database and build a small semantic search.
  2. 14 Vector Stores and File Search Use hosted OpenAI vector stores: upload files, search them, filter by attributes, tune chunking, use the file_search tool and read file citations.

Images, Audio and Realtime

  1. 15 Images and Files as Input Send images and PDF files to the OpenAI API with input_image and input_file, choose URLs, uploads or Base64, set detail and verify extracted facts.
  2. 16 Image Generation and Editing Generate and edit images with the OpenAI Image API and the image_generation tool, decode Base64 results, write clear prompts and review output safely.
  3. 17 Speech and Realtime Voice Transcribe audio, turn text into speech and understand realtime voice sessions with WebRTC and client secrets, including deprecations to avoid in 2027.

Reliability and Cost

  1. 18 Errors, Retries and Rate Limits Handle OpenAI API errors, understand rate limits and 429 or 503 responses, configure SDK retries and timeouts, and add backoff without duplicate work.
  2. 19 Tokens, Costs and Prompt Caching Estimate and control OpenAI API costs: read token usage, count input tokens, calculate prices, use prompt caching and set budgets for your app.
  3. 20 Batch API, Background Mode and Webhooks Run large or slow OpenAI jobs with the Batch API and background mode: build JSONL files, match results by custom_id, poll status and verify webhooks.

Safety and Production

  1. 21 Safety and Moderation Protect your OpenAI app with the free moderation endpoint, safety identifiers, input limits, prompt injection defenses, human review and user reporting.
  2. 22 Testing and Evaluating Your OpenAI App Test OpenAI apps with fake clients, a labeled evaluation set, code checks and rubric grading, then compare prompt and model changes before release.
  3. 23 Production Checklist and Data Privacy Prepare an OpenAI app for launch: data privacy and retention, logging, deprecations, rollouts, monitoring, cost controls and a practical checklist.

Projects and Reference

  1. 24 Project: Support Ticket Triage Build a support ticket triage service with Structured Outputs, validation, routing rules, a stand-in model, tests and human review for risky cases.
  2. 25 Project: A Document Assistant with RAG Build a retrieval augmented generation assistant that searches your documents, answers with citations, refuses to guess and checks access and quality.
  3. 26 OpenAI API Glossary and Cheat Sheet A quick OpenAI API reference: Responses API calls, parameters, output types, tools, error handling, retired features and a glossary of key terms.