diff --git a/Dockerfile b/Dockerfile index 50bc20c..534c3c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.11-slim WORKDIR /app COPY . . -RUN pip install flask praisonai==2.0.41 gunicorn markdown +RUN pip install flask praisonai==2.0.42 gunicorn markdown EXPOSE 8080 CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"] diff --git a/agents/praisonaiagents/process/process.py b/agents/praisonaiagents/process/process.py index 4a441ca..43201f0 100644 --- a/agents/praisonaiagents/process/process.py +++ b/agents/praisonaiagents/process/process.py @@ -130,22 +130,28 @@ async def aworkflow(self) -> AsyncGenerator[str, None]: # Determine next task based on result next_task = None - if current_task.result: + if current_task and current_task.result: if current_task.task_type in ["decision", "loop"]: result = current_task.result.raw.lower() # Check conditions for condition, tasks in current_task.condition.items(): - if condition.lower() in result and tasks: - next_task_name = tasks[0] + if condition.lower() in result: + # Handle both list and direct string values + task_value = tasks[0] if isinstance(tasks, list) else tasks + if not task_value or task_value == "exit": # If empty or explicit exit + logging.info("Workflow exit condition met, ending workflow") + current_task = None + break + next_task_name = task_value next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) # For loops, allow revisiting the same task if next_task and next_task.id == current_task.id: visited_tasks.discard(current_task.id) break - if not next_task and current_task.next_tasks: - next_task_name = current_task.next_tasks[0] - next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) + if not next_task and current_task and current_task.next_tasks: + next_task_name = current_task.next_tasks[0] + next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) current_task = next_task if not current_task: @@ -391,22 +397,28 @@ def workflow(self): # Determine next task based on result next_task = None - if current_task.result: + if current_task and current_task.result: if current_task.task_type in ["decision", "loop"]: result = current_task.result.raw.lower() # Check conditions for condition, tasks in current_task.condition.items(): - if condition.lower() in result and tasks: - next_task_name = tasks[0] + if condition.lower() in result: + # Handle both list and direct string values + task_value = tasks[0] if isinstance(tasks, list) else tasks + if not task_value or task_value == "exit": # If empty or explicit exit + logging.info("Workflow exit condition met, ending workflow") + current_task = None + break + next_task_name = task_value next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) # For loops, allow revisiting the same task if next_task and next_task.id == current_task.id: visited_tasks.discard(current_task.id) break - if not next_task and current_task.next_tasks: - next_task_name = current_task.next_tasks[0] - next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) + if not next_task and current_task and current_task.next_tasks: + next_task_name = current_task.next_tasks[0] + next_task = next((t for t in self.tasks.values() if t.name == next_task_name), None) current_task = next_task if not current_task: diff --git a/agents/prompt_chaining.py b/agents/prompt_chaining.py index 185ee30..31f96cb 100644 --- a/agents/prompt_chaining.py +++ b/agents/prompt_chaining.py @@ -10,82 +10,72 @@ def get_time_check(): print(f"Time check: {current_time} is {result}") return result -def create_prompt_chain(): - # Create agents for each step in the chain - agent1 = Agent( - name="Time Checker", - role="Time checker", - goal="Check if the time is even or odd", - instructions="Check if the time is even or odd", - tools=[get_time_check] - ) +# Create agents for each step in the chain +agent1 = Agent( + name="Time Checker", + role="Time checker", + goal="Check if the time is even or odd", + instructions="Check if the time is even or odd", + tools=[get_time_check] +) - agent2 = Agent( - name="Advanced Analyzer", - role="Advanced data analyzer", - goal="Perform in-depth analysis of processed data", - instructions="Analyze the processed data in detail" - ) +agent2 = Agent( + name="Advanced Analyzer", + role="Advanced data analyzer", + goal="Perform in-depth analysis of processed data", + instructions="Analyze the processed data in detail" +) - agent3 = Agent( - name="Final Processor", - role="Final data processor", - goal="Generate final output based on analysis", - instructions="Create final output based on analyzed data" - ) +agent3 = Agent( + name="Final Processor", + role="Final data processor", + goal="Generate final output based on analysis", + instructions="Create final output based on analyzed data" +) - # Create tasks for each step - initial_task = Task( - name="time_check", - description="Getting time check and checking if it is even or odd", - expected_output="Getting time check and checking if it is even or odd", - agent=agent1, - is_start=True, # Mark as the starting task - task_type="decision", # This task will make a decision - next_tasks=["advanced_analysis"], # Next task if condition passes - condition={ - "even": ["advanced_analysis"], # If passes, go to advanced analysis - "odd": ["final_processing"] # If fails, exit the chain - } - ) +# Create tasks for each step +initial_task = Task( + name="time_check", + description="Getting time check and checking if it is even or odd", + expected_output="Getting time check and checking if it is even or odd", + agent=agent1, + is_start=True, # Mark as the starting task + task_type="decision", # This task will make a decision + next_tasks=["advanced_analysis"], # Next task if condition passes + condition={ + "even": ["advanced_analysis"], # If passes, go to advanced analysis + "odd": "" # If fails, exit the chain + } +) - analysis_task = Task( - name="advanced_analysis", - description="Perform advanced analysis on the processed data", - expected_output="Analyzed data ready for final processing", - agent=agent2, - next_tasks=["final_processing"] - ) +analysis_task = Task( + name="advanced_analysis", + description="Perform advanced analysis on the processed data", + expected_output="Analyzed data ready for final processing", + agent=agent2, + next_tasks=["final_processing"] +) - final_task = Task( - name="final_processing", - description="Generate final output", - expected_output="Final processed result", - agent=agent3 - ) +final_task = Task( + name="final_processing", + description="Generate final output", + expected_output="Final processed result", + agent=agent3 +) - # Create the workflow manager - workflow = PraisonAIAgents( - agents=[agent1, agent2, agent3], - tasks=[initial_task, analysis_task, final_task], - process="workflow", # Use workflow process type - verbose=True - ) +# Create the workflow manager +workflow = PraisonAIAgents( + agents=[agent1, agent2, agent3], + tasks=[initial_task, analysis_task, final_task], + process="workflow", # Use workflow process type + verbose=True +) - return workflow +# Run the workflow +results = workflow.start() -def main(): - # Create and run the prompt chain - workflow = create_prompt_chain() - - # Run the workflow - results = workflow.start() - - # Print results - print("\nWorkflow Results:") - for task_id, result in results["task_results"].items(): - if result: - print(f"Task {task_id}: {result.raw}") - -if __name__ == "__main__": - main() +# Print results +print("\nWorkflow Results:") +for task_id, result in results["task_results"].items(): + if result: + print(f"Task {task_id}: {result.raw}") diff --git a/agents/pyproject.toml b/agents/pyproject.toml index a336d8b..ac5b131 100644 --- a/agents/pyproject.toml +++ b/agents/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "praisonaiagents" -version = "0.0.31" +version = "0.0.32" description = "Praison AI agents for completing complex tasks with Self Reflection Agents" authors = [ { name="Mervin Praison" } diff --git a/agents/uv.lock b/agents/uv.lock index 4215730..eedf77e 100644 --- a/agents/uv.lock +++ b/agents/uv.lock @@ -990,7 +990,7 @@ wheels = [ [[package]] name = "praisonaiagents" -version = "0.0.31" +version = "0.0.32" source = { editable = "." } dependencies = [ { name = "openai" }, diff --git a/cookbooks/general/prompt_chaining.py b/cookbooks/general/prompt_chaining.py new file mode 100644 index 0000000..31f96cb --- /dev/null +++ b/cookbooks/general/prompt_chaining.py @@ -0,0 +1,81 @@ +from praisonaiagents.agent import Agent +from praisonaiagents.task import Task +from praisonaiagents.agents import PraisonAIAgents +from typing import List, Dict +import time + +def get_time_check(): + current_time = int(time.time()) + result = "even" if current_time % 2 == 0 else "odd" + print(f"Time check: {current_time} is {result}") + return result + +# Create agents for each step in the chain +agent1 = Agent( + name="Time Checker", + role="Time checker", + goal="Check if the time is even or odd", + instructions="Check if the time is even or odd", + tools=[get_time_check] +) + +agent2 = Agent( + name="Advanced Analyzer", + role="Advanced data analyzer", + goal="Perform in-depth analysis of processed data", + instructions="Analyze the processed data in detail" +) + +agent3 = Agent( + name="Final Processor", + role="Final data processor", + goal="Generate final output based on analysis", + instructions="Create final output based on analyzed data" +) + +# Create tasks for each step +initial_task = Task( + name="time_check", + description="Getting time check and checking if it is even or odd", + expected_output="Getting time check and checking if it is even or odd", + agent=agent1, + is_start=True, # Mark as the starting task + task_type="decision", # This task will make a decision + next_tasks=["advanced_analysis"], # Next task if condition passes + condition={ + "even": ["advanced_analysis"], # If passes, go to advanced analysis + "odd": "" # If fails, exit the chain + } +) + +analysis_task = Task( + name="advanced_analysis", + description="Perform advanced analysis on the processed data", + expected_output="Analyzed data ready for final processing", + agent=agent2, + next_tasks=["final_processing"] +) + +final_task = Task( + name="final_processing", + description="Generate final output", + expected_output="Final processed result", + agent=agent3 +) + +# Create the workflow manager +workflow = PraisonAIAgents( + agents=[agent1, agent2, agent3], + tasks=[initial_task, analysis_task, final_task], + process="workflow", # Use workflow process type + verbose=True +) + +# Run the workflow +results = workflow.start() + +# Print results +print("\nWorkflow Results:") +for task_id, result in results["task_results"].items(): + if result: + print(f"Task {task_id}: {result.raw}") diff --git a/docs/api/praisonai/deploy.html b/docs/api/praisonai/deploy.html index 3c0aa76..6417215 100644 --- a/docs/api/praisonai/deploy.html +++ b/docs/api/praisonai/deploy.html @@ -110,7 +110,7 @@