-
-
Save jdanproject/af25f98d1b4a8cb03599995eb681251b to your computer and use it in GitHub Desktop.
gpt-3-encoder-netlify-function.js
This file contains hidden or 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 { encode } = require('@nem035/gpt-3-encoder') | |
exports.handler = async (event, context) => { | |
if (event.httpMethod === 'POST') { | |
try { | |
const payload = JSON.parse(event.body); | |
const text = payload.text; | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
tokens: encode(text).length | |
}), | |
}; | |
} catch (error) { | |
return { | |
statusCode: 500, | |
body: JSON.stringify({ error: error.message }), | |
}; | |
} | |
} else { | |
return { | |
statusCode: 501, | |
body: JSON.stringify({ message: "Not Implemented" }), | |
headers: { 'content-type': 'application/json' } | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment