-
-
Save elchappo/a61199de3a636228e4bb540fec07eb9a to your computer and use it in GitHub Desktop.
gpt-3-encoder-netlify-function.js
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 { 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