MCPcopy Create free account
hub / github.com/Archie818/jobeasy / handler

Function handler

pages/api/gpt-example.js:3–35  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

1import axios from "axios";
2
3export default async function handler(req, res) {
4 if (req.method === "POST") {
5 try {
6 const { prompt } = req.body;
7 console.log("prompt", prompt);
8
9 const response = await axios.post(
10 "https://api.openai.com/v1/engines/text-davinci-003/completions",
11 {
12 prompt: prompt,
13 max_tokens: 50,
14 },
15 {
16 headers: {
17 "Content-Type": "application/json",
18 Authorization: `Bearer OPENAI_API_KEY`,
19 },
20 }
21 );
22
23 const data = response.data.choices[0].text;
24 console.log("data", data);
25
26 res.status(200).json({ data: data });
27 } catch (error) {
28 console.error("Error in GPT API request:", error.message); // Log the error message
29 console.error("Error response data:", error.response.data); // Log the error response data
30 res.status(500).json({ error: error.message });
31 }
32 } else {
33 res.status(405).json({ error: "Method not allowed" });
34 }
35}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected