-
Notifications
You must be signed in to change notification settings - Fork 1
POST ask_v2
Do Le Long An edited this page Aug 23, 2024
·
1 revision
-
Endpoint:
POST /ask_v2/{user_id}
-
Tag:
rag-system
- Description: This endpoint processes a user’s question by leveraging a RAG (Retrieval-Augmented Generation) system. It fetches the user's relevant transcripts, saves them as a temporary JSON file, and uses this data to answer the question. The file is then removed after processing. The response includes the generated answer.
POST /ask_v2/{user_id}
-
user_id
(required): The unique identifier for the user whose transcripts will be processed.
-
question_body
(required): A JSON object containing the question details.
{
"question": "What is the patient's diagnosis?",
"source_type": "json"
}
-
Fields:
-
question
(string, required): The question that the user wants to ask the RAG system. -
source_type
(string, required): The type of source data being used (e.g.,json
).
-
Here’s an example of how to make a request to this endpoint:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<void> askQuestion(String userId, String question, String sourceType) async {
final String baseUrl = "https://medvoice-fastapi.ngrok.dev";
final Uri url = Uri.parse('$baseUrl/ask_v2/$userId');
final response = await http.post(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'question': question,
'source_type': sourceType,
}),
);
if (response.statusCode == 200) {
final responseBody = jsonDecode(response.body);
print('Response: ${responseBody['response']}');
} else {
print('Failed to process question: ${response.statusCode}');
}
}
Example Usage in Dart:
void main() async {
await askQuestion(
'12345',
'What is the patient\'s diagnosis?',
'json',
);
}
-
Status Code:
200 OK
-
Content-Type:
application/json
{
"response": "The patient is diagnosed with hypertension.",
"message": "Question answered successfully"
}
-
Status Code:
500 Internal Server Error
- This status is returned if there is an error during processing. The error details will be included in the response body.
A successful response might look like this:
{
"response": "The patient is diagnosed with hypertension.",
"message": "Question answered successfully"
}
-
File Handling:
- The backend first checks if a file named
patients_from_user_{user_id}.json
already exists in theassets
directory. If it does, the file is removed before proceeding. - The user's transcripts are then fetched and saved as a new JSON file (
patients_from_user_{user_id}.json
).
- The backend first checks if a file named
-
RAG System Processing:
- The
RAGSystem_JSON
object is initialized with the path to the saved JSON file. - The system processes the question and generates an answer.
- The
-
Cleanup:
- After the question is processed, the temporary JSON file is removed to ensure no unnecessary data is stored on the server.
- If an error occurs during processing, the file is still removed to maintain a clean environment.
- Ensure that the
user_id
is correctly passed in the URL path. - The request body must include the
question
andsource_type
fields. - The response will include both the answer to the question and a confirmation message.
- If an error occurs, the backend will return a 500 status with the error details.
- Consider implementing error handling in your Dart code to manage potential issues during the request.
- For more details on how to handle HTTP requests in Flutter/Dart, you can refer to the official Flutter documentation.