mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 22:18:09 +01:00
Change agent goal to be an element of the workflow, including query
This commit is contained in:
36
api/main.py
36
api/main.py
@@ -20,7 +20,7 @@ temporal_client: Optional[Client] = None
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def get_agent_goal():
|
||||
def get_initial_agent_goal():
|
||||
"""Get the agent goal from environment variables."""
|
||||
goals = {
|
||||
"goal_match_train_invoice": goal_match_train_invoice,
|
||||
@@ -121,6 +121,27 @@ async def get_conversation_history():
|
||||
raise HTTPException(
|
||||
status_code=500, detail="Internal server error while querying workflow."
|
||||
)
|
||||
|
||||
@app.get("/agent-goal")
|
||||
async def get_agent_goal():
|
||||
"""Calls the workflow's 'get_agent_goal' query."""
|
||||
try:
|
||||
# Get workflow handle
|
||||
handle = temporal_client.get_workflow_handle("agent-workflow")
|
||||
|
||||
# Check if the workflow is completed
|
||||
workflow_status = await handle.describe()
|
||||
if workflow_status.status == 2:
|
||||
# Workflow is completed; return an empty response
|
||||
return {}
|
||||
|
||||
# Query the workflow
|
||||
agent_goal = await handle.query("get_agent_goal")
|
||||
return agent_goal
|
||||
except TemporalError as e:
|
||||
# Workflow not found; return an empty response
|
||||
print(e)
|
||||
return {}
|
||||
|
||||
|
||||
@app.post("/send-prompt")
|
||||
@@ -128,7 +149,8 @@ async def send_prompt(prompt: str):
|
||||
# Create combined input with goal from environment
|
||||
combined_input = CombinedInput(
|
||||
tool_params=AgentGoalWorkflowParams(None, None),
|
||||
agent_goal=get_agent_goal(),
|
||||
agent_goal=get_initial_agent_goal(),
|
||||
#change to get from workflow query
|
||||
)
|
||||
|
||||
workflow_id = "agent-workflow"
|
||||
@@ -172,13 +194,13 @@ async def end_chat():
|
||||
|
||||
@app.post("/start-workflow")
|
||||
async def start_workflow():
|
||||
# Get the configured goal
|
||||
agent_goal = get_agent_goal()
|
||||
# Get the initial goal as set in shared/config or env or just...always should be "pick a goal?"
|
||||
initial_agent_goal = get_initial_agent_goal()
|
||||
|
||||
# Create combined input
|
||||
combined_input = CombinedInput(
|
||||
tool_params=AgentGoalWorkflowParams(None, None),
|
||||
agent_goal=agent_goal,
|
||||
agent_goal=initial_agent_goal,
|
||||
)
|
||||
|
||||
workflow_id = "agent-workflow"
|
||||
@@ -190,9 +212,9 @@ async def start_workflow():
|
||||
id=workflow_id,
|
||||
task_queue=TEMPORAL_TASK_QUEUE,
|
||||
start_signal="user_prompt",
|
||||
start_signal_args=["### " + agent_goal.starter_prompt],
|
||||
start_signal_args=["### " + initial_agent_goal.starter_prompt],
|
||||
)
|
||||
|
||||
return {
|
||||
"message": f"Workflow started with goal's starter prompt: {agent_goal.starter_prompt}."
|
||||
"message": f"Workflow started with goal's starter prompt: {initial_agent_goal.starter_prompt}."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user