-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathactivities.py
More file actions
34 lines (28 loc) · 895 Bytes
/
activities.py
File metadata and controls
34 lines (28 loc) · 895 Bytes
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
from dataclasses import dataclass
from langchain_openai import ChatOpenAI
from temporalio import activity
from langchain.prompts import ChatPromptTemplate
@dataclass
class TranslateParams:
phrase: str
language: str
@activity.defn
async def translate_phrase(params: TranslateParams) -> str:
# LangChain setup
template = """You are a helpful assistant who translates between languages.
Translate the following phrase into the specified language: {phrase}
Language: {language}"""
chat_prompt = ChatPromptTemplate.from_messages(
[
("system", template),
("human", "Translate"),
]
)
chain = chat_prompt | ChatOpenAI()
# Use the asynchronous invoke method
return (
dict(
await chain.ainvoke({"phrase": params.phrase, "language": params.language})
).get("content")
or ""
)