File size: 8,076 Bytes
105b369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from uuid import uuid4
from typing import List, Any, Optional, Dict, Iterator, Union

from pydantic import BaseModel, ConfigDict, field_validator, Field

from phi.llm.base import LLM
from phi.task.task import Task
from phi.utils.log import logger, set_log_level_to_debug
from phi.utils.message import get_text_from_message
from phi.utils.timer import Timer


class Workflow(BaseModel):
    # -*- Workflow settings
    # LLM to use for this Workflow
    llm: Optional[LLM] = None
    # Workflow name
    name: Optional[str] = None

    # -*- Run settings
    # Run UUID (autogenerated if not set)
    run_id: Optional[str] = Field(None, validate_default=True)
    # Metadata associated with this run
    run_data: Optional[Dict[str, Any]] = None

    # -*- User settings
    # ID of the user running this workflow
    user_id: Optional[str] = None
    # Metadata associated the user running this workflow
    user_data: Optional[Dict[str, Any]] = None

    # -*- Tasks in this workflow (required)
    tasks: List[Task]
    # Metadata associated with the assistant tasks
    task_data: Optional[Dict[str, Any]] = None

    # -*- Workflow Output
    # Final output of this Workflow
    output: Optional[Any] = None
    # Save the output to a file
    save_output_to_file: Optional[str] = None

    # debug_mode=True enables debug logs
    debug_mode: bool = False
    # monitoring=True logs Workflow runs on phidata.app
    monitoring: bool = False

    model_config = ConfigDict(arbitrary_types_allowed=True)

    @field_validator("debug_mode", mode="before")
    def set_log_level(cls, v: bool) -> bool:
        if v:
            set_log_level_to_debug()
            logger.debug("Debug logs enabled")
        return v

    @field_validator("run_id", mode="before")
    def set_run_id(cls, v: Optional[str]) -> str:
        return v if v is not None else str(uuid4())

    def _run(
        self,
        message: Optional[Union[List, Dict, str]] = None,
        *,
        stream: bool = True,
        **kwargs: Any,
    ) -> Iterator[str]:
        logger.debug(f"*********** Workflow Run Start: {self.run_id} ***********")

        # List of tasks that have been run
        executed_tasks: List[Task] = []
        workflow_output: List[str] = []

        # -*- Generate response by running tasks
        for idx, task in enumerate(self.tasks, start=1):
            logger.debug(f"*********** Task {idx} Start ***********")

            # -*- Prepare input message for the current_task
            task_input: List[str] = []
            if message is not None:
                task_input.append(get_text_from_message(message))

            if len(executed_tasks) > 0:
                previous_task_outputs = []
                for previous_task_idx, previous_task in enumerate(executed_tasks, start=1):
                    previous_task_output = previous_task.get_task_output_as_str()
                    if previous_task_output is not None:
                        previous_task_outputs.append(
                            (previous_task_idx, previous_task.description, previous_task_output)
                        )

                if len(previous_task_outputs) > 0:
                    task_input.append("\nHere are previous tasks and and their results:\n---")
                    for previous_task_idx, previous_task_description, previous_task_output in previous_task_outputs:
                        task_input.append(f"Task {previous_task_idx}: {previous_task_description}")
                        task_input.append(previous_task_output)
                    task_input.append("---")

            # -*- Run Task
            task_output = ""
            input_for_current_task = "\n".join(task_input)
            if stream and task.streamable:
                for chunk in task.run(message=input_for_current_task, stream=True, **kwargs):
                    task_output += chunk if isinstance(chunk, str) else ""
                    yield chunk if isinstance(chunk, str) else ""
            else:
                task_output = task.run(message=input_for_current_task, stream=False, **kwargs)  # type: ignore

            executed_tasks.append(task)
            workflow_output.append(task_output)
            logger.debug(f"*********** Task {idx} End ***********")
            if not stream:
                yield task_output

        self.output = "\n".join(workflow_output)
        if self.save_output_to_file:
            fn = self.save_output_to_file.format(name=self.name, run_id=self.run_id, user_id=self.user_id)
            with open(fn, "w") as f:
                f.write(self.output)
        logger.debug(f"*********** Workflow Run End: {self.run_id} ***********")

    def run(
        self,
        message: Optional[Union[List, Dict, str]] = None,
        *,
        stream: bool = True,
        **kwargs: Any,
    ) -> Union[Iterator[str], str]:
        if stream:
            resp = self._run(message=message, stream=True, **kwargs)
            return resp
        else:
            resp = self._run(message=message, stream=True, **kwargs)
            return next(resp)

    def print_response(
        self,
        message: Optional[Union[List, Dict, str]] = None,
        *,
        stream: bool = True,
        markdown: bool = False,
        show_message: bool = True,
        **kwargs: Any,
    ) -> None:
        from phi.cli.console import console
        from rich.live import Live
        from rich.table import Table
        from rich.status import Status
        from rich.progress import Progress, SpinnerColumn, TextColumn
        from rich.box import ROUNDED
        from rich.markdown import Markdown

        if stream:
            response = ""
            with Live() as live_log:
                status = Status("Working...", spinner="dots")
                live_log.update(status)
                response_timer = Timer()
                response_timer.start()
                for resp in self.run(message=message, stream=True, **kwargs):
                    if isinstance(resp, str):
                        response += resp
                    _response = Markdown(response) if markdown else response

                    table = Table(box=ROUNDED, border_style="blue", show_header=False)
                    if message and show_message:
                        table.show_header = True
                        table.add_column("Message")
                        table.add_column(get_text_from_message(message))
                    table.add_row(f"Response\n({response_timer.elapsed:.1f}s)", _response)  # type: ignore
                    live_log.update(table)
                response_timer.stop()
        else:
            response_timer = Timer()
            response_timer.start()
            with Progress(
                SpinnerColumn(spinner_name="dots"), TextColumn("{task.description}"), transient=True
            ) as progress:
                progress.add_task("Working...")
                response = self.run(message=message, stream=False, **kwargs)  # type: ignore

            response_timer.stop()
            _response = Markdown(response) if markdown else response

            table = Table(box=ROUNDED, border_style="blue", show_header=False)
            if message and show_message:
                table.show_header = True
                table.add_column("Message")
                table.add_column(get_text_from_message(message))
            table.add_row(f"Response\n({response_timer.elapsed:.1f}s)", _response)  # type: ignore
            console.print(table)

    def cli_app(
        self,
        user: str = "User",
        emoji: str = ":sunglasses:",
        stream: bool = True,
        markdown: bool = False,
        exit_on: Optional[List[str]] = None,
    ) -> None:
        from rich.prompt import Prompt

        _exit_on = exit_on or ["exit", "quit", "bye"]
        while True:
            message = Prompt.ask(f"[bold] {emoji} {user} [/bold]")
            if message in _exit_on:
                break

            self.print_response(message=message, stream=stream, markdown=markdown)