-
-
Save daichan4649/98e4b23b9e6d64cb484a53499814ff20 to your computer and use it in GitHub Desktop.
for blog
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
// env | |
const config = useRuntimeConfig() | |
const OPENAI_API_KEY = config.openaiApiKey | |
// model | |
const MODEL_GPT35 = { name: 'gpt-3.5-turbo', endpoint: 'https://api.openai.com/v1/chat/completions' } | |
// const MODEL_GPT4 = { name: 'gpt-4', endpoint: 'https://api.openai.com/v1/chat/completions' } | |
const model = MODEL_GPT35 | |
export default defineEventHandler(async (event) => { | |
const { prompt } = await readBody(event) | |
const payload = { | |
model: model.name, | |
'messages': [ | |
{ 'role': 'system', 'content': 'You are a helpful assistant.' }, | |
{ 'role': 'user', 'content': prompt }, | |
] | |
} | |
console.log('payload', payload) | |
const response = await fetch(model.endpoint, { | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bearer ${OPENAI_API_KEY ?? ''}`, | |
}, | |
method: 'POST', | |
body: JSON.stringify(payload), | |
}) | |
const json = await response.json() | |
console.log(JSON.stringify(json)) | |
return json | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment