| 10 | |
| 11 | @PROMPT.register_module(force=True) |
| 12 | class Prompt(): |
| 13 | def __init__(self, *args, **kwargs): |
| 14 | super().__init__(*args, **kwargs) |
| 15 | |
| 16 | def _replace_keys(self, text: str, params: Dict): |
| 17 | keys = params.keys() |
| 18 | for key in keys: |
| 19 | if key in text: |
| 20 | if isinstance(params[key], list) or isinstance(params[key], dict): |
| 21 | text = text.replace(f'<${key}$>', json.dumps(params[key], indent=4)) |
| 22 | else: |
| 23 | text = text.replace(f'<${key}$>', str(params[key])) |
| 24 | return text |
| 25 | |
| 26 | def convert_state_info_to_parmas(self, *args, **kwargs): |
| 27 | raise NotImplementedError |
| 28 | |
| 29 | def to_message(self, *args, |
| 30 | params: Dict = None, |
| 31 | template: Any = None, |
| 32 | **kwargs): |
| 33 | |
| 34 | assert params is not None, "params is None" |
| 35 | assert template is not None, "template is None" |
| 36 | |
| 37 | html = generate_prompt_html(params, template) |
| 38 | |
| 39 | system_message = None |
| 40 | system_div_tag = html.find("div", class_="message", role="system") |
| 41 | if system_div_tag is not None: |
| 42 | p_tag = system_div_tag.find("p") |
| 43 | content = p_tag.text |
| 44 | |
| 45 | content = content_replace(content) |
| 46 | |
| 47 | message = { |
| 48 | "role": "system", |
| 49 | "content": [ |
| 50 | { |
| 51 | "type": "text", |
| 52 | "text": content |
| 53 | } |
| 54 | ] |
| 55 | } |
| 56 | system_message = message |
| 57 | |
| 58 | assert system_message is not None, "system_message is None" |
| 59 | |
| 60 | user_messages = [] |
| 61 | user_div_tags = html.find_all("div", class_="message", role="user") |
| 62 | |
| 63 | for user_div_tag in user_div_tags: |
| 64 | tags = user_div_tag.find_all(["p", "img"]) |
| 65 | |
| 66 | message = { |
| 67 | "role": "user", |
| 68 | "content": [] |
| 69 | } |
nothing calls this directly
no outgoing calls
no test coverage detected