Skip to content

Instantly share code, notes, and snippets.

@elchappo
Forked from iceener/encoder.js
Created April 28, 2023 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchappo/a61199de3a636228e4bb540fec07eb9a to your computer and use it in GitHub Desktop.
Save elchappo/a61199de3a636228e4bb540fec07eb9a 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