Skip to content

Instantly share code, notes, and snippets.

@Bekz7
Forked from iceener/encoder.js
Created May 1, 2023 08:30
Show Gist options
  • Save Bekz7/cedec8e969d53fe7065ce8fc1ffa4a78 to your computer and use it in GitHub Desktop.
Save Bekz7/cedec8e969d53fe7065ce8fc1ffa4a78 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