Skip to content

Instantly share code, notes, and snippets.

@iceener
Last active July 9, 2023 18:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save iceener/a46c185122eb8f061ed02cc0494a2577 to your computer and use it in GitHub Desktop.
Save iceener/a46c185122eb8f061ed02cc0494a2577 to your computer and use it in GitHub Desktop.
gpt-3-encoder-netlify-function.js
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