mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 22:18:09 +01:00
dynamic activity to call tool
This commit is contained in:
@@ -3,6 +3,9 @@ from temporalio import activity
|
||||
from temporalio.exceptions import ApplicationError
|
||||
from ollama import chat, ChatResponse
|
||||
import json
|
||||
from models.tool_definitions import ToolsData
|
||||
from typing import Sequence
|
||||
from temporalio.common import RawValue
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -44,6 +47,47 @@ class ToolActivities:
|
||||
|
||||
return data
|
||||
|
||||
@activity.defn
|
||||
def validate_and_parse_json(
|
||||
self,
|
||||
response_prechecked: str,
|
||||
tools_data: ToolsData,
|
||||
conversation_history: str,
|
||||
) -> dict:
|
||||
"""
|
||||
1) Build JSON validation instructions
|
||||
2) Call LLM with those instructions
|
||||
3) Parse the result
|
||||
4) If parsing fails, raise exception -> triggers retry
|
||||
"""
|
||||
|
||||
# 1) Build validation instructions
|
||||
# (Generate the validation prompt exactly as you do in your workflow.)
|
||||
from prompts.agent_prompt_generators import (
|
||||
generate_json_validation_prompt_from_tools_data,
|
||||
)
|
||||
|
||||
validation_prompt = generate_json_validation_prompt_from_tools_data(
|
||||
tools_data, conversation_history, response_prechecked
|
||||
)
|
||||
|
||||
# 2) Call LLM
|
||||
prompt_input = ToolPromptInput(
|
||||
prompt=response_prechecked,
|
||||
context_instructions=validation_prompt,
|
||||
)
|
||||
validated_response = self.prompt_llm(prompt_input)
|
||||
|
||||
# 3) Parse
|
||||
# If parse fails, we raise ApplicationError -> triggers retry
|
||||
try:
|
||||
parsed = self.parse_tool_data(validated_response)
|
||||
except Exception as e:
|
||||
raise ApplicationError(f"Failed to parse validated JSON: {e}")
|
||||
|
||||
# 4) If we get here, parse succeeded
|
||||
return parsed
|
||||
|
||||
|
||||
def get_current_date_human_readable():
|
||||
"""
|
||||
@@ -54,3 +98,30 @@ def get_current_date_human_readable():
|
||||
from datetime import datetime
|
||||
|
||||
return datetime.now().strftime("%A, %B %d, %Y")
|
||||
|
||||
|
||||
@activity.defn(dynamic=True)
|
||||
def dynamic_tool_activity(args: Sequence[RawValue]) -> dict:
|
||||
"""Dynamic activity that is invoked via an unknown activity type."""
|
||||
tool_name = activity.info().activity_type # e.g. "SearchFlights"
|
||||
|
||||
# The first payload is the dictionary of arguments
|
||||
tool_args = activity.payload_converter().from_payload(args[0].payload, dict)
|
||||
|
||||
# Extract fields from the arguments
|
||||
date_depart = tool_args.get("dateDepart")
|
||||
date_return = tool_args.get("dateReturn")
|
||||
origin = tool_args.get("origin")
|
||||
destination = tool_args.get("destination")
|
||||
|
||||
# Print (or log) them
|
||||
activity.logger.info(f"Tool: {tool_name}")
|
||||
activity.logger.info(f"Depart: {date_depart}, Return: {date_return}")
|
||||
activity.logger.info(f"Origin: {origin}, Destination: {destination}")
|
||||
|
||||
# For now, just return them
|
||||
return {
|
||||
"tool": tool_name,
|
||||
"args": tool_args,
|
||||
"status": "OK - dynamic activity stub",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user