File size: 3,136 Bytes
2319518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import re

import json5

from qwen_agent.actions.base import Action
from qwen_agent.actions.expand_writing import ExpandWriting
from qwen_agent.actions.outline_writing import OutlineWriting
from qwen_agent.actions.summarize import Summarize

default_plan = """{"action1": "summarize", "action2": "outline", "action3": "expand"}"""


def is_roman_numeral(s):
    pattern = r'^(I|V|X|L|C|D|M)+'
    match = re.match(pattern, s)
    return match is not None


class WriteFromScratch(Action):

    def _run(self, user_request, ref_doc, lang: str = 'en'):
        # plan
        yield '\n========================= \n'
        yield '> Use Default plans: \n'
        yield default_plan
        res_plans = json5.loads(default_plan)

        summ = ''
        outline = ''
        for plan_id in sorted(res_plans.keys()):
            plan = res_plans[plan_id]
            if plan == 'summarize':
                yield '\n========================= \n'
                yield '> Summarize Browse Content: \n'
                summ = ''
                sum_agent = Summarize(llm=self.llm, stream=self.stream)
                res_sum = sum_agent.run(ref_doc=ref_doc, lang=lang)
                for trunk in res_sum:
                    summ += trunk
                    yield trunk
            elif plan == 'outline':
                yield '\n========================= \n'
                yield '> Generate Outline: \n'
                outline = ''
                otl_agent = OutlineWriting(llm=self.llm, stream=self.stream)
                res_otl = otl_agent.run(user_request=user_request,
                                        ref_doc=summ,
                                        lang=lang)
                for trunk in res_otl:
                    outline += trunk
                    yield trunk
            elif plan == 'expand':
                yield '\n========================= \n'
                yield '> Writing Text: \n'
                outline_list_all = outline.split('\n')
                outline_list = []
                for x in outline_list_all:
                    if is_roman_numeral(x):
                        outline_list.append(x)

                otl_num = len(outline_list)
                for i, v in enumerate(outline_list):
                    yield '\n# '
                    text = ''
                    index = i + 1
                    capture = v.strip()
                    capture_later = ''
                    if i < otl_num - 1:
                        capture_later = outline_list[i + 1].strip()
                    exp_agent = ExpandWriting(llm=self.llm, stream=self.stream)
                    res_exp = exp_agent.run(
                        user_request=user_request,
                        ref_doc=ref_doc,
                        outline=outline,
                        index=str(index),
                        capture=capture,
                        capture_later=capture_later,
                        lang=lang,
                    )
                    for trunk in res_exp:
                        text += trunk
                        yield trunk
            else:
                pass