|
1 | 1 | import express from "express"; |
2 | 2 | import cors from "cors"; |
3 | 3 | import dotenv from "dotenv"; |
4 | | -import fetch from "node-fetch"; |
5 | 4 | import { megaSearch } from "./mega-search/mega-search.js"; |
6 | 5 | import { getFlashcards } from "./flashcards/flashcards.js"; |
7 | 6 | import { reporterAPIHandler } from "./reporter-input-response/reporter-API-handler.js"; |
| 7 | +import { reporterStreamHandler } from "./reporter-input-response/reporter-stream-handler.js"; |
8 | 8 |
|
9 | 9 | const app = express(); |
10 | 10 |
|
@@ -46,90 +46,25 @@ app.post("/flashcards/v1", async (req, res) => { |
46 | 46 | message: "Access code accepted", |
47 | 47 | cardSets: await getFlashcards("v1"), |
48 | 48 | }; |
49 | | - // console.log(responseData); |
50 | 49 | res.json(responseData); |
51 | 50 | } else { |
52 | 51 | res.status(401).send("Unauthorized"); |
53 | 52 | } |
54 | 53 | }); |
55 | 54 |
|
| 55 | +app.post("/reporter/stream", async (req, res) => { |
| 56 | + console.log("post call to /reporter/stream"); |
| 57 | + console.log(req.body); |
| 58 | + reporterStreamHandler(req, res); |
| 59 | +}); |
| 60 | + |
56 | 61 | app.post("/reporter", async (req, res) => { |
57 | 62 | console.log("post call to /reporter"); |
58 | 63 | console.log(req.body); |
59 | 64 | var response = await reporterAPIHandler(req.body); |
60 | 65 | res.json(response); |
61 | 66 | }); |
62 | 67 |
|
63 | | -app.post("/test/chat", async (req, res) => { |
64 | | - const { message } = req.body; |
65 | | - |
66 | | - const response = await fetch("https://api.openai.com/v1/chat/completions", { |
67 | | - method: "post", |
68 | | - headers: { |
69 | | - "Content-Type": "application/json", |
70 | | - Authorization: `Bearer ${process.env.REPORTER_OPENAI_API_KEY}`, |
71 | | - }, |
72 | | - body: JSON.stringify({ |
73 | | - model: "gpt-3.5-turbo", |
74 | | - messages: [{ role: "user", content: message }], |
75 | | - stream: true, |
76 | | - }), |
77 | | - }); |
78 | | - |
79 | | - res.setHeader("Content-Type", "text/event-stream"); |
80 | | - res.setHeader("Cache-Control", "no-cache"); |
81 | | - |
82 | | - // Initialize variables to handle streaming data |
83 | | - let buffer = ""; |
84 | | - |
85 | | - response.body.on("data", (chunk) => { |
86 | | - buffer += chunk.toString(); |
87 | | - |
88 | | - // Split the buffer by newlines |
89 | | - const lines = buffer.split("\n"); |
90 | | - |
91 | | - // Keep the last partial line in the buffer |
92 | | - buffer = lines.pop(); |
93 | | - |
94 | | - for (const line of lines) { |
95 | | - const message = line.trim(); |
96 | | - |
97 | | - // Ignore empty lines |
98 | | - if (!message) continue; |
99 | | - |
100 | | - // Stream finished |
101 | | - if (message === "data: [DONE]") { |
102 | | - return res.end(); |
103 | | - } |
104 | | - |
105 | | - if (message.startsWith("data: ")) { |
106 | | - const jsonString = message.replace("data: ", ""); |
107 | | - try { |
108 | | - const parsed = JSON.parse(jsonString); |
109 | | - const content = parsed.choices[0].delta.content; |
110 | | - if (content) { |
111 | | - console.log(content); |
112 | | - if (content == " ") console.log("space"); |
113 | | - res.write(`data: ${content}\n\n`); |
114 | | - } |
115 | | - } catch (error) { |
116 | | - // Handle JSON parsing errors |
117 | | - console.error("Error parsing JSON:", error); |
118 | | - } |
119 | | - } |
120 | | - } |
121 | | - }); |
122 | | - |
123 | | - response.body.on("end", () => { |
124 | | - res.end(); |
125 | | - }); |
126 | | - |
127 | | - response.body.on("error", (error) => { |
128 | | - console.error("Error with OpenAI API stream:", error); |
129 | | - res.end(); |
130 | | - }); |
131 | | -}); |
132 | | - |
133 | 68 | const PORT = process.env.PORT || 3000; |
134 | 69 | app.listen(PORT, () => { |
135 | 70 | console.log(`Server is running on port ${PORT}`); |
|
0 commit comments