Skip to content

Instantly share code, notes, and snippets.

@fayazara
Created January 29, 2025 08:58
Show Gist options
  • Save fayazara/bc2b86bde3e2da230fc21c679f205ac9 to your computer and use it in GitHub Desktop.
Save fayazara/bc2b86bde3e2da230fc21c679f205ac9 to your computer and use it in GitHub Desktop.
/// <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