load_dotenv()
True
Benedict Thekkel
For Python integration:
A typical API request sends a conversation history and receives a model-generated response.
POST https://api.openai.com/v1/chat/completions
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
{
"id": "chatcmpl-7aA9aB...",
"object": "chat.completion",
"created": 1689219200,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The solar system consists of the Sun and all the objects that orbit it..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 30,
"total_tokens": 50
}
}
Parameter | Description | Default |
---|---|---|
model |
Specifies the model (e.g., gpt-4 , gpt-3.5-turbo ). |
Required |
messages |
List of conversation messages with roles (system , user , assistant ). |
Required |
temperature |
Controls response randomness (0 = deterministic, 1 = more random). | 1 |
max_tokens |
Limits the length of the response. | 4096 tokens (model-specific) |
top_p |
Controls diversity via nucleus sampling. Lower values focus responses on high-probability words. | 1 |
frequency_penalty |
Penalizes repeated phrases (higher values reduce repetition). | 0 |
presence_penalty |
Encourages discussion of new topics (higher values make responses more varied). | 0 |
Maintain context by appending past interactions to the messages
array:
[
{"role": "system", "content": "You are an AI tutor."},
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What about Germany?"}
]
gpt-4
: Higher cost, more powerful.gpt-3.5-turbo
: Cheaper, suitable for many use cases.Common API errors: - 401 Unauthorized: Invalid or missing API key. - 429 Too Many Requests: Exceeded rate limit or quota. - 500 Server Error: Retry after some time.
Define assistant behavior:
Train custom models with domain-specific data.
Stream results for real-time applications:
Define and call functions for structured outputs:
"functions": [
{
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["C", "F"]}
}
}
}
]
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "your-api-key",
});
const openai = new OpenAIApi(configuration);
async function ask() {
const response = await openai.createChatCompletion({
model: "gpt-4",
messages: [
{ role: "system", content: "You are
::: {#c7b8debd-1c0d-42ea-b0be-b38f3af21909 .cell}
``` {.python .cell-code}
from openai import OpenAI
import json
from dotenv import load_dotenv
import os
from IPython.display import Markdown, display
:::
To get remaining credits through an API, the specific method can vary depending on the service you are using. However, I can provide you with general steps that you might follow to retrieve remaining credits via an API. Here’s a typical process:
Read the Documentation: Start by checking the API documentation for the platform you are using. Look for endpoints related to user accounts or usage stats, which may include remaining credits.
Authentication: Ensure you are authenticated to make API calls. This typically involves:
Make a Request: Identify the endpoint