-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemporal_worker.py
More file actions
57 lines (43 loc) · 1.95 KB
/
temporal_worker.py
File metadata and controls
57 lines (43 loc) · 1.95 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
54
55
56
57
import asyncio
import concurrent.futures
import logging
import os
from temporalio.client import Client
from temporalio.worker import Worker
from workflows.conversation_thread_workflow import ConversationThreadWorkflow
from activities.conversation_thread_activities import ConversationThreadActivities
openai_api_key = os.getenv("OPENAI_API_KEY")
openai_assistant_id = os.getenv("OPENAI_ASSISTANT_ID")
openai_gateway_url = os.getenv("OPENAI_GATEWAY_URL")
TEMPORAL_HOST = os.getenv("TEMPORAL_HOST")
TEMPORAL_PORT = os.getenv("TEMPORAL_PORT")
temporal_connection_string = TEMPORAL_HOST + ":" + TEMPORAL_PORT
print(temporal_connection_string)
async def main():
while(True):
try:
client = await Client.connect(temporal_connection_string)
conversation_thread_activities = ConversationThreadActivities(openai_api_key, openai_gateway_url, openai_assistant_id)
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as activity_executor:
worker = Worker(
client,
task_queue="conversation-workflow-task-queue",
workflows=[ConversationThreadWorkflow],
activities=[
conversation_thread_activities.create_thread,
conversation_thread_activities.add_message_to_thread,
conversation_thread_activities.get_response,
conversation_thread_activities.function_call,
conversation_thread_activities.get_city,
conversation_thread_activities.submit_tool_call_result,
],
activity_executor=activity_executor,
)
await worker.run()
except RuntimeError as e:
print(e)
await asyncio.sleep(5)
if __name__ == "__main__":
print("Starting worker")
logging.basicConfig(level=logging.INFO)
asyncio.run(main())