forked from temporalio/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivities.py
29 lines (23 loc) · 823 Bytes
/
activities.py
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
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) -> dict:
# 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 await chain.ainvoke({"phrase": params.phrase, "language": params.language})