示例#1
0
def get_chat_params(conversation_id):
    ''' Retrieving the set of search paramaters belonging to a given conversation '''
    # Retrieve conversation
    conversation = db.session.query(Conversation).filter_by(
        id=conversation_id).first()
    # Deserialize the chat history
    chat_history = deserialize(conversation.chat_history)
    chatbot_response = chat_history[-1]['content'] if len(chat_history) else "Hi there!"
    params = {}
    if len(conversation.query_params):
        params = extract_required_params(json.loads(MessageToJson(deserialize(conversation.query_params))))
    # Return the chat history
    return jsonify(chatbot_response=chatbot_response, params=params), 200
示例#2
0
def update_chat_params(conversation_id):
    ''' Updating the set of search paramaters belonging to a given conversation '''
    # Retrieve conversation
    conversation = db.session.query(Conversation).filter_by(
        id=conversation_id).first()
    # Retrieve query_params
    update_params = deserialize(conversation.query_params)
    # Update fields
    update_fields = request.json
    for field in update_fields:
        update_params[field] = update_fields[field]
    # Commit changes
    conversation.query_params = serialize(update_params)
    db.session.commit()
    # Ensure params are JSON serializable
    params = json.loads(MessageToJson(update_params))
    return jsonify(params=params)
示例#3
0
def get_all_chat_history():
    ''' Retrieve all the user's conversations '''
    token = request.headers.get("AUTH_TOKEN")
    data = jwt.decode(token, app.secret_key)
    user_id = data['user']
    user_conversations = db.session.query(Conversation).filter_by(user_id=user_id).all()
    chat_histories = []
    for conversation in user_conversations:
        # Retrieve the conversation's parameters
        chat_history = {'id': conversation.id}
        if len(conversation.query_params):
            params = deserialize(conversation.query_params)
            params = json.loads(MessageToJson(params))
            chat_history = extract_required_params(params, chat_history)        
        chat_histories.append(chat_history)
    # Conversations with larger IDs are more recent
    chat_histories = sorted(chat_histories, reverse=True, key=lambda x: x['id'])
    return jsonify(chat_histories=chat_histories), 200
示例#4
0
def new_message():
    ''' Receive a new message from the user for a given conversation '''
    # Extract new message
    request_data = request.json
    message_content = request_data['content']
    # Identify conversation
    conversation_id = request_data['conversation_id']
    # Retrieve conversation
    conversation = db.session.query(Conversation).filter_by(
        id=conversation_id).first()
    # Extract entities and generate response
    chatbot_response, params = chatbot.parse(message_content, conversation_id)
    # Update query params with newly extracted entities
    conversation.query_params = serialize(params)
    # Update conversation with new messages and params
    db.session.commit()
    # Extract main params to display on frontend
    updated_params = extract_required_params(json.loads(
                     MessageToJson(deserialize(conversation.query_params))))
    # Return 200 OK
    return jsonify(chatbot_response=chatbot_response, params=updated_params), 200
示例#5
0
def get_search_results(conversation_id):
    ''' Retrieving search results for a conversation '''
    # Identify conversation
    topk = 10
    conversation = db.session.query(Conversation).filter_by(
        id=conversation_id).first()
    # Extract query params
    query_params = deserialize(conversation.query_params)
    # Run search
    search_results, id_list = search_engine.search(query_params, df)
    query_params = json.loads(MessageToJson(query_params))
    if not search_results:
        #TODO add a prompt to user we cant find anything
        search_results = recommend(df, query_params)
    elif len(search_results) < topk:
        search_results2 = recommend(df, query_params, True,
                                    topk - len(search_results), id_list)
        search_results += search_results2
    elif len(search_results) == topk:
        pass
    # Return search results
    return jsonify(listings=search_results), 200