search.js 539 B

1234567891011121314151617181920212223
  1. import { Router } from "express";
  2. import { z } from "zod";
  3. import { searchDocs } from "../chat/searchChat.js";
  4. export const searchRouter = Router();
  5. const searchBodySchema = z.object({
  6. query: z.string().min(1),
  7. topK: z.number().int().positive().optional()
  8. });
  9. searchRouter.post("/", async (req, res, next) => {
  10. try {
  11. const body = searchBodySchema.parse(req.body);
  12. const results = await searchDocs({
  13. query: body.query,
  14. topK: body.topK
  15. });
  16. res.json({ results });
  17. } catch (err) {
  18. next(err);
  19. }
  20. });