login.js 1005 B

1234567891011121314151617181920212223242526272829303132333435
  1. const { r2rClient } = require("r2r-js");
  2. const client = new r2rClient("http://localhost:8000");
  3. module.exports = async function handler(req, res) {
  4. console.log("Received a request at /api/login");
  5. if (req.method === "POST") {
  6. const { query } = req.body;
  7. try {
  8. // Login with each request. In a production app, you'd want to manage sessions.
  9. await client.login("admin@example.com", "change_me_immediately");
  10. const response = await client.rag({
  11. query: query,
  12. rag_generation_config: {
  13. model: "gpt-4o",
  14. temperature: 0.0,
  15. stream: false,
  16. },
  17. });
  18. res.status(200).json({
  19. result: response.results.completion.choices[0].message.content,
  20. });
  21. } catch (error) {
  22. res.status(500).json({
  23. error: error instanceof Error ? error.message : "An error occurred",
  24. });
  25. }
  26. } else {
  27. res.setHeader("Allow", ["POST"]);
  28. res.status(405).end(`Method ${req.method} Not Allowed`);
  29. }
  30. };