Skip to content

POST ask_v2

Do Le Long An edited this page Aug 23, 2024 · 1 revision

API Documentation: ask_v2 Endpoint

Endpoint Overview

  • 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.

URL

POST /ask_v2/{user_id}

Path Parameters

  • user_id (required): The unique identifier for the user whose transcripts will be processed.

Request Body

  • 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).

Example Request

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',
  );
}

Response

  • Status Code: 200 OK
  • Content-Type: application/json
Response Body
{
  "response": "The patient is diagnosed with hypertension.",
  "message": "Question answered successfully"
}

Error Handling

  • 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.

Response Example

A successful response might look like this:

{
  "response": "The patient is diagnosed with hypertension.",
  "message": "Question answered successfully"
}

Backend Process Overview

  1. File Handling:

    • The backend first checks if a file named patients_from_user_{user_id}.json already exists in the assets 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).
  2. 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.
  3. 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.

Notes for Mobile Team

  • Ensure that the user_id is correctly passed in the URL path.
  • The request body must include the question and source_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.

References

  • For more details on how to handle HTTP requests in Flutter/Dart, you can refer to the official Flutter documentation.