Created
January 29, 2025 08:58
-
-
Save fayazara/bc2b86bde3e2da230fc21c679f205ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// <reference path="../pb_data/types.d.ts" /> | |
routerAdd('GET', '/ai-chat', async (e) => { | |
try { | |
const OpenAI = require(`${__hooks}/node_modules/openai/index.js`); | |
const openai = new OpenAI({ | |
apiKey: 'OPEN_API_KEY', | |
}); | |
const completion = await openai.chat.completions.create({ | |
model: 'gpt-4o-mini', | |
messages: [ | |
{ role: 'system', content: 'You are a helpful assistant.' }, | |
{ | |
role: 'user', | |
content: 'Write a haiku about recursion in programming.', | |
}, | |
], | |
}); | |
return e.json(200, { | |
message: completion.choices[0].message, | |
status: 'success', | |
}); | |
} catch (error) { | |
console.error('OpenAI Error:', error); | |
return e.json(500, { | |
error: error.message, | |
status: 'error', | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment