-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathworkflow.py
More file actions
53 lines (45 loc) · 1.55 KB
/
workflow.py
File metadata and controls
53 lines (45 loc) · 1.55 KB
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
import asyncio
from dataclasses import dataclass
from datetime import timedelta
from typing import List
from temporalio import workflow
with workflow.unsafe.imports_passed_through():
from activities import TranslateParams, translate_phrase
@workflow.defn
class LangChainChildWorkflow:
@workflow.run
async def run(self, params: TranslateParams) -> str:
return await workflow.execute_activity(
translate_phrase,
params,
schedule_to_close_timeout=timedelta(seconds=30),
)
@dataclass
class TranslateWorkflowParams:
phrase: str
languages: List[str]
@workflow.defn
class LangChainWorkflow:
@workflow.run
async def run(self, params: TranslateWorkflowParams) -> dict:
result1, result2, result3 = await asyncio.gather(
workflow.execute_activity(
translate_phrase,
TranslateParams(params.phrase, params.languages[0]),
schedule_to_close_timeout=timedelta(seconds=30),
),
workflow.execute_activity(
translate_phrase,
TranslateParams(params.phrase, params.languages[1]),
schedule_to_close_timeout=timedelta(seconds=30),
),
workflow.execute_child_workflow(
LangChainChildWorkflow.run,
TranslateParams(params.phrase, params.languages[2]),
),
)
return {
params.languages[0]: result1,
params.languages[1]: result2,
params.languages[2]: result3,
}