File size: 1,406 Bytes
91ca409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from app.services.chatbot import ask
from app.models import Question, db
from datetime import datetime

bp = Blueprint('chat', __name__, url_prefix='/chat')

@bp.route('/ask', methods=['POST'])
@jwt_required()
def ask_question():
    user_id = str(get_jwt_identity())
    data = request.get_json()
    question_text = data.get('question')

    if not question_text:
        return jsonify({'error': 'Missing question'}), 400

    question = Question(
        user_id=user_id,
        content=question_text,
        status='pending'
    )
    db.session.add(question)
    db.session.commit()

    try:
        k = int(data.get("k", 6))  # if k is missing, fallback to 6
        results = ask(question_text, k=k)
        question.status = 'answered'
    except Exception as e:
        results = []
        question.status = 'error'

    question.updated_at = datetime.utcnow()
    db.session.commit()

    return jsonify({
        "question": {
            "id": question.id,
            "user_id": question.user_id,
            "content": question.content,
            "date": question.date.isoformat(),
            "status": question.status,
            "updated_at": question.updated_at.isoformat()
        },
        "results": results
    }), 200