gpt client in javascript

To use the OpenAI GPT-3 API in JavaScript, you typically need to send HTTP requests to the API endpoint. Here's a basic example using fetch to make a POST request to the GPT-3 API:

index.tsx
const apiKey = 'YOUR_API_KEY';
const prompt = 'Once upon a time';
const maxTokens = 100;

fetch('https://api.openai.com/v1/engines/davinci/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apiKey}`
    },
    body: JSON.stringify({
      prompt: prompt,
      max_tokens: maxTokens
    })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
485 chars
19 lines

Make sure to replace 'YOUR_API_KEY' with your actual OpenAI API key. This example sends a prompt ('Once upon a time') to the GPT-3 engine called davinci and requests a completion with a maximum of 100 tokens.

Please note that using GPT-3 requires an API key and there may be rate limits or usage charges associated with it, depending on your plan.

Remember to handle errors and responses according to your specific use case.

related categories

gistlibby LogSnag