Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Performance Optimization with OpenAI API

Introduction

Performance optimization is crucial for efficiently using the OpenAI API. This tutorial covers various strategies to optimize performance, including batching requests, reducing token usage, and caching, with examples in JavaScript and Python.

Batching Requests

Batching requests can significantly reduce the number of API calls and improve performance. Instead of sending individual requests for each input, you can batch multiple inputs into a single request.

// Example in JavaScript

const axios = require('axios');

const API_KEY = process.env.OPENAI_API_KEY;

const requests = [
    { prompt: "Translate the following English text to French: 'Hello, how are you?'" },
    { prompt: "Translate the following English text to Spanish: 'Good morning, everyone.'" }
];

axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', 
    { requests: requests },
    {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${API_KEY}`
        }
    })
.then(response => {
    console.log('API Response:', response.data);
})
.catch(error => {
    console.error('Error:', error);
});
                    
# Example in Python

import os
import requests

API_KEY = os.getenv('OPENAI_API_KEY')

requests = [
    {'prompt': "Translate the following English text to French: 'Hello, how are you?'"},
    {'prompt': "Translate the following English text to Spanish: 'Good morning, everyone.'"}
]

response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions',
                         json={'requests': requests},
                         headers={'Content-Type': 'application/json',
                                  'Authorization': f'Bearer {API_KEY}'})

print('API Response:', response.json())
                    

Reducing Token Usage

Reducing the number of tokens used in requests can lower costs and improve performance. Here are some tips:

  • Use shorter prompts.
  • Avoid unnecessary context or instructions.
  • Summarize long texts before sending them.
// Example in JavaScript

const API_KEY = process.env.OPENAI_API_KEY;

const requestData = {
    prompt: "Translate to French: 'Hello, how are you?'",
    max_tokens: 60
};

axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', requestData, {
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${API_KEY}`
    }
})
.then(response => {
    console.log('API Response:', response.data);
})
.catch(error => {
    console.error('Error:', error);
});
                    
# Example in Python

API_KEY = os.getenv('OPENAI_API_KEY')

request_data = {
    'prompt': "Translate to French: 'Hello, how are you?'",
    'max_tokens': 60
}

response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions',
                         json=request_data,
                         headers={'Content-Type': 'application/json',
                                  'Authorization': f'Bearer {API_KEY}'})

print('API Response:', response.json())
                    

Caching

Implementing caching can reduce the number of API calls and improve response times. Cache responses for frequently requested data to avoid redundant API calls.

// Example in JavaScript

const cache = {};

const fetchData = async (prompt) => {
    if (cache[prompt]) {
        return cache[prompt];
    }

    const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', 
        { prompt: prompt, max_tokens: 60 },
        {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${API_KEY}`
            }
        });

    cache[prompt] = response.data;
    return response.data;
};

fetchData("Translate to French: 'Hello, how are you?'")
    .then(data => console.log('API Response:', data))
    .catch(error => console.error('Error:', error));
                    
# Example in Python

cache = {}

def fetch_data(prompt):
    if prompt in cache:
        return cache[prompt]

    response = requests.post('https://api.openai.com/v1/engines/davinci-codex/completions',
                             json={'prompt': prompt, 'max_tokens': 60},
                             headers={'Content-Type': 'application/json',
                                      'Authorization': f'Bearer {API_KEY}'})

    cache[prompt] = response.json()
    return response.json()

data = fetch_data("Translate to French: 'Hello, how are you?'")
print('API Response:', data)