Skip to content

Instantly share code, notes, and snippets.

@greenido
Created March 20, 2023 18:13
Show Gist options
  • Save greenido/c8d938eeeabe8c95063e7c3057ec9ebe to your computer and use it in GitHub Desktop.
Save greenido/c8d938eeeabe8c95063e7c3057ec9ebe to your computer and use it in GitHub Desktop.
An openai API example of using the DaVinci engine for language translation
const axios = require('axios');
const API_KEY = 'your_api_key_here';
const API_URL = 'https://api.openai.com/v1/';
async function translateText(text, targetLanguage) {
const response = await axios.post(`${API_URL}engines/davinci/translate`, {
text,
target_language: targetLanguage,
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
});
const translatedText = response.data.translations[0].text;
console.log(translatedText);
}
translateText('All the world is crazy', 'es');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment