gaurv007 commited on
Commit
c146e54
·
verified ·
1 Parent(s): cdcee3d

v4.0: Add web /api/chat route for RAG chatbot

Browse files
Files changed (1) hide show
  1. web/app/api/chat/route.ts +37 -0
web/app/api/chat/route.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from "next/server";
2
+
3
+ const API_URL = process.env.CLAUSEGUARD_API_URL || "https://gaurv007-clauseguard-api.hf.space";
4
+
5
+ export async function POST(req: NextRequest) {
6
+ try {
7
+ const body = await req.json();
8
+ const { message, session_id, history } = body;
9
+
10
+ if (!message || !session_id) {
11
+ return NextResponse.json(
12
+ { error: "message and session_id are required" },
13
+ { status: 400 }
14
+ );
15
+ }
16
+
17
+ const response = await fetch(`${API_URL}/api/chat`, {
18
+ method: "POST",
19
+ headers: { "Content-Type": "application/json" },
20
+ body: JSON.stringify({ message, session_id, history: history || [] }),
21
+ });
22
+
23
+ if (!response.ok) {
24
+ const err = await response.text().catch(() => "");
25
+ throw new Error(err || `Backend error: ${response.status}`);
26
+ }
27
+
28
+ const result = await response.json();
29
+ return NextResponse.json(result);
30
+ } catch (error: any) {
31
+ console.error("Chat error:", error.message);
32
+ return NextResponse.json(
33
+ { error: error.message || "Chat failed. Try again." },
34
+ { status: 500 }
35
+ );
36
+ }
37
+ }