<reponame>kuPacemaker/restful-unilm-django
from remote import AbstractProtocol
from remote.annotation import terminate
from base import BaseKnowledge

class GQQAProtocol(AbstractProtocol):
    node = ('172.16.58.3', 3421)

    def __init__(self, bkd: BaseKnowledge):
        self.bkd = bkd
        self.questions = None
        self.response_attach_head = 0

    @terminate(AbstractProtocol.TERMINATOR)
    def gen_query(self):
        for passage in self.bkd.passages:
            text = passage.text
            aqset = passage.aqset
            query = ''
            
            self.questions = list(map(lambda x: x[1], aqset))
            for answer, question in aqset:
                query += self.sep(text, question)
            yield query

    def notify_response(self, response):
        answers, scores = [], []
        for res in response:
            answer, score = self.detok_score(res)
            answers.append(answer)
            scores.append(score)
            
        self.bkd.attach_aqset(self.response_attach_head, list(zip(answers, self.questions, scores)))
        self.response_attach_head += 1

    def protocol_reset(self):
        self.response_attach_head = 0
from remote import AbstractProtocol
from remote.annotation import terminate
from base import BaseKnowledge

class GQQAProtocol(AbstractProtocol):
    node = ('172.16.58.3', 3421)

    def __init__(self, bkd: BaseKnowledge):
        self.bkd = bkd
        self.questions = None
        self.response_attach_head = 0

    @terminate(AbstractProtocol.TERMINATOR)
    def gen_query(self):
        for passage in self.bkd.passages:
            text = passage.text
            aqset = passage.aqset
            query = ''
            
            self.questions = list(map(lambda x: x[1], aqset))
            for answer, question in aqset:
                query += self.sep(text, question)
            yield query

    def notify_response(self, response):
        answers, scores = [], []
        for res in response:
            answer, score = self.detok_score(res)
            answers.append(answer)
            scores.append(score)
            
        self.bkd.attach_aqset(self.response_attach_head, list(zip(answers, self.questions, scores)))
        self.response_attach_head += 1

    def protocol_reset(self):
        self.response_attach_head = 0