added confirmation signal step, Temporal is awesome

This commit is contained in:
Steve Androulakis
2025-01-02 10:35:04 -08:00
parent d1b8e150ba
commit fe3d9b7e90
2 changed files with 13 additions and 1 deletions

View File

@@ -1,6 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from temporalio import activity from temporalio import activity
from temporalio.exceptions import ApplicationError
from ollama import chat, ChatResponse from ollama import chat, ChatResponse
import json import json
from typing import Sequence from typing import Sequence

View File

@@ -22,6 +22,7 @@ class ToolWorkflow:
self.chat_ended: bool = False self.chat_ended: bool = False
self.tool_data = None self.tool_data = None
self.max_turns_before_continue: int = 250 self.max_turns_before_continue: int = 250
self.confirm = False
@workflow.run @workflow.run
async def run(self, combined_input: CombinedInput) -> str: async def run(self, combined_input: CombinedInput) -> str:
@@ -96,6 +97,14 @@ class ToolWorkflow:
) # e.g. "FindEvents", "SearchFlights", "CreateInvoice" ) # e.g. "FindEvents", "SearchFlights", "CreateInvoice"
if next_step == "confirm" and current_tool: if next_step == "confirm" and current_tool:
self.confirm = False
# Wait for a 'confirm' signal
await workflow.wait_condition(lambda: self.confirm)
workflow.logger.info(
"Confirmed. Proceeding with tool execution: " + current_tool
)
# We have enough info to call the tool # We have enough info to call the tool
dynamic_result = await workflow.execute_activity( dynamic_result = await workflow.execute_activity(
current_tool, current_tool,
@@ -188,6 +197,10 @@ class ToolWorkflow:
async def end_chat(self) -> None: async def end_chat(self) -> None:
self.chat_ended = True self.chat_ended = True
@workflow.signal
async def confirm(self) -> None:
self.confirm = True
@workflow.query @workflow.query
def get_conversation_history(self) -> List[Tuple[str, str]]: def get_conversation_history(self) -> List[Tuple[str, str]]:
return self.conversation_history return self.conversation_history