Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created August 12, 2023 14:34
Show Gist options
  • Save daichan4649/98e4b23b9e6d64cb484a53499814ff20 to your computer and use it in GitHub Desktop.
Save daichan4649/98e4b23b9e6d64cb484a53499814ff20 to your computer and use it in GitHub Desktop.
for blog
// 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