Closed laurencecnerual closed 2 days ago
API Endpoint
• URL: https://api.openai.com/v1/chat/completions (for chat-based models like gpt-3.5-turbo).
• Method: POST
Request Headers
• Authorization: Include the API key in the header.
• Authorization: Bearer OPENAI_API_KEY
• Content-Type: application/json.
Request Body Parameters
Here are the key parameters to use in our requests:
model (string, required): The model we are are using (potentially gpt-3.5-turbo).
messages (array, required): A list of message objects that form the conversation. Each message has two properties: • role: The role of the message author ("system", "user", or "assistant"). • content: The content of the message.
temperature (number, optional): Controls the randomness of the output. • Range: 0 to 2. • Lower values make the output more deterministic; higher values make it more creative. • Default: 1.0
max_tokens (integer, optional): The maximum number of tokens to generate in the response. • Note: The total tokens in the input and output are limited (e.g., 4096 tokens for gpt-3.5-turbo).
top_p (number, optional): An alternative to sampling with temperature, called nucleus sampling. • Default: 1.0
frequency_penalty (number, optional): Penalizes new tokens based on their frequency so far. • Range: -2.0 to 2.0 • Positive values discourage repetition.
presence_penalty (number, optional): Penalizes new tokens based on whether they appear in the text so far. • Range: -2.0 to 2.0 • Positive values encourage introducing new topics.
Example Request :
import OpenAI from "openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create(
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a programming tutor helping students understand coding concepts without providing direct code examples. Encourage critical thinking and problem-solving."
},
{
"role": "user",
"content": "Can you explain what closures are in JavaScript? Could you provide some code examples?"
}
],
"temperature": 0.7,
"max_tokens": 500,
"frequency_penalty": 0.5,
"presence_penalty": 0.0
});
console.log(completion.choices[0].message);
Response Structure
The API returns a JSON object with the following key properties:
Example Response :
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1697059200,
"model": "gpt-3.5-turbo-0301",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "In JavaScript, a closure is a function that has access to its own scope, the outer function's scope, and the global scope. It allows a function to access variables from an enclosing scope or environment even after it leaves the scope in which it was created. Closures are useful for creating private variables and functions, and they enable powerful programming patterns like currying and function factories."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 85,
"total_tokens": 185
}
}```
Based on the previous example of interaction:
"usage": {
"prompt_tokens": 100,
"completion_tokens": 85,
"total_tokens": 185
}
OpenAI Pricing for GPT-3.5-Turbo
As of my knowledge cutoff in September 2023, the pricing for the GPT-3.5-Turbo model is:
• Prompt (Input) Tokens: $0.0015 per 1,000 tokens
• Completion (Output) Tokens: $0.0020 per 1,000 tokens
Calculating Cost per Interaction
Cost of Prompt Tokens
• Number of Prompt Tokens: 100 tokens • Cost per 1,000 Tokens: $0.0015
Calculation:
Cost of Prompt Tokens = (100/1000) x $0.0015 = $0.00015
Cost of Completion Tokens
• Number of Completion Tokens: 85 tokens • Cost per 1,000 Tokens: $0.0020
Calculation:
Cost of Completion Tokens = (85/1000) x $0.0020 = $0.00017
Calculation:
Total Cost per Interaction = $0.00015 + $0.00017 = $0.00032
Calculating Number of Interactions with $10
Total Budget: $10
Calculation:
Number of Interactions = $10/$0.00032 ~ 31,250 interactions
Summary
With an average usage of 185 tokens per interaction (100 prompt tokens and 85 completion tokens), you can handle approximately 31,250 questions and replies with a budget of $10.
For some context let's see a quick overview of the available solutions. OpenAI offers several models, each with different capabilities, performance characteristics, and costs. The main models we should consider are from the GPT-3.5 and GPT-4 series.
GPT-3.5 Models
gpt-3.5-turbo
• Description: Optimized for conversational applications and chat interfaces. • Capabilities: Excellent at understanding context in a conversation, maintaining state, and generating coherent, contextually appropriate responses. • Use Cases: Chatbots, virtual assistants, interactive tutorials. • Cost: More cost-effective compared to GPT-4 models.
text-davinci-003
• Description: One of the most capable models in the GPT-3 series for generating detailed and complex text. • Capabilities: Excels at tasks requiring understanding of complex instructions, creative writing, and content generation. • Use Cases: Content creation, detailed explanations, problem-solving assistance. • Cost: Higher cost per token compared to other GPT-3 models but less than GPT-4.
GPT-4 Models
gpt-4
• Description: OpenAI’s latest and most advanced model with enhanced reasoning and comprehension abilities. • Capabilities: Better at understanding nuanced instructions, handling complex tasks, and producing more accurate and context-aware responses. • Use Cases: Advanced tutoring systems, complex problem-solving, applications requiring high accuracy. • Cost: More expensive than GPT-3.5 models. • Availability: May require access approval and has usage limits.
For an educational chatbot application, probably the best option is using gpt-3.5-turbo. Here’s why: