import { Configuration, OpenAIApi } from "openai"; import { GoogleCustomSearch } from "openai-function-calling-tools"; export default function handler(req, res) { if (req.method !== 'GET') { res.status(405).send({ error: 'Method Not Allowed', method: req.method }); return; } const QUESTION = req.query.question; if (!QUESTION) { res.status(400).send({ error: 'Question is missing in request' }); return; } const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration); const messages = [ { role: "user", content: QUESTION, }, ]; const { googleCustomSearch, googleCustomSearchSchema } = new GoogleCustomSearch({ apiKey: process.env.API_KEY, googleCSEId: process.env.CONTEXT_KEY, }); const functions = { googleCustomSearch, }; const getCompletion = async (messages) => { const response = await openai.createChatCompletion({ model: "gpt-3.5-turbo-0613", messages, functions: [googleCustomSearchSchema], temperature: 0, }); return response; }; res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); const processCompletions = async () => { let response; while (true) { response = await getCompletion(messages); console.log(response); if (response.data.choices[0].finish_reason === "stop") { res.write(`data: ${JSON.stringify({ result: response.data.choices[0].message.content })}\n\n`); break; } else if (response.data.choices[0].finish_reason === "function_call") { const fnName = response.data.choices[0].message.function_call.name; const args = response.data.choices[0].message.function_call.arguments; const fn = functions[fnName]; const result = await fn(...Object.values(JSON.parse(args))); messages.push({ role: "assistant", content: "", function_call: { name: fnName, arguments: args, }, }); messages.push({ role: "function", name: fnName, content: JSON.stringify({ result: result }), }); } } }; processCompletions().catch(err => { console.error(err); res.status(500).send({ error: 'Internal Server Error' }); }); }