[READ-ONLY] Mirror of https://github.com/danielroe/devhub-north-2024.
0

Configure Feed

Select the types of activity you want to include in your feed.

devhub-north-2024 / server / api / completion.post.ts
976 B 31 lines
1import { OpenAI } from 'openai' 2 3export default defineEventHandler(async event => { 4 const { prompt, malleables } = await readBody(event) 5 const api = new OpenAI({ 6 apiKey: useRuntimeConfig().openai.token 7 }) 8 const result = await api.chat.completions.create({ 9 model: 'gpt-3.5-turbo-16k-0613', 10 messages: [ 11 { 12 role: 'system', 13 content: 'You are an AI who is responsible for manipulating the state of a web application to suit user needs. You only speak JSON.' 14 }, 15 { 16 role: 'system', 17 content: 'The current state of the application is:\n\n' + JSON.stringify(malleables, null, 2) 18 }, 19 { 20 role: 'user', 21 content: prompt 22 }, 23 { 24 role: 'system', 25 content: 'Print the current state of the application reflecting the user\'s request:\n\n' 26 }, 27 ] 28 }) 29 console.log(result.choices[0].message.content) 30 return JSON.parse(result.choices[0].message.content as string) 31})