ChenyuRabbitLove commited on
Commit
f1b9d63
·
1 Parent(s): 8412443

chore: modify backend api and chatbot comp headers

Browse files
Files changed (3) hide show
  1. app/components/ChatBot.tsx +5 -0
  2. backend/main.py +4 -1
  3. next.config.ts +9 -2
app/components/ChatBot.tsx CHANGED
@@ -31,6 +31,11 @@ export default function LandingPageChatBot() {
31
  const { messages: rawMessages, input, handleInputChange, handleSubmit, isLoading, append } = useChat({
32
  api: '/api/landing_page_chat',
33
  streamProtocol: 'data',
 
 
 
 
 
34
  onError: (error) => {
35
  console.error('Chat error:', error);
36
  },
 
31
  const { messages: rawMessages, input, handleInputChange, handleSubmit, isLoading, append } = useChat({
32
  api: '/api/landing_page_chat',
33
  streamProtocol: 'data',
34
+ headers: {
35
+ 'Accept': 'text/event-stream',
36
+ 'Cache-Control': 'no-cache',
37
+ 'Connection': 'keep-alive',
38
+ },
39
  onError: (error) => {
40
  console.error('Chat error:', error);
41
  },
backend/main.py CHANGED
@@ -71,10 +71,13 @@ async def stream_text(messages: List[Message]):
71
  yield f"Error: {str(e)}".encode('utf-8')
72
 
73
  @app.post("/api/landing_page_chat")
74
- async def landing_page_chat(request: ChatRequest,):
75
  response = StreamingResponse(
76
  stream_text(request.messages),
 
77
  )
 
 
78
  response.headers['x-vercel-ai-data-stream'] = 'v1'
79
  return response
80
 
 
71
  yield f"Error: {str(e)}".encode('utf-8')
72
 
73
  @app.post("/api/landing_page_chat")
74
+ async def landing_page_chat(request: ChatRequest):
75
  response = StreamingResponse(
76
  stream_text(request.messages),
77
+ media_type='text/event-stream',
78
  )
79
+ response.headers['Cache-Control'] = 'no-cache'
80
+ response.headers['Connection'] = 'keep-alive'
81
  response.headers['x-vercel-ai-data-stream'] = 'v1'
82
  return response
83
 
next.config.ts CHANGED
@@ -6,10 +6,17 @@ const nextConfig: NextConfig = {
6
  return [
7
  {
8
  source: '/api/:path*',
9
- destination: 'http://localhost:8000/api/:path*' // FastAPI backend
 
 
 
 
 
 
 
10
  }
11
  ]
12
- }
13
  };
14
 
15
  export default nextConfig;
 
6
  return [
7
  {
8
  source: '/api/:path*',
9
+ destination: 'http://localhost:8000/api/:path*',
10
+ has: [
11
+ {
12
+ type: 'header',
13
+ key: 'accept',
14
+ value: 'text/event-stream',
15
+ },
16
+ ],
17
  }
18
  ]
19
+ },
20
  };
21
 
22
  export default nextConfig;