Vitrous commited on
Commit
a3b42bd
·
verified ·
1 Parent(s): a498248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -100,23 +100,31 @@ async def api_home():
100
  # Endpoint to start a new conversation thread
101
  @app.post('/start_conversation/')
102
  async def start_conversation(request: Request):
103
- data = await request.json()
104
- prompt = data.get('prompt')
 
105
 
106
- # Generate a response for the initial prompt
107
- response = generate_response(prompt)
108
 
109
- # Create a new conversation thread and store the prompt and response
110
- thread_id = len(conversations) + 1
111
- conversations[thread_id] = {'prompt': prompt, 'responses': [response]}
112
 
113
- return {'thread_id': thread_id, 'response': response}
 
114
 
 
 
 
 
 
 
 
 
 
115
 
116
-
117
- # Endpoint to get the response of a conversation thread
118
 
119
-
120
  @app.get('/get_response/{thread_id}')
121
  async def get_response(thread_id: int):
122
  if thread_id not in conversations:
 
100
  # Endpoint to start a new conversation thread
101
  @app.post('/start_conversation/')
102
  async def start_conversation(request: Request):
103
+ try:
104
+ data = await request.json()
105
+ prompt = data.get('prompt')
106
 
107
+ if not prompt:
108
+ raise HTTPException(status_code=400, detail="No prompt provided")
109
 
110
+ # Check if conversations dictionary is empty
111
+ if not conversations:
112
+ raise HTTPException(status_code=404, detail="No chat history available")
113
 
114
+ # Generate a response for the initial prompt
115
+ response = generate_response(prompt)
116
 
117
+ # Create a new conversation thread and store the prompt and response
118
+ thread_id = len(conversations) + 1
119
+ conversations[thread_id] = {'prompt': prompt, 'responses': [response]}
120
+
121
+ return {'thread_id': thread_id, 'response': response}
122
+ except HTTPException:
123
+ raise # Re-raise HTTPException to return it directly
124
+ except Exception as e:
125
+ raise HTTPException(status_code=500, detail=str(e))
126
 
 
 
127
 
 
128
  @app.get('/get_response/{thread_id}')
129
  async def get_response(thread_id: int):
130
  if thread_id not in conversations: