greenfield-hippos / backend

0 stars 1 forks source link

JSON request/response how to (Open AI API) #2

Closed laurencecnerual closed 2 days ago

vicentbe10 commented 2 days ago
  1. Differences Between the Main OpenAI Models

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-4 Models

  1. Deciding the right model

For an educational chatbot application, probably the best option is using gpt-3.5-turbo. Here’s why:

•   Cost-Effective: It offers a good balance between performance and cost, important for managing API usage within a tight project budget.
•   Optimized for Chat: Designed specifically for conversational interfaces, making it ideal for chat-based application.
•   Strong Performance: Provides coherent and contextually appropriate responses suitable for educational purposes.
vicentbe10 commented 2 days ago
  1. Making Requests: Options and Parameters

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:

vicentbe10 commented 2 days ago

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);
vicentbe10 commented 2 days ago
  1. Understanding Responses and Backend Integration

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
  }
}```
vicentbe10 commented 2 days ago

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

  1. 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

  1. 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

  1. Total Cost per Interaction

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.