from typing import List, Tuple, Dict, Optional from towhee.operator import PyOperator class TemplatePrompt(PyOperator): def __init__(self, temp: str): super().__init__() self._template = temp def __call__(self, **kwargs) -> List[Dict[str, str]]: history = kwargs.get('history', []) prompt_str = self._template.format(**kwargs) ret = [{'question': prompt_str}] if not isinstance(history, list): return ret else: history_data = [] for item in history: history_data.append({'question': item[0], 'answer': item[1]}) return history_data + ret