Created
March 20, 2023 18:13
-
-
Save greenido/c8d938eeeabe8c95063e7c3057ec9ebe to your computer and use it in GitHub Desktop.
An openai API example of using the DaVinci engine for language translation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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