Compare commits

...
26 Commits
Author SHA1 Message Date
Mervin Praison df3c97e73a Merge pull request #286 from MervinPraison/develop
Update PraisonAI to version 2.0.42 and praisonaiagents to version 0.0.32
2025-01-12 06:40:44 +00:00
MervinPraison 27808964be Update PraisonAI to version 2.0.42 and praisonaiagents to version 0.0.32
- Updated the PraisonAI package version from 2.0.41 to 2.0.42 in Dockerfile, pyproject.toml, and related files.
- Adjusted installation command in Dockerfile and deploy scripts to reflect the new version.
- Updated the praisonaiagents package version from 0.0.31 to 0.0.32 in relevant files.
- Enhanced the prompt chaining implementation in agents/prompt_chaining.py for improved task management and workflow execution.
- Added new documentation on agentic prompt chaining to guide users in creating complex workflows.
- Updated dependencies in uv.lock for compatibility with the new versions.
2025-01-12 06:40:29 +00:00
Mervin Praison b5daf16e0d Merge pull request #285 from MervinPraison/develop
adding docs
2025-01-11 17:13:14 +00:00
MervinPraison a2cac05d16 adding docs 2025-01-11 17:12:56 +00:00
Mervin Praison ad9828d3bb Merge pull request #284 from MervinPraison/develop
Update PraisonAI to version 2.0.41 and enhance documentation
2025-01-11 16:13:02 +00:00
MervinPraison 85ddf04b47 Update PraisonAI to version 2.0.41 and enhance documentation
- Updated PraisonAI package version from 2.0.40 to 2.0.41 in Dockerfile, pyproject.toml, and related files.
- Adjusted installation command in Dockerfile and deploy scripts to reflect the new version.
- Improved logging in agents_generator.py to handle missing tools.py file gracefully.
- Updated deployment documentation to ensure consistency with the new version.
- Adjusted dependencies in uv.lock for compatibility with the updated PraisonAI version.
2025-01-11 16:12:41 +00:00
Mervin Praison 1c1a1b719b Merge pull request #283 from MervinPraison/develop
Update PraisonAI to version 2.0.40 and enhance logging
2025-01-11 16:02:16 +00:00
MervinPraison a8e0914e05 Update PraisonAI to version 2.0.40 and enhance logging
- Updated the PraisonAI package version from 2.0.39 to 2.0.40 in Dockerfile, pyproject.toml, and related files.
- Added a new memory configuration option in agents.yaml to control memory usage.
- Improved logging throughout the codebase by changing info logs to debug logs for better traceability.
- Updated .gitignore to include new files for better project management.
- Adjusted dependencies in pyproject.toml and uv.lock for compatibility with the new version.
2025-01-11 16:01:54 +00:00
Mervin Praison ec6d883f4f Merge pull request #282 from MervinPraison/develop
adding langchain agent doc
2025-01-11 08:21:48 +00:00
MervinPraison d094cbfac5 adding langchain agent doc 2025-01-11 08:21:25 +00:00
Mervin Praison 14e3724aed Merge pull request #281 from MervinPraison/develop
Update praisonaiagents to version 0.0.31 and enhance agent functionality
2025-01-11 07:51:59 +00:00
MervinPraison 92436277d9 Update praisonaiagents to version 0.0.31 and enhance agent functionality
- Updated the version of the praisonaiagents package from 0.0.30 to 0.0.31 in pyproject.toml and related files.
- Added a new Fact Agent to store and retrieve facts, improving the overall functionality of the agent system.
- Enhanced the Research Agent with multiple tasks focused on AI topics, including healthcare and education.
- Updated display functions to include agent information in the output, improving clarity during interactions.
- Improved logging for agent processing and responses, aiding in debugging and tracking agent activities.
- Adjusted dependencies in pyproject.toml and uv.lock for compatibility with the new version.
2025-01-11 07:51:05 +00:00
Mervin Praison 3e0fd298e2 Merge pull request #280 from MervinPraison/develop
AI Agents with memory
2025-01-10 17:21:44 +00:00
MervinPraison fad0b41b52 Update PraisonAI to version 2.0.38 and enhance memory management
- Updated PraisonAI package version from 2.0.37 to 2.0.38 in Dockerfile, pyproject.toml, and related files.
- Refactored memory handling in memory_example.py to improve database path management and logging.
- Introduced new memory management features in the PraisonAIAgents class, allowing for better context retention across tasks.
- Enhanced logging throughout the codebase for improved debugging and tracking of memory operations.
- Added comprehensive documentation on memory capabilities, including short-term and long-term memory management.
- Updated dependencies in pyproject.toml and uv.lock for compatibility with the new version.
2025-01-10 17:20:54 +00:00
MervinPraison 6c3e5ee187 Agents(memory=True) working
Refactor memory management and enhance agent initialization

- Removed direct memory initialization from memory_example.py and integrated shared memory handling into PraisonAIAgents class.
- Updated PraisonAIAgents constructor to accept a memory flag, initializing shared memory if enabled.
- Assigned shared memory to tasks dynamically, improving data sharing between agents.
- Enhanced logging for memory initialization and task assignment to facilitate debugging.
2025-01-10 06:11:35 +00:00
MervinPraison f842ee7048 Update chainlit dependency to version 2.0.1 and improve tool registration in realtime.py
- Updated chainlit version from 1.3.2 to 2.0.1 in pyproject.toml and uv.lock.
- Enhanced tool registration logic in realtime.py to ensure only valid tools are added, with improved error handling and logging for invalid tool definitions.
- Refactored custom tools processing to support both formatted and unformatted tool definitions.
2025-01-10 06:01:47 +00:00
MervinPraison b0ff6f3f60 Update .gitignore and add new ignore files; enhance memory_example.py and memory.py functionality
- Updated .gitignore to include chroma_db/ directory.
- Added .praisoncontext and .praisonignore files for better project structure.
- Refactored memory_example.py to improve memory retrieval and logging.
- Enhanced memory.py to handle exceptions during ChromaDB collection initialization and updated embedding model to 'text-embedding-3-small'.
- Improved logging for embedding operations and ensured API key is set correctly.
2025-01-10 05:40:32 +00:00
MervinPraison 798f6dad14 Refactor memory handling and enhance task execution flow
- Improved memory initialization and management in memory_example.py and Task class.
- Integrated shared memory for tasks to facilitate data sharing between agents.
- Enhanced error handling and logging for memory operations in the Memory class.
- Updated task execution to include memory context and quality metrics.
- Added new task for querying both short-term and long-term memory.
- Improved overall structure and clarity of code with better logging practices.
2025-01-09 10:24:53 +00:00
MervinPraison 629bf0d669 Refactor memory management and enhance search functionality
- Removed unused imports and improved logging in memory_example.py.
- Updated Task description and expected output for clarity.
- Enhanced Memory class to support OpenAI embeddings for improved search capabilities.
- Added quality and relevance filtering in search_short_term and search_long_term methods.
- Implemented deletion of existing ChromaDB collection before creating a new one.
- Improved error handling and logging for database operations.
2025-01-09 06:44:25 +00:00
MervinPraison eaca447b82 Enhance code structure and documentation:
- Updated cursor rules for minimal code changes and user-friendly function naming.
- Integrated Memory module into the PraisonAIAgents initialization.
- Modified Agent class parameters for improved flexibility and default values.
- Enhanced Task class with memory and quality check features, including logging for quality metrics during execution.
2025-01-09 06:23:00 +00:00
Mervin Praison bef3693627 Merge pull request #278 from MervinPraison/develop
updating docs
2025-01-08 16:43:47 +00:00
MervinPraison aa62babed6 updating docs 2025-01-08 16:43:30 +00:00
Mervin Praison b73f181200 Merge pull request #277 from MervinPraison/develop
Updating Docs
2025-01-08 13:42:39 +00:00
MervinPraison 66d2998047 Updating Docs 2025-01-08 13:42:00 +00:00
Mervin Praison c5b6f8b91a Merge pull request #276 from MervinPraison/develop
adding autoagents
2025-01-08 06:47:17 +00:00
MervinPraison 823e46ca84 adding autoagents 2025-01-08 06:47:02 +00:00
90 changed files with 12750 additions and 3199 deletions

No files matched your search

+2 -1
View File
@@ -65,4 +65,5 @@ outputs
agents/praisonaiagents/build
agents/praisonaiagents/dist
agents/praisonaiagents/praisonaiagents.egg-info
.codegpt
.codegpt
.praison
+1 -1
View File
@@ -1,6 +1,6 @@
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install flask praisonai==2.0.36 gunicorn markdown
RUN pip install flask praisonai==2.0.42 gunicorn markdown
EXPOSE 8080
CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]
+1
View File
@@ -1,6 +1,7 @@
framework: praisonai
process: sequential
topic: create movie script about cat in mars
memory: false
roles:
researcher:
backstory: Skilled in research, with a focus on gathering accurate and relevant
+8 -6
View File
@@ -1,6 +1,8 @@
Make it minimal change as possible
Firstly try not to make any modification to the existing code as possible
Only if its highly required, without that if it can't be done, then modify the existing.
So by default try to add new code section and not modify the existing.
1. Make minimal code changes as possible
2. if you are creating function, make sure to name it in more easy, user friendly , optimised manner.
3. Never copy the exact code if presented with a code from other location or file or codebase, just make it optimised
4. Always Follow the principle LESS IS MORE
5. Make it minimal change as possible
6. Firstly try not to make any modification to the existing code as possible
7. Only modify the existing code if its highly required, without that if it cant be done, then add new code section.
8. If you are adding new code, make sure to add it in a way that it can be easily integrated with the existing codebase.
+2 -1
View File
@@ -83,4 +83,5 @@ dist
build
*.mp4
*.png
graph.py
graph.py
chroma_db/
View File
Whitespace-only changes.
+32
View File
@@ -0,0 +1,32 @@
.cursorrules
.gitignore
.praisoncontext
.praisonignore
.windsurfrules
async_example.py
async_example_full.py
async_example_full_multigroups.py
auto_agents_example.py
code_agents_example.py
example_callback.py
example_custom_tools.py
example_sequential.py
image.jpg
langchain_example.py
langchain_logs.py
long_term.db
mini_agents_example.py
multimodal.py
praisonaiagents/tools/*
praisonaiagents/process/*
pyproject.toml
short_term.db
structured_agents_example.py
structured_response_example.py
test.py
tools_example.py
uv.lock
workflow_example_basic.py
workflow_example_detailed.py
__pycache__
__pycache__/*
+8
View File
@@ -0,0 +1,8 @@
1. Make minimal code changes as possible
2. if you are creating function, make sure to name it in more easy, user friendly , optimised manner.
3. Never copy the exact code if presented with a code from other location or file or codebase, just make it optimised
4. Always Follow the principle LESS IS MORE
5. Make it minimal change as possible
6. Firstly try not to make any modification to the existing code as possible
7. Only modify the existing code if its highly required, without that if it cant be done, then add new code section.
8. If you are adding new code, make sure to add it in a way that it can be easily integrated with the existing codebase.
+11
View File
@@ -0,0 +1,11 @@
from praisonaiagents import AutoAgents
from praisonaiagents.tools import duckduckgo
agents = AutoAgents(
instructions="Search for information about AI Agents",
tools=[duckduckgo],
process="sequential",
verbose=True
)
agents.start()
+183
View File
@@ -0,0 +1,183 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
import logging
import os
def main():
# Initialize memory config
memory_config = {
"provider": "rag",
"use_embedding": True,
"storage": {
"type": "sqlite",
"path": "./.praison/memory.db"
},
"rag_db_path": "./.praison/chroma_db"
}
# Test facts
fact1 = "The capital city of Jujuha is Hahanu and its population is 102300"
fact2 = "Three main ingredients in a classic proloder are eggs, sugar, and flour"
fact3 = "The year the first Josinga was released is 2007"
# # Check if database exists
# if os.path.exists("./memory.db"):
# logger.info("Found existing memory database")
# else:
# logger.info("Creating new memory database")
# Create task config (without memory config since it's moved to PraisonAIAgents)
task_config = {}
# Create agents with different roles
researcher = Agent(
role="Research Analyst",
goal="Research and document key information about topics",
backstory="Expert at analyzing and storing information in memory",
llm="gpt-4o-mini"
)
retriever = Agent(
role="Information Retriever",
goal="Retrieve and verify stored information from memory",
backstory="Specialist in searching and validating information from memory",
llm="gpt-4o-mini"
)
# Task 1: Process the facts
store_task = Task(
description=f"""
Process and analyze this information:
1. {fact1}
2. {fact2}
3. {fact3}
Provide a clear summary of each fact.
""",
expected_output="""
Clear statements summarizing each fact.
Example format:
1. [Summary of fact 1]
2. [Summary of fact 2]
3. [Summary of fact 3]
""",
agent=researcher
)
# Task 2: Write essay about AI
verify_task = Task(
description="""
write few points about AI
""",
expected_output="Points about AI",
agent=retriever
)
# Task 3: Query memory
query_task = Task(
description="""
Using ONLY information found in memory:
1. What is stored in memory about Hahanu?
2. What ingredients for proloder are recorded in memory?
3. What year is stored in memory for the Josinga release?
For each answer, cite the memory record you found.
""",
expected_output="Answers based solely on memory records with citations",
agent=retriever
)
# Task 4: Query both short-term and long-term memory
query_both_task = Task(
description="""
Using ONLY information found in memory:
1. What is stored in both short-term and long-term memory about Jujuha?
2. What ingredients for proloder are recorded in both short-term and long-term memory?
3. What year is stored in both short-term and long-term memory for the Josinga release?
For each answer, cite the memory record you found.
""",
expected_output="Answers based solely on memory records with citations",
agent=retriever
)
# Initialize PraisonAIAgents with memory configuration
agents = PraisonAIAgents(
agents=[researcher, retriever],
tasks=[store_task, verify_task, query_task, query_both_task],
verbose=True, # Use same verbose level as memory
memory=True,
embedder={
"provider": "openai",
"config": {
"model": "text-embedding-3-small"
}
}
)
# agents = PraisonAIAgents(
# agents=[researcher, retriever],
# tasks=[store_task, verify_task, query_task, query_both_task],
# verbose=True, # Use same verbose level as memory
# memory=True
# )
# Execute tasks
print("\nExecuting Memory Test Tasks...")
print("-" * 50)
agents.start()
# Use shared memory for final checks
memory = agents.shared_memory
# Test memory retrieval with different quality thresholds
if memory:
print("\nFinal Memory Check:")
print("-" * 50)
queries = ["Jujuha", "proloder", "Josinga"]
for query in queries:
print(f"\nSearching memory for: {query}")
# Search in both short-term and long-term memory
print("\nShort-term memory results:")
stm_results = memory.search_short_term(query)
if stm_results:
for item in stm_results:
print(f"Content: {item.get('content', '')[:200]}")
if 'meta' in item:
print(f"Metadata: {item['meta']}")
print("-" * 20)
else:
print("No results found in short-term memory")
print("\nLong-term memory results:")
ltm_results = memory.search_long_term(query)
if ltm_results:
for item in ltm_results:
print(f"Content: {item.get('text', '')[:200]}")
if 'metadata' in item:
print(f"Metadata: {item['metadata']}")
print("-" * 20)
else:
print("No results found in long-term memory")
# Also check ChromaDB if using RAG
if memory.use_rag and hasattr(memory, "chroma_col"):
print("\nChromaDB results:")
try:
all_items = memory.chroma_col.get()
print(f"Found {len(all_items['ids'])} items in ChromaDB")
for i in range(len(all_items['ids'])):
print(f"ID: {all_items['ids'][i]}")
print(f"Content: {all_items['documents'][i][:200]}")
print(f"Metadata: {all_items['metadatas'][i]}")
print("-" * 20)
except Exception as e:
print(f"Error querying ChromaDB: {e}")
print("-" * 30)
else:
print("\nNo memory available for final checks")
if __name__ == "__main__":
main()
+63
View File
@@ -0,0 +1,63 @@
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import duckduckgo
# Test facts
fact1 = "The capital city of Jujuha is Hahanu and its population is 102300"
fact2 = "Three main ingredients in a classic proloder are eggs, sugar, and flour"
fact3 = "The year the first Josinga was released is 2007"
fact_agent = Agent(
name="Fact Agent",
instructions="You are a fact agent, you store and retrieve facts in memory",
llm="gpt-4o-mini"
)
research_agent = Agent(
name="Research Agent",
instructions="You are a research analyst, you research and document key points about topics",
llm="gpt-4o-mini"
)
blog_agent = Agent(
name="Blog Agent",
instructions="You are a blog writer, you write a blog post about the research",
llm="gpt-4o-mini"
)
fact_task = Task(
description="Store the following facts in memory: " + fact1 + ", " + fact2 + ", " + fact3,
agent=fact_agent
)
research_task = Task(
description="Research and document 2 key points about AI",
agent=research_agent
)
research_task2 = Task(
description="Research and document 2 key points about AI in healthcare",
agent=research_agent
)
research_task3 = Task(
description="Research and document 2 key points about AI in education",
agent=research_agent
)
research_task4 = Task(
description="Research and document 2 key points about AI in finance",
agent=research_agent
)
blog_task = Task(
description="Write a blog post about Jujuha",
agent=blog_agent
)
agents = PraisonAIAgents(
agents=[fact_agent, research_agent, blog_agent],
tasks=[fact_task, research_task, research_task2, research_task3, research_task4, blog_task],
memory=True
)
agents.start()
+4
View File
@@ -6,6 +6,8 @@ from .agent.agent import Agent
from .agents.agents import PraisonAIAgents
from .task.task import Task
from .tools.tools import Tools
from .agents.autoagents import AutoAgents
from .memory.memory import Memory
from .main import (
TaskOutput,
ReflectionOutput,
@@ -33,6 +35,8 @@ __all__ = [
'Task',
'TaskOutput',
'ReflectionOutput',
'AutoAgents',
'Memory',
'display_interaction',
'display_self_reflection',
'display_instruction',
+34 -6
View File
@@ -162,7 +162,7 @@ class Agent:
max_iter: int = 20,
max_rpm: Optional[int] = None,
max_execution_time: Optional[int] = None,
memory: bool = True,
memory: Optional[Any] = None,
verbose: bool = True,
allow_delegation: bool = False,
step_callback: Optional[Any] = None,
@@ -178,7 +178,7 @@ class Agent:
knowledge_sources: Optional[List[Any]] = None,
use_system_prompt: Optional[bool] = True,
markdown: bool = True,
self_reflect: Optional[bool] = None,
self_reflect: bool = False,
max_reflect: int = 3,
min_reflect: int = 1,
reflect_llm: Optional[str] = None
@@ -381,7 +381,7 @@ Your Goal: {self.goal}
display_generating("", start_time),
console=self.console,
refresh_per_second=4,
transient=False, # Changed to False to preserve output
transient=True, # Changed to False to preserve output
vertical_overflow="ellipsis",
auto_refresh=True
) as live:
@@ -466,7 +466,15 @@ Your Goal: {self.goal}
display_text = next((item["text"] for item in prompt if item["type"] == "text"), "")
if display_text and str(display_text).strip():
display_instruction(f"Agent {self.name} is processing prompt: {display_text}", console=self.console)
# Pass agent information to display_instruction
agent_tools = [t.__name__ if hasattr(t, '__name__') else str(t) for t in self.tools]
display_instruction(
f"Agent {self.name} is processing prompt: {display_text}",
console=self.console,
agent_name=self.name,
agent_role=self.role,
agent_tools=agent_tools
)
response = self._chat_completion(messages, temperature=temperature, tools=tools if tools else None)
if not response:
@@ -525,7 +533,7 @@ Your Goal: {self.goal}
self.chat_history.append({"role": "user", "content": original_prompt})
self.chat_history.append({"role": "assistant", "content": response_text})
if self.verbose:
logging.info(f"Agent {self.name} final response: {response_text}")
logging.debug(f"Agent {self.name} final response: {response_text}")
display_interaction(original_prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
return response_text
@@ -630,6 +638,22 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
item["text"] += "\nReturn ONLY a valid JSON object. No other text or explanation."
break
# Display instruction with agent info if verbose
if self.verbose:
display_text = prompt
if isinstance(prompt, list):
display_text = next((item["text"] for item in prompt if item["type"] == "text"), "")
if display_text and str(display_text).strip():
agent_tools = [t.__name__ if hasattr(t, '__name__') else str(t) for t in self.tools]
await adisplay_instruction(
f"Agent {self.name} is processing prompt: {display_text}",
console=self.console,
agent_name=self.name,
agent_role=self.role,
agent_tools=agent_tools
)
# Format tools if provided
formatted_tools = []
if tools:
@@ -733,4 +757,8 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
return None
except Exception as e:
display_error(f"Error in _achat_completion: {e}")
return None
return None
def run(self):
"""Alias for start() method"""
return self.start()
+2 -1
View File
@@ -1,4 +1,5 @@
"""Agents module for managing multiple AI agents"""
from .agents import PraisonAIAgents
from .autoagents import AutoAgents
__all__ = ['PraisonAIAgents']
__all__ = ['PraisonAIAgents', 'AutoAgents']
+169 -35
View File
@@ -13,6 +13,9 @@ from ..task.task import Task
from ..process.process import Process, LoopItems
import asyncio
# Set up logger
logger = logging.getLogger(__name__)
def encode_file_to_base64(file_path: str) -> str:
"""Base64-encode a file."""
import base64
@@ -41,7 +44,7 @@ def process_video(video_path: str, seconds_per_frame=2):
return base64_frames
class PraisonAIAgents:
def __init__(self, agents, tasks=None, verbose=0, completion_checker=None, max_retries=5, process="sequential", manager_llm=None):
def __init__(self, agents, tasks=None, verbose=0, completion_checker=None, max_retries=5, process="sequential", manager_llm=None, memory=False, memory_config=None, embedder=None):
if not agents:
raise ValueError("At least one agent must be provided")
@@ -57,8 +60,21 @@ class PraisonAIAgents:
# Check for manager_llm in environment variable if not provided
self.manager_llm = manager_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o')
# Set logger level based on verbose
if verbose >= 5:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.WARNING)
# Also set third-party loggers to WARNING
logging.getLogger('chromadb').setLevel(logging.WARNING)
logging.getLogger('openai').setLevel(logging.WARNING)
logging.getLogger('httpx').setLevel(logging.WARNING)
logging.getLogger('httpcore').setLevel(logging.WARNING)
if self.verbose:
logging.info(f"Using model {self.manager_llm} for manager")
logger.info(f"Using model {self.manager_llm} for manager")
# If no tasks provided, generate them from agents
if tasks is None:
@@ -66,12 +82,12 @@ class PraisonAIAgents:
for agent in self.agents:
task = agent.generate_task()
tasks.append(task)
logging.info(f"Auto-generated {len(tasks)} tasks from agents")
logger.info(f"Auto-generated {len(tasks)} tasks from agents")
else:
# Validate tasks for backward compatibility
if not tasks:
raise ValueError("If tasks are provided, at least one task must be present")
logging.info(f"Using {len(tasks)} provided tasks")
logger.info(f"Using {len(tasks)} provided tasks")
# Add tasks and set their status
for task in tasks:
@@ -87,9 +103,65 @@ class PraisonAIAgents:
if tasks[i + 1].context is None:
tasks[i + 1].context = []
tasks[i + 1].context.append(tasks[i])
logging.info("Set up sequential flow with automatic context passing")
logger.info("Set up sequential flow with automatic context passing")
self._state = {} # Add state storage at PraisonAIAgents level
# Initialize memory system
self.shared_memory = None
if memory:
try:
from ..memory.memory import Memory
# Get memory config from parameter or first task
mem_cfg = memory_config
if not mem_cfg:
mem_cfg = next((t.config.get('memory_config') for t in tasks if hasattr(t, 'config') and t.config), None)
# Set default memory config if none provided
if not mem_cfg:
mem_cfg = {
"provider": "rag",
"use_embedding": True,
"storage": {
"type": "sqlite",
"path": "./.praison/memory.db"
},
"rag_db_path": "./.praison/chroma_db"
}
# Add embedder config if provided
if embedder:
if isinstance(embedder, dict):
mem_cfg = mem_cfg or {}
mem_cfg["embedder"] = embedder
else:
# Handle direct embedder function
mem_cfg = mem_cfg or {}
mem_cfg["embedder_function"] = embedder
if mem_cfg:
# Pass verbose level to Memory
self.shared_memory = Memory(config=mem_cfg, verbose=verbose)
if verbose >= 5:
logger.info("Initialized shared memory for PraisonAIAgents")
# Distribute memory to tasks
for task in tasks:
if not task.memory:
task.memory = self.shared_memory
if verbose >= 5:
logger.info(f"Assigned shared memory to task {task.id}")
except Exception as e:
logger.error(f"Failed to initialize shared memory: {e}")
# Update tasks with shared memory
if self.shared_memory:
for task in tasks:
if not task.memory:
task.memory = self.shared_memory
logger.info(f"Assigned shared memory to task {task.id}")
def add_task(self, task):
task_id = self.task_id_counter
@@ -142,7 +214,7 @@ class PraisonAIAgents:
task_prompt = f"""
You need to do the following task: {task.description}.
Expected Output: {task.expected_output}.
"""
"""
if task.context:
context_results = ""
for context_task in task.context:
@@ -157,8 +229,8 @@ Here are the results of previous tasks that might be useful:\n
task_prompt += "Please provide only the final result of your work. Do not add any conversation or extra explanation."
if self.verbose >= 2:
logging.info(f"Executing task {task_id}: {task.description} using {executor_agent.name}")
logging.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
logger.info(f"Executing task {task_id}: {task.description} using {executor_agent.name}")
logger.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
if task.images:
def _get_multimodal_message(text_prompt, images):
@@ -223,8 +295,8 @@ Here are the results of previous tasks that might be useful:\n
task_output.json_dict = parsed
task_output.output_format = "JSON"
except:
logging.warning(f"Warning: Could not parse output of task {task_id} as JSON")
logging.debug(f"Output that failed JSON parsing: {agent_output}")
logger.warning(f"Warning: Could not parse output of task {task_id} as JSON")
logger.debug(f"Output that failed JSON parsing: {agent_output}")
if task.output_pydantic:
cleaned = self.clean_json_output(agent_output)
@@ -234,8 +306,8 @@ Here are the results of previous tasks that might be useful:\n
task_output.pydantic = pyd_obj
task_output.output_format = "Pydantic"
except:
logging.warning(f"Warning: Could not parse output of task {task_id} as Pydantic Model")
logging.debug(f"Output that failed Pydantic parsing: {agent_output}")
logger.warning(f"Warning: Could not parse output of task {task_id} as Pydantic Model")
logger.debug(f"Output that failed Pydantic parsing: {agent_output}")
task.result = task_output
return task_output
@@ -250,37 +322,53 @@ Here are the results of previous tasks that might be useful:\n
return
task = self.tasks[task_id]
if task.status == "completed":
logging.info(f"Task with ID {task_id} is already completed")
logger.info(f"Task with ID {task_id} is already completed")
return
retries = 0
while task.status != "completed" and retries < self.max_retries:
logging.debug(f"Attempt {retries+1} for task {task_id}")
logger.debug(f"Attempt {retries+1} for task {task_id}")
if task.status in ["not started", "in progress"]:
task_output = await self.aexecute_task(task_id)
if task_output and self.completion_checker(task, task_output.raw):
task.status = "completed"
if task.callback:
# Run execute_callback for memory operations
try:
await task.execute_callback(task_output)
except Exception as e:
logger.error(f"Error executing memory callback for task {task_id}: {e}")
logger.exception(e)
# Run task callback if exists
if task.callback:
try:
if asyncio.iscoroutinefunction(task.callback):
await task.callback(task_output)
else:
task.callback(task_output)
except Exception as e:
logger.error(f"Error executing task callback for task {task_id}: {e}")
logger.exception(e)
self.save_output_to_file(task, task_output)
if self.verbose >= 1:
logging.info(f"Task {task_id} completed successfully.")
logger.info(f"Task {task_id} completed successfully.")
else:
task.status = "in progress"
if self.verbose >= 1:
logging.info(f"Task {task_id} not completed, retrying")
logger.info(f"Task {task_id} not completed, retrying")
await asyncio.sleep(1)
retries += 1
else:
if task.status == "failed":
logging.info("Task is failed, resetting to in-progress for another try...")
logger.info("Task is failed, resetting to in-progress for another try...")
task.status = "in progress"
else:
logging.info("Invalid Task status")
logger.info("Invalid Task status")
break
if retries == self.max_retries and task.status != "completed":
logging.info(f"Task {task_id} failed after {self.max_retries} retries.")
logger.info(f"Task {task_id} failed after {self.max_retries} retries.")
async def arun_all_tasks(self):
"""Async version of run_all_tasks method"""
@@ -328,7 +416,7 @@ Here are the results of previous tasks that might be useful:\n
with open(task.output_file, "w") as f:
f.write(str(task_output))
if self.verbose >= 1:
logging.info(f"Task output saved to {task.output_file}")
logger.info(f"Task output saved to {task.output_file}")
except Exception as e:
display_error(f"Error saving task output to file: {e}")
@@ -339,6 +427,15 @@ Here are the results of previous tasks that might be useful:\n
return
task = self.tasks[task_id]
logger.info(f"Starting execution of task {task_id}")
logger.info(f"Task config: {task.config}")
# Initialize memory before task execution
if not task.memory:
task.memory = task.initialize_memory()
logger.info(f"Task memory status: {'Initialized' if task.memory else 'Not initialized'}")
# Only import multimodal dependencies if task has images
if task.images and task.status == "not started":
try:
@@ -371,11 +468,20 @@ Expected Output: {task.expected_output}.
Here are the results of previous tasks that might be useful:\n
{context_results}
"""
# Add memory context if available
if task.memory:
try:
memory_context = task.memory.build_context_for_task(task.description)
if memory_context:
task_prompt += f"\n\nRelevant memory context:\n{memory_context}"
except Exception as e:
logger.error(f"Error getting memory context: {e}")
task_prompt += "Please provide only the final result of your work. Do not add any conversation or extra explanation."
if self.verbose >= 2:
logging.info(f"Executing task {task_id}: {task.description} using {executor_agent.name}")
logging.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
logger.info(f"Executing task {task_id}: {task.description} using {executor_agent.name}")
logger.debug(f"Starting execution of task {task_id} with prompt:\n{task_prompt}")
if task.images:
def _get_multimodal_message(text_prompt, images):
@@ -425,6 +531,17 @@ Here are the results of previous tasks that might be useful:\n
)
if agent_output:
# Store the response in memory
if task.memory:
try:
task.store_in_memory(
content=agent_output,
agent_name=executor_agent.name,
task_id=task_id
)
except Exception as e:
logger.error(f"Failed to store agent output in memory: {e}")
task_output = TaskOutput(
description=task.description,
summary=task.description[:10],
@@ -440,8 +557,8 @@ Here are the results of previous tasks that might be useful:\n
task_output.json_dict = parsed
task_output.output_format = "JSON"
except:
logging.warning(f"Warning: Could not parse output of task {task_id} as JSON")
logging.debug(f"Output that failed JSON parsing: {agent_output}")
logger.warning(f"Warning: Could not parse output of task {task_id} as JSON")
logger.debug(f"Output that failed JSON parsing: {agent_output}")
if task.output_pydantic:
cleaned = self.clean_json_output(agent_output)
@@ -451,8 +568,8 @@ Here are the results of previous tasks that might be useful:\n
task_output.pydantic = pyd_obj
task_output.output_format = "Pydantic"
except:
logging.warning(f"Warning: Could not parse output of task {task_id} as Pydantic Model")
logging.debug(f"Output that failed Pydantic parsing: {agent_output}")
logger.warning(f"Warning: Could not parse output of task {task_id} as Pydantic Model")
logger.debug(f"Output that failed Pydantic parsing: {agent_output}")
task.result = task_output
return task_output
@@ -467,37 +584,54 @@ Here are the results of previous tasks that might be useful:\n
return
task = self.tasks[task_id]
if task.status == "completed":
logging.info(f"Task with ID {task_id} is already completed")
logger.info(f"Task with ID {task_id} is already completed")
return
retries = 0
while task.status != "completed" and retries < self.max_retries:
logging.debug(f"Attempt {retries+1} for task {task_id}")
logger.debug(f"Attempt {retries+1} for task {task_id}")
if task.status in ["not started", "in progress"]:
task_output = self.execute_task(task_id)
if task_output and self.completion_checker(task, task_output.raw):
task.status = "completed"
# Run execute_callback for memory operations
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(task.execute_callback(task_output))
except Exception as e:
logger.error(f"Error executing memory callback for task {task_id}: {e}")
logger.exception(e)
# Run task callback if exists
if task.callback:
task.callback(task_output)
try:
if asyncio.iscoroutinefunction(task.callback):
loop.run_until_complete(task.callback(task_output))
else:
task.callback(task_output)
except Exception as e:
logger.error(f"Error executing task callback for task {task_id}: {e}")
logger.exception(e)
self.save_output_to_file(task, task_output)
if self.verbose >= 1:
logging.info(f"Task {task_id} completed successfully.")
logger.info(f"Task {task_id} completed successfully.")
else:
task.status = "in progress"
if self.verbose >= 1:
logging.info(f"Task {task_id} not completed, retrying")
logger.info(f"Task {task_id} not completed, retrying")
time.sleep(1)
retries += 1
else:
if task.status == "failed":
logging.info("Task is failed, resetting to in-progress for another try...")
logger.info("Task is failed, resetting to in-progress for another try...")
task.status = "in progress"
else:
logging.info("Invalid Task status")
logger.info("Invalid Task status")
break
if retries == self.max_retries and task.status != "completed":
logging.info(f"Task {task_id} failed after {self.max_retries} retries.")
logger.info(f"Task {task_id} failed after {self.max_retries} retries.")
def run_all_tasks(self):
"""Synchronous version of run_all_tasks method"""
+335
View File
@@ -0,0 +1,335 @@
"""
AutoAgents - A class for automatically creating and managing AI agents and tasks.
This class provides a simplified interface for creating and running AI agents with tasks.
It automatically handles agent creation, task setup, and execution flow.
"""
from .agents import PraisonAIAgents
from ..agent.agent import Agent
from ..task.task import Task
from typing import List, Any, Optional, Dict, Union
import logging
import os
from pydantic import BaseModel, Field
from ..main import display_instruction, display_tool_call, display_interaction, client
# Define Pydantic models for structured output
class TaskConfig(BaseModel):
name: str
description: str
expected_output: str
tools: List[str]
class AgentConfig(BaseModel):
name: str
role: str
goal: str
backstory: str
tools: List[str]
tasks: List[TaskConfig]
class AutoAgentsConfig(BaseModel):
main_instruction: str
process_type: str
agents: List[AgentConfig]
class AutoAgents(PraisonAIAgents):
def __init__(
self,
instructions: str,
tools: Optional[List[Any]] = None,
verbose: bool = False,
process: str = "sequential",
manager_llm: Optional[str] = None,
max_retries: int = 5,
completion_checker: Optional[Any] = None,
allow_code_execution: bool = False,
memory: bool = True,
markdown: bool = True,
self_reflect: bool = False,
max_reflect: int = 3,
min_reflect: int = 1,
llm: Optional[str] = None,
function_calling_llm: Optional[str] = None,
respect_context_window: bool = True,
code_execution_mode: str = "safe",
embedder_config: Optional[Dict[str, Any]] = None,
knowledge_sources: Optional[List[Any]] = None,
use_system_prompt: bool = True,
cache: bool = True,
allow_delegation: bool = False,
step_callback: Optional[Any] = None,
system_template: Optional[str] = None,
prompt_template: Optional[str] = None,
response_template: Optional[str] = None,
max_rpm: Optional[int] = None,
max_execution_time: Optional[int] = None,
max_iter: int = 20,
reflect_llm: Optional[str] = None,
base_url: Optional[str] = None,
api_key: Optional[str] = None,
max_agents: int = 3 # New parameter for maximum number of agents
):
"""Initialize AutoAgents with configuration for automatic agent and task creation."""
if max_agents < 1:
raise ValueError("max_agents must be at least 1")
if max_agents > 10:
raise ValueError("max_agents cannot exceed 10")
# Store all configuration parameters first
self.instructions = instructions
self.tools = tools or []
self.verbose = verbose # Store verbose parameter
self.max_agents = max_agents # Store max_agents parameter
self.allow_code_execution = allow_code_execution
self.memory = memory
self.markdown = markdown
self.self_reflect = self_reflect
self.max_reflect = max_reflect
self.min_reflect = min_reflect
self.llm = llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o')
self.function_calling_llm = function_calling_llm
self.respect_context_window = respect_context_window
self.code_execution_mode = code_execution_mode
self.embedder_config = embedder_config
self.knowledge_sources = knowledge_sources
self.use_system_prompt = use_system_prompt
self.cache = cache
self.allow_delegation = allow_delegation
self.step_callback = step_callback
self.system_template = system_template
self.prompt_template = prompt_template
self.response_template = response_template
self.max_rpm = max_rpm
self.max_execution_time = max_execution_time
self.max_iter = max_iter
self.reflect_llm = reflect_llm
# Display initial instruction
if self.verbose:
display_instruction(f"🎯 Main Task: {self.instructions}")
display_instruction(f"📊 Maximum Agents: {self.max_agents}")
if self.tools:
tool_names = [t.__name__ if hasattr(t, '__name__') else str(t) for t in self.tools]
display_tool_call(f"🛠️ Available Tools: {', '.join(tool_names)}")
# Generate agents and tasks configuration
config = self._generate_config()
agents, tasks = self._create_agents_and_tasks(config)
# Display agents and their tasks
if self.verbose:
self._display_agents_and_tasks(agents, tasks)
# Initialize parent class with generated agents and tasks
super().__init__(
agents=agents,
tasks=tasks,
verbose=verbose,
completion_checker=completion_checker,
max_retries=max_retries,
process=process,
manager_llm=manager_llm
)
def _display_agents_and_tasks(self, agents: List[Agent], tasks: List[Task]):
"""Display the created agents and their assigned tasks"""
display_instruction("\n🤖 Generated Agents and Tasks:")
# Create a mapping of agents to their tasks
agent_tasks = {}
for task in tasks:
if task.agent not in agent_tasks:
agent_tasks[task.agent] = []
agent_tasks[task.agent].append(task)
# Display each agent and their tasks
for agent in agents:
agent_tools = [t.__name__ if hasattr(t, '__name__') else str(t) for t in agent.tools]
display_interaction(
f"\n👤 Agent: {agent.name}",
f"""Role: {agent.role}
Goal: {agent.goal}
Tools: {', '.join(agent_tools)}"""
)
# Display tasks for this agent
if agent in agent_tasks:
for i, task in enumerate(agent_tasks[agent], 1):
task_tools = [t.__name__ if hasattr(t, '__name__') else str(t) for t in task.tools]
display_instruction(
f""" 📋 Task {i}: {task.name}
Description: {task.description}
Expected Output: {task.expected_output}
Tools: {', '.join(task_tools)}"""
)
def _get_available_tools(self) -> List[str]:
"""Get list of available tools"""
if not self.tools:
return []
return [t.__name__ if hasattr(t, '__name__') else str(t) for t in self.tools]
def _get_tool_by_name(self, tool_name: str) -> Optional[Any]:
"""Get tool object by its name"""
for tool in self.tools:
if (hasattr(tool, '__name__') and tool.__name__ == tool_name) or str(tool) == tool_name:
return tool
return None
def _assign_tools_to_agent(self, agent_config: AgentConfig) -> List[Any]:
"""
Assign appropriate tools to an agent based on its role and tasks.
Args:
agent_config: The agent configuration containing role and required tools
Returns:
List of tool objects assigned to this agent
"""
assigned_tools = []
tool_names = set(agent_config.tools)
# Also look at task requirements
for task in agent_config.tasks:
tool_names.update(task.tools)
# Assign tools that match the requirements
for tool_name in tool_names:
tool = self._get_tool_by_name(tool_name)
if tool:
assigned_tools.append(tool)
# If no specific tools matched but we have tools available,
# assign all tools to ensure functionality
if not assigned_tools and self.tools:
assigned_tools = self.tools
return assigned_tools
def _generate_config(self) -> AutoAgentsConfig:
"""Generate the configuration for agents and tasks"""
prompt = f"""
Generate a configuration for AI agents to accomplish this task: "{self.instructions}"
The configuration should include:
1. A main instruction that clearly states the overall goal
2. A process type (sequential, workflow, or hierarchical)
3. A list of maximum {self.max_agents} agents (no more, no less), each with:
- Name, role, goal, and backstory
- List of required tools from: {self._get_available_tools()}
- Only add tools that are needed for the agent to perform its task
- Only one task per agent. Add more than one task if absolutely necessary.
- List of specific tasks they need to perform
- Whether they should self-reflect or allow delegation
Requirements:
1. Each agent should have clear, focused responsibilities
2. Tasks should be broken down into manageable steps
3. Tool selection should be appropriate for each task
4. The process type should match the task requirements
5. Generate maximum {self.max_agents} agents to handle this task efficiently
Return the configuration in a structured JSON format matching the AutoAgentsConfig schema.
"""
try:
response = client.beta.chat.completions.parse(
model=self.llm,
response_format=AutoAgentsConfig,
messages=[
{"role": "system", "content": "You are a helpful assistant designed to generate AI agent configurations."},
{"role": "user", "content": prompt}
]
)
# Ensure we have exactly max_agents number of agents
if len(response.choices[0].message.parsed.agents) > self.max_agents:
response.choices[0].message.parsed.agents = response.choices[0].message.parsed.agents[:self.max_agents]
elif len(response.choices[0].message.parsed.agents) < self.max_agents:
logging.warning(f"Generated {len(response.choices[0].message.parsed.agents)} agents, expected {self.max_agents}")
return response.choices[0].message.parsed
except Exception as e:
logging.error(f"Error generating configuration: {e}")
raise
def _create_agents_and_tasks(self, config: AutoAgentsConfig) -> tuple[List[Agent], List[Task]]:
"""Create agents and tasks from configuration"""
agents = []
tasks = []
for agent_config in config.agents:
# Get appropriate tools for this agent
agent_tools = self._assign_tools_to_agent(agent_config)
# Create the agent with all parameters
agent = Agent(
name=agent_config.name,
role=agent_config.role,
goal=agent_config.goal,
backstory=agent_config.backstory,
tools=agent_tools, # Use assigned tools
verbose=self.verbose >= 1,
allow_code_execution=self.allow_code_execution,
memory=self.memory,
markdown=self.markdown,
self_reflect=self.self_reflect,
max_reflect=self.max_reflect,
min_reflect=self.min_reflect,
llm=self.llm,
function_calling_llm=self.function_calling_llm,
respect_context_window=self.respect_context_window,
code_execution_mode=self.code_execution_mode,
embedder_config=self.embedder_config,
knowledge_sources=self.knowledge_sources,
use_system_prompt=self.use_system_prompt,
cache=self.cache,
allow_delegation=self.allow_delegation,
step_callback=self.step_callback,
system_template=self.system_template,
prompt_template=self.prompt_template,
response_template=self.response_template,
max_rpm=self.max_rpm,
max_execution_time=self.max_execution_time,
max_iter=self.max_iter,
reflect_llm=self.reflect_llm
)
agents.append(agent)
# Create tasks for this agent
for task_config in agent_config.tasks:
# Get task-specific tools
task_tools = [self._get_tool_by_name(t) for t in task_config.tools]
task_tools = [t for t in task_tools if t] # Remove None values
# If no specific tools matched, use agent's tools
if not task_tools:
task_tools = agent_tools
task = Task(
name=task_config.name,
description=task_config.description,
expected_output=task_config.expected_output,
agent=agent,
tools=task_tools # Use task-specific tools
)
tasks.append(task)
return agents, tasks
async def astart(self):
"""
Async version of start() method.
Creates tasks based on the instructions, then starts execution.
Returns the task status and results dictionary.
"""
return await super().astart()
def start(self):
"""
Creates tasks based on the instructions, then starts execution.
Returns the task status and results dictionary.
"""
return super().start()
+32 -8
View File
@@ -116,10 +116,10 @@ def display_interaction(message, response, markdown=True, generation_time=None,
console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
if markdown:
console.print(Panel.fit(Markdown(message), title="Message", border_style="cyan"))
console.print(Panel.fit(Markdown(message), title="Task", border_style="cyan"))
console.print(Panel.fit(Markdown(response), title="Response", border_style="cyan"))
else:
console.print(Panel.fit(Text(message, style="bold green"), title="Message", border_style="cyan"))
console.print(Panel.fit(Text(message, style="bold green"), title="Task", border_style="cyan"))
console.print(Panel.fit(Text(response, style="bold blue"), title="Response", border_style="cyan"))
def display_self_reflection(message: str, console=None):
@@ -135,7 +135,7 @@ def display_self_reflection(message: str, console=None):
console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
def display_instruction(message: str, console=None):
def display_instruction(message: str, console=None, agent_name: str = None, agent_role: str = None, agent_tools: List[str] = None):
if not message or not message.strip():
return
if console is None:
@@ -146,7 +146,19 @@ def display_instruction(message: str, console=None):
if 'instruction' in sync_display_callbacks:
sync_display_callbacks['instruction'](message=message)
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
# Display agent info if available
if agent_name:
agent_info = f"[bold #FF9B9B]👤 Agent:[/] [#FFE5E5]{agent_name}[/]"
if agent_role:
agent_info += f"\n[bold #B4B4B3]Role:[/] [#FFE5E5]{agent_role}[/]"
if agent_tools:
tools_str = ", ".join(f"[italic #B4D4FF]{tool}[/]" for tool in agent_tools)
agent_info += f"\n[bold #86A789]Tools:[/] {tools_str}"
console.print(Panel(agent_info, border_style="#D2E3C8", title="[bold]Agent Info[/]", title_align="left", padding=(1, 2)))
# Only print if log level is DEBUG
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
def display_tool_call(message: str, console=None):
if not message or not message.strip():
@@ -222,10 +234,10 @@ async def adisplay_interaction(message, response, markdown=True, generation_time
console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
if markdown:
console.print(Panel.fit(Markdown(message), title="Message", border_style="cyan"))
console.print(Panel.fit(Markdown(message), title="Task", border_style="cyan"))
console.print(Panel.fit(Markdown(response), title="Response", border_style="cyan"))
else:
console.print(Panel.fit(Text(message, style="bold green"), title="Message", border_style="cyan"))
console.print(Panel.fit(Text(message, style="bold green"), title="Task", border_style="cyan"))
console.print(Panel.fit(Text(response, style="bold blue"), title="Response", border_style="cyan"))
async def adisplay_self_reflection(message: str, console=None):
@@ -241,7 +253,7 @@ async def adisplay_self_reflection(message: str, console=None):
console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
async def adisplay_instruction(message: str, console=None):
async def adisplay_instruction(message: str, console=None, agent_name: str = None, agent_role: str = None, agent_tools: List[str] = None):
"""Async version of display_instruction."""
if not message or not message.strip():
return
@@ -252,7 +264,19 @@ async def adisplay_instruction(message: str, console=None):
if 'instruction' in async_display_callbacks:
await async_display_callbacks['instruction'](message=message)
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
# Display agent info if available
if agent_name:
agent_info = f"[bold #FF9B9B]👤 Agent:[/] [#FFE5E5]{agent_name}[/]"
if agent_role:
agent_info += f"\n[bold #B4B4B3]Role:[/] [#FFE5E5]{agent_role}[/]"
if agent_tools:
tools_str = ", ".join(f"[italic #B4D4FF]{tool}[/]" for tool in agent_tools)
agent_info += f"\n[bold #86A789]Tools:[/] {tools_str}"
console.print(Panel(agent_info, border_style="#D2E3C8", title="[bold]Agent Info[/]", title_align="left", padding=(1, 2)))
# Only print if log level is DEBUG
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
async def adisplay_tool_call(message: str, console=None):
"""Async version of display_tool_call."""
+931
View File
@@ -0,0 +1,931 @@
import os
import sqlite3
import json
import time
import shutil
from typing import Any, Dict, List, Optional, Union, Literal
import logging
# Set up logger
logger = logging.getLogger(__name__)
try:
import chromadb
from chromadb.config import Settings as ChromaSettings
CHROMADB_AVAILABLE = True
except ImportError:
CHROMADB_AVAILABLE = False
logger.warning("To use memory features, please run: pip install \"praisonaiagents[memory]\"")
try:
import mem0
MEM0_AVAILABLE = True
except ImportError:
MEM0_AVAILABLE = False
try:
import openai
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
class Memory:
"""
A single-file memory manager covering:
- Short-term memory (STM) for ephemeral context
- Long-term memory (LTM) for persistent knowledge
- Entity memory (structured data about named entities)
- User memory (preferences/history for each user)
- Quality score logic for deciding which data to store in LTM
- Context building from multiple memory sources
Config example:
{
"provider": "rag" or "mem0" or "none",
"use_embedding": True,
"short_db": "short_term.db",
"long_db": "long_term.db",
"rag_db_path": "rag_db", # optional path for local embedding store
"config": {
"api_key": "...", # if mem0 usage
"org_id": "...",
"project_id": "...",
...
}
}
"""
def __init__(self, config: Dict[str, Any], verbose: int = 0):
self.cfg = config or {}
self.verbose = verbose
# Set logger level based on verbose
if verbose >= 5:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.WARNING)
# Also set ChromaDB and OpenAI client loggers to WARNING
logging.getLogger('chromadb').setLevel(logging.WARNING)
logging.getLogger('openai').setLevel(logging.WARNING)
logging.getLogger('httpx').setLevel(logging.WARNING)
logging.getLogger('httpcore').setLevel(logging.WARNING)
logging.getLogger('chromadb.segment.impl.vector.local_persistent_hnsw').setLevel(logging.ERROR)
self.provider = self.cfg.get("provider", "rag")
self.use_mem0 = (self.provider.lower() == "mem0") and MEM0_AVAILABLE
self.use_rag = (self.provider.lower() == "rag") and CHROMADB_AVAILABLE and self.cfg.get("use_embedding", False)
# Create .praison directory if it doesn't exist
os.makedirs(".praison", exist_ok=True)
# Short-term DB
self.short_db = self.cfg.get("short_db", ".praison/short_term.db")
self._init_stm()
# Long-term DB
self.long_db = self.cfg.get("long_db", ".praison/long_term.db")
self._init_ltm()
# Conditionally init Mem0 or local RAG
if self.use_mem0:
self._init_mem0()
elif self.use_rag:
self._init_chroma()
def _log_verbose(self, msg: str, level: int = logging.INFO):
"""Only log if verbose >= 5"""
if self.verbose >= 5:
logger.log(level, msg)
# -------------------------------------------------------------------------
# Initialization
# -------------------------------------------------------------------------
def _init_stm(self):
"""Creates or verifies short-term memory table."""
os.makedirs(os.path.dirname(self.short_db) or ".", exist_ok=True)
conn = sqlite3.connect(self.short_db)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS short_mem (
id TEXT PRIMARY KEY,
content TEXT,
meta TEXT,
created_at REAL
)
""")
conn.commit()
conn.close()
def _init_ltm(self):
"""Creates or verifies long-term memory table."""
os.makedirs(os.path.dirname(self.long_db) or ".", exist_ok=True)
conn = sqlite3.connect(self.long_db)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS long_mem (
id TEXT PRIMARY KEY,
content TEXT,
meta TEXT,
created_at REAL
)
""")
conn.commit()
conn.close()
def _init_mem0(self):
"""Initialize Mem0 client for agent or user memory."""
from mem0 import MemoryClient
mem_cfg = self.cfg.get("config", {})
api_key = mem_cfg.get("api_key", os.getenv("MEM0_API_KEY"))
org_id = mem_cfg.get("org_id")
proj_id = mem_cfg.get("project_id")
if org_id and proj_id:
self.mem0_client = MemoryClient(api_key=api_key, org_id=org_id, project_id=proj_id)
else:
self.mem0_client = MemoryClient(api_key=api_key)
def _init_chroma(self):
"""Initialize a local Chroma client for embedding-based search."""
try:
# Create directory if it doesn't exist
rag_path = self.cfg.get("rag_db_path", "chroma_db")
os.makedirs(rag_path, exist_ok=True)
# Initialize ChromaDB with persistent storage
self.chroma_client = chromadb.PersistentClient(
path=rag_path,
settings=ChromaSettings(
anonymized_telemetry=False,
allow_reset=True
)
)
collection_name = "memory_store"
try:
self.chroma_col = self.chroma_client.get_collection(name=collection_name)
self._log_verbose("Using existing ChromaDB collection")
except Exception as e:
self._log_verbose(f"Collection '{collection_name}' not found. Creating new collection. Error: {e}")
self.chroma_col = self.chroma_client.create_collection(
name=collection_name,
metadata={"hnsw:space": "cosine"}
)
self._log_verbose("Created new ChromaDB collection")
except Exception as e:
self._log_verbose(f"Failed to initialize ChromaDB: {e}", logging.ERROR)
self.use_rag = False
# -------------------------------------------------------------------------
# Basic Quality Score Computation
# -------------------------------------------------------------------------
def compute_quality_score(
self,
completeness: float,
relevance: float,
clarity: float,
accuracy: float,
weights: Dict[str, float] = None
) -> float:
"""
Combine multiple sub-metrics into one final score, as an example.
Args:
completeness (float): 0-1
relevance (float): 0-1
clarity (float): 0-1
accuracy (float): 0-1
weights (Dict[str, float]): optional weighting like {"completeness": 0.25, "relevance": 0.3, ...}
Returns:
float: Weighted average 0-1
"""
if not weights:
weights = {
"completeness": 0.25,
"relevance": 0.25,
"clarity": 0.25,
"accuracy": 0.25
}
total = (completeness * weights["completeness"]
+ relevance * weights["relevance"]
+ clarity * weights["clarity"]
+ accuracy * weights["accuracy"]
)
return round(total, 3) # e.g. round to 3 decimal places
# -------------------------------------------------------------------------
# Short-Term Methods
# -------------------------------------------------------------------------
def store_short_term(
self,
text: str,
metadata: Dict[str, Any] = None,
completeness: float = None,
relevance: float = None,
clarity: float = None,
accuracy: float = None,
weights: Dict[str, float] = None,
evaluator_quality: float = None
):
"""Store in short-term memory with optional quality metrics"""
logger.info(f"Storing in short-term memory: {text[:100]}...")
logger.info(f"Metadata: {metadata}")
metadata = self._process_quality_metrics(
metadata, completeness, relevance, clarity,
accuracy, weights, evaluator_quality
)
logger.info(f"Processed metadata: {metadata}")
# Existing store logic
try:
conn = sqlite3.connect(self.short_db)
ident = str(time.time_ns())
conn.execute(
"INSERT INTO short_mem (id, content, meta, created_at) VALUES (?,?,?,?)",
(ident, text, json.dumps(metadata), time.time())
)
conn.commit()
conn.close()
logger.info(f"Successfully stored in short-term memory with ID: {ident}")
except Exception as e:
logger.error(f"Failed to store in short-term memory: {e}")
raise
def search_short_term(
self,
query: str,
limit: int = 5,
min_quality: float = 0.0,
relevance_cutoff: float = 0.0
) -> List[Dict[str, Any]]:
"""Search short-term memory with optional quality filter"""
self._log_verbose(f"Searching short memory for: {query}")
if self.use_mem0 and hasattr(self, "mem0_client"):
results = self.mem0_client.search(query=query, limit=limit)
filtered = [r for r in results if r.get("score", 1.0) >= relevance_cutoff]
return filtered
elif self.use_rag and hasattr(self, "chroma_col"):
try:
from openai import OpenAI
client = OpenAI()
response = client.embeddings.create(
input=query,
model="text-embedding-3-small"
)
query_embedding = response.data[0].embedding
resp = self.chroma_col.query(
query_embeddings=[query_embedding],
n_results=limit
)
results = []
if resp["ids"]:
for i in range(len(resp["ids"][0])):
metadata = resp["metadatas"][0][i] if "metadatas" in resp else {}
quality = metadata.get("quality", 0.0)
score = 1.0 - (resp["distances"][0][i] if "distances" in resp else 0.0)
if quality >= min_quality and score >= relevance_cutoff:
results.append({
"id": resp["ids"][0][i],
"text": resp["documents"][0][i],
"metadata": metadata,
"score": score
})
return results
except Exception as e:
self._log_verbose(f"Error searching ChromaDB: {e}", logging.ERROR)
return []
else:
# Local fallback
conn = sqlite3.connect(self.short_db)
c = conn.cursor()
rows = c.execute(
"SELECT id, content, meta FROM short_mem WHERE content LIKE ? LIMIT ?",
(f"%{query}%", limit)
).fetchall()
conn.close()
results = []
for row in rows:
meta = json.loads(row[2] or "{}")
quality = meta.get("quality", 0.0)
if quality >= min_quality:
results.append({
"id": row[0],
"text": row[1],
"metadata": meta
})
return results
def reset_short_term(self):
"""Completely clears short-term memory."""
conn = sqlite3.connect(self.short_db)
conn.execute("DELETE FROM short_mem")
conn.commit()
conn.close()
# -------------------------------------------------------------------------
# Long-Term Methods
# -------------------------------------------------------------------------
def _sanitize_metadata(self, metadata: Dict) -> Dict:
"""Sanitize metadata for ChromaDB - convert to acceptable types"""
sanitized = {}
for k, v in metadata.items():
if v is None:
continue
if isinstance(v, (str, int, float, bool)):
sanitized[k] = v
elif isinstance(v, dict):
# Convert dict to string representation
sanitized[k] = str(v)
else:
# Convert other types to string
sanitized[k] = str(v)
return sanitized
def store_long_term(
self,
text: str,
metadata: Dict[str, Any] = None,
completeness: float = None,
relevance: float = None,
clarity: float = None,
accuracy: float = None,
weights: Dict[str, float] = None,
evaluator_quality: float = None
):
"""Store in long-term memory with optional quality metrics"""
logger.info(f"Storing in long-term memory: {text[:100]}...")
logger.info(f"Initial metadata: {metadata}")
# Process metadata
metadata = metadata or {}
metadata = self._process_quality_metrics(
metadata, completeness, relevance, clarity,
accuracy, weights, evaluator_quality
)
logger.info(f"Processed metadata: {metadata}")
# Generate unique ID
ident = str(time.time_ns())
created = time.time()
# Store in SQLite
try:
conn = sqlite3.connect(self.long_db)
conn.execute(
"INSERT INTO long_mem (id, content, meta, created_at) VALUES (?,?,?,?)",
(ident, text, json.dumps(metadata), created)
)
conn.commit()
conn.close()
logger.info(f"Successfully stored in SQLite with ID: {ident}")
except Exception as e:
logger.error(f"Error storing in SQLite: {e}")
return
# Store in vector database if enabled
if self.use_rag and hasattr(self, "chroma_col"):
try:
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # Ensure API key is correctly set
logger.info("Getting embeddings from OpenAI...")
logger.debug(f"Embedding input text: {text}") # Log the input text
response = client.embeddings.create(
input=text,
model="text-embedding-3-small"
)
embedding = response.data[0].embedding
logger.info("Successfully got embeddings")
logger.debug(f"Received embedding of length: {len(embedding)}") # Log embedding details
# Sanitize metadata for ChromaDB
sanitized_metadata = self._sanitize_metadata(metadata)
# Store in ChromaDB with embedding
self.chroma_col.add(
documents=[text],
metadatas=[sanitized_metadata],
ids=[ident],
embeddings=[embedding]
)
logger.info(f"Successfully stored in ChromaDB with ID: {ident}")
except Exception as e:
logger.error(f"Error storing in ChromaDB: {e}")
elif self.use_mem0 and hasattr(self, "mem0_client"):
try:
self.mem0_client.add(text, metadata=metadata)
logger.info("Successfully stored in Mem0")
except Exception as e:
logger.error(f"Error storing in Mem0: {e}")
def search_long_term(
self,
query: str,
limit: int = 5,
relevance_cutoff: float = 0.0,
min_quality: float = 0.0
) -> List[Dict[str, Any]]:
"""Search long-term memory with optional quality filter"""
self._log_verbose(f"Searching long memory for: {query}")
self._log_verbose(f"Min quality: {min_quality}")
found = []
if self.use_mem0 and hasattr(self, "mem0_client"):
results = self.mem0_client.search(query=query, limit=limit)
# Filter by quality
filtered = [r for r in results if r.get("metadata", {}).get("quality", 0.0) >= min_quality]
logger.info(f"Found {len(filtered)} results in Mem0")
return filtered
elif self.use_rag and hasattr(self, "chroma_col"):
try:
from openai import OpenAI
client = OpenAI()
# Get query embedding
response = client.embeddings.create(
input=query,
model="text-embedding-3-small" # Using consistent model
)
query_embedding = response.data[0].embedding
# Search ChromaDB with embedding
resp = self.chroma_col.query(
query_embeddings=[query_embedding],
n_results=limit,
include=["documents", "metadatas", "distances"]
)
results = []
if resp["ids"]:
for i in range(len(resp["ids"][0])):
metadata = resp["metadatas"][0][i] if "metadatas" in resp else {}
text = resp["documents"][0][i]
# Add memory record citation
text = f"{text} (Memory record: {text})"
found.append({
"id": resp["ids"][0][i],
"text": text,
"metadata": metadata,
"score": 1.0 - (resp["distances"][0][i] if "distances" in resp else 0.0)
})
logger.info(f"Found {len(found)} results in ChromaDB")
except Exception as e:
self._log_verbose(f"Error searching ChromaDB: {e}", logging.ERROR)
# Always try SQLite as fallback or additional source
conn = sqlite3.connect(self.long_db)
c = conn.cursor()
rows = c.execute(
"SELECT id, content, meta, created_at FROM long_mem WHERE content LIKE ? LIMIT ?",
(f"%{query}%", limit)
).fetchall()
conn.close()
for row in rows:
meta = json.loads(row[2] or "{}")
text = row[1]
# Add memory record citation if not already present
if "(Memory record:" not in text:
text = f"{text} (Memory record: {text})"
# Only add if not already found by ChromaDB/Mem0
if not any(f["id"] == row[0] for f in found):
found.append({
"id": row[0],
"text": text,
"metadata": meta,
"created_at": row[3]
})
logger.info(f"Found {len(found)} total results after SQLite")
results = found
# Filter by quality if needed
if min_quality > 0:
self._log_verbose(f"Found {len(results)} initial results")
results = [
r for r in results
if r.get("metadata", {}).get("quality", 0.0) >= min_quality
]
self._log_verbose(f"After quality filter: {len(results)} results")
# Apply relevance cutoff if specified
if relevance_cutoff > 0:
results = [r for r in results if r.get("score", 1.0) >= relevance_cutoff]
logger.info(f"After relevance filter: {len(results)} results")
return results[:limit]
def reset_long_term(self):
"""Clear local LTM DB, plus Chroma or mem0 if in use."""
conn = sqlite3.connect(self.long_db)
conn.execute("DELETE FROM long_mem")
conn.commit()
conn.close()
if self.use_mem0 and hasattr(self, "mem0_client"):
# Mem0 has no universal reset API. Could implement partial or no-op.
pass
if self.use_rag and hasattr(self, "chroma_client"):
self.chroma_client.reset() # entire DB
self._init_chroma() # re-init fresh
# -------------------------------------------------------------------------
# Entity Memory Methods
# -------------------------------------------------------------------------
def store_entity(self, name: str, type_: str, desc: str, relations: str):
"""
Save entity info in LTM (or mem0/rag).
We'll label the metadata type = entity for easy filtering.
"""
data = f"Entity {name}({type_}): {desc} | relationships: {relations}"
self.store_long_term(data, metadata={"category": "entity"})
def search_entity(self, query: str, limit: int = 5) -> List[Dict[str, Any]]:
"""
Filter to items that have metadata 'category=entity'.
"""
all_hits = self.search_long_term(query, limit=20) # gather more
ents = []
for h in all_hits:
meta = h.get("metadata") or {}
if meta.get("category") == "entity":
ents.append(h)
return ents[:limit]
def reset_entity_only(self):
"""
If you only want to drop entity items from LTM, you'd do a custom
delete from local DB where meta LIKE '%category=entity%'.
For brevity, we do a full LTM reset here.
"""
self.reset_long_term()
# -------------------------------------------------------------------------
# User Memory Methods
# -------------------------------------------------------------------------
def store_user_memory(self, user_id: str, text: str, extra: Dict[str, Any] = None):
"""
If mem0 is used, do user-based addition. Otherwise store in LTM with user in metadata.
"""
meta = {"user_id": user_id}
if extra:
meta.update(extra)
if self.use_mem0 and hasattr(self, "mem0_client"):
self.mem0_client.add(text, user_id=user_id, metadata=meta)
else:
self.store_long_term(text, metadata=meta)
def search_user_memory(self, user_id: str, query: str, limit: int = 5) -> List[Dict[str, Any]]:
"""
If mem0 is used, pass user_id in. Otherwise fallback to local filter on user in metadata.
"""
if self.use_mem0 and hasattr(self, "mem0_client"):
return self.mem0_client.search(query=query, limit=limit, user_id=user_id)
else:
hits = self.search_long_term(query, limit=20)
filtered = []
for h in hits:
meta = h.get("metadata", {})
if meta.get("user_id") == user_id:
filtered.append(h)
return filtered[:limit]
def reset_user_memory(self):
"""
Clear all user-based info. For simplicity, we do a full LTM reset.
Real usage might filter only metadata "user_id".
"""
self.reset_long_term()
# -------------------------------------------------------------------------
# Putting it all Together: Task Finalization
# -------------------------------------------------------------------------
def finalize_task_output(
self,
content: str,
agent_name: str,
quality_score: float,
threshold: float = 0.7,
metrics: Dict[str, Any] = None,
task_id: str = None
):
"""Store task output in memory with appropriate metadata"""
logger.info(f"Finalizing task output: {content[:100]}...")
logger.info(f"Agent: {agent_name}, Quality: {quality_score}, Threshold: {threshold}")
metadata = {
"task_id": task_id,
"agent": agent_name,
"quality": quality_score,
"metrics": metrics,
"task_type": "output",
"stored_at": time.time()
}
logger.info(f"Prepared metadata: {metadata}")
# Always store in short-term memory
try:
logger.info("Storing in short-term memory...")
self.store_short_term(
text=content,
metadata=metadata
)
logger.info("Successfully stored in short-term memory")
except Exception as e:
logger.error(f"Failed to store in short-term memory: {e}")
# Store in long-term memory if quality meets threshold
if quality_score >= threshold:
try:
logger.info(f"Quality score {quality_score} >= {threshold}, storing in long-term memory...")
self.store_long_term(
text=content,
metadata=metadata
)
logger.info("Successfully stored in long-term memory")
except Exception as e:
logger.error(f"Failed to store in long-term memory: {e}")
else:
logger.info(f"Quality score {quality_score} < {threshold}, skipping long-term storage")
# -------------------------------------------------------------------------
# Building Context (Short, Long, Entities, User)
# -------------------------------------------------------------------------
def build_context_for_task(
self,
task_descr: str,
user_id: Optional[str] = None,
additional: str = "",
max_items: int = 3
) -> str:
"""
Merges relevant short-term, long-term, entity, user memories
into a single text block with deduplication and clean formatting.
"""
q = (task_descr + " " + additional).strip()
lines = []
seen_contents = set() # Track unique contents
def normalize_content(content: str) -> str:
"""Normalize content for deduplication"""
# Extract just the main content without citations for comparison
normalized = content.split("(Memory record:")[0].strip()
# Keep more characters to reduce false duplicates
normalized = ''.join(c.lower() for c in normalized if not c.isspace())
return normalized
def format_content(content: str, max_len: int = 150) -> str:
"""Format content with clean truncation at word boundaries"""
if not content:
return ""
# Clean up content by removing extra whitespace and newlines
content = ' '.join(content.split())
# If content contains a memory citation, preserve it
if "(Memory record:" in content:
return content # Keep original citation format
# Regular content truncation
if len(content) <= max_len:
return content
truncate_at = content.rfind(' ', 0, max_len - 3)
if truncate_at == -1:
truncate_at = max_len - 3
return content[:truncate_at] + "..."
def add_section(title: str, hits: List[Any]) -> None:
"""Add a section of memory hits with deduplication"""
if not hits:
return
formatted_hits = []
for h in hits:
content = h.get('text', '') if isinstance(h, dict) else str(h)
if not content:
continue
# Keep original format if it has a citation
if "(Memory record:" in content:
formatted = content
else:
formatted = format_content(content)
# Only add if we haven't seen this normalized content before
normalized = normalize_content(formatted)
if normalized not in seen_contents:
seen_contents.add(normalized)
formatted_hits.append(formatted)
if formatted_hits:
# Add section header
if lines:
lines.append("") # Space before new section
lines.append(title)
lines.append("=" * len(title)) # Underline the title
lines.append("") # Space after title
# Add formatted content with bullet points
for content in formatted_hits:
lines.append(f" • {content}")
# Add each section
# First get all results
short_term = self.search_short_term(q, limit=max_items)
long_term = self.search_long_term(q, limit=max_items)
entities = self.search_entity(q, limit=max_items)
user_mem = self.search_user_memory(user_id, q, limit=max_items) if user_id else []
# Add sections in order of priority
add_section("Short-term Memory Context", short_term)
add_section("Long-term Memory Context", long_term)
add_section("Entity Context", entities)
if user_id:
add_section("User Context", user_mem)
return "\n".join(lines) if lines else ""
# -------------------------------------------------------------------------
# Master Reset (Everything)
# -------------------------------------------------------------------------
def reset_all(self):
"""
Fully wipes short-term, long-term, and any memory in mem0 or rag.
"""
self.reset_short_term()
self.reset_long_term()
# Entities & user memory are stored in LTM or mem0, so no separate step needed.
def _process_quality_metrics(
self,
metadata: Dict[str, Any],
completeness: float = None,
relevance: float = None,
clarity: float = None,
accuracy: float = None,
weights: Dict[str, float] = None,
evaluator_quality: float = None
) -> Dict[str, Any]:
"""Process and store quality metrics in metadata"""
metadata = metadata or {}
# Handle sub-metrics if provided
if None not in [completeness, relevance, clarity, accuracy]:
metadata.update({
"completeness": completeness,
"relevance": relevance,
"clarity": clarity,
"accuracy": accuracy,
"quality": self.compute_quality_score(
completeness, relevance, clarity, accuracy, weights
)
})
# Handle external evaluator quality if provided
elif evaluator_quality is not None:
metadata["quality"] = evaluator_quality
return metadata
def calculate_quality_metrics(
self,
output: str,
expected_output: str,
llm: Optional[str] = None,
custom_prompt: Optional[str] = None
) -> Dict[str, float]:
"""Calculate quality metrics using LLM"""
logger.info("Calculating quality metrics for output")
logger.info(f"Output: {output[:100]}...")
logger.info(f"Expected: {expected_output[:100]}...")
# Default evaluation prompt
default_prompt = f"""
Evaluate the following output against expected output.
Score each metric from 0.0 to 1.0:
- Completeness: Does it address all requirements?
- Relevance: Does it match expected output?
- Clarity: Is it clear and well-structured?
- Accuracy: Is it factually correct?
Expected: {expected_output}
Actual: {output}
Return ONLY a JSON with these keys: completeness, relevance, clarity, accuracy
Example: {{"completeness": 0.95, "relevance": 0.8, "clarity": 0.9, "accuracy": 0.85}}
"""
try:
# Use OpenAI client from main.py
from ..main import client
response = client.chat.completions.create(
model=llm or "gpt-4o",
messages=[{
"role": "user",
"content": custom_prompt or default_prompt
}],
response_format={"type": "json_object"},
temperature=0.3
)
metrics = json.loads(response.choices[0].message.content)
# Validate metrics
required = ["completeness", "relevance", "clarity", "accuracy"]
if not all(k in metrics for k in required):
raise ValueError("Missing required metrics in LLM response")
logger.info(f"Calculated metrics: {metrics}")
return metrics
except Exception as e:
logger.error(f"Error calculating metrics: {e}")
return {
"completeness": 0.0,
"relevance": 0.0,
"clarity": 0.0,
"accuracy": 0.0
}
def store_quality(
self,
text: str,
quality_score: float,
task_id: Optional[str] = None,
iteration: Optional[int] = None,
metrics: Optional[Dict[str, float]] = None,
memory_type: Literal["short", "long"] = "long"
) -> None:
"""Store quality metrics in memory"""
logger.info(f"Attempting to store in {memory_type} memory: {text[:100]}...")
metadata = {
"quality": quality_score,
"task_id": task_id,
"iteration": iteration
}
if metrics:
metadata.update({
k: v for k, v in metrics.items() # Remove metric_ prefix
})
logger.info(f"With metadata: {metadata}")
try:
if memory_type == "short":
self.store_short_term(text, metadata=metadata)
logger.info("Successfully stored in short-term memory")
else:
self.store_long_term(text, metadata=metadata)
logger.info("Successfully stored in long-term memory")
except Exception as e:
logger.error(f"Failed to store in memory: {e}")
def search_with_quality(
self,
query: str,
min_quality: float = 0.0,
memory_type: Literal["short", "long"] = "long",
limit: int = 5
) -> List[Dict[str, Any]]:
"""Search with quality filter"""
logger.info(f"Searching {memory_type} memory for: {query}")
logger.info(f"Min quality: {min_quality}")
search_func = (
self.search_short_term if memory_type == "short"
else self.search_long_term
)
results = search_func(query, limit=limit)
logger.info(f"Found {len(results)} initial results")
filtered = [
r for r in results
if r.get("metadata", {}).get("quality", 0.0) >= min_quality
]
logger.info(f"After quality filter: {len(filtered)} results")
return filtered
+24 -12
View File
@@ -130,22 +130,28 @@ Return a JSON object with an 'items' array containing the items to process.
# 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 @@ Return a JSON object with an 'items' array containing the items to process.
# 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:
+157 -9
View File
@@ -5,12 +5,17 @@ from pydantic import BaseModel
from ..main import TaskOutput
from ..agent.agent import Agent
import uuid
import os
import time
# Set up logger
logger = logging.getLogger(__name__)
class Task:
def __init__(
self,
description: str,
expected_output: str,
expected_output: Optional[str] = None,
agent: Optional[Agent] = None,
name: Optional[str] = None,
tools: Optional[List[Any]] = None,
@@ -30,12 +35,14 @@ class Task:
task_type: str = "task",
condition: Optional[Dict[str, List[str]]] = None,
is_start: bool = False,
loop_state: Optional[Dict[str, Union[str, int]]] = None
loop_state: Optional[Dict[str, Union[str, int]]] = None,
memory=None,
quality_check=True
):
self.id = str(uuid.uuid4()) if id is None else str(id)
self.name = name
self.description = description
self.expected_output = expected_output
self.expected_output = expected_output if expected_output is not None else "Complete the task successfully"
self.agent = agent
self.tools = tools if tools else []
self.context = context if context else []
@@ -54,6 +61,21 @@ class Task:
self.condition = condition if condition else {}
self.is_start = is_start
self.loop_state = loop_state if loop_state else {}
self.memory = memory
self.quality_check = quality_check
# Set logger level based on config verbose level
verbose = self.config.get("verbose", 0)
if verbose >= 5:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.WARNING)
# Also set third-party loggers to WARNING
logging.getLogger('chromadb').setLevel(logging.WARNING)
logging.getLogger('openai').setLevel(logging.WARNING)
logging.getLogger('httpx').setLevel(logging.WARNING)
logging.getLogger('httpcore').setLevel(logging.WARNING)
if self.output_json and self.output_pydantic:
raise ValueError("Only one output type can be defined")
@@ -64,11 +86,137 @@ class Task:
def __str__(self):
return f"Task(name='{self.name if self.name else 'None'}', description='{self.description}', agent='{self.agent.name if self.agent else 'None'}', status='{self.status}')"
def initialize_memory(self):
"""Initialize memory if config exists but memory doesn't"""
if not self.memory and self.config.get('memory_config'):
try:
from ..memory.memory import Memory
logger.info(f"Task {self.id}: Initializing memory from config: {self.config['memory_config']}")
self.memory = Memory(config=self.config['memory_config'])
logger.info(f"Task {self.id}: Memory initialized successfully")
# Verify database was created
if os.path.exists(self.config['memory_config']['storage']['path']):
logger.info(f"Task {self.id}: Memory database exists after initialization")
else:
logger.error(f"Task {self.id}: Failed to create memory database!")
return self.memory
except Exception as e:
logger.error(f"Task {self.id}: Failed to initialize memory: {e}")
logger.exception(e)
return None
def store_in_memory(self, content: str, agent_name: str = None, task_id: str = None):
"""Store content in memory with metadata"""
if self.memory:
try:
logger.info(f"Task {self.id}: Storing content in memory...")
self.memory.store_long_term(
text=content,
metadata={
"agent_name": agent_name or "Agent",
"task_id": task_id or self.id,
"timestamp": time.time()
}
)
logger.info(f"Task {self.id}: Content stored in memory")
except Exception as e:
logger.error(f"Task {self.id}: Failed to store content in memory: {e}")
logger.exception(e)
async def execute_callback(self, task_output: TaskOutput) -> None:
"""Execute the callback function, handling both sync and async callbacks"""
"""Execute callback and store quality metrics if enabled"""
logger.info(f"Task {self.id}: execute_callback called")
logger.info(f"Quality check enabled: {self.quality_check}")
# Initialize memory if not already initialized
if not self.memory:
self.memory = self.initialize_memory()
logger.info(f"Memory object exists: {self.memory is not None}")
if self.memory:
logger.info(f"Memory config: {self.memory.cfg}")
# Store task output in memory
try:
logger.info(f"Task {self.id}: Storing task output in memory...")
self.store_in_memory(
content=task_output.raw,
agent_name=self.agent.name if self.agent else "Agent",
task_id=self.id
)
logger.info(f"Task {self.id}: Task output stored in memory")
except Exception as e:
logger.error(f"Task {self.id}: Failed to store task output in memory: {e}")
logger.exception(e)
logger.info(f"Task output: {task_output.raw[:100]}...")
if self.quality_check and self.memory:
try:
logger.info(f"Task {self.id}: Starting memory operations")
logger.info(f"Task {self.id}: Calculating quality metrics for output: {task_output.raw[:100]}...")
# Get quality metrics from LLM
metrics = self.memory.calculate_quality_metrics(
task_output.raw,
self.expected_output
)
logger.info(f"Task {self.id}: Quality metrics calculated: {metrics}")
quality_score = metrics.get("accuracy", 0.0)
logger.info(f"Task {self.id}: Quality score: {quality_score}")
# Store in both short and long-term memory with higher threshold
logger.info(f"Task {self.id}: Finalizing task output in memory...")
self.memory.finalize_task_output(
content=task_output.raw,
agent_name=self.agent.name if self.agent else "Agent",
quality_score=quality_score,
threshold=0.7, # Only high quality outputs in long-term memory
metrics=metrics,
task_id=self.id
)
logger.info(f"Task {self.id}: Finalized task output in memory")
# Store quality metrics separately
logger.info(f"Task {self.id}: Storing quality metrics...")
self.memory.store_quality(
text=task_output.raw,
quality_score=quality_score,
task_id=self.id,
metrics=metrics
)
# Store in both short and long-term memory with higher threshold
self.memory.finalize_task_output(
content=task_output.raw,
agent_name=self.agent.name if self.agent else "Agent",
quality_score=quality_score,
threshold=0.7 # Only high quality outputs in long-term memory
)
# Build context for next tasks
if self.next_tasks:
logger.info(f"Task {self.id}: Building context for next tasks...")
context = self.memory.build_context_for_task(
task_descr=task_output.raw,
max_items=5
)
logger.info(f"Task {self.id}: Built context for next tasks: {len(context)} items")
logger.info(f"Task {self.id}: Memory operations complete")
except Exception as e:
logger.error(f"Task {self.id}: Failed to process memory operations: {e}")
logger.exception(e) # Print full stack trace
# Continue execution even if memory operations fail
# Execute original callback
if self.callback:
if asyncio.iscoroutinefunction(self.callback):
await self.callback(task_output)
else:
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, self.callback, task_output)
try:
if asyncio.iscoroutinefunction(self.callback):
await self.callback(task_output)
else:
self.callback(task_output)
except Exception as e:
logger.error(f"Task {self.id}: Failed to execute callback: {e}")
logger.exception(e)
+91
View File
@@ -0,0 +1,91 @@
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
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]
)
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": ["final_processing"] # 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
)
return workflow
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()
+81
View File
@@ -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}")
+6 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "praisonaiagents"
version = "0.0.27"
version = "0.0.32"
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
authors = [
{ name="Mervin Praison" }
@@ -13,4 +13,9 @@ dependencies = [
"pydantic",
"rich",
"openai"
]
[project.optional-dependencies]
memory = [
"chromadb>=0.6.0"
]
+82
View File
@@ -0,0 +1,82 @@
from praisonaiagents.agent import Agent
from praisonaiagents.task import Task
from praisonaiagents.agents import PraisonAIAgents
import time
current_time = int(time.time())
result = "even" if current_time % 2 == 0 else "odd"
print(f"Time check: {current_time} is {result}")
# Create specialized agents
router = Agent(
name="Router",
role="Input Router",
goal="Evaluate input and determine routing path",
instructions="Analyze input and decide whether to proceed or exit",
tools=[get_time_check]
)
processor1 = Agent(
name="Processor 1",
role="Secondary Processor",
goal="Process valid inputs that passed initial check",
instructions="Process data that passed the routing check"
)
processor2 = Agent(
name="Processor 2",
role="Final Processor",
goal="Perform final processing on validated data",
instructions="Generate final output for processed data"
)
# Create tasks with routing logic
routing_task = Task(
name="initial_routing",
description="check the time and return according to what is returned",
expected_output="pass or fail based on what is returned",
agent=router,
is_start=True,
task_type="decision",
condition={
"pass": ["process_valid"],
"fail": "exit"
}
)
processing_task = Task(
name="process_valid",
description="Process validated input",
expected_output="Processed data ready for final step",
agent=processor1,
next_tasks=["final_process"]
)
final_task = Task(
name="final_process",
description="Generate final output",
expected_output="Final processed result",
agent=processor2
)
# Create and run workflow
workflow = PraisonAIAgents(
agents=[router, processor1, processor2],
tasks=[routing_task, processing_task, final_task],
process="workflow",
verbose=True
)
print("\nStarting Routing Workflow...")
print("=" * 50)
results = workflow.start()
print("\nWorkflow Results:")
print("=" * 50)
for task_id, result in results["task_results"].items():
if result:
task_name = result.description
print(f"\nTask: {task_name}")
print(f"Result: {result.raw}")
print("-" * 50)
+83
View File
@@ -0,0 +1,83 @@
from praisonaiagents.agent import Agent
from praisonaiagents.task import Task
from praisonaiagents.agents import PraisonAIAgents
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 specialized agents
router = Agent(
name="Router",
role="Input Router",
goal="Evaluate input and determine routing path",
instructions="Analyze input and decide whether to proceed or exit",
tools=[get_time_check]
)
processor1 = Agent(
name="Processor 1",
role="Secondary Processor",
goal="Process valid inputs that passed initial check",
instructions="Process data that passed the routing check"
)
processor2 = Agent(
name="Processor 2",
role="Final Processor",
goal="Perform final processing on validated data",
instructions="Generate final output for processed data"
)
# Create tasks with routing logic
routing_task = Task(
name="initial_routing",
description="check the time and return according to what is returned",
expected_output="pass or fail based on what is returned",
agent=router,
is_start=True,
task_type="decision",
condition={
"pass": ["process_valid"],
"fail": ["process_invalid"]
}
)
processing_task = Task(
name="process_valid",
description="Process validated input",
expected_output="Processed data ready for final step",
agent=processor1,
)
final_task = Task(
name="process_invalid",
description="Generate final output",
expected_output="Final processed result",
agent=processor2
)
# Create and run workflow
workflow = PraisonAIAgents(
agents=[router, processor1, processor2],
tasks=[routing_task, processing_task, final_task],
process="workflow",
verbose=True
)
print("\nStarting Routing Workflow...")
print("=" * 50)
results = workflow.start()
print("\nWorkflow Results:")
print("=" * 50)
for task_id, result in results["task_results"].items():
if result:
task_name = result.description
print(f"\nTask: {task_name}")
print(f"Result: {result.raw}")
print("-" * 50)
+95
View File
@@ -0,0 +1,95 @@
from praisonaiagents import Agent
from pydantic import BaseModel
from typing import List, Optional
# Define structured output models
class SearchResult(BaseModel):
title: str
url: Optional[str]
snippet: str
class AnalysisReport(BaseModel):
topic: str
key_findings: List[str]
search_results: List[SearchResult]
summary: str
confidence_score: float
def get_structured_analysis(query: str, verbose: bool = True) -> AnalysisReport:
"""
Performs a search and returns a structured analysis using an AI agent.
Args:
query (str): The search query or topic to analyze
verbose (bool): Whether to show detailed output
Returns:
AnalysisReport: A structured report containing the analysis
"""
# Create an agent with search capabilities
agent = Agent(
name="StructuredAnalyst",
role="Research Analyst",
goal="Analyze topics and provide structured reports",
backstory="Expert at gathering information and providing structured analysis",
verbose=verbose,
self_reflect=True, # Enable self-reflection for better quality
markdown=True,
llm="gpt-4o-mini"
)
# Create the analysis prompt
prompt = f"""
Analyze the following topic: "{query}"
Provide a structured analysis in JSON format that matches this schema:
{{
"topic": "string",
"key_findings": ["string"],
"search_results": [
{{
"title": "string",
"url": "string",
"snippet": "string"
}}
],
"summary": "string",
"confidence_score": float (0-1)
}}
Requirements:
1. Include at least 3 key findings
2. Include at least 2 relevant search results
3. Provide a comprehensive summary
4. Set confidence score based on quality of sources (0-1)
Return ONLY the JSON object, no other text.
"""
# Get structured response from agent
response = agent.chat(
prompt=prompt,
output_json=AnalysisReport # This ensures response matches our model
)
return AnalysisReport.model_validate_json(response)
if __name__ == "__main__":
# Example usage
analysis = get_structured_analysis("Latest developments in AI agents and autonomous systems")
print(analysis)
# Print the structured results
print("\n=== Structured Analysis Report ===")
print(f"Topic: {analysis.topic}")
print("\nKey Findings:")
for i, finding in enumerate(analysis.key_findings, 1):
print(f"{i}. {finding}")
print("\nSearch Results:")
for result in analysis.search_results:
print(f"\nTitle: {result.title}")
print(f"URL: {result.url}")
print(f"Snippet: {result.snippet}")
print(f"\nSummary: {analysis.summary}")
print(f"Confidence Score: {analysis.confidence_score:.2f}")
+1270 -1
View File
File diff suppressed because it is too large. Load diff
+233
View File
@@ -0,0 +1,233 @@
import asyncio
import time
from typing import List, Dict
from praisonaiagents import Agent, Task, PraisonAIAgents, TaskOutput
from praisonaiagents.main import (
display_error,
display_interaction,
display_tool_call,
display_instruction,
error_logs,
Console
)
from duckduckgo_search import DDGS
from pydantic import BaseModel
console = Console()
# 1. Define output model for structured results
class SearchResult(BaseModel):
query: str
results: List[Dict[str, str]]
total_results: int
# 2. Define both sync and async tools
def sync_search_tool(query: str) -> List[Dict]:
"""
Synchronous search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: Search results
"""
display_tool_call(f"Running sync search for: {query}", console)
time.sleep(1) # Simulate network delay
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
error_msg = f"Error during sync search: {e}"
display_error(error_msg, console)
error_logs.append(error_msg)
return []
async def async_search_tool(query: str) -> List[Dict]:
"""
Asynchronous search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: Search results
"""
display_tool_call(f"Running async search for: {query}", console)
await asyncio.sleep(1) # Simulate network delay
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
error_msg = f"Error during async search: {e}"
display_error(error_msg, console)
error_logs.append(error_msg)
return []
# 3. Define both sync and async callbacks
def sync_callback(output: TaskOutput):
display_interaction("Sync Callback", f"Processing output: {output.raw[:100]}...", markdown=True, console=console)
time.sleep(1) # Simulate processing
if output.output_format == "JSON":
display_tool_call(f"Processed JSON result: {output.json_dict}", console)
elif output.output_format == "Pydantic":
display_tool_call(f"Processed Pydantic result: {output.pydantic}", console)
async def async_callback(output: TaskOutput):
display_interaction("Async Callback", f"Processing output: {output.raw[:100]}...", markdown=True, console=console)
await asyncio.sleep(1) # Simulate processing
if output.output_format == "JSON":
display_tool_call(f"Processed JSON result: {output.json_dict}", console)
elif output.output_format == "Pydantic":
display_tool_call(f"Processed Pydantic result: {output.pydantic}", console)
# 4. Create agents with different tools
sync_agent = Agent(
name="SyncAgent",
role="Synchronous Search Specialist",
goal="Perform synchronous searches and return structured results",
backstory="Expert in sync operations and data organization",
tools=[sync_search_tool],
self_reflect=False,
verbose=True,
markdown=True
)
async_agent = Agent(
name="AsyncAgent",
role="Asynchronous Search Specialist",
goal="Perform asynchronous searches and return structured results",
backstory="Expert in async operations and data organization",
tools=[async_search_tool],
self_reflect=False,
verbose=True,
markdown=True
)
# 5. Create tasks with different configurations
sync_task = Task(
name="sync_search",
description="Search for 'Python programming' using sync tool and return structured results",
expected_output="SearchResult model with query details and results",
agent=sync_agent,
async_execution=False,
callback=sync_callback,
output_pydantic=SearchResult
)
async_task = Task(
name="async_search",
description="Search for 'Async programming' using async tool and return structured results",
expected_output="SearchResult model with query details and results",
agent=async_agent,
async_execution=True,
callback=async_callback,
output_pydantic=SearchResult
)
# 6. Create workflow tasks
workflow_sync_task = Task(
name="workflow_sync",
description="Workflow sync search for 'AI trends' with structured output",
expected_output="SearchResult model with AI trends data",
agent=sync_agent,
async_execution=False,
is_start=True,
next_tasks=["workflow_async"],
output_pydantic=SearchResult
)
workflow_async_task = Task(
name="workflow_async",
description="Workflow async search for 'Future of AI' with structured output",
expected_output="SearchResult model with Future of AI data",
agent=async_agent,
async_execution=True,
output_pydantic=SearchResult
)
# 7. Example usage functions
def run_sync_example():
"""Run synchronous example"""
display_instruction("\nRunning Synchronous Example...", console)
agents = PraisonAIAgents(
agents=[sync_agent],
tasks=[sync_task],
verbose=1,
process="sequential"
)
result = agents.start()
display_interaction("Sync Example", f"Result: {result}", markdown=True, console=console)
async def run_async_example():
"""Run asynchronous example"""
display_instruction("\nRunning Asynchronous Example...", console)
agents = PraisonAIAgents(
agents=[async_agent],
tasks=[async_task],
verbose=1,
process="sequential"
)
result = await agents.astart()
display_interaction("Async Example", f"Result: {result}", markdown=True, console=console)
async def run_mixed_example():
"""Run mixed sync/async example"""
display_instruction("\nRunning Mixed Sync/Async Example...", console)
agents = PraisonAIAgents(
agents=[sync_agent, async_agent],
tasks=[sync_task, async_task],
verbose=1,
process="sequential"
)
result = await agents.astart()
display_interaction("Mixed Example", f"Result: {result}", markdown=True, console=console)
async def run_workflow_example():
"""Run workflow example with both sync and async tasks"""
display_instruction("\nRunning Workflow Example...", console)
agents = PraisonAIAgents(
agents=[sync_agent, async_agent],
tasks=[workflow_sync_task, workflow_async_task],
verbose=1,
process="workflow"
)
result = await agents.astart()
display_interaction("Workflow Example", f"Result: {result}", markdown=True, console=console)
# 8. Main execution
async def main():
"""Main execution function"""
display_instruction("Starting PraisonAI Agents Examples...", console)
try:
# Run sync example in a separate thread to not block the event loop
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, run_sync_example)
# Run async examples
await run_async_example()
await run_mixed_example()
await run_workflow_example()
if error_logs:
display_error("\nErrors encountered during execution:", console)
for error in error_logs:
display_error(error, console)
except Exception as e:
display_error(f"Error in main execution: {e}", console)
error_logs.append(str(e))
if __name__ == "__main__":
# Run the main function
asyncio.run(main())
+152
View File
@@ -0,0 +1,152 @@
import asyncio
import time
from typing import List, Dict
from praisonaiagents import Agent, Task, PraisonAIAgents, TaskOutput
from duckduckgo_search import DDGS
from pydantic import BaseModel
# 1. Define output model for structured results
class SearchResult(BaseModel):
query: str
results: List[Dict[str, str]]
total_results: int
# 2. Define async tool
async def async_search_tool(query: str) -> Dict:
"""Perform asynchronous search and return structured results."""
await asyncio.sleep(1) # Simulate network delay
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return {
"query": query,
"results": results,
"total_results": len(results)
}
except Exception as e:
print(f"Error during async search: {e}")
return {
"query": query,
"results": [],
"total_results": 0
}
# 3. Define async callback
async def async_callback(output: TaskOutput):
await asyncio.sleep(1) # Simulate processing
if output.output_format == "JSON":
print(f"Processed JSON result: {output.json_dict}")
elif output.output_format == "Pydantic":
print(f"Processed Pydantic result: {output.pydantic}")
# 4. Create specialized agents
async_agent = Agent(
name="AsyncSearchAgent",
role="Search Specialist",
goal="Perform fast parallel searches with structured results",
backstory="Expert in efficient data retrieval and parallel search operations",
tools=[async_search_tool],
self_reflect=False,
verbose=True,
markdown=True
)
summary_agent = Agent(
name="SummaryAgent",
role="Research Synthesizer",
goal="Create concise summaries from multiple search results",
backstory="Expert in analyzing and synthesizing information from multiple sources",
self_reflect=True,
verbose=True,
markdown=True
)
# 5. Create async tasks
async_task = Task(
name="async_search",
description="Search for 'Async programming' and return results in JSON format with query, results array, and total_results count.",
expected_output="SearchResult model with structured data",
agent=async_agent,
async_execution=True,
callback=async_callback,
output_json=SearchResult
)
async def run_parallel_tasks():
"""Run multiple async tasks in parallel"""
print("\nRunning Parallel Async Tasks...")
# Define different search topics
search_topics = [
"Latest AI Developments 2024",
"Machine Learning Best Practices",
"Neural Networks Architecture"
]
# Create tasks for different topics
parallel_tasks = [
Task(
name=f"search_task_{i}",
description=f"Search for '{topic}' and return structured results with query details and findings.",
expected_output="SearchResult model with search data",
agent=async_agent,
async_execution=True,
callback=async_callback,
output_json=SearchResult
) for i, topic in enumerate(search_topics)
]
# Create summarization task
summary_task = Task(
name="summary_task",
description="Analyze all search results and create a concise summary highlighting key findings, patterns, and implications.",
expected_output="Structured summary with key findings and insights",
agent=summary_agent,
async_execution=False,
callback=async_callback,
context=parallel_tasks
)
# Create a single PraisonAIAgents instance with both agents
agents = PraisonAIAgents(
agents=[async_agent, summary_agent],
tasks=parallel_tasks + [summary_task],
verbose=1,
process="sequential"
)
# Run all tasks
results = await agents.astart()
print(f"Tasks Results: {results}")
# Return results in a serializable format
return {
"search_results": {
"task_status": {k: v for k, v in results["task_status"].items() if k != summary_task.id},
"task_results": [str(results["task_results"][i]) if results["task_results"][i] else None
for i in range(len(parallel_tasks))]
},
"summary": str(results["task_results"][summary_task.id]) if results["task_results"].get(summary_task.id) else None,
"topics": search_topics
}
# 6. Main execution
async def main():
"""Main execution function"""
print("Starting Async AI Agents Examples...")
try:
await run_parallel_tasks()
except Exception as e:
print(f"Error in main execution: {e}")
if __name__ == "__main__":
# Run the main function
asyncio.run(main())
@@ -0,0 +1,248 @@
import asyncio
import time
from typing import List, Dict
from praisonaiagents import Agent, Task, PraisonAIAgents, TaskOutput
from duckduckgo_search import DDGS
from pydantic import BaseModel
# 1. Define output model for structured results
class SearchResult(BaseModel):
query: str
results: List[Dict[str, str]]
total_results: int
# 2. Define async tool
async def async_search_tool(query: str) -> Dict:
"""
Asynchronous search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
dict: Search results in SearchResult model format
"""
await asyncio.sleep(1) # Simulate network delay
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
# Format response to match SearchResult model
return {
"query": query,
"results": results,
"total_results": len(results)
}
except Exception as e:
print(f"Error during async search: {e}")
return {
"query": query,
"results": [],
"total_results": 0
}
# 3. Define async callback
async def async_callback(output: TaskOutput):
await asyncio.sleep(1) # Simulate processing
if output.output_format == "JSON":
print(f"Processed JSON result: {output.json_dict}")
elif output.output_format == "Pydantic":
print(f"Processed Pydantic result: {output.pydantic}")
# 4. Create specialized agents
async_agent = Agent(
name="AsyncSearchAgent",
role="Asynchronous Search Specialist",
goal="Perform fast and efficient asynchronous searches with structured results",
backstory="Expert in parallel search operations and data retrieval",
tools=[async_search_tool],
self_reflect=False,
verbose=True,
markdown=True
)
summary_agent = Agent(
name="SummaryAgent",
role="Research Synthesizer",
goal="Create comprehensive summaries and identify patterns across multiple search results",
backstory="""Expert in analyzing and synthesizing information from multiple sources.
Skilled at identifying patterns, trends, and connections between different topics.
Specializes in creating clear, structured summaries that highlight key insights.""",
self_reflect=True, # Enable self-reflection for better summary quality
verbose=True,
markdown=True
)
# 5. Create async tasks
async_task = Task(
name="async_search",
description="""Search for 'Async programming' and return results in the following JSON format:
{
"query": "the search query",
"results": [
{
"title": "result title",
"url": "result url",
"snippet": "result snippet"
}
],
"total_results": number of results
}""",
expected_output="SearchResult model with query details and results",
agent=async_agent,
async_execution=True,
callback=async_callback,
output_json=SearchResult
)
# 6. Example usage functions
async def run_single_task():
"""Run single async task"""
print("\nRunning Single Async Task...")
agents = PraisonAIAgents(
agents=[async_agent],
tasks=[async_task],
verbose=1,
process="sequential"
)
result = await agents.astart()
print(f"Single Task Result: {result}")
async def run_parallel_tasks():
"""Run multiple async tasks in parallel"""
print("\nRunning Parallel Async Tasks...")
# Define different search topics
search_topics = [
"Latest AI Developments 2024",
"Machine Learning Best Practices",
"Neural Networks Architecture"
]
# Create tasks for different topics
parallel_tasks = [
Task(
name=f"search_task_{i}",
description=f"""Search for '{topic}' and return results in the following JSON format:
{{
"query": "{topic}",
"results": [
{{
"title": "result title",
"url": "result url",
"snippet": "result snippet"
}}
],
"total_results": number of results
}}""",
expected_output="SearchResult model with detailed information",
agent=async_agent,
async_execution=True,
callback=async_callback,
output_json=SearchResult
) for i, topic in enumerate(search_topics)
]
# Create summarization task with the specialized summary agent
summary_task = Task(
name="summary_task",
description="""As a Research Synthesizer, analyze the search results and create a comprehensive summary. Your task:
1. Analyze Results:
- Review all search results thoroughly
- Extract key findings from each topic
- Identify main themes and concepts
2. Find Connections:
- Identify relationships between topics
- Spot common patterns or contradictions
- Note emerging trends across sources
3. Create Structured Summary:
- Main findings per topic
- Cross-cutting themes
- Emerging trends
- Practical implications
- Future directions
4. Quality Checks:
- Ensure all topics are covered
- Verify accuracy of connections
- Confirm clarity of insights
- Validate practical relevance
Present the summary in a clear, structured format with sections for findings, patterns, trends, and implications.""",
expected_output="""A comprehensive research synthesis containing:
- Detailed findings from each search topic
- Cross-topic patterns and relationships
- Emerging trends and their implications
- Practical applications and future directions""",
agent=summary_agent, # Use the specialized summary agent
async_execution=False, # Run synchronously after search tasks
callback=async_callback
)
# First run parallel search tasks
agents = PraisonAIAgents(
agents=[async_agent],
tasks=parallel_tasks, # Only run search tasks first
verbose=1,
process="sequential"
)
search_results = await agents.astart()
print(f"Search Tasks Results: {search_results}")
# Create task objects with results for context
completed_tasks = []
for i, topic in enumerate(search_topics):
task = Task(
name=f"search_task_{i}_result",
description=f"Search results for: {topic}",
expected_output="Search results from previous task",
agent=async_agent,
result=search_results["task_results"][i]
)
completed_tasks.append(task)
# Update summary task with context from search results
summary_task.context = completed_tasks
# Run summarization task with summary agent
summary_agents = PraisonAIAgents(
agents=[summary_agent], # Use summary agent for synthesis
tasks=[summary_task],
verbose=1,
process="sequential"
)
summary_result = await summary_agents.astart()
print(f"Summary Task Result: {summary_result}")
# Return results in a serializable format
return {
"search_results": {
"task_status": search_results["task_status"],
"task_results": [str(result) if result else None for result in search_results["task_results"]]
},
"summary": str(summary_result),
"topics": search_topics
}
# 7. Main execution
async def main():
"""Main execution function"""
print("Starting Async AI Agents Examples...")
try:
# Run different async patterns
await run_single_task()
await run_parallel_tasks()
except Exception as e:
print(f"Error in main execution: {e}")
if __name__ == "__main__":
# Run the main function
asyncio.run(main())
+11
View File
@@ -0,0 +1,11 @@
from praisonaiagents import AutoAgents
from praisonaiagents.tools import duckduckgo
agents = AutoAgents(
instructions="Search for information about AI Agents",
tools=[duckduckgo],
process="sequential",
verbose=True
)
agents.start()
+50
View File
@@ -0,0 +1,50 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
import json
from e2b_code_interpreter import Sandbox
def code_interpreter(code: str):
"""
A function to demonstrate running Python code dynamically using e2b_code_interpreter.
"""
print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
exec_result = Sandbox().run_code(code)
if exec_result.error:
print("[Code Interpreter error]", exec_result.error)
return {"error": str(exec_result.error)}
else:
results = []
for result in exec_result.results:
if hasattr(result, '__iter__'):
results.extend(list(result))
else:
results.append(str(result))
logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
return json.dumps({"results": results, "logs": logs})
code_agent = Agent(
name="code_agent",
llm="gpt-4o-mini",
backstory="Expert in writing Python scripts",
self_reflect=False
)
execution_agent = Agent(
name="execution_agent",
llm="gpt-4o-mini",
backstory="Expert in executing Python scripts",
self_reflect=False,
tools=[code_interpreter]
)
code_agent_task = Task(
description="Write a simple Python script to print 'Hello, World!'",
expected_output="A Python script that prints 'Hello, World!'",
agent=code_agent
)
execution_agent_task = Task(
description="Execute the Python script",
expected_output="The output of the Python script",
agent=execution_agent
)
agents = PraisonAIAgents(agents=[code_agent, execution_agent], tasks=[code_agent_task, execution_agent_task])
agents.start()
+204
View File
@@ -0,0 +1,204 @@
from praisonaiagents import (
Agent,
Task,
PraisonAIAgents,
error_logs,
register_display_callback,
sync_display_callbacks,
async_display_callbacks
)
from duckduckgo_search import DDGS
from rich.console import Console
import json
from datetime import datetime
import logging
# Setup logging
logging.basicConfig(
filename='ai_interactions.log',
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Callback functions for different display types
def interaction_callback(message=None, response=None, markdown=None, generation_time=None):
"""Callback for display_interaction"""
logging.info(f"""
=== INTERACTION ===
Time: {datetime.now()}
Generation Time: {generation_time}s
Message: {message}
Response: {response}
Markdown: {markdown}
""")
def error_callback(message=None):
"""Callback for display_error"""
logging.error(f"""
=== ERROR ===
Time: {datetime.now()}
Message: {message}
""")
def tool_call_callback(message=None):
"""Callback for display_tool_call"""
logging.info(f"""
=== TOOL CALL ===
Time: {datetime.now()}
Message: {message}
""")
def instruction_callback(message=None):
"""Callback for display_instruction"""
logging.info(f"""
=== INSTRUCTION ===
Time: {datetime.now()}
Message: {message}
""")
def self_reflection_callback(message=None):
"""Callback for display_self_reflection"""
logging.info(f"""
=== SELF REFLECTION ===
Time: {datetime.now()}
Message: {message}
""")
def generating_callback(content=None, elapsed_time=None):
"""Callback for display_generating"""
logging.info(f"""
=== GENERATING ===
Time: {datetime.now()}
Content: {content}
Elapsed Time: {elapsed_time}
""")
# Register all callbacks
register_display_callback('interaction', interaction_callback)
register_display_callback('error', error_callback)
register_display_callback('tool_call', tool_call_callback)
register_display_callback('instruction', instruction_callback)
register_display_callback('self_reflection', self_reflection_callback)
# register_display_callback('generating', generating_callback)
def task_callback(output):
"""Callback for task completion"""
logging.info(f"""
=== TASK COMPLETED ===
Time: {datetime.now()}
Description: {output.description}
Agent: {output.agent}
Output: {output.raw[:200]}...
""")
def internet_search_tool(query) -> list:
"""
Perform a search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: A list of search result titles and URLs.
"""
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=10):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
print(f"Error during DuckDuckGo search: {e}")
return []
def main():
# Create agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI and data science",
backstory="""You are an expert at a technology research group,
skilled in identifying trends and analyzing complex data.""",
verbose=True,
allow_delegation=False,
tools=[internet_search_tool],
llm="gpt-4o",
markdown=True,
reflect_llm="gpt-4o",
min_reflect=2,
max_reflect=4
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="""You are a content strategist known for
making complex tech topics interesting and easy to understand.""",
verbose=True,
allow_delegation=True,
llm="gpt-4o",
tools=[],
markdown=True
)
# Create tasks with callbacks
task1 = Task(
name="research_task",
description="""Analyze 2024's AI advancements.
Find major trends, new technologies, and their effects.""",
expected_output="""A detailed report on 2024 AI advancements""",
agent=researcher,
tools=[internet_search_tool],
callback=task_callback
)
task2 = Task(
name="writing_task",
description="""Create a blog post about major AI advancements using the insights you have.
Make it interesting, clear, and suited for tech enthusiasts.
It should be at least 4 paragraphs long.""",
expected_output="A blog post of at least 4 paragraphs",
agent=writer,
context=[task1],
callback=task_callback,
tools=[]
)
task3 = Task(
name="json_task",
description="""Create a json object with a title of "My Task" and content of "My content".""",
expected_output="""JSON output with title and content""",
agent=researcher,
callback=task_callback
)
task4 = Task(
name="save_output_task",
description="""Save the AI blog post to a file""",
expected_output="""File saved successfully""",
agent=writer,
context=[task2],
output_file='test.txt',
create_directory=True,
callback=task_callback
)
# Create and run agents manager
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2, task3, task4],
verbose=True,
process="sequential",
manager_llm="gpt-4o"
)
agents.start()
if __name__ == "__main__":
main()
+151
View File
@@ -0,0 +1,151 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
from typing import List, Dict, Union
from duckduckgo_search import DDGS
from langchain_community.tools import YouTubeSearchTool
from langchain_community.utilities import WikipediaAPIWrapper
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform a search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: A list of search result titles, URLs, and snippets.
"""
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=10):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
print(f"Error during DuckDuckGo search: {e}")
return []
def youtube_search_tool(query: str, inspect: bool = False, max_results: int = 2):
"""
Provide a custom wrapper around the YouTubeSearchTool.
Args:
query (str): The search query for YouTube.
inspect (bool): If True, returns tool inspection info instead of search results.
max_results (int): Maximum number of results to return (default: 2).
Returns:
Union[List[str], dict]: List of YouTube video URLs or tool inspection info.
"""
yt = YouTubeSearchTool()
if inspect:
inspection_info = {
"type": type(yt),
"attributes": [attr for attr in dir(yt) if not attr.startswith('_')],
"methods": {
"run": getattr(yt, 'run', None),
"arun": getattr(yt, 'arun', None)
},
"properties": {
"name": getattr(yt, 'name', 'youtube_search'),
"description": getattr(yt, 'description', 'Search YouTube videos'),
"return_direct": getattr(yt, 'return_direct', False)
}
}
return inspection_info
# Format query with max_results
formatted_query = f"{query}, {max_results}"
return yt.run(formatted_query)
def wikipedia_search_tool(query: str, inspect: bool = False, max_chars: int = 4000, top_k: int = 3):
"""
Provide a custom wrapper around langchain_community's WikipediaAPIWrapper.
Args:
query (str): A search query for Wikipedia.
inspect (bool): If True, returns tool inspection info instead of search results.
max_chars (int): Maximum characters to return (default: 4000).
top_k (int): Number of top results to consider (default: 3).
Returns:
Union[str, dict]: Summary from Wikipedia or tool inspection info if inspect=True.
"""
w = WikipediaAPIWrapper(
top_k_results=top_k,
doc_content_chars_max=max_chars,
lang='en'
)
if inspect:
inspection_info = {
"type": type(w),
"attributes": [attr for attr in dir(w) if not attr.startswith('_')],
"methods": {
"run": getattr(w, 'run', None),
"arun": getattr(w, 'arun', None)
},
"properties": {
"name": "wikipedia",
"description": "Search and get summaries from Wikipedia",
"top_k": w.top_k_results,
"lang": w.lang,
"max_chars": w.doc_content_chars_max
}
}
return inspection_info
try:
result = w.run(query)
return result
except Exception as e:
return f"Error searching Wikipedia: {str(e)}"
# 2. Agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organizing internet data from multiple sources.",
tools=[internet_search_tool, youtube_search_tool, wikipedia_search_tool],
self_reflect=False
)
# 3. Tasks
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
is_start=True,
next_tasks=["validate_data"]
)
validate_task = Task(
description="""Validate the collected data. Check if:
1. At least 5 results are returned.
2. Each result contains a title and a URL.
Return validation_result as 'valid' or 'invalid' only no other text.""",
expected_output="Validation result indicating if data is valid or invalid.",
agent=data_agent,
name="validate_data",
task_type="decision",
condition={
"valid": [], # End the workflow on valid data
"invalid": ["collect_data"] # Retry data collection on invalid data
},
)
# 4. Workflow
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task, validate_task],
verbose=1,
process="workflow"
)
agents.start()
+141
View File
@@ -0,0 +1,141 @@
from praisonaiagents import Agent, Task, PraisonAIAgents, error_logs
from duckduckgo_search import DDGS
def my_callback(output):
print(f"Callback Task output: {output}")
def internet_search_tool(query) -> list:
"""
Perform a search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: A list of search result titles and URLs.
"""
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=10):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
print(f"Error during DuckDuckGo search: {e}")
return []
# Create agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI and data science",
backstory="""You are an expert at a technology research group,
skilled in identifying trends and analyzing complex data.""",
verbose=True,
allow_delegation=False,
tools=[internet_search_tool],
llm="gpt-4o",
markdown=True,
reflect_llm="gpt-4o",
min_reflect=2,
max_reflect=4
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="""You are a content strategist known for
making complex tech topics interesting and easy to understand.""",
verbose=True,
allow_delegation=True,
llm="gpt-4o",
tools=[],
markdown=True
)
# Create tasks
task1 = Task(
name="research_task",
description="""Analyze 2024's AI advancements.
Find major trends, new technologies, and their effects.""",
expected_output="""A detailed report on 2024 AI advancements""",
agent=researcher,
tools=[internet_search_tool]
)
task2 = Task(
name="writing_task",
description="""Create a blog post about major AI advancements using the insights you have.
Make it interesting, clear, and suited for tech enthusiasts.
It should be at least 4 paragraphs long.
Also, call the get_weather tool to get the weather in Paris.""",
expected_output="A blog post of at least 4 paragraphs, and weather in Paris",
agent=writer,
context=[task1],
callback=my_callback,
tools=[]
)
task3 = Task(
name="json_task",
description="""Create a json object with a title of "My Task" and content of "My content".""",
expected_output="""JSON output with title and content""",
agent=researcher,
)
task4 = Task(
name="save_output_task",
description="""Save the AI blog post to a file""",
expected_output="""File saved successfully""",
agent=writer,
context=[task2],
output_file='outputs/ai_blog_post.txt',
create_directory=True
)
# Create and run agents manager
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2, task3, task4],
verbose=False,
process="sequential", # "sequential" or "hierarchical"
manager_llm="gpt-4o"
)
result = agents.start()
# Print results and error summary
print("\n=== Task Results ===")
for task_id, task_status in result['task_status'].items():
print(f"Task {task_id}: {task_status}")
if task_result := result['task_results'].get(task_id):
print(f"Output: {task_result.raw[:200]}...") # Show first 200 chars
# Print task details
print("\n=== Task Details ===")
for i in range(4):
print(agents.get_task_details(i))
# Print agent details
print("\n=== Agent Details ===")
print(agents.get_agent_details('Researcher'))
print(agents.get_agent_details('Writer'))
# Print any errors
if error_logs:
print("\n=== Error Summary ===")
for err in error_logs:
print(f"- {err}")
if "parsing self-reflection json" in err:
print(" Reason: The self-reflection JSON response was not valid JSON.")
elif "Error: Task with ID" in err:
print(" Reason: Task ID referenced does not exist.")
elif "saving task output to file" in err:
print(" Reason: Possible file permissions or invalid path.")
else:
print(" Reason not identified")
+30
View File
@@ -0,0 +1,30 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
from langchain_community.tools import YouTubeSearchTool
from langchain_community.utilities import WikipediaAPIWrapper
# Create an agent with both tools
agent = Agent(
name="SearchAgent",
role="Research Assistant",
goal="Search for information from multiple sources",
backstory="I am an AI assistant that can search YouTube and Wikipedia.",
tools=[YouTubeSearchTool, WikipediaAPIWrapper],
self_reflect=False
)
# Create tasks to demonstrate both tools
task = Task(
name="search_task",
description="Search for information about 'AI advancements' on both YouTube and Wikipedia",
expected_output="Combined information from YouTube videos and Wikipedia articles",
agent=agent
)
# Create and start the workflow
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
verbose=True
)
agents.start()
+183
View File
@@ -0,0 +1,183 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
import logging
import os
def main():
# Initialize memory config
memory_config = {
"provider": "rag",
"use_embedding": True,
"storage": {
"type": "sqlite",
"path": "./.praison/memory.db"
},
"rag_db_path": "./.praison/chroma_db"
}
# Test facts
fact1 = "The capital city of Jujuha is Hahanu and its population is 102300"
fact2 = "Three main ingredients in a classic proloder are eggs, sugar, and flour"
fact3 = "The year the first Josinga was released is 2007"
# # Check if database exists
# if os.path.exists("./memory.db"):
# logger.info("Found existing memory database")
# else:
# logger.info("Creating new memory database")
# Create task config (without memory config since it's moved to PraisonAIAgents)
task_config = {}
# Create agents with different roles
researcher = Agent(
role="Research Analyst",
goal="Research and document key information about topics",
backstory="Expert at analyzing and storing information in memory",
llm="gpt-4o-mini"
)
retriever = Agent(
role="Information Retriever",
goal="Retrieve and verify stored information from memory",
backstory="Specialist in searching and validating information from memory",
llm="gpt-4o-mini"
)
# Task 1: Process the facts
store_task = Task(
description=f"""
Process and analyze this information:
1. {fact1}
2. {fact2}
3. {fact3}
Provide a clear summary of each fact.
""",
expected_output="""
Clear statements summarizing each fact.
Example format:
1. [Summary of fact 1]
2. [Summary of fact 2]
3. [Summary of fact 3]
""",
agent=researcher
)
# Task 2: Write essay about AI
verify_task = Task(
description="""
write few points about AI
""",
expected_output="Points about AI",
agent=retriever
)
# Task 3: Query memory
query_task = Task(
description="""
Using ONLY information found in memory:
1. What is stored in memory about Hahanu?
2. What ingredients for proloder are recorded in memory?
3. What year is stored in memory for the Josinga release?
For each answer, cite the memory record you found.
""",
expected_output="Answers based solely on memory records with citations",
agent=retriever
)
# Task 4: Query both short-term and long-term memory
query_both_task = Task(
description="""
Using ONLY information found in memory:
1. What is stored in both short-term and long-term memory about Jujuha?
2. What ingredients for proloder are recorded in both short-term and long-term memory?
3. What year is stored in both short-term and long-term memory for the Josinga release?
For each answer, cite the memory record you found.
""",
expected_output="Answers based solely on memory records with citations",
agent=retriever
)
# Initialize PraisonAIAgents with memory configuration
agents = PraisonAIAgents(
agents=[researcher, retriever],
tasks=[store_task, verify_task, query_task, query_both_task],
verbose=True, # Use same verbose level as memory
memory=True,
embedder={
"provider": "openai",
"config": {
"model": "text-embedding-3-small"
}
}
)
# agents = PraisonAIAgents(
# agents=[researcher, retriever],
# tasks=[store_task, verify_task, query_task, query_both_task],
# verbose=True, # Use same verbose level as memory
# memory=True
# )
# Execute tasks
print("\nExecuting Memory Test Tasks...")
print("-" * 50)
agents.start()
# Use shared memory for final checks
memory = agents.shared_memory
# Test memory retrieval with different quality thresholds
if memory:
print("\nFinal Memory Check:")
print("-" * 50)
queries = ["Jujuha", "proloder", "Josinga"]
for query in queries:
print(f"\nSearching memory for: {query}")
# Search in both short-term and long-term memory
print("\nShort-term memory results:")
stm_results = memory.search_short_term(query)
if stm_results:
for item in stm_results:
print(f"Content: {item.get('content', '')[:200]}")
if 'meta' in item:
print(f"Metadata: {item['meta']}")
print("-" * 20)
else:
print("No results found in short-term memory")
print("\nLong-term memory results:")
ltm_results = memory.search_long_term(query)
if ltm_results:
for item in ltm_results:
print(f"Content: {item.get('text', '')[:200]}")
if 'metadata' in item:
print(f"Metadata: {item['metadata']}")
print("-" * 20)
else:
print("No results found in long-term memory")
# Also check ChromaDB if using RAG
if memory.use_rag and hasattr(memory, "chroma_col"):
print("\nChromaDB results:")
try:
all_items = memory.chroma_col.get()
print(f"Found {len(all_items['ids'])} items in ChromaDB")
for i in range(len(all_items['ids'])):
print(f"ID: {all_items['ids'][i]}")
print(f"Content: {all_items['documents'][i][:200]}")
print(f"Metadata: {all_items['metadatas'][i]}")
print("-" * 20)
except Exception as e:
print(f"Error querying ChromaDB: {e}")
print("-" * 30)
else:
print("\nNo memory available for final checks")
if __name__ == "__main__":
main()
+63
View File
@@ -0,0 +1,63 @@
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import duckduckgo
# Test facts
fact1 = "The capital city of Jujuha is Hahanu and its population is 102300"
fact2 = "Three main ingredients in a classic proloder are eggs, sugar, and flour"
fact3 = "The year the first Josinga was released is 2007"
fact_agent = Agent(
name="Fact Agent",
instructions="You are a fact agent, you store and retrieve facts in memory",
llm="gpt-4o-mini"
)
research_agent = Agent(
name="Research Agent",
instructions="You are a research analyst, you research and document key points about topics",
llm="gpt-4o-mini"
)
blog_agent = Agent(
name="Blog Agent",
instructions="You are a blog writer, you write a blog post about the research",
llm="gpt-4o-mini"
)
fact_task = Task(
description="Store the following facts in memory: " + fact1 + ", " + fact2 + ", " + fact3,
agent=fact_agent
)
research_task = Task(
description="Research and document 2 key points about AI",
agent=research_agent
)
research_task2 = Task(
description="Research and document 2 key points about AI in healthcare",
agent=research_agent
)
research_task3 = Task(
description="Research and document 2 key points about AI in education",
agent=research_agent
)
research_task4 = Task(
description="Research and document 2 key points about AI in finance",
agent=research_agent
)
blog_task = Task(
description="Write a blog post about Jujuha",
agent=blog_agent
)
agents = PraisonAIAgents(
agents=[fact_agent, research_agent, blog_agent],
tasks=[fact_task, research_task, research_task2, research_task3, research_task4, blog_task],
memory=True
)
agents.start()
+6
View File
@@ -0,0 +1,6 @@
from praisonaiagents import Agent, Agents, Tools
research_agent = Agent(instructions="You are a research agent to search internet about AI 2024", tools=[Tools.internet_search])
summarise_agent = Agent(instructions="You are a summarize agent to summarise in points")
agents = Agents(agents=[research_agent, summarise_agent])
agents.start()
+59
View File
@@ -0,0 +1,59 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create Vision Analysis Agent
vision_agent = Agent(
name="VisionAnalyst",
role="Computer Vision Specialist",
goal="Analyze images and videos to extract meaningful information",
backstory="""You are an expert in computer vision and image analysis.
You excel at describing images, detecting objects, and understanding visual content.""",
llm="gpt-4o-mini",
self_reflect=False
)
# 1. Task with Image URL
task1 = Task(
name="analyze_landmark",
description="Describe this famous landmark and its architectural features.",
expected_output="Detailed description of the landmark's architecture and significance",
agent=vision_agent,
images=["https://upload.wikimedia.org/wikipedia/commons/b/bf/Krakow_-_Kosciol_Mariacki.jpg"]
)
# 2. Task with Local Image File
task2 = Task(
name="analyze_local_image",
description="What objects can you see in this image? Describe their arrangement.",
expected_output="Detailed description of objects and their spatial relationships",
agent=vision_agent,
images=["image.jpg"]
)
# 3. Task with Video File
task3 = Task(
name="analyze_video",
description="""Watch this video and provide:
1. A summary of the main events
2. Key objects and people visible
3. Any text or important information shown
4. The overall context and setting""",
expected_output="Comprehensive analysis of the video content",
agent=vision_agent,
images=["video.mp4"]
)
# Create PraisonAIAgents instance
agents = PraisonAIAgents(
agents=[vision_agent],
tasks=[task1, task2, task3],
process="sequential",
verbose=1
)
# Run all tasks
result = agents.start()
# Print results
for task_id, task_result in result["task_results"].items():
print(f"\nTask {task_id} Result:")
print(task_result.raw)
+81
View File
@@ -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}")
@@ -0,0 +1,56 @@
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
from pydantic import BaseModel
class AnalysisReport(BaseModel):
title: str
findings: str
summary: str
# Create a researcher agent
researcher = Agent(
name="AIResearcher",
role="Technology Research Analyst",
goal="Analyze and structure information about AI developments",
backstory="Expert analyst specializing in AI technology trends",
verbose=True,
llm="gpt-4o-mini",
tools=[Tools.internet_search],
self_reflect=False
)
# Create an analyst agent
analyst = Agent(
name="DataAnalyst",
role="Data Insights Specialist",
goal="Structure and analyze research findings",
backstory="Senior data analyst with expertise in pattern recognition",
verbose=True,
llm="gpt-4o-mini",
self_reflect=False
)
# Define structured tasks
research_task = Task(
name="gather_research",
description="Research recent AI developments in 2024",
agent=researcher,
expected_output="Research findings"
)
analysis_task = Task(
name="analyze_findings",
description="Analyze research findings and create structured report. No additional text or explanation.",
agent=analyst,
output_json=AnalysisReport,
expected_output="JSON object"
)
# Initialize and run agents
agents = PraisonAIAgents(
agents=[researcher, analyst],
tasks=[research_task, analysis_task],
process="sequential",
verbose=True
)
result = agents.start()
print(result)
@@ -0,0 +1,95 @@
from praisonaiagents import Agent
from pydantic import BaseModel
from typing import List, Optional
# Define structured output models
class SearchResult(BaseModel):
title: str
url: Optional[str]
snippet: str
class AnalysisReport(BaseModel):
topic: str
key_findings: List[str]
search_results: List[SearchResult]
summary: str
confidence_score: float
def get_structured_analysis(query: str, verbose: bool = True) -> AnalysisReport:
"""
Performs a search and returns a structured analysis using an AI agent.
Args:
query (str): The search query or topic to analyze
verbose (bool): Whether to show detailed output
Returns:
AnalysisReport: A structured report containing the analysis
"""
# Create an agent with search capabilities
agent = Agent(
name="StructuredAnalyst",
role="Research Analyst",
goal="Analyze topics and provide structured reports",
backstory="Expert at gathering information and providing structured analysis",
verbose=verbose,
self_reflect=True, # Enable self-reflection for better quality
markdown=True,
llm="gpt-4o-mini"
)
# Create the analysis prompt
prompt = f"""
Analyze the following topic: "{query}"
Provide a structured analysis in JSON format that matches this schema:
{{
"topic": "string",
"key_findings": ["string"],
"search_results": [
{{
"title": "string",
"url": "string",
"snippet": "string"
}}
],
"summary": "string",
"confidence_score": float (0-1)
}}
Requirements:
1. Include at least 3 key findings
2. Include at least 2 relevant search results
3. Provide a comprehensive summary
4. Set confidence score based on quality of sources (0-1)
Return ONLY the JSON object, no other text.
"""
# Get structured response from agent
response = agent.chat(
prompt=prompt,
output_json=AnalysisReport # This ensures response matches our model
)
return AnalysisReport.model_validate_json(response)
if __name__ == "__main__":
# Example usage
analysis = get_structured_analysis("Latest developments in AI agents and autonomous systems")
print(analysis)
# Print the structured results
print("\n=== Structured Analysis Report ===")
print(f"Topic: {analysis.topic}")
print("\nKey Findings:")
for i, finding in enumerate(analysis.key_findings, 1):
print(f"{i}. {finding}")
print("\nSearch Results:")
for result in analysis.search_results:
print(f"\nTitle: {result.title}")
print(f"URL: {result.url}")
print(f"Snippet: {result.snippet}")
print(f"\nSummary: {analysis.summary}")
print(f"Confidence Score: {analysis.confidence_score:.2f}")
+30
View File
@@ -0,0 +1,30 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import get_article, get_news_sources, get_articles_from_source, get_trending_topics
# Create Wikipedia agent
news_agent = Agent(
name="NewsAgent",
role="News Analyst",
goal="Collect and analyze news articles from various sources.",
backstory="Expert in news gathering and content analysis.",
tools=[get_article, get_news_sources, get_articles_from_source, get_trending_topics],
self_reflect=False
)
# Define research task
news_task = Task(
description="Analyze news articles about 'AI developments' from major tech news sources.",
expected_output="Summary of key AI developments with source articles.",
agent=news_agent,
name="ai_news"
)
# Run agent
agents = PraisonAIAgents(
agents=[news_agent],
tasks=[news_task],
process="sequential"
)
agents.start()
@@ -0,0 +1,74 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
from typing import List, Dict
from duckduckgo_search import DDGS
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform a search using DuckDuckGo.
Args:
query (str): The search query.
Returns:
list: A list of search result titles, URLs, and snippets.
"""
try:
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=10):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
except Exception as e:
print(f"Error during DuckDuckGo search: {e}")
return []
# 2. Agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organising internet data.",
tools=[internet_search_tool],
self_reflect=False
)
# 3. Tasks
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
is_start=True,
next_tasks=["validate_data"]
)
validate_task = Task(
description="""Validate the collected data. Check if:
1. At least 5 results are returned.
2. Each result contains a title and a URL.
Return validation_result as 'valid' or 'invalid' only no other text.""",
expected_output="Validation result indicating if data is valid or invalid.",
agent=data_agent,
name="validate_data",
task_type="decision",
condition={
"valid": [], # End the workflow on valid data
"invalid": ["collect_data"] # Retry data collection on invalid data
},
)
# 4. AI Agents Workflow
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task, validate_task],
verbose=1,
process="workflow"
)
agents.start()
@@ -0,0 +1,253 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
import random
from typing import List, Dict, Union
import json
from pydantic import BaseModel
# Add Pydantic models for data validation
class Person(BaseModel):
name: str
age: int
job: str
city: str
salary: int
class ProcessedPerson(Person):
salary_category: str
age_group: str
processing_status: str
class DataList(BaseModel):
items: List[Dict]
class ValidationResult(BaseModel):
validation_result: str
details: str = ""
def random_data_of_individuals() -> List[Dict]:
"""Generate random individual data"""
names = ["John", "Jane", "Mike", "Sarah", "David", "Emma"]
jobs = ["Engineer", "Doctor", "Teacher", "Artist", "Developer"]
cities = ["New York", "London", "Tokyo", "Paris", "Berlin"]
data = []
for _ in range(random.randint(3, 7)):
person = {
"name": random.choice(names),
"age": random.randint(25, 65),
"job": random.choice(jobs),
"city": random.choice(cities),
"salary": random.randint(30000, 120000)
}
data.append(person)
return data
def process_data_of_individuals(data: Union[List[Dict], Dict, str]) -> Dict:
"""Process individual data by adding categories and analysis"""
try:
print("\n[DEBUG] process_data_of_individuals input:", data)
print("[DEBUG] Current workflow state:")
print("- total_records:", workflow.get_state("total_records"))
print("- current_index:", workflow.get_state("current_index"))
print("- remaining:", workflow.get_state("remaining"))
# Get the items list from the collect_task result
collect_result = None
for task in workflow.tasks.values():
if task.name == "collect_data" and task.result:
try:
collect_data = json.loads(task.result.raw)
collect_result = collect_data.get("items", [])
print("[DEBUG] Found collect_data items:", len(collect_result))
except:
print("[DEBUG] Failed to parse collect_data result")
# Handle string input by trying to parse it as JSON
if isinstance(data, str):
if ":" in data and not data.strip().startswith("{"):
# Convert string format to dictionary
pairs = [pair.strip() for pair in data.split(",")]
data_dict = {}
for pair in pairs:
key, value = pair.split(":")
key = key.strip().lower()
value = value.strip()
if key == "age" or key == "salary":
value = int(value)
data_dict[key] = value
data = data_dict
else:
data = json.loads(data)
print("[DEBUG] Parsed data:", data)
# Handle single record
if isinstance(data, dict):
person = data
# Initialize total records if not set
total_records = workflow.get_state("total_records")
if total_records is None and collect_result:
total_records = len(collect_result)
workflow.set_state("total_records", total_records)
print(f"[DEBUG] Initialized total_records to {total_records}")
current_index = workflow.get_state("current_index", 0)
total_records = total_records or 1
remaining = total_records - (current_index + 1)
workflow.set_state("remaining", remaining)
print(f"[DEBUG] Processing record {current_index + 1}/{total_records}")
elif isinstance(data, list):
if len(data) == 0:
raise ValueError("Empty data list")
person = data[0]
workflow.set_state("total_records", len(data))
workflow.set_state("current_index", 0)
workflow.set_state("remaining", len(data) - 1)
print(f"[DEBUG] First record from list of {len(data)} items")
else:
raise ValueError("Input must be a dictionary or list with at least one record")
processed_person = person.copy()
# Add salary category
salary = person.get("salary", 0)
if salary < 50000:
processed_person["salary_category"] = "entry"
elif salary < 90000:
processed_person["salary_category"] = "mid"
else:
processed_person["salary_category"] = "senior"
# Add age group
age = person.get("age", 0)
if age < 35:
processed_person["age_group"] = "young"
elif age < 50:
processed_person["age_group"] = "mid"
else:
processed_person["age_group"] = "senior"
# Add processing status using workflow state
remaining = workflow.get_state("remaining", 0)
current_index = workflow.get_state("current_index", 0)
total_records = workflow.get_state("total_records", 1)
# Update current index for next iteration
workflow.set_state("current_index", current_index + 1)
print(f"[DEBUG] Status check - remaining: {remaining}, current_index: {current_index}, total_records: {total_records}")
if remaining <= 0 and current_index >= total_records - 1:
print("[DEBUG] Setting status to 'all records processed'")
processed_person["processing_status"] = "all records processed"
else:
print(f"[DEBUG] More records to process. Remaining: {remaining}")
processed_person["processing_status"] = f"more records to process ({remaining} remaining)"
print("[DEBUG] Final processed person:", processed_person)
return processed_person
except Exception as e:
print(f"[DEBUG] Error processing data: {str(e)}")
return {"error": str(e), "processing_status": "error occurred"}
# Create agents
data_agent = Agent(
name="DataCollector",
role="Data collection specialist",
goal="Collect and validate data about individuals",
backstory="Expert in gathering and validating demographic data",
tools=[random_data_of_individuals],
self_reflect=False
)
process_agent = Agent(
name="DataProcessor",
role="Data processor",
goal="Process and categorize individual data",
backstory="Expert in data analysis and categorization",
tools=[process_data_of_individuals],
self_reflect=False
)
# Modify tasks to use Pydantic models
collect_task = Task(
description="Collect random individual data using the random_data_of_individuals tool. Return as a JSON object with 'items' array.",
expected_output="List of individual records with basic information",
agent=data_agent,
name="collect_data",
tools=[random_data_of_individuals],
is_start=True,
next_tasks=["validate_data"],
output_json=DataList
)
validate_task = Task(
description="""Validate the collected data. Check if:
1. All required fields are present (name, age, job, city, salary)
2. Age is between 25 and 65
3. Salary is between 30000 and 120000
Return validation_result as 'valid' or 'invalid' with optional details.""",
expected_output="Validation result indicating if data is valid or invalid",
agent=data_agent,
name="validate_data",
task_type="decision",
condition={
"valid": ["process_data"],
"invalid": ["collect_data"]
},
output_json=ValidationResult
)
process_task = Task(
description="""Process one record at a time from the input data.
Current progress will be shown in Loop Status.
For the current record:
1. Use process_data_of_individuals tool to add categories
2. Return the processed record with remaining count
Current remaining: {remaining}
Current item: {current_item}
Process this record and indicate if more records remain.""",
expected_output="Processed record with categories and status",
agent=process_agent,
name="process_data",
tools=[process_data_of_individuals],
task_type="loop",
condition={
"more records to process": ["process_data"],
"all records processed": []
},
context=[collect_task],
output_json=ProcessedPerson
)
# Create PraisonAIAgents instance with workflow process
workflow = PraisonAIAgents(
agents=[data_agent, process_agent],
tasks=[collect_task, validate_task, process_task],
verbose=1,
process="workflow"
)
# Run the workflow
result = workflow.start()
# Print results
print("\nWorkflow Results:")
print("----------------")
for task_id, task in workflow.tasks.items():
print(f"\nTask: {task.name}")
print(f"Status: {task.status}")
if task.result:
print("Output:")
try:
# Try to format as pretty JSON
import json
output = json.loads(task.result.raw)
print(json.dumps(output, indent=2))
except:
# If not JSON, print raw output
print(task.result.raw[:500] + "..." if len(task.result.raw) > 500 else task.result.raw)
+1 -1
View File
@@ -110,7 +110,7 @@ Loads environment variables from .env file or system and sets them.</p>
file.write(&#34;FROM python:3.11-slim\n&#34;)
file.write(&#34;WORKDIR /app\n&#34;)
file.write(&#34;COPY . .\n&#34;)
file.write(&#34;RUN pip install flask praisonai==2.0.36 gunicorn markdown\n&#34;)
file.write(&#34;RUN pip install flask praisonai==2.0.42 gunicorn markdown\n&#34;)
file.write(&#34;EXPOSE 8080\n&#34;)
file.write(&#39;CMD [&#34;gunicorn&#34;, &#34;-b&#34;, &#34;0.0.0.0:8080&#34;, &#34;api:app&#34;]\n&#39;)
@@ -0,0 +1,187 @@
---
title: "AutoAgents Module"
description: "Documentation for the praisonaiagents.agents.autoagents module"
icon: "robot"
---
# Module praisonaiagents.agents.autoagents
The AutoAgents module provides automatic creation and management of AI agents and tasks based on high-level instructions.
## Classes
### AutoAgents
The main class for automatically creating and managing AI agents and tasks.
#### Parameters
- `instructions: str` - High-level task description for the agents
- `tools: Optional[List[Any]] = None` - List of tools available to the agents
- `verbose: bool = False` - Enable detailed logging
- `process: str = "sequential"` - Process type (sequential or hierarchical)
- `manager_llm: Optional[str] = None` - Language model for manager agent
- `max_retries: int = 5` - Maximum retry attempts
- `completion_checker: Optional[Any] = None` - Custom completion checker
- `allow_code_execution: bool = False` - Allow code execution
- `memory: bool = True` - Enable agent memory
- `markdown: bool = True` - Enable markdown formatting
- `self_reflect: bool = False` - Enable agent self-reflection
- `max_reflect: int = 3` - Maximum reflection iterations
- `min_reflect: int = 1` - Minimum reflection iterations
- `llm: Optional[str] = None` - Language model for agents
- `function_calling_llm: Optional[str] = None` - Language model for tool calling
- `respect_context_window: bool = True` - Respect model context window
- `code_execution_mode: str = "safe"` - Code execution mode (safe/unsafe)
- `embedder_config: Optional[Dict[str, Any]] = None` - Embedder configuration
- `knowledge_sources: Optional[List[Any]] = None` - Knowledge sources
- `use_system_prompt: bool = True` - Use system prompts
- `cache: bool = True` - Enable caching
- `allow_delegation: bool = False` - Allow task delegation
- `step_callback: Optional[Any] = None` - Callback for each step
- `system_template: Optional[str] = None` - Custom system template
- `prompt_template: Optional[str] = None` - Custom prompt template
- `response_template: Optional[str] = None` - Custom response template
- `max_rpm: Optional[int] = None` - Maximum requests per minute
- `max_execution_time: Optional[int] = None` - Maximum execution time
- `max_iter: int = 20` - Maximum iterations
- `reflect_llm: Optional[str] = None` - Language model for reflection
- `max_agents: int = 3` - Maximum number of agents to create
#### Methods
##### start()
Start the agents synchronously.
```python
def start(self):
"""
Creates tasks based on the instructions, then starts execution.
Returns the task status and results dictionary.
"""
return super().start()
```
##### astart()
Start the agents asynchronously.
```python
async def astart(self):
"""
Async version of start() method.
Creates tasks based on the instructions, then starts execution.
Returns the task status and results dictionary.
"""
return await super().astart()
```
#### Internal Methods
##### _generate_config()
Generate the configuration for agents and tasks.
```python
def _generate_config(self) -> AutoAgentsConfig:
"""
Generate the configuration for agents and tasks based on instructions.
Returns AutoAgentsConfig object containing agent and task configurations.
"""
```
##### _create_agents_and_tasks()
Create agents and tasks from configuration.
```python
def _create_agents_and_tasks(self, config: AutoAgentsConfig) -> tuple[List[Agent], List[Task]]:
"""
Create agents and tasks based on the generated configuration.
Returns tuple of (agents, tasks).
"""
```
##### _assign_tools_to_agent()
Assign appropriate tools to an agent.
```python
def _assign_tools_to_agent(self, agent_config: AgentConfig) -> List[Any]:
"""
Assign tools to an agent based on its role and tasks.
Returns list of assigned tools.
"""
```
## Pydantic Models
### TaskConfig
Configuration for a task.
#### Attributes
- `name: str` - Task name
- `description: str` - Task description
- `expected_output: str` - Expected output description
- `tools: List[str]` - Required tools for the task
### AgentConfig
Configuration for an agent.
#### Attributes
- `name: str` - Agent name
- `role: str` - Agent role
- `goal: str` - Agent goal
- `backstory: str` - Agent backstory
- `tools: List[str]` - Required tools
- `tasks: List[TaskConfig]` - Tasks assigned to the agent
### AutoAgentsConfig
Overall configuration for AutoAgents.
#### Attributes
- `main_instruction: str` - Main instruction for the agents
- `process_type: str` - Process type (sequential/hierarchical)
- `agents: List[AgentConfig]` - List of agent configurations
## Example Usage
### Basic Usage
```python
from praisonaiagents import AutoAgents
from praisonaiagents.tools import SerperDevTool
agents = AutoAgents(
instructions="Research recent AI developments",
tools=[SerperDevTool()],
verbose=True
)
result = agents.start()
```
### Async Usage
```python
async def main():
agents = AutoAgents(
instructions="Research recent AI developments",
tools=[SerperDevTool()],
process="hierarchical"
)
result = await agents.astart()
import asyncio
asyncio.run(main())
```
### Advanced Configuration
```python
agents = AutoAgents(
instructions="Complex research task",
tools=[SerperDevTool()],
max_agents=5,
process="hierarchical",
manager_llm="gpt-4o",
memory=True,
allow_delegation=True,
max_execution_time=600,
self_reflect=True
)
```
<Note>
For optimal results, provide clear instructions and appropriate tools for your use case.
</Note>
-69
View File
@@ -1,69 +0,0 @@
---
title: Installation
description: Set up PraisonAI in your development environment
icon: download
---
<Note>
**Requirements**
- Python 3.10 or higher
- pip package manager
- Virtual environment (recommended)
</Note>
# Installing PraisonAI
Follow these steps to set up PraisonAI in your development environment.
<Steps>
<Step title="Create Virtual Environment (Optional)">
First, create and activate a virtual environment:
<CodeGroup>
```bash Mac/Linux
python -m venv praisonai-env
source praisonai-env/bin/activate
```
```bash Windows
python -m venv praisonai-env
.\praisonai-env\Scripts\activate
```
</CodeGroup>
</Step>
<Step title="Install PraisonAI">
Install the core package and tools:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
You can also use other LLM providers like Anthropic, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
</Steps>
## Next Steps
<CardGroup cols={2}>
<Card
title="Quick Start Guide"
icon="bolt"
href="/code/quickstart"
>
Build your first AI agent
</Card>
<Card
title="API Reference"
icon="code"
href="/api/praisonaiagents/index"
>
Explore the API documentation
</Card>
</CardGroup>
-188
View File
@@ -1,188 +0,0 @@
---
title: "Introduction"
description: "Welcome to PraisonAI - The Next Generation AI Agent Framework"
icon: "book-open"
---
<Note>
PraisonAI is a powerful framework for building and deploying AI agents that can understand, reason, and execute complex tasks autonomously.
</Note>
# What is PraisonAI?
```mermaid
graph LR
%% Define the main flow
Start([▶ Start]) --> Agent1
Agent1 --> Process[⚙ Process]
Process --> Agent2
Agent2 --> Output([✓ Output])
Process -.-> Agent1
%% Styling for main elements
style Start fill:#2D3047,stroke:#7C90A0,color:#fff
style Output fill:#2D3047,stroke:#7C90A0,color:#fff
style Process fill:#5C80BC,stroke:#7C90A0,color:#fff
%% Define subgraphs for agents and their tasks
subgraph Agent1[" "]
Task1[📋 Task]
AgentIcon1[🤖 AI Agent]
Tools1[🔧 Tools]
Task1 --- AgentIcon1
AgentIcon1 --- Tools1
style Task1 fill:#2D3047,stroke:#7C90A0,color:#fff
style AgentIcon1 fill:#5C80BC,stroke:#7C90A0,color:#fff
style Tools1 fill:#2D3047,stroke:#7C90A0,color:#fff
end
subgraph Agent2[" "]
Task2[📋 Task]
AgentIcon2[🤖 AI Agent]
Tools2[🔧 Tools]
Task2 --- AgentIcon2
AgentIcon2 --- Tools2
style Task2 fill:#2D3047,stroke:#7C90A0,color:#fff
style AgentIcon2 fill:#5C80BC,stroke:#7C90A0,color:#fff
style Tools2 fill:#2D3047,stroke:#7C90A0,color:#fff
end
%% Style for agent containers
style Agent1 fill:#1B1F3B,stroke:#7C90A0,color:#fff
style Agent2 fill:#1B1F3B,stroke:#7C90A0,color:#fff
%% Global styles
classDef default fill:#2D3047,stroke:#7C90A0,color:#fff
linkStyle default stroke:#7C90A0,stroke-width:2px
```
## Core Components
<CardGroup cols={2}>
<Card title="Agents" icon="user-robot">
Autonomous AI entities that can understand context and execute tasks
</Card>
<Card title="Tasks" icon="list-check">
Units of work that agents can execute independently or collaboratively
</Card>
<Card title="Process" icon="diagram-project">
Workflow management for coordinating agent activities
</Card>
<Card title="Tools" icon="toolbox">
Extensible capabilities that agents can leverage
</Card>
</CardGroup>
## Key Features
<Steps>
<Step title="Autonomous Agents">
Create intelligent agents that can:
- Understand natural language instructions
- Make decisions based on context
- Execute complex tasks autonomously
</Step>
<Step title="Flexible Architecture">
Build with:
- Modular components
- Extensible tools
- Customizable workflows
</Step>
<Step title="Advanced Capabilities">
Leverage:
- Multi-agent collaboration
- Memory and context management
- Tool integration
</Step>
</Steps>
## Use Cases
<CardGroup cols={2}>
<Card title="Customer Service" icon="headset">
Build intelligent support agents that can handle customer inquiries and resolve issues autonomously.
</Card>
<Card title="Data Analysis" icon="chart-line">
Create agents that can process, analyze, and derive insights from complex datasets.
</Card>
<Card title="Content Creation" icon="pen-nib">
Deploy agents that can generate, edit, and optimize content across various formats.
</Card>
<Card title="Process Automation" icon="gears">
Automate complex workflows with intelligent agents that can coordinate and execute tasks.
</Card>
</CardGroup>
## Getting Started
<Warning>
Before proceeding, ensure you have Python 3.9+ installed on your system.
</Warning>
### Quick Setup
<CodeGroup>
```bash pip
pip install praisonai
```
</CodeGroup>
### Basic Example
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
## Next Steps
<CardGroup cols={2}>
<Card
title="Installation"
icon="download"
href="/code/installation"
>
Complete installation guide and setup instructions
</Card>
<Card
title="Quick Start"
icon="play"
href="/code/quickstart"
>
Build your first AI agent in minutes
</Card>
</CardGroup>
<Tip>
Join our community on [Discord](https://www.youtube.com/redirect?event=channel_description&redir_token=QUFFLUhqbUpiZE00YlF1MjB1LVNIeTFxTnV6MWRRc18wZ3xBQ3Jtc0ttQVZ0aFJ4RXRDanlBQU1Vd3lHU3BBcml3bzhoek1pTmlkZkFkWjlNVDBpRkxZd1dTOWhPc20xbHJOb1NibmNYeTEydklJT0pZOVJFQW93MmJOQXU0WkJNVzNsd29SZ3FBNFVwMTFGNThNSHBWVUhjSQ&q=https%3A%2F%2Fdiscord.gg%2FnNZu5gGT59) to connect with other developers and get help!
</Tip>
-195
View File
@@ -1,195 +0,0 @@
---
title: Quick Start
description: Build your first AI agent with PraisonAI
icon: bolt
---
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
You can also use other LLM providers like Anthropic, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Create Agent">
Create `app.py`:
## Code Example
<CodeGroup>
```python Basic
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
```python Advanced
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
<Step title="Run the Program">
Execute your script:
```bash Terminal
python app.py
```
You should see:
- Agent initialization
- Agents progress
- Final results
- Generated report
</Step>
</Steps>
## Advanced Usage
### Creating Custom Tool
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from duckduckgo_search import DDGS
from typing import List, Dict
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
# 2. Agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organising internet data.",
tools=[internet_search_tool],
self_reflect=False
)
# 3. Tasks
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
)
# 4. Start Agents
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task],
process="sequential"
)
agents.start()
```
## Next Steps
<CardGroup cols={2}>
<Card
title="Core Concepts"
icon="book"
href="/concepts/agents"
>
Learn about agents, tasks, and processes
</Card>
<Card
title="API Reference"
icon="code"
href="/api/praisonaiagents/index"
>
Explore detailed API documentation
</Card>
</CardGroup>
+237 -26
View File
@@ -105,43 +105,254 @@ graph LR
```
</CodeGroup>
## Agent Types
<Steps>
<Step title="Basic Agent">
- Single-purpose focus
- Direct user interaction
- Limited tool set
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Specialized Agent">
- Domain expertise
- Advanced capabilities
- Custom tools
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Collaborative Agent">
- Team interaction
- Task delegation
- Shared context
<Step title="Create Agent">
Create `app.py`:
<CodeGroup>
```python Single Agent
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
```python Multiple Agents
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
python app.py
```
You should see:
- Agent initialization
- Agents progress
- Final results
- Generated report
</Step>
</Steps>
## Agent Types
<CardGroup cols={3}>
<Card title="Basic Agent" icon="user" iconType="solid">
<AccordionGroup>
<Accordion title="Overview">
Perfect for straightforward tasks and direct interactions
</Accordion>
<Accordion title="Features">
- Single-purpose focus
- Direct user interaction
- Limited tool set
- Ideal for simple tasks
</Accordion>
</AccordionGroup>
</Card>
<Card title="Specialized Agent" icon="user-gear" iconType="solid">
<AccordionGroup>
<Accordion title="Overview">
Experts in specific domains with advanced capabilities
</Accordion>
<Accordion title="Features">
- Domain expertise
- Advanced capabilities
- Custom tools
- Deep knowledge base
</Accordion>
</AccordionGroup>
</Card>
<Card title="Collaborative Agent" icon="users" iconType="solid">
<AccordionGroup>
<Accordion title="Overview">
Designed for team-based operations and complex workflows
</Accordion>
<Accordion title="Features">
- Team interaction
- Task delegation
- Shared context
- Coordinated actions
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
## Best Practices
<Warning>
Always implement proper error handling and resource management in your agent configurations.
</Warning>
<Note>
Always implement proper error handling and resource management in your agent configurations.
</Note>
### Agent Design
1. Clear role definition
2. Specific, measurable goals
3. Relevant tool selection
4. Appropriate memory configuration
<CardGroup cols={2}>
<Card title="Agent Design" icon="compass-drafting" iconType="solid">
<Steps>
<Step title="Role Definition">
Define clear, specific roles for each agent
</Step>
<Step title="Goal Setting">
Set specific, measurable goals
</Step>
<Step title="Tool Selection">
Choose relevant tools for the task
</Step>
<Step title="Memory Setup">
Configure appropriate memory settings
</Step>
</Steps>
</Card>
### Agent Interaction
1. Clear communication protocols
2. Defined delegation rules
3. Error handling mechanisms
4. Resource management
<Card title="Agent Interaction" icon="network-wired" iconType="solid">
<Steps>
<Step title="Communication">
Establish clear communication protocols
</Step>
<Step title="Delegation">
Define explicit delegation rules
</Step>
<Step title="Error Handling">
Implement robust error handling
</Step>
<Step title="Resource Management">
Set up efficient resource allocation
</Step>
</Steps>
</Card>
</CardGroup>
## Async Capabilities
<CardGroup cols={1}>
<Card title="Key Features" icon="bolt" iconType="solid">
<Tabs>
<Tab title="Async Support">
```python
async def main():
agent = Agent(name="AsyncAgent")
result = await agent.aprocess_task()
```
- Full async/await support
- Non-blocking operations
- Enhanced performance
</Tab>
<Tab title="Parallel Execution">
```python
async def parallel_tasks():
tasks = [agent1.task(), agent2.task()]
results = await asyncio.gather(*tasks)
```
- Parallel task execution
- Efficient resource usage
- Improved throughput
</Tab>
<Tab title="Integration">
```python
@agent.async_tool
async def custom_tool():
async with aiohttp.ClientSession() as session:
# Async operations
pass
```
- Async tool integration
- Callback support
- Mixed sync/async operations
</Tab>
</Tabs>
</Card>
</CardGroup>
## Advanced Features
+441
View File
@@ -0,0 +1,441 @@
---
title: "AI Agents with Memory"
sidebarTitle: "Memory"
description: "Learn how to create AI agents with memory capabilities for maintaining context and information across tasks."
icon: "brain"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install "praisonaiagents[memory]" duckduckgo_search
```
duckduckgo_search is a tool that allows agents to search the web.
It is required for the multiple agents example shown below.
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
<CodeGroup>
```python Single Agent
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
# Create blog writer agent
blog_agent = Agent(
role="Blog Writer",
goal="Write a blog post about AI",
backstory="Expert at writing blog posts",
llm="gpt-4o-mini"
)
# Create blog writing task
blog_task = Task(
description="Write a blog post about AI trends",
expected_output="Well-written blog post about AI trends",
agent=blog_agent
)
# Create and start the agents with memory enabled
agents = PraisonAIAgents(
agents=[blog_agent],
tasks=[blog_task],
memory=True
)
# Start execution
result = agents.start()
print(result)
```
```python Multiple Agents
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import duckduckgo
# Create research agent with memory
research_agent = Agent(
role="Research Analyst",
goal="Research and document key information about topics",
backstory="Expert at analyzing and storing information in memory",
llm="gpt-4o-mini",
tools=[duckduckgo]
)
# Create blog writer agent
blog_agent = Agent(
role="Blog Writer",
goal="Write a blog post about the research",
backstory="Expert at writing blog posts",
llm="gpt-4o-mini"
)
# Create research task
research_task = Task(
description="Research and document key information about AI trends",
expected_output="Detailed research findings about AI trends",
agent=research_agent
)
# Create blog writing task
blog_task = Task(
description="Write a blog post about the research findings",
expected_output="Well-written blog post based on research",
agent=blog_agent
)
# Create and start the agents with advanced memory configuration
agents = PraisonAIAgents(
agents=[research_agent, blog_agent],
tasks=[research_task, blog_task],
memory=True
)
# Start execution
result = agents.start()
print(result)
```
</CodeGroup>
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
memory: true
roles:
researcher:
backstory: Expert at analyzing and storing information in memory.
goal: Research and document key information about topics
role: Research Analyst
llm: gpt-4o-mini
tools:
- duckduckgo
tasks:
research_task:
description: Research and document key information about topics.
expected_output: Detailed research findings.
writer:
backstory: Expert at writing blog posts.
goal: Write a blog post about the research
role: Blog Writer
llm: gpt-4o-mini
tasks:
blog_task:
description: Write a blog post about the research.
expected_output: Well-written blog post based on research.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of Python
</Note>
## Understanding Memory
<Card title="What is Agent Memory?" icon="question">
Memory in AI agents enables them to:
- Maintain context across multiple tasks
- Remember previous interactions and findings
- Build upon past knowledge
- Share information between agents
- Create more coherent and contextual responses
</Card>
## Features
<CardGroup cols={2}>
<Card title="Context Retention" icon="brain">
Maintain information across multiple interactions.
</Card>
<Card title="Information Sharing" icon="share-nodes">
Share knowledge between multiple agents.
</Card>
<Card title="Long-term Storage" icon="database">
Store and retrieve information over extended periods.
</Card>
<Card title="Memory Types" icon="layer-group">
Support for different memory types (short-term, long-term).
</Card>
</CardGroup>
## Multi-Agent Memory
<Tabs>
<Tab title="Code">
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import duckduckgo
# Create first agent for research
researcher = Agent(
role="Research Analyst",
goal="Research and analyze market trends",
backstory="Expert in market research and data analysis",
tools=[duckduckgo],
verbose=True
)
# Create second agent for report writing
writer = Agent(
role="Report Writer",
goal="Create comprehensive market reports",
backstory="Expert in technical writing and report creation",
verbose=True
)
# Create research task
research_task = Task(
description="Research current market trends",
expected_output="Detailed market analysis",
agent=researcher
)
# Create writing task
report_task = Task(
description="Create a market report based on research",
expected_output="Comprehensive market report",
agent=writer
)
# Create and start the agents with memory
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[research_task, report_task],
memory=True,
process="sequential"
)
# Start execution
result = agents.start()
```
</Tab>
<Tab title="No Code">
```yaml
framework: praisonai
process: sequential
memory: true
roles:
researcher:
backstory: Expert in market research and data analysis.
goal: Research and analyze market trends
role: Research Analyst
tools:
- duckduckgo
tasks:
research_task:
description: Research current market trends.
expected_output: Detailed market analysis.
writer:
backstory: Expert in technical writing and report creation.
goal: Create comprehensive market reports
role: Report Writer
tasks:
report_task:
description: Create a market report based on research.
expected_output: Comprehensive market report.
```
</Tab>
</Tabs>
### Configuration Options
```python
# Create an agent with memory configuration
agent = Agent(
role="Research Analyst",
goal="Research and retain information",
backstory="Expert in research and analysis",
tools=[duckduckgo],
verbose=True, # Enable detailed logging
llm="gpt-4o" # Language model to use
)
# Create agents with memory options
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
memory=True, # Enable memory
memory_config={
"provider": "rag", # Use RAG for semantic search
"use_embedding": True, # Enable embeddings for better search
"short_db": ".praison/short_term.db", # Path for short-term memory
"long_db": ".praison/long_term.db", # Path for long-term memory
"rag_db_path": ".praison/chroma_db" # Path for vector database
}
)
```
## Troubleshooting
<CardGroup cols={2}>
<Card title="Memory Issues" icon="triangle-exclamation">
If memory isn't working as expected:
- Check memory configuration
- Enable verbose mode for debugging
- Verify memory provider settings
</Card>
<Card title="Context Flow" icon="code">
If context isn't being maintained:
- Review task dependencies
- Check memory configuration
- Verify agent communication
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="../features/autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="../features/mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
For optimal results, configure memory settings based on your specific use case requirements and expected interaction patterns.
</Note>
### Memory Configuration Options
The memory system in PraisonAI supports various configuration options to customize how agents store and retrieve information:
```python
memory_config = {
# Memory Provider
"provider": "rag", # Options: "rag", "mem0", "none"
"use_embedding": True, # Enable semantic search with embeddings
# Storage Paths
"short_db": ".praison/short_term.db", # Short-term memory SQLite DB
"long_db": ".praison/long_term.db", # Long-term memory SQLite DB
"rag_db_path": ".praison/chroma_db", # Vector database path
# Memory Settings
"ttl": 3600, # Time to live for memory items (in seconds)
# Optional Mem0 Config (if using mem0 provider)
"config": {
"api_key": "...", # Mem0 API key
"org_id": "...", # Organization ID
"project_id": "..." # Project ID
}
}
# Create agents with memory configuration
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
memory=True,
memory_config=memory_config
)
```
### Memory Types
PraisonAI's memory system includes several types of memory:
<CardGroup cols={2}>
<Card title="Short-term Memory" icon="bolt">
- Temporary storage for current context
- Automatically cleared between sessions
- Fast access for immediate task context
</Card>
<Card title="Long-term Memory" icon="database">
- Persistent storage for important information
- Semantic search capabilities with RAG
- Quality-based storage decisions
</Card>
<Card title="Entity Memory" icon="diagram-project">
- Store information about specific entities
- Track relationships and attributes
- Specialized search for entity information
</Card>
<Card title="User Memory" icon="user">
- Store user-specific information
- Personalized context for each user
- Persistent across sessions
</Card>
</CardGroup>
### Memory Quality Control
PraisonAI includes built-in quality control for memory storage:
```python
# Example of storing with quality metrics
agents.memory.store_long_term(
text="Important information to remember",
metadata={
"task_id": "task_123",
"agent": "research_agent"
},
completeness=0.9, # How complete is the information
relevance=0.85, # How relevant to the task
clarity=0.95, # How clear and well-structured
accuracy=0.9, # How accurate is the information
weights={ # Custom weights for quality score
"completeness": 0.3,
"relevance": 0.3,
"clarity": 0.2,
"accuracy": 0.2
}
)
# Search with quality filter
results = agents.memory.search_long_term(
query="search query",
min_quality=0.8, # Only return high-quality matches
limit=5 # Maximum number of results
)
```
+327 -172
View File
@@ -10,96 +10,209 @@ Process types in PraisonAI define how tasks are executed and how agents collabor
## Process Types Overview
Three types of processes are implemented in PraisonAI:
<CardGroup cols={3}>
<Card
title="Sequential Process"
icon="arrow-right"
href="/sequential-process"
>
Linear task execution in a predefined order
</Card>
<Card
title="Hierarchical Process"
icon="sitemap"
href="/hierarchical-process"
>
Manager-coordinated task execution with dynamic assignment
</Card>
<Card
title="Workflow Process"
icon="diagram-project"
href="/workflow-process"
>
Complex task flows with conditional execution and state management
</Card>
</CardGroup>
<Frame>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-blue-100 dark:bg-blue-900 rounded-lg">
<svg className="w-6 h-6 text-blue-600 dark:text-blue-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16m-7 6h7" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">Sequential Process</h3>
</div>
<p>Linear task execution in a predefined order</p>
</div>
## Sequential Process
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-green-100 dark:bg-green-900 rounded-lg">
<svg className="w-6 h-6 text-green-600 dark:text-green-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">Hierarchical Process</h3>
</div>
<p>Manager-coordinated task execution with dynamic assignment</p>
</div>
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-purple-100 dark:bg-purple-900 rounded-lg">
<svg className="w-6 h-6 text-purple-600 dark:text-purple-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">Workflow Process</h3>
</div>
<p>Complex task flows with conditional execution and state management</p>
</div>
</div>
</Frame>
### Sequential Process
The simplest form of task execution where tasks are performed one after another.
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
#### Characteristics
- Linear execution flow
- Predictable order
- Simple dependency management
- Direct task progression
<Card>
### Characteristics
- Linear execution flow
- Predictable order
- Simple dependency management
- Direct task progression
#### Usage
```python
agents = PraisonAIAgents(
agents=[agent1, agent2],
tasks=[task1, task2, task3],
process="sequential"
)
```
</div>
</Frame>
### Usage
```python
agents = PraisonAIAgents(
agents=[agent1, agent2],
tasks=[task1, task2, task3],
process="sequential"
)
```
</Card>
## Hierarchical Process
### Hierarchical Process
Uses a manager agent to coordinate task execution and agent assignments.
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
#### Characteristics
- Manager-driven coordination
- Dynamic task assignment
- Flexible execution order
- Intelligent resource allocation
<Card>
### Characteristics
- Manager-driven coordination
- Dynamic task assignment
- Flexible execution order
- Intelligent resource allocation
#### Configuration
```python
agents = PraisonAIAgents(
agents=[manager_agent, worker_agent1, worker_agent2],
tasks=[task1, task2],
process="hierarchical",
manager_llm="gpt-4o"
)
```
</div>
</Frame>
### Configuration
```python
agents = PraisonAIAgents(
agents=[manager_agent, worker_agent1, worker_agent2],
tasks=[task1, task2],
process="hierarchical",
manager_llm="gpt-4o"
)
```
</Card>
## Workflow Process
### Workflow Process
Advanced process type supporting complex task relationships and conditional execution.
<Card>
### Features
- Task dependencies
- Conditional branching
- Loop handling
- Context sharing
- State management
### Implementation
```python
task_workflow = {
"start_task": {
"next": ["decision_task"],
"type": "task"
},
"decision_task": {
"type": "decision",
"conditions": {
"success": ["process_task"],
"failure": ["error_task"]
}
}
}
```
</Card>
## Getting Started
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Create Agent">
Create `app.py`:
<CodeGroup>
```python Single Agent Sequential Process
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False,
process="sequential"
)
result = agents.start()
```
```python Multiple Agents Hierarchical Process
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
</Steps>
<br /><br />
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
@@ -111,110 +224,152 @@ Advanced process type supporting complex task relationships and conditional exec
</div>
<br />
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
#### Features
- Task dependencies
- Conditional branching
- Loop handling
- Context sharing
- State management
#### Implementation
```python
task_workflow = {
"start_task": {
"next": ["decision_task"],
"type": "task"
},
"decision_task": {
"type": "decision",
"conditions": {
"success": ["process_task"],
"failure": ["error_task"]
}
}
}
```
</div>
</Frame>
## Advanced Features
### State Management
- Task state tracking
- Context preservation
- Data flow control
<CardGroup cols={2}>
<Card title="State Management" icon="database" iconType="solid">
<AccordionGroup>
<Accordion title="Task State Tracking">
Monitor and manage the progress of each task in real-time
</Accordion>
<Accordion title="Context Preservation">
Maintain important information across different stages of execution
</Accordion>
<Accordion title="Data Flow Control">
Manage how data moves between tasks and agents efficiently
</Accordion>
</AccordionGroup>
</Card>
### Error Handling
- Graceful failure recovery
- Alternative path execution
- Error reporting
<Card title="Error Handling" icon="shield-check" iconType="solid">
<AccordionGroup>
<Accordion title="Graceful Recovery">
Automatically handle failures and continue execution
</Accordion>
<Accordion title="Alternative Paths">
Switch to backup plans when primary execution fails
</Accordion>
<Accordion title="Error Reporting">
Detailed error logs and diagnostic information
</Accordion>
</AccordionGroup>
</Card>
### Monitoring
- Progress tracking
- Performance metrics
- Resource utilization
<Card title="Monitoring" icon="chart-line" iconType="solid">
<AccordionGroup>
<Accordion title="Progress Tracking">
Real-time visibility into task completion status
</Accordion>
<Accordion title="Performance Metrics">
Measure execution time and resource efficiency
</Accordion>
<Accordion title="Resource Usage">
Monitor system resource utilization
</Accordion>
</AccordionGroup>
</Card>
### Integration
- External system coordination
- API synchronization
- Event handling
<Card title="Integration" icon="plug" iconType="solid">
<AccordionGroup>
<Accordion title="External Systems">
Connect with other services and platforms
</Accordion>
<Accordion title="API Sync">
Maintain data consistency across systems
</Accordion>
<Accordion title="Event Handling">
React to system and external events
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
## Async Process Execution
All process types (sequential, workflow, hierarchical) support async execution through async generators. This enables efficient handling of async tasks and parallel processing.
## Async Processing
### Async Process Methods
- `asequential`: Async version of sequential process
- `aworkflow`: Async version of workflow process
- `ahierarchical`: Async version of hierarchical process
<Note>
All process types support asynchronous execution through async generators, enabling efficient parallel processing and non-blocking operations.
</Note>
### Example Usage:
### Core Async Methods
```python
import asyncio
from praisonaiagents import Agent, Task, PraisonAIAgents
<CardGroup cols={3}>
<Card title="asequential" icon="arrow-right">
Async version of sequential process for non-blocking linear execution
</Card>
<Card title="aworkflow" icon="diagram-project">
Async workflow process for complex parallel task execution
</Card>
<Card title="ahierarchical" icon="sitemap">
Async hierarchical process for distributed task management
</Card>
</CardGroup>
async def main():
# Create agents and tasks
agents = [async_agent1, async_agent2]
tasks = [async_task1, async_task2]
### Process-Specific Features
# Initialize PraisonAIAgents with async workflow
agents = PraisonAIAgents(
agents=agents,
tasks=tasks,
process="workflow" # or "sequential" or "hierarchical"
)
<Tabs>
<Tab title="Sequential Process">
<Card>
- Tasks execute in order but don't block
- Maintains sequence while allowing async operations
- Perfect for I/O-heavy tasks
```python
async def main():
agents = PraisonAIAgents(
process="sequential",
async_mode=True
)
await agents.astart()
```
</Card>
</Tab>
<Tab title="Workflow Process">
<Card>
- Parallel execution of independent tasks
- Async branching and merging
- Dynamic task scheduling based on conditions
```python
async def main():
agents = PraisonAIAgents(
process="workflow",
async_mode=True,
parallel_tasks=True
)
await agents.astart()
```
</Card>
</Tab>
<Tab title="Hierarchical Process">
<Card>
- Async manager delegation
- Parallel subtask execution
- Dynamic worker assignment
```python
async def main():
agents = PraisonAIAgents(
process="hierarchical",
async_mode=True,
dynamic_assignment=True
)
await agents.astart()
```
</Card>
</Tab>
</Tabs>
# Start async execution
result = await agents.astart()
print(result)
### Key Benefits
# Run the async workflow
asyncio.run(main())
```
### Async Process Features:
- Parallel task execution in workflow
- Async task delegation in hierarchical process
- Non-blocking task execution
- Efficient resource utilization
- Mixed sync/async task support
### Process-Specific Async Behavior:
1. **Sequential Process**:
- Tasks are executed in order
- Async tasks don't block other tasks
- Maintains sequential order while allowing async execution
2. **Workflow Process**:
- Parallel execution of independent tasks
- Async branching and merging
- Dynamic task scheduling
3. **Hierarchical Process**:
- Async manager delegation
- Parallel subtask execution
- Dynamic task assignment
<CardGroup cols={2}>
<Card title="Performance" icon="bolt">
- Efficient resource utilization
- Reduced waiting time
- Better throughput
</Card>
<Card title="Flexibility" icon="puzzle-piece">
- Mix sync and async tasks
- Adaptable execution patterns
- Easy scaling
</Card>
</CardGroup>
+365 -179
View File
@@ -8,114 +8,6 @@ icon: "list-check"
Tasks are the fundamental units of work in PraisonAI, defining what needs to be done, how it should be done, and who should do it.
</Note>
# Understanding Tasks
<Frame>
<div className="p-6 bg-white dark:bg-gray-800 rounded-lg">
<div className="grid grid-cols-1 gap-6">
{/* Task Flow Steps */}
<div className="flex justify-between items-center">
{/* Task Definition */}
<div className="flex-1 p-4 border-2 border-blue-500 rounded-lg text-center">
<div className="flex items-center justify-center mb-2">
<div className="p-2 bg-blue-100 dark:bg-blue-900 rounded-full">
<svg className="w-6 h-6 text-blue-600 dark:text-blue-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
</svg>
</div>
</div>
<h4 className="font-semibold">Task Definition</h4>
<p className="text-sm mt-1">Description & Requirements</p>
</div>
{/* Arrow 1 */}
<div className="flex-none px-4">
<svg className="w-6 h-6 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 8l4 4m0 0l-4 4m4-4H3" />
</svg>
</div>
{/* Agent Assignment */}
<div className="flex-1 p-4 border-2 border-green-500 rounded-lg text-center">
<div className="flex items-center justify-center mb-2">
<div className="p-2 bg-green-100 dark:bg-green-900 rounded-full">
<svg className="w-6 h-6 text-green-600 dark:text-green-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
</div>
</div>
<h4 className="font-semibold">Agent Assignment</h4>
<p className="text-sm mt-1">Matching & Delegation</p>
</div>
{/* Arrow 2 */}
<div className="flex-none px-4">
<svg className="w-6 h-6 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 8l4 4m0 0l-4 4m4-4H3" />
</svg>
</div>
{/* Execution */}
<div className="flex-1 p-4 border-2 border-purple-500 rounded-lg text-center">
<div className="flex items-center justify-center mb-2">
<div className="p-2 bg-purple-100 dark:bg-purple-900 rounded-full">
<svg className="w-6 h-6 text-purple-600 dark:text-purple-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
</div>
</div>
<h4 className="font-semibold">Execution</h4>
<p className="text-sm mt-1">Processing & Tools</p>
</div>
{/* Arrow 3 */}
<div className="flex-none px-4">
<svg className="w-6 h-6 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 8l4 4m0 0l-4 4m4-4H3" />
</svg>
</div>
{/* Output */}
<div className="flex-1 p-4 border-2 border-yellow-500 rounded-lg text-center">
<div className="flex items-center justify-center mb-2">
<div className="p-2 bg-yellow-100 dark:bg-yellow-900 rounded-full">
<svg className="w-6 h-6 text-yellow-600 dark:text-yellow-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 9l3 3-3 3m5 0h3M5 20h14a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
</div>
</div>
<h4 className="font-semibold">Output</h4>
<p className="text-sm mt-1">Results & Validation</p>
</div>
</div>
{/* Task States */}
<div className="grid grid-cols-4 gap-4 mt-4">
<div className="p-3 border border-gray-200 rounded-lg bg-blue-50 dark:bg-blue-900">
<div className="flex items-center justify-center">
<span className="font-semibold">Pending</span>
</div>
</div>
<div className="p-3 border border-gray-200 rounded-lg bg-yellow-50 dark:bg-yellow-900">
<div className="flex items-center justify-center">
<span className="font-semibold">In Progress</span>
</div>
</div>
<div className="p-3 border border-gray-200 rounded-lg bg-green-50 dark:bg-green-900">
<div className="flex items-center justify-center">
<span className="font-semibold">Completed</span>
</div>
</div>
<div className="p-3 border border-gray-200 rounded-lg bg-red-50 dark:bg-red-900">
<div className="flex items-center justify-center">
<span className="font-semibold">Failed</span>
</div>
</div>
</div>
</div>
</div>
</Frame>
## Core Components
<CardGroup cols={2}>
@@ -155,6 +47,79 @@ icon: "list-check"
```
</CodeGroup>
# Understanding Tasks
<br />
```mermaid
graph LR
%% Task Flow Steps
Start([▶ Start]) --> TaskDef
TaskDef --> AgentAssign
AgentAssign --> Exec
Exec --> Output([✓ Output])
%% Define subgraphs for each step
subgraph TaskDef[" "]
TD_Icon[📋 Task Definition]
TD_Desc[Description & Requirements]
TD_Icon --- TD_Desc
style TD_Icon fill:#2D3047,stroke:#7C90A0,color:#fff
style TD_Desc fill:#5C80BC,stroke:#7C90A0,color:#fff
end
subgraph AgentAssign[" "]
AA_Icon[🤖 Agent Assignment]
AA_Desc[Matching & Delegation]
AA_Icon --- AA_Desc
style AA_Icon fill:#2D3047,stroke:#7C90A0,color:#fff
style AA_Desc fill:#5C80BC,stroke:#7C90A0,color:#fff
end
subgraph Exec[" "]
E_Icon[⚙️ Execution]
E_Desc[Processing & Tools]
E_Icon --- E_Desc
style E_Icon fill:#2D3047,stroke:#7C90A0,color:#fff
style E_Desc fill:#5C80BC,stroke:#7C90A0,color:#fff
end
%% Styling for main elements
style Start fill:#2D3047,stroke:#7C90A0,color:#fff
style Output fill:#2D3047,stroke:#7C90A0,color:#fff
%% Style for containers
style TaskDef fill:#1B1F3B,stroke:#7C90A0,color:#fff
style AgentAssign fill:#1B1F3B,stroke:#7C90A0,color:#fff
style Exec fill:#1B1F3B,stroke:#7C90A0,color:#fff
%% Global styles
classDef default fill:#2D3047,stroke:#7C90A0,color:#fff
linkStyle default stroke:#7C90A0,stroke-width:2px
```
<br /><br />
```mermaid
graph LR
%% Task States
Pending([📥 Pending]) --> InProgress([⚡ In Progress])
InProgress --> Completed([✅ Completed])
InProgress --> Failed([❌ Failed])
%% Styling
style Pending fill:#2D3047,stroke:#7C90A0,color:#fff
style InProgress fill:#5C80BC,stroke:#7C90A0,color:#fff
style Completed fill:#2D3047,stroke:#7C90A0,color:#fff
style Failed fill:#2D3047,stroke:#7C90A0,color:#fff
%% Global styles
classDef default fill:#2D3047,stroke:#7C90A0,color:#fff
linkStyle default stroke:#7C90A0,stroke-width:2px
```
## Task Types
<Steps>
@@ -223,17 +188,300 @@ task_b = Task(
</Card>
</CardGroup>
## Getting Started
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Create Agent">
Create `app.py`:
<CodeGroup>
```python Single Agent
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
```python Multiple Agents
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
</Steps>
## Best Practices
<Tip>
Design tasks to be atomic and focused. Each task should have a single, clear responsibility.
</Tip>
<Note>
Design tasks to be atomic and focused. Each task should have a single, clear responsibility.
</Note>
1. Clear task descriptions
2. Well-defined success criteria
3. Appropriate tool selection
4. Error handling consideration
5. Resource management
<CardGroup cols={2}>
<Card title="Task Design Principles" icon="compass-drafting" iconType="solid">
<AccordionGroup>
<Accordion title="Clear Task Descriptions">
Write precise, unambiguous task descriptions that clearly state the task's purpose and requirements.
```python
task = Task(
description=(
"Analyze Q4 2024 sales data "
"and generate growth metrics"
),
expected_output="Sales growth report"
)
```
</Accordion>
<Accordion title="Success Criteria">
Define measurable success criteria to ensure task completion can be properly evaluated.
```python
task = Task(
success_criteria={
"format": "markdown",
"metrics": [
"growth_rate",
"revenue_change"
],
"min_confidence": 0.95
}
)
```
</Accordion>
<Accordion title="Task Dependencies">
Specify any dependencies or prerequisites for the task.
```python
task = Task(
description="Generate final report",
dependencies=[
data_collection_task,
analysis_task
],
wait_for_dependencies=True
)
```
</Accordion>
</AccordionGroup>
</Card>
<Card title="Resource Management" icon="server" iconType="solid">
<AccordionGroup>
<Accordion title="Tool Selection">
Choose appropriate tools that match your task's requirements.
```python
task = Task(
tools=[
DataAnalyzer(),
ReportGenerator(),
MetricsCalculator()
],
tool_config={
"timeout": 30,
"retry": True
}
)
```
</Accordion>
<Accordion title="Error Handling">
Implement comprehensive error management strategies.
```python
task = Task(
error_strategy="retry",
max_retries=3,
fallback_handler=handler,
error_callbacks=[
log_error,
notify_admin
]
)
```
</Accordion>
<Accordion title="Resource Limits">
Set appropriate resource constraints for optimal performance.
```python
task = Task(
memory_limit="2GB",
timeout=300,
max_concurrent=5,
cpu_priority="high",
auto_scale=True
)
```
</Accordion>
<Accordion title="Performance Monitoring">
Configure monitoring and logging for task execution.
```python
task = Task(
monitoring={
"metrics": ["duration", "memory", "cpu"],
"log_level": "INFO",
"trace_enabled": True,
"alert_threshold": 0.85
}
)
```
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
## Async Task Execution
<CardGroup cols={1}>
<Card title="Async Task Features" icon="bolt" iconType="solid">
<Tabs>
<Tab title="Basic Configuration">
```python
async_task = Task(
description="Perform async operation",
expected_output="Async result",
agent=async_agent,
async_execution=True, # Enable async execution
callback=async_callback # Optional async callback
)
```
- Enable async execution with `async_execution=True`
- Perfect for I/O-heavy operations
- Supports parallel task execution
</Tab>
<Tab title="Async Callbacks">
```python
async def async_callback(output: TaskOutput):
await asyncio.sleep(1) # Simulate async operation
print(f"Processed output: {output.raw}")
task = Task(
description="Task with async callback",
expected_output="Result",
agent=agent,
async_execution=True,
callback=async_callback
)
```
- Support for async callbacks
- Process results asynchronously
- Handle task completion events
</Tab>
<Tab title="Mixed Execution">
```python
# Create mixed task types
sync_task = Task(
description="Synchronous operation",
async_execution=False
)
async_task = Task(
description="Asynchronous operation",
async_execution=True
)
# Run with mixed execution
agents = PraisonAIAgents(
agents=[sync_agent, async_agent],
tasks=[sync_task, async_task]
)
result = await agents.astart()
```
- Mix sync and async tasks
- Flexible execution patterns
- Optimal resource utilization
</Tab>
</Tabs>
</Card>
</CardGroup>
<Tip>
**Pro Tip**: Use async execution for tasks that involve I/O operations (like API calls or file operations) to improve overall performance. Keep CPU-intensive tasks synchronous.
</Tip>
## Next Steps
@@ -252,66 +500,4 @@ task_b = Task(
>
Explore the complete Task API documentation
</Card>
</CardGroup>
## Async Task Execution
Tasks can be executed asynchronously by setting `async_execution=True`. This is particularly useful for tasks that involve I/O operations or when you want to run multiple tasks in parallel.
### Async Task Configuration
```python
async_task = Task(
description="Perform async operation",
expected_output="Async result",
agent=async_agent,
async_execution=True, # Enable async execution
callback=async_callback # Optional async callback
)
```
### Async Callbacks
Tasks support both synchronous and asynchronous callbacks:
```python
async def async_callback(output: TaskOutput):
await asyncio.sleep(1) # Simulate async operation
print(f"Processed output: {output.raw}")
# Task with async callback
task = Task(
description="Task with async callback",
expected_output="Result",
agent=agent,
async_execution=True,
callback=async_callback
)
```
### Mixing Sync and Async Tasks
You can mix synchronous and asynchronous tasks in the same workflow:
```python
# Sync task
sync_task = Task(
description="Synchronous operation",
expected_output="Sync result",
agent=sync_agent,
async_execution=False
)
# Async task
async_task = Task(
description="Asynchronous operation",
expected_output="Async result",
agent=async_agent,
async_execution=True
)
# Create agents with mixed tasks
agents = PraisonAIAgents(
agents=[sync_agent, async_agent],
tasks=[sync_task, async_task]
)
# Run asynchronously
result = await agents.astart()
```
</CardGroup>
+501 -226
View File
@@ -8,48 +8,6 @@ icon: "screwdriver-wrench"
Tools in PraisonAI are functions that agents can use to interact with external systems, perform computations, or access data. They extend the capabilities of agents beyond just language processing.
## Tools Overview
<Frame>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-blue-100 dark:bg-blue-900 rounded-lg">
<svg className="w-6 h-6 text-blue-600 dark:text-blue-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">Search Tools</h3>
</div>
<p>Tools for searching and retrieving information from various sources</p>
</div>
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-green-100 dark:bg-green-900 rounded-lg">
<svg className="w-6 h-6 text-green-600 dark:text-green-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7v8a2 2 0 002 2h6M8 7V5a2 2 0 012-2h4.586a1 1 0 01.707.293l4.414 4.414a1 1 0 01.293.707V15a2 2 0 01-2 2h-2M8 7H6a2 2 0 00-2 2v10a2 2 0 002 2h8a2 2 0 002-2v-2" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">File Tools</h3>
</div>
<p>Tools for reading, writing, and manipulating files</p>
</div>
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<div className="flex items-center mb-4">
<div className="p-2 bg-purple-100 dark:bg-purple-900 rounded-lg">
<svg className="w-6 h-6 text-purple-600 dark:text-purple-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 6l3 1m0 0l-3 9a5.002 5.002 0 006.001 0M6 7l3 9M6 7l6-2m6 2l3-1m-3 1l-3 9a5.002 5.002 0 006.001 0M18 7l3 9m-3-9l-6-2m0-2v2m0 16V5m0 16H9m3 0h3" />
</svg>
</div>
<h3 className="ml-3 text-lg font-semibold">API Tools</h3>
</div>
<p>Tools for interacting with external APIs and services</p>
</div>
</div>
</Frame>
## Creating Custom Tool
<Steps>
<Step>
@@ -99,237 +57,554 @@ Assign the tool to an agent
</Card>
## Implementing Tools Full Code Example
<Tabs>
Tools are implemented as Python functions with type hints and docstrings. Here's a complete example:
<Tab title="Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents duckduckgo-search
```
</Step>
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from duckduckgo_search import DDGS
from typing import List, Dict
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
# 1. Tool Implementation
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search using DuckDuckGo
<Step title="Create Agent with Tool">
Create `app.py`
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from duckduckgo_search import DDGS
from typing import List, Dict
# 1. Tool Implementation
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search using DuckDuckGo
Args:
query (str): The search query string
Args:
query (str): The search query string
Returns:
List[Dict]: List of search results containing title, URL, and snippet
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
Returns:
List[Dict]: List of search results containing title, URL, and snippet
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
# 2. Assign the tool to an agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organising internet data.",
tools=[internet_search_tool],
self_reflect=False
)
# 2. Assign the tool to an agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organising internet data.",
tools=[internet_search_tool],
self_reflect=False
)
# 3. Task Definition
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
)
# 3. Task Definition
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
)
# 4. Start Agents
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task],
process="sequential"
)
# 4. Start Agents
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task],
process="sequential"
)
agents.start()
```
agents.start()
```
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package and duckduckgo_search package:
```bash Terminal
pip install praisonai duckduckgo_search
```
</Step>
<Step title="Create Custom Tool">
<Info>
To add additional tools/features you need some coding which can be generated using ChatGPT or any LLM
</Info>
Create a new file `tools.py` with the following content:
```python
from duckduckgo_search import DDGS
from typing import List, Dict
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
```
</Step>
<Step title="Create Agent">
Create a new file `agents.yaml` with the following content:
```yaml
framework: praisonai
topic: create movie script about cat in mars
roles:
scriptwriter:
backstory: Expert in dialogue and script structure, translating concepts into
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
tools:
- internet_search_tool # <-- Tool assigned to Agent here
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
including dialogue and scene details.
expected_output: Final movie script with dialogue and scene details.
```
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
## In-build Tools in PraisonAI
<CardGroup cols={2}>
<Card title="Search Tools" icon="magnifying-glass" href="/tools/search">
Tools for searching and retrieving information from various sources
</Card>
<Card title="Python Tools" icon="python" href="/tools/python_tools">
Essential Python utilities for data manipulation and scripting
</Card>
<Card title="Spider Tools" icon="spider" href="/tools/spider_tools">
Web crawling and scraping capabilities for data extraction
</Card>
<Card title="Arxiv Tools" icon="book" href="/tools/arxiv_tools">
Access and search academic papers from arXiv repository
</Card>
<Card title="Newspaper Tools" icon="newspaper" href="/tools/newspaper_tools">
Extract and parse content from news articles and websites
</Card>
<Card title="DuckDB Tools" icon="database" href="/tools/duckdb_tools">
Fast analytical SQL database operations and queries
</Card>
<Card title="DuckDuckGo Tools" icon="duck" href="/tools/duckduckgo_tools">
Web search functionality using DuckDuckGo's API
</Card>
<Card title="Calculator Tools" icon="calculator" href="/tools/calculator_tools">
Perform mathematical calculations and conversions
</Card>
<Card title="YAML Tools" icon="file-code" href="/tools/yaml_tools">
Parse and manipulate YAML format data
</Card>
<Card title="JSON Tools" icon="brackets-curly" href="/tools/json_tools">
Handle JSON data structures and operations
</Card>
<Card title="Pandas Tools" icon="table" href="/tools/pandas_tools">
Data analysis and manipulation using Pandas
</Card>
<Card title="YFinance Tools" icon="chart-line" href="/tools/yfinance_tools">
Fetch financial market data from Yahoo Finance
</Card>
<Card title="Shell Tools" icon="terminal" href="/tools/shell_tools">
Execute shell commands and system operations
</Card>
<Card title="Wikipedia Tools" icon="book-open" href="/tools/wikipedia_tools">
Access and search Wikipedia articles and data
</Card>
<Card title="XML Tools" icon="code" href="/tools/xml_tools">
Process and manipulate XML format data
</Card>
<Card title="File Tools" icon="file" href="/tools/file_tools">
File system operations and management utilities
</Card>
<Card title="Excel Tools" icon="file-excel" href="/tools/excel_tools">
Work with Excel spreadsheets and workbooks
</Card>
<Card title="CSV Tools" icon="file-csv" href="/tools/csv_tools">
Handle CSV file operations and transformations
</Card>
</CardGroup>
## Tools Overview
<CardGroup cols={3}>
<Card
title="Search Tools"
icon="magnifying-glass"
iconType="solid"
>
Tools for searching and retrieving information from various sources
</Card>
<Card
title="File Tools"
icon="file"
iconType="solid"
>
Tools for reading, writing, and manipulating files
</Card>
<Card
title="API Tools"
icon="code"
iconType="solid"
>
Tools for interacting with external APIs and services
</Card>
</CardGroup>
## Advanced Tool Features
### Tool Configuration
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
```python
def configured_tool(
query: str,
max_results: int = 5,
timeout: int = 10
) -> List[Dict]:
"""
Example of a configurable tool
```python
def configured_tool(
query: str,
max_results: int = 5,
timeout: int = 10
) -> List[Dict]:
"""
Example of a configurable tool
Args:
query (str): Search query
max_results (int): Maximum number of results
timeout (int): Request timeout in seconds
Args:
query (str): Search query
max_results (int): Maximum number of results
timeout (int): Request timeout in seconds
Returns:
List[Dict]: Search results
"""
# Tool implementation
pass
```
</div>
Returns:
List[Dict]: Search results
"""
# Tool implementation
pass
```
</Frame>
### Tool Chaining
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
```python
def chain_tools(input_data: str) -> Dict:
"""
Example of chaining multiple tools
```python
def chain_tools(input_data: str) -> Dict:
"""
Example of chaining multiple tools
Args:
input_data (str): Input data
Args:
input_data (str): Input data
Returns:
Dict: Processed results
"""
# 1. Search for data
search_results = internet_search_tool(input_data)
# 2. Process results
processed_data = process_tool(search_results)
# 3. Format output
return format_tool(processed_data)
```
</div>
Returns:
Dict: Processed results
"""
# 1. Search for data
search_results = internet_search_tool(input_data)
# 2. Process results
processed_data = process_tool(search_results)
# 3. Format output
return format_tool(processed_data)
```
</Frame>
### Tool Categories
1. **Data Collection Tools**
- Web scraping
- API integration
- Database queries
2. **Processing Tools**
- Data transformation
- Text analysis
- Image processing
3. **Output Tools**
- File generation
- Report creation
- Data visualization
<CardGroup cols={3}>
<Card title="Data Collection Tools">
- Web scraping
- API integration
- Database queries
</Card>
<Card title="Processing Tools">
- Data transformation
- Text analysis
- Image processing
</Card>
<Card title="Output Tools">
- File generation
- Report creation
- Data visualization
</Card>
</CardGroup>
## Tool Integration
### Adding Tools to Agents
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
```python
# Multiple tools
agent = Agent(
name="MultiTool Agent",
tools=[
internet_search_tool,
file_processing_tool,
api_integration_tool
]
)
```
</div>
```python
# Multiple tools
agent = Agent(
name="MultiTool Agent",
tools=[
internet_search_tool,
file_processing_tool,
api_integration_tool
]
)
```
</Frame>
### Tool Dependencies
<Frame>
<div className="p-4 bg-white dark:bg-gray-800 rounded-lg">
```python
# Tool with dependencies
def advanced_tool(data: Dict) -> Dict:
"""
Tool that depends on external libraries
```python
# Tool with dependencies
def advanced_tool(data: Dict) -> Dict:
"""
Tool that depends on external libraries
Args:
data (Dict): Input data
Args:
data (Dict): Input data
Returns:
Dict: Processed data
"""
try:
import required_library
# Tool implementation
return processed_result
except ImportError:
raise Exception("Required library not installed")
```
</div>
Returns:
Dict: Processed data
"""
try:
import required_library
# Tool implementation
return processed_result
except ImportError:
raise Exception("Required library not installed")
```
</Frame>
## Tool Guidelines
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<h3 className="text-lg font-semibold mb-3">Best Practices</h3>
1. **Type Hints**
- Use Python type hints
- Define clear input/output types
- Document complex types
2. **Documentation**
- Write clear docstrings
- Explain parameters
- Provide usage examples
<CardGroup cols={2}>
<Card title="Best Practices">
1. **Type Hints**
- Use Python type hints
- Define clear input/output types
- Document complex types
3. **Error Handling**
- Handle exceptions gracefully
- Return meaningful errors
- Validate inputs
</div>
2. **Documentation**
- Write clear docstrings
- Explain parameters
- Provide usage examples
<div className="p-4 border rounded-lg bg-white dark:bg-gray-800">
<h3 className="text-lg font-semibold mb-3">Tool Types</h3>
1. **Search Tools**
- Web search
- Database queries
- Document search
3. **Error Handling**
- Handle exceptions gracefully
- Return meaningful errors
- Validate inputs
</Card>
2. **File Tools**
- Read/write operations
- File conversion
- Data extraction
<Card title="Tool Types">
1. **Search Tools**
- Web search
- Database queries
- Document search
3. **API Tools**
- REST API calls
- GraphQL queries
- Service integration
</div>
</div>
2. **File Tools**
- Read/write operations
- File conversion
- Data extraction
3. **API Tools**
- REST API calls
- GraphQL queries
- Service integration
</Card>
</CardGroup>
## Best Practices Summary
1. **Design Principles**
- Single responsibility
- Clear interfaces
- Proper documentation
- Error handling
<Note>
Following these best practices will help you create robust, efficient, and secure tools in PraisonAI.
</Note>
2. **Performance**
- Efficient processing
- Resource management
- Caching when appropriate
- Asynchronous operations
<CardGroup cols={1}>
<Card title="Design Principles" icon="compass-drafting" iconType="solid">
<AccordionGroup>
<Accordion title="Single Responsibility">
Each tool should have one clear purpose and do it well. Avoid creating tools that try to do too many things.
```python
# Good Example
def process_image(image: np.array) -> np.array:
return processed_image
3. **Security**
- Input validation
- Rate limiting
- API key management
- Error masking
# Avoid
def process_and_save_and_upload(image):
# Too many responsibilities
pass
```
</Accordion>
<Accordion title="Clear Interfaces">
Define explicit input/output types and maintain consistent parameter naming.
```python
def search_tool(
query: str,
max_results: int = 10
) -> List[Dict[str, Any]]:
"""
Search for information with clear parameters
"""
pass
```
</Accordion>
<Accordion title="Documentation">
Always include detailed docstrings and type hints.
```python
def analyze_text(
text: str,
language: str = "en"
) -> Dict[str, float]:
"""
Analyze text sentiment and emotions.
Args:
text: Input text to analyze
language: ISO language code
Returns:
Dict with sentiment scores
"""
pass
```
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
<br />
<CardGroup cols={1}>
<Card title="Performance Optimization" icon="bolt" iconType="solid">
<AccordionGroup>
<Accordion title="Efficient Processing">
Optimize resource usage and processing time.
```python
# Use generators for large datasets
def process_large_data():
for chunk in data_generator():
yield process_chunk(chunk)
```
</Accordion>
<Accordion title="Resource Management">
Properly handle resource allocation and cleanup.
```python
async with aiohttp.ClientSession() as session:
# Resource automatically managed
await process_data(session)
```
</Accordion>
<Accordion title="Caching">
Implement caching for frequently accessed data.
```python
@cache.memoize(timeout=300)
def expensive_operation(data: str) -> Dict:
return process_expensive(data)
```
</Accordion>
<Accordion title="Async Operations">
Use async/await for I/O-bound operations.
```python
async def fetch_data(urls: List[str]):
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
return await asyncio.gather(*tasks)
```
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
<br />
<CardGroup cols={1}>
<Card title="Security Best Practices" icon="shield-check" iconType="solid">
<AccordionGroup>
<Accordion title="Input Validation">
Always validate and sanitize inputs to prevent security vulnerabilities.
```python
def process_user_input(data: str) -> str:
if not isinstance(data, str):
raise ValueError("Input must be string")
return sanitize_input(data.strip())
```
</Accordion>
<Accordion title="Rate Limiting">
Implement rate limiting for API calls to prevent abuse.
```python
@rate_limit(calls=100, period=60)
async def api_call():
return await make_request()
```
</Accordion>
<Accordion title="API Key Management">
Securely handle API keys and credentials using environment variables.
```python
# Use environment variables
api_key = os.getenv('API_KEY')
if not api_key:
raise ConfigError("API key not found")
```
</Accordion>
<Accordion title="Error Masking">
Hide sensitive information in error messages to prevent information leakage.
```python
try:
result = process_sensitive_data()
except Exception as e:
# Log detailed error internally
logger.error(f"Detailed error: {str(e)}")
# Return sanitized error to user
raise PublicError("Processing failed")
```
</Accordion>
</AccordionGroup>
</Card>
</CardGroup>
<br />
<Tip>
**Pro Tip**: Start with these practices from the beginning of your project. It's easier to maintain good practices than to retrofit them later.
</Tip>
+224
View File
@@ -0,0 +1,224 @@
---
title: "Agentic Routing"
description: "Learn how to create AI agents with dynamic routing capabilities for workflow management."
icon: "route"
---
## Quick Start
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents.agent import Agent
from praisonaiagents.task import Task
from praisonaiagents.agents import PraisonAIAgents
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 specialized agents
router = Agent(
name="Router",
role="Input Router",
goal="Evaluate input and determine routing path",
instructions="Analyze input and decide whether to proceed or exit",
tools=[get_time_check]
)
processor1 = Agent(
name="Processor 1",
role="Secondary Processor",
goal="Process valid inputs that passed initial check",
instructions="Process data that passed the routing check"
)
processor2 = Agent(
name="Processor 2",
role="Final Processor",
goal="Perform final processing on validated data",
instructions="Generate final output for processed data"
)
# Create tasks with routing logic
routing_task = Task(
name="initial_routing",
description="check the time and return according to what is returned",
expected_output="pass or fail based on what is returned",
agent=router,
is_start=True,
task_type="decision",
condition={
"pass": ["process_valid"],
"fail": ["process_invalid"]
}
)
processing_task = Task(
name="process_valid",
description="Process validated input",
expected_output="Processed data ready for final step",
agent=processor1,
)
final_task = Task(
name="process_invalid",
description="Generate final output",
expected_output="Final processed result",
agent=processor2
)
# Create and run workflow
workflow = PraisonAIAgents(
agents=[router, processor1, processor2],
tasks=[routing_task, processing_task, final_task],
process="workflow",
verbose=True
)
print("\nStarting Routing Workflow...")
print("=" * 50)
results = workflow.start()
print("\nWorkflow Results:")
print("=" * 50)
for task_id, result in results["task_results"].items():
if result:
task_name = result.description
print(f"\nTask: {task_name}")
print(f"Result: {result.raw}")
print("-" * 50)
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of Python
</Note>
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
src="https://www.youtube.com/embed/KNDVWGN3TpM"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
></iframe>
</div>
## Understanding Agentic Routing
<Card title="What is Agentic Routing?" icon="question">
Agentic routing enables:
- Dynamic decision-making in workflows
- Conditional task execution paths
- Automated process branching
- Intelligent workflow management
</Card>
## Features
<CardGroup cols={2}>
<Card title="Dynamic Routing" icon="route">
Route tasks based on real-time decisions and conditions.
</Card>
<Card title="Conditional Logic" icon="code-branch">
Implement complex branching logic in workflows.
</Card>
<Card title="Task Management" icon="tasks">
Handle task dependencies and execution order.
</Card>
<Card title="Process Control" icon="sliders">
Control workflow execution with detailed monitoring.
</Card>
</CardGroup>
## Configuration Options
```python
# Create a router agent
router = Agent(
name="Router",
role="Input Router",
goal="Evaluate input and determine routing path",
instructions="Analyze input and decide whether to proceed or exit",
tools=[get_time_check], # Custom tools for routing decisions
verbose=True # Enable detailed logging
)
# Task with routing configuration
routing_task = Task(
name="initial_routing",
description="Route based on conditions",
expected_output="Routing decision",
agent=router,
is_start=True,
task_type="decision",
condition={
"pass": ["next_task"],
"fail": ["alternate_task"]
}
)
```
## Troubleshooting
<CardGroup cols={2}>
<Card title="Routing Issues" icon="triangle-exclamation">
If routing doesn't work as expected:
- Verify condition mappings
- Check task dependencies
- Enable verbose mode for debugging
</Card>
<Card title="Workflow Flow" icon="diagram-project">
If workflow is unclear:
- Review task connections
- Verify agent configurations
- Check routing conditions
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
For optimal results, ensure your routing conditions are well-defined and your task dependencies are properly configured.
</Note>
+136 -69
View File
@@ -1,82 +1,126 @@
---
title: "Async AI Agents"
title: "Async Agents"
description: "Async AI Agents allow you to run AI tasks asynchronously, improving performance and efficiency in your applications."
icon: "clock"
---
<Note>
**Requirements**
- Python 3.10 or higher
- Basic understanding of async/await in Python
</Note>
## Quick Start
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create Project">
Create a new file `app.py` with the basic setup:
```python
import asyncio
from praisonaiagents import Agent, Task, PraisonAIAgents
<Step title="Create Project">
Create a new file `app.py` with the basic setup:
```python
import asyncio
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
agent = Agent(
name="AsyncAgent",
role="Assistant",
goal="Help with tasks",
backstory="Expert in async operations"
)
# Create an agent
agent = Agent(
name="AsyncAgent",
role="Assistant",
goal="Help with tasks",
backstory="Expert in async operations"
)
# Create a task
task = Task(
name="hello_task",
description="Say hello and introduce yourself",
agent=agent,
async_execution=True,
expected_output="Answer to the question"
)
# Create a task
task = Task(
name="hello_task",
description="Say hello and introduce yourself",
agent=agent,
async_execution=True,
expected_output="Answer to the question"
)
# Create agents manager
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
process="sequential"
)
# Create agents manager
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
process="sequential"
)
# Main async function
async def main():
result = await agents.astart()
print(result)
# Main async function
async def main():
result = await agents.astart()
print(result)
# Run the program
if __name__ == "__main__":
asyncio.run(main())
# Run the program
if __name__ == "__main__":
asyncio.run(main())
```
</Step>
```
</Step>
<Step title="Run the Program">
Run your async AI agent:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create Project">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: async operations
roles:
assistant:
name: AsyncAgent
role: Assistant
goal: Help with tasks
backstory: Expert in async operations
tasks:
hello_task:
description: Say hello and introduce yourself
expected_output: Answer to the question
async_execution: true
```
</Step>
<Step title="Run the Program">
Run your async AI agent:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Step title="Run the Program">
Run your async AI agent:
```bash
python app.py
```
</Step>
</Steps>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of async/await in Python
</Note>
<div className="relative w-full aspect-video">
<iframe
@@ -129,7 +173,9 @@ icon: "clock"
## Advanced Usage
### Async Tasks with Callbacks and Tools Example Code
<Tabs>
<Tab title="Code">
### Async Tasks with Callbacks and Tools Example Code
```python
import asyncio
@@ -357,7 +403,6 @@ async def main():
if __name__ == "__main__":
# Run the main function
asyncio.run(main())
```
### 1. Async Tasks with Callbacks
@@ -402,6 +447,28 @@ async def process_multiple_tasks():
)
return results
```
</Tab>
<Tab title="No Code">
```yaml
framework: praisonai
process: sequential
topic: async operations
roles:
async_agent:
name: AsyncSearchAgent
role: Asynchronous Search Specialist
goal: Perform fast and efficient asynchronous searches
backstory: Expert in parallel search operations
tools:
- async_search_tool
tasks:
search_task:
description: Perform async search operations
expected_output: Structured search results
async_execution: true
```
</Tab>
</Tabs>
## Best Practices
@@ -481,11 +548,11 @@ async def process_multiple_tasks():
## Next Steps
<CardGroup cols={2}>
<Card title="Examples" icon="code" href="./examples">
Check out more examples in our examples directory
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Advanced Topics" icon="book" href="./advanced">
Learn about advanced async patterns and optimizations
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
+308
View File
@@ -0,0 +1,308 @@
---
title: "AutoAgents"
description: "AutoAgents automatically creates and manages AI agents and tasks based on high-level instructions."
icon: "robot"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents duckduckgo_search
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import AutoAgents
from praisonaiagents.tools import duckduckgo
# Create AutoAgents instance
agents = AutoAgents(
instructions="Search for information about AI Agents",
tools=[duckduckgo],
process="sequential",
verbose=True,
max_agents=3 # Maximum number of agents to create
)
# Start the agents
result = agents.start()
print(result)
```
</Step>
<Step title="Start AutoAgents">
Run your AutoAgents:
```bash
python app.py
```
</Step>
</Steps>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
</Note>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Start AutoAgents">
Run your AutoAgents:
```bash
praisonai --auto "Create a movie script about a robot in Mars"
```
</Step>
</Steps>
</Tab>
</Tabs>
## Understanding AutoAgents
<Card title="What are AutoAgents?" icon="question">
AutoAgents automatically:
- Creates appropriate AI agents based on your instructions
- Assigns relevant tools to each agent
- Breaks down tasks into manageable steps
- Manages execution flow between agents
- Handles agent coordination and task delegation
</Card>
## Features
<CardGroup cols={2}>
<Card title="Automatic Agent Creation" icon="wand-magic-sparkles">
Creates specialized agents based on task requirements.
</Card>
<Card title="Smart Tool Assignment" icon="toolbox">
Automatically assigns relevant tools to each agent.
</Card>
<Card title="Task Management" icon="list-check">
Breaks down complex tasks into manageable steps.
</Card>
<Card title="Process Optimization" icon="gauge-high">
Chooses optimal execution process (sequential/hierarchical).
</Card>
</CardGroup>
<CodeGroup>
```python Basic
# Basic usage with default settings
agents = AutoAgents(
instructions="Your high-level task description",
tools=[tool1, tool2]
)
```
```python Advanced
# Advanced usage with custom settings
agents = AutoAgents(
instructions="Your task description",
tools=[tool1, tool2],
max_agents=3,
process="hierarchical",
verbose=True,
memory=True,
allow_delegation=True
)
```
</CodeGroup>
## Advanced Usage
### Configuration Options
```python
# Create AutoAgents with advanced configuration
agents = AutoAgents(
instructions="Research and summarize recent AI developments",
tools=[SerperDevTool, WikipediaTools],
max_agents=3, # Maximum number of agents to create
verbose=True, # Enable detailed logging
process="hierarchical", # Use hierarchical process
memory=True, # Enable memory for agents
allow_delegation=True, # Allow task delegation
max_rpm=60, # Maximum requests per minute
max_execution_time=300, # Maximum execution time in seconds
allow_code_execution=True, # Allow code execution
code_execution_mode="safe", # Use safe mode for code execution
self_reflect=True, # Enable agent self-reflection
markdown=True # Enable markdown formatting
)
```
### Process Types
<AccordionGroup>
<Accordion title="Sequential Process">
```python
# Tasks are executed in sequence
agents = AutoAgents(
instructions="Your task",
process="sequential"
)
```
</Accordion>
<Accordion title="Hierarchical Process">
```python
# Manager agent coordinates other agents
agents = AutoAgents(
instructions="Your task",
process="hierarchical",
manager_llm="gpt-4o" # Specify LLM for manager
)
```
</Accordion>
</AccordionGroup>
## Best Practices
<AccordionGroup>
<Accordion title="Clear Instructions">
```python
# Good instruction example
agents = AutoAgents(
instructions="""
Research the latest developments in AI for 2024:
1. Focus on breakthrough technologies
2. Include real-world applications
3. Consider future implications
"""
)
```
</Accordion>
<Accordion title="Tool Selection">
```python
# Provide relevant tools for the task
agents = AutoAgents(
instructions="Research task",
tools=[
SerperDevTool, # For web search
WikipediaTools, # For background info
CustomTool # Your custom tool
]
)
```
</Accordion>
<Accordion title="Resource Management">
```python
# Configure resource limits
agents = AutoAgents(
instructions="Your task",
max_rpm=60, # Rate limiting
max_execution_time=300, # Timeout
max_agents=3 # Agent limit
)
```
</Accordion>
</AccordionGroup>
## Troubleshooting
<CardGroup cols={2}>
<Card title="Tool Assignment Issues" icon="triangle-exclamation">
If tools aren't being assigned correctly:
- Check tool compatibility
- Verify tool names
- Enable verbose mode for debugging
</Card>
<Card title="Performance Issues" icon="gauge">
If execution is slow:
- Reduce max_agents
- Adjust max_rpm
- Consider process type
</Card>
</CardGroup>
## API Reference
### Main Parameters
<ResponseField name="instructions" type="str" required>
High-level task description for the agents
</ResponseField>
<ResponseField name="tools" type="List[Any]">
List of tools available to the agents
</ResponseField>
<ResponseField name="max_agents" type="int" default="3">
Maximum number of agents to create
</ResponseField>
<ResponseField name="process" type="str" default="sequential">
Process type: "sequential" or "hierarchical"
</ResponseField>
### Optional Parameters
<ResponseField name="verbose" type="bool" default="False">
Enable detailed logging
</ResponseField>
<ResponseField name="memory" type="bool" default="True">
Enable agent memory
</ResponseField>
<ResponseField name="allow_delegation" type="bool" default="False">
Allow agents to delegate tasks
</ResponseField>
### Methods
<ResponseField name="start()" type="method">
Start the agents synchronously
</ResponseField>
<ResponseField name="astart()" type="method">
Start the agents asynchronously
</ResponseField>
## Next Steps
<CardGroup cols={2}>
<Card title="Examples" icon="code" href="./examples">
Explore more examples in our examples directory
</Card>
<Card title="Custom Tools" icon="screwdriver-wrench" href="./tools">
Learn how to create custom tools for your agents
</Card>
</CardGroup>
<Note>
For optimal results, provide clear instructions and appropriate tools for your use case.
</Note>
File diff suppressed because it is too large. Load diff
+297 -231
View File
@@ -5,73 +5,134 @@ description: "Learn how to create AI agents that can write and execute Python co
icon: "code"
---
<Note>
**Prerequisites**
- Python 3.10 or higher
- PraisonAI Agents package installed
- e2b_code_interpreter package installed
- Basic understanding of Python
</Note>
## Quick Start
Create AI agents that can write and execute Python code in a sandboxed environment!
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the required packages:
```bash
pip install praisonaiagents e2b_code_interpreter
```
</Step>
<Steps>
<Step title="Install Packages">
First, install the required packages:
```bash
pip install praisonaiagents e2b_code_interpreter
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key and E2B API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
export E2B_API_KEY=your_e2b_api_key_here
```
</Step>
<Step title="Import Required Components">
Import the necessary components:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox
<Step title="Define Code Interpreter">
Create a code interpreter function:
```python
def code_interpreter(code: str):
print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
exec_result = Sandbox().run_code(code)
if exec_result.error:
print("[Code Interpreter error]", exec_result.error)
return {"error": str(exec_result.error)}
else:
results = []
for result in exec_result.results:
if hasattr(result, '__iter__'):
results.extend(list(result))
def code_interpreter(code: str):
print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
exec_result = Sandbox().run_code(code)
if exec_result.error:
print("[Code Interpreter error]", exec_result.error)
return {"error": str(exec_result.error)}
else:
results.append(str(result))
logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
return json.dumps({"results": results, "logs": logs})
```
</Step>
results = []
for result in exec_result.results:
if hasattr(result, '__iter__'):
results.extend(list(result))
else:
results.append(str(result))
logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
return json.dumps({"results": results, "logs": logs})
<Step title="Create Code Agents">
Set up your code writing and execution agents:
```python
code_agent = Agent(
name="code_agent",
backstory="Expert in writing Python scripts",
self_reflect=False
)
# Create code agent
code_agent = Agent(
role="Code Developer",
goal="Write and execute Python code",
backstory="Expert Python developer with strong coding skills",
tools=[code_interpreter],
verbose=True
)
execution_agent = Agent(
name="execution_agent",
backstory="Expert in executing Python scripts",
self_reflect=False,
tools=[code_interpreter]
)
```
</Step>
</Steps>
# Create a task
task = Task(
description="Write and execute a Python script to analyze data",
expected_output="Working Python script with execution results",
agent=code_agent
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[code_agent],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai e2b_code_interpreter
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: write and execute Python code
roles:
developer:
backstory: Expert Python developer with strong coding skills.
goal: Write and execute Python code safely
role: Code Developer
tools:
- code_interpreter
tasks:
coding_task:
description: Write and execute a Python script to analyze data.
expected_output: Working Python script with execution results.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- e2b_code_interpreter package installed
</Note>
## Understanding Code Agents
@@ -83,205 +144,210 @@ Create AI agents that can write and execute Python code in a sandboxed environme
- Work together in a pipeline (writer → executor)
</Card>
## Key Components
## Features
<CardGroup cols={2}>
<Card title="Code Writer Agent" icon="pencil">
Specialized in writing Python code
```python
Agent(
name="code_agent",
backstory="Expert in writing Python scripts"
)
```
<Card title="Code Writer" icon="pencil">
Writes Python code based on requirements.
</Card>
<Card title="Code Executor Agent" icon="play">
Executes code using sandbox
```python
Agent(
name="execution_agent",
tools=[code_interpreter]
)
```
<Card title="Safe Execution" icon="shield">
Executes code in a sandboxed environment.
</Card>
<Card title="Sandbox Environment" icon="box">
Safe code execution environment using e2b
<Card title="Error Handling" icon="bug">
Manages code execution errors and debugging.
</Card>
<Card title="Execution Results" icon="output">
Structured output with results and logs
<Card title="Results Processing" icon="output">
Processes and formats execution results.
</Card>
</CardGroup>
## Examples
## Multi-Agent Code Development
### Simple Hello World Example
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the required packages:
```bash
pip install praisonaiagents e2b_code_interpreter
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox
def code_interpreter(code: str):
print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
exec_result = Sandbox().run_code(code)
if exec_result.error:
print("[Code Interpreter error]", exec_result.error)
return {"error": str(exec_result.error)}
else:
results = []
for result in exec_result.results:
if hasattr(result, '__iter__'):
results.extend(list(result))
else:
results.append(str(result))
logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
return json.dumps({"results": results, "logs": logs})
# Create first agent for writing code
code_writer = Agent(
role="Code Writer",
goal="Write efficient Python code",
backstory="Expert Python developer specializing in code writing",
verbose=True
)
# Create second agent for code execution
code_executor = Agent(
role="Code Executor",
goal="Execute and validate Python code",
backstory="Expert in code execution and testing",
tools=[code_interpreter],
verbose=True
)
# Create first task
writing_task = Task(
description="Write a Python script for data analysis",
expected_output="Complete Python script",
agent=code_writer
)
# Create second task
execution_task = Task(
description="Execute and validate the Python script",
expected_output="Execution results and validation",
agent=code_executor
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[code_writer, code_executor],
tasks=[writing_task, execution_task],
process="sequential"
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai e2b_code_interpreter
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: develop and execute Python code
roles:
writer:
backstory: Expert Python developer specializing in code writing.
goal: Write efficient Python code
role: Code Writer
tasks:
writing_task:
description: Write a Python script for data analysis.
expected_output: Complete Python script.
executor:
backstory: Expert in code execution and testing.
goal: Execute and validate Python code
role: Code Executor
tools:
- code_interpreter
tasks:
execution_task:
description: Execute and validate the Python script.
expected_output: Execution results and validation.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox
# Create agents
code_agent = Agent(
name="code_agent",
backstory="Expert in writing Python scripts",
self_reflect=False
)
execution_agent = Agent(
name="execution_agent",
backstory="Expert in executing Python scripts",
self_reflect=False,
tools=[code_interpreter]
)
# Define tasks
code_task = Task(
description="Write a simple Python script to print 'Hello, World!'",
expected_output="A Python script that prints 'Hello, World!'",
agent=code_agent
)
execution_task = Task(
description="Execute the Python script",
expected_output="The output of the Python script",
agent=execution_agent
)
# Initialize and run agents
agents = PraisonAIAgents(
agents=[code_agent, execution_agent],
tasks=[code_task, execution_task]
)
result = agents.start()
```
### Advanced Code Generation
<CodeGroup>
```python Data Analysis
# Create a data analysis script
code_task = Task(
description="""
Write a Python script that:
1. Creates a sample dataset
2. Performs basic statistical analysis
3. Generates a plot
""",
agent=code_agent
)
```
```python Web Scraping
# Create a web scraping script
code_task = Task(
description="""
Write a Python script that:
1. Scrapes a website
2. Extracts specific data
3. Saves to CSV
""",
agent=code_agent
)
```
</CodeGroup>
## Best Practices
<AccordionGroup>
<Accordion title="Code Safety">
Ensure safe code execution:
```python
# Always use sandbox environment
exec_result = Sandbox().run_code(code)
# Handle errors gracefully
if exec_result.error:
return {"error": str(exec_result.error)}
```
</Accordion>
<Accordion title="Task Description">
Write clear code requirements:
```python
# Good
Task(description="""
Write a Python script that:
1. Takes user input
2. Validates the input
3. Processes the data
4. Returns formatted output
""")
# Too vague
Task(description="Write some Python code")
```
</Accordion>
</AccordionGroup>
## Common Patterns
### Code Generation and Testing
```python
# Generate code
code_task = Task(
description="Write a function to calculate fibonacci numbers",
agent=code_agent
)
# Generate test cases
test_task = Task(
description="Write test cases for the fibonacci function",
agent=code_agent
)
# Execute tests
execution_task = Task(
description="Run the test cases",
agent=execution_agent
)
```
### Interactive Code Development
```python
# Write initial code
initial_code_task = Task(
description="Write initial version of the script",
agent=code_agent
)
# Test and improve
improvement_task = Task(
description="Analyze execution results and improve the code",
agent=code_agent
# Create an agent with advanced code execution configuration
agent = Agent(
role="Code Developer",
goal="Write and execute Python code",
backstory="Expert in Python development",
tools=[code_interpreter],
verbose=True, # Enable detailed logging
llm="gpt-4o" # Language model to use
)
```
## Troubleshooting
<CardGroup cols={2}>
<Card title="Execution Errors" icon="triangle-exclamation">
Check sandbox execution results:
```python
if exec_result.error:
print("[Error]", exec_result.error)
```
<Card title="Code Errors" icon="triangle-exclamation">
If code execution fails:
- Check syntax errors
- Verify package imports
- Enable verbose mode for debugging
</Card>
<Card title="Code Generation" icon="code">
Provide clear requirements and constraints in task description
<Card title="Sandbox Issues" icon="box">
If sandbox execution fails:
- Check environment setup
- Verify permissions
- Review resource limits
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="Advanced Code Generation" icon="graduation-cap" href="./advanced-code">
Learn about complex code generation patterns
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Custom Tools" icon="wrench" href="./custom-tools">
Create custom code execution tools
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
Code execution agents provide a powerful way to generate and run Python code safely. They're perfect for automated code generation, testing, and execution in a controlled environment.
For optimal results, ensure code is properly formatted and tested in the sandbox environment before production use.
</Note>
+219
View File
@@ -0,0 +1,219 @@
---
title: "LangChain Agents"
sidebarTitle: "LangChain Agents"
description: "Learn how to use LangChain tools and utilities with PraisonAI agents."
icon: "link-simple"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the required packages:
```bash
pip install praisonaiagents langchain-community wikipedia
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
<CodeGroup>
```python Single Tool
from praisonaiagents import Agent, Task, PraisonAIAgents
from langchain_community.utilities import WikipediaAPIWrapper
# Create an agent with Wikipedia tool
agent = Agent(
name="WikiAgent",
role="Research Assistant",
goal="Search Wikipedia for accurate information",
backstory="I am an AI assistant specialized in Wikipedia research",
tools=[WikipediaAPIWrapper],
self_reflect=False
)
# Create a research task
task = Task(
name="wiki_search",
description="Research 'Artificial Intelligence' on Wikipedia",
expected_output="Comprehensive information from Wikipedia articles",
agent=agent
)
# Create and start the workflow
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
verbose=True
)
agents.start()
```
```python Multiple Tools
from praisonaiagents import Agent, Task, PraisonAIAgents
from langchain_community.tools import YouTubeSearchTool
from langchain_community.utilities import WikipediaAPIWrapper
# Create YouTube search agent
agent = Agent(
name="SearchAgent",
role="Research Assistant",
goal="Search for information from YouTube",
backstory="I am an AI assistant that can search YouTube for relevant videos.",
tools=[YouTubeSearchTool],
self_reflect=False
)
# Create Wikipedia research agent
agent2 = Agent(
name="WikiAgent",
role="Research Assistant",
goal="Search for information from Wikipedia",
backstory="I am an AI assistant that can search Wikipedia for accurate information.",
tools=[WikipediaAPIWrapper],
self_reflect=False
)
# Create YouTube search task
task = Task(
name="search_task",
description="Search for information about 'AI advancements' on YouTube",
expected_output="Relevant information from YouTube videos",
agent=agent
)
# Create Wikipedia research task
task2 = Task(
name="wiki_task",
description="Search for information about 'AI advancements' on Wikipedia",
expected_output="Comprehensive information from Wikipedia articles",
agent=agent2
)
# Create and start the workflow
agents = PraisonAIAgents(
agents=[agent, agent2],
tasks=[task, task2],
verbose=True
)
agents.start()
```
</CodeGroup>
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
(Upcoming Feature)
Install the PraisonAI package:
```bash
pip install "praisonai"
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
roles:
researcher:
name: SearchAgent
role: Research Assistant
goal: Search for information from multiple sources
backstory: I am an AI assistant that can search YouTube and Wikipedia.
tools:
- youtube_search
- wikipedia
tasks:
search_task:
name: search_task
description: Search for information about 'AI advancements' on both YouTube and Wikipedia
expected_output: Combined information from YouTube videos and Wikipedia articles
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys)
- LangChain compatible tools and utilities
</Note>
## Understanding LangChain Integration
<Card title="What is LangChain Integration?" icon="question">
LangChain integration enables agents to:
- Use LangChain's extensive tool ecosystem
- Access various data sources and APIs
- Leverage pre-built utilities and wrappers
- Combine multiple tools in a single agent
- Extend agent capabilities with community tools
</Card>
## Features
<CardGroup cols={2}>
<Card title="Tool Integration" icon="plug">
Seamlessly use LangChain tools with PraisonAI agents.
</Card>
<Card title="Multiple Sources" icon="database">
Access various data sources through LangChain utilities.
</Card>
<Card title="Community Tools" icon="users">
Leverage the extensive LangChain community ecosystem.
</Card>
<Card title="Custom Tools" icon="wrench">
Create and integrate custom LangChain tools.
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="Memory Integration" icon="brain" href="../concepts/memory">
Learn how to combine LangChain tools with agent memory
</Card>
<Card title="Custom Tools" icon="wrench" href="./tools">
Create your own custom tools for agents
</Card>
</CardGroup>
<Note>
For optimal results, ensure all required dependencies are installed and API keys are properly configured for each tool.
</Note>
+288 -220
View File
@@ -5,51 +5,128 @@ description: "Learn how to create AI agents that can perform complex mathematica
icon: "calculator"
---
<Note>
**Prerequisites**
- Python 3.10 or higher
- PraisonAI Agents package installed
- Basic understanding of Python
- sympy and pint packages (automatically installed when needed)
</Note>
## Quick Start
Create AI agents that can perform complex mathematical calculations, unit conversions, and financial computations!
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents sympy pint
```
</Step>
<Steps>
<Step title="Install Package">
First, install the required package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Import Required Components">
Import the necessary components:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import (
evaluate, solve_equation, convert_units,
calculate_statistics, calculate_financial
)
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import (
evaluate, solve_equation, convert_units,
calculate_statistics, calculate_financial
)
<Step title="Create Math Agent">
Set up your mathematical computation agent:
```python
math_agent = Agent(
name="MathAssistant",
description="Expert in mathematical calculations and analysis",
tools=[
evaluate, solve_equation, convert_units,
calculate_statistics, calculate_financial
]
)
```
</Step>
</Steps>
# Create math agent
math_agent = Agent(
role="Math Expert",
goal="Perform complex mathematical calculations",
backstory="Expert in mathematical computations and analysis",
tools=[
evaluate, solve_equation, convert_units,
calculate_statistics, calculate_financial
],
verbose=True
)
# Create a task
task = Task(
description="Calculate compound interest and statistical analysis",
expected_output="Detailed mathematical analysis",
agent=math_agent
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[math_agent],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai sympy pint
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: perform mathematical analysis
roles:
mathematician:
backstory: Expert in mathematical computations and analysis.
goal: Perform complex mathematical calculations
role: Math Expert
tools:
- evaluate
- solve_equation
- convert_units
- calculate_statistics
- calculate_financial
tasks:
math_task:
description: Calculate compound interest and perform statistical analysis.
expected_output: Detailed mathematical analysis with results.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- sympy and pint packages (automatically installed when needed)
</Note>
## Understanding Math Agents
@@ -62,180 +139,177 @@ Create AI agents that can perform complex mathematical calculations, unit conver
- Handle financial calculations
</Card>
## Key Components
## Features
<CardGroup cols={2}>
<Card title="Expression Evaluator" icon="function">
Evaluates mathematical expressions
```python
evaluate("2 + 2 * 3")
evaluate("sin(pi/2) + sqrt(16)")
```
Evaluates mathematical expressions and formulas.
</Card>
<Card title="Equation Solver" icon="equals">
Solves mathematical equations
```python
solve_equation("2*x + 3 = 7")
solve_equation("x^2 - 5*x + 6 = 0")
```
Solves mathematical equations and systems.
</Card>
<Card title="Unit Converter" icon="arrows-rotate">
Converts between different units
```python
convert_units(100, "meters", "kilometers")
```
Converts between different units of measurement.
</Card>
<Card title="Statistical Calculator" icon="chart-simple">
Calculates statistical metrics
```python
calculate_statistics([1, 2, 3, 4, 5])
```
Calculates statistical metrics and analysis.
</Card>
</CardGroup>
## Examples
## Multi-Agent AI Math Analysis
### Basic Calculations Example
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import (
evaluate, solve_equation, convert_units,
calculate_statistics, calculate_financial
)
# Create first agent for calculations
calculator = Agent(
role="Mathematical Calculator",
goal="Perform complex calculations and analysis",
backstory="Expert in mathematical computations",
tools=[evaluate, solve_equation, convert_units],
verbose=True
)
# Create second agent for statistics
statistician = Agent(
role="Statistical Analyst",
goal="Perform statistical analysis and interpretation",
backstory="Expert in statistical analysis",
tools=[calculate_statistics, calculate_financial],
verbose=True
)
# Create first task
calc_task = Task(
description="Calculate compound interest for $10000 at 5% for 3 years",
expected_output="Detailed interest calculation",
agent=calculator
)
# Create second task
stats_task = Task(
description="Analyze the monthly interest distribution",
expected_output="Statistical analysis of interest data",
agent=statistician
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[calculator, statistician],
tasks=[calc_task, stats_task],
process="sequential"
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: perform financial and statistical analysis
roles:
calculator:
backstory: Expert in mathematical computations and analysis.
goal: Perform complex calculations
role: Mathematical Calculator
tools:
- evaluate
- solve_equation
- convert_units
tasks:
calc_task:
description: Calculate compound interest for investment scenario.
expected_output: Detailed interest calculations.
statistician:
backstory: Expert in statistical analysis and interpretation.
goal: Analyze numerical data and patterns
role: Statistical Analyst
tools:
- calculate_statistics
- calculate_financial
tasks:
stats_task:
description: Analyze the monthly interest distribution.
expected_output: Statistical analysis report.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import evaluate, solve_equation
# Create agent
math_agent = Agent(
name="MathAssistant",
description="Expert in mathematical calculations",
tools=[evaluate, solve_equation]
)
# Define tasks
calculation_task = Task(
description="Calculate the value of: (3 + 4) * 2 - sqrt(16)",
expected_output="The result of the mathematical expression",
agent=math_agent
)
equation_task = Task(
description="Solve the equation: x^2 - 5x + 6 = 0",
expected_output="The solutions to the quadratic equation",
agent=math_agent
)
# Initialize and run agents
agents = PraisonAIAgents(
agents=[math_agent],
tasks=[calculation_task, equation_task]
)
result = agents.start()
```
### Advanced Mathematical Operations
<CodeGroup>
```python Statistical Analysis
# Create a statistical analysis task
stats_task = Task(
description="""
Calculate statistical metrics for the dataset:
[12, 15, 18, 22, 25, 28, 32, 35, 38, 42]
""",
agent=math_agent
)
```
```python Financial Calculations
# Create a financial calculation task
finance_task = Task(
description="""
Calculate compound interest for:
- Principal: $10,000
- Rate: 5% annually
- Time: 3 years
- Compounding: Monthly
""",
agent=math_agent
)
```
</CodeGroup>
## Best Practices
<AccordionGroup>
<Accordion title="Expression Handling">
Handle expressions safely:
```python
# Good - Using variables dictionary
evaluate(
"x^2 + y",
variables={"x": 3, "y": 4}
)
# Good - Setting precision
evaluate(
"pi + e",
precision=5
)
```
</Accordion>
<Accordion title="Task Description">
Write clear mathematical requirements:
```python
# Good
Task(description="""
Calculate the following:
1. Area of a circle with radius 5
2. Convert the result to square inches
3. Round to 2 decimal places
""")
# Too vague
Task(description="Do some math")
```
</Accordion>
</AccordionGroup>
## Common Patterns
### Multi-Step Calculations
```python
# Calculate area
area_task = Task(
description="Calculate the area of a circle with radius 5",
agent=math_agent
)
# Convert units
conversion_task = Task(
description="Convert the area from square meters to square feet",
agent=math_agent
)
# Format result
formatting_task = Task(
description="Round the result to 2 decimal places",
agent=math_agent
)
```
### Statistical Analysis Pipeline
```python
# Gather data
data_task = Task(
description="Generate a dataset of 100 random numbers",
agent=math_agent
)
# Calculate statistics
stats_task = Task(
description="Calculate mean, median, mode, and standard deviation",
agent=math_agent
)
# Visualize results
viz_task = Task(
description="Create a summary of the statistical analysis",
agent=math_agent
# Create an agent with advanced math configuration
agent = Agent(
role="Math Expert",
goal="Perform complex mathematical analysis",
backstory="Expert in mathematical computations",
tools=[
evaluate,
solve_equation,
convert_units,
calculate_statistics,
calculate_financial
],
verbose=True, # Enable detailed logging
llm="gpt-4o" # Language model to use
)
```
@@ -243,37 +317,31 @@ viz_task = Task(
<CardGroup cols={2}>
<Card title="Expression Errors" icon="triangle-exclamation">
Check expression syntax and variable definitions:
```python
try:
result = evaluate("x + y", variables={"x": 1})
except Exception as e:
print(f"Error: Missing variable 'y'")
```
If expressions are not evaluating:
- Check syntax and formatting
- Verify variable definitions
- Enable verbose mode for debugging
</Card>
<Card title="Unit Conversion" icon="ruler">
Ensure compatible unit types and valid unit names
```python
# Valid conversion
convert_units(100, "celsius", "fahrenheit")
# Invalid conversion
# convert_units(100, "meters", "celsius")
```
<Card title="Calculation Issues" icon="calculator">
If calculations are incorrect:
- Review input formats
- Check unit compatibility
- Verify formula structure
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="Advanced Mathematics" icon="graduation-cap" href="./advanced-math">
Learn about complex mathematical operations
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Custom Math Tools" icon="wrench" href="./custom-tools">
Create custom mathematical computation tools
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
Math agents provide a powerful way to perform complex mathematical operations, statistical analysis, and financial calculations. They're perfect for data analysis, scientific computing, and financial modeling tasks.
For optimal results, ensure mathematical expressions and units are properly formatted for your specific use case.
</Note>
+93 -65
View File
@@ -4,47 +4,101 @@ description: "Learn how to create simple yet powerful AI agents in just a few li
icon: "robot"
---
<Note>
**Prerequisites**
- Python 3.10 or higher
- PraisonAI Agents package installed
- Basic understanding of Python
</Note>
## Quick Start
Create multiple AI agents that can work together in just a few lines of code!
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents duckduckgo_search
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create Your Agents">
<CodeGroup>
```python Single Agent
from praisonaiagents import Agent, PraisonAIAgents
summarise_agent = Agent(instructions="Summarise Photosynthesis")
agents = PraisonAIAgents(agents=[summarise_agent])
agents.start()
```
```python Multiple Agents
# Agent with Internet Search Tool
from praisonaiagents import Agent, PraisonAIAgents
research_agent = Agent(instructions="Research about AI 2024", tools=[Tools.internet_search])
summarise_agent = Agent(instructions="Summarise research agent's findings")
agents = PraisonAIAgents(agents=[research_agent, summarise_agent])
agents.start()
```
</CodeGroup>
</Step>
<Step title="Run Your Agents">
Execute your agents by running:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents duckduckgo_search
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create Your Agents">
<CodeGroup>
```yaml Single Agent
# Create a new file `agents.yaml` with the following content:
roles:
summarise_agent
instructions: Summarise Photosynthesis
```
```yaml Multiple Agents
# Create a new file `agents.yaml` with the following content:
# Agent with Internet Search Tool
roles:
research_agent
instructions: Research about AI 2024
tools:
- internet_search
summarise_agent
instructions: Summarise research agent's findings
```
</CodeGroup>
</Step>
<Step title="Run Your Agents">
Execute your agents by running:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
</Tabs>
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Import Required Components">
Import the necessary components:
```python
from praisonaiagents import Agent, Agents
```
</Step>
<Step title="Create Your Agents">
Create specialized agents with specific roles:
```python
summarise_agent = Agent(instructions="Summarise what is Photosynthesis")
```
</Step>
<Step title="Start Your Agents">
Initialize and run your agents:
```python
agents = Agents(agents=[summarise_agent])
agents.start()
```
</Step>
</Steps>
<Note>
**Prerequisites**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
</Note>
<br />
<div className="relative w-full aspect-video">
<iframe
@@ -55,6 +109,7 @@ Create multiple AI agents that can work together in just a few lines of code!
allowFullScreen
></iframe>
</div>
<br />
## Understanding Mini AI Agents
@@ -106,34 +161,7 @@ Create multiple AI agents that can work together in just a few lines of code!
</Card>
</CardGroup>
## Examples
### Basic Research and Summary
```terminal
pip install praisonaiagents duckduckgo_search
```
```python
from praisonaiagents import Agent, Agents, Tools
# Create a research agent
research_agent = Agent(
instructions="You are a research agent to search internet about AI 2024",
tools=[Tools.internet_search]
)
# Create a summarize agent
summarise_agent = Agent(
instructions="You are a summarize agent to summarise research agent's findings"
)
# Initialize and run agents
agents = Agents(agents=[research_agent, summarise_agent])
agents.start()
```
### Custom Instructions
## Custom Instructions
<CodeGroup>
```python Basic
+314
View File
@@ -0,0 +1,314 @@
---
title: "Multimodal Agents"
description: "Guide for creating and using multimodal AI agents in PraisonAI for processing images, videos, and other media types"
icon: "images"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create Vision Analysis Agent
vision_agent = Agent(
name="VisionAnalyst",
role="Computer Vision Specialist",
goal="Analyze images and videos to extract meaningful information",
backstory="""You are an expert in computer vision and image analysis.
You excel at describing images, detecting objects, and understanding visual content.""",
llm="gpt-4o-mini",
self_reflect=False
)
# Create tasks with different media types
task = Task(
name="analyze_landmark",
description="Describe this famous landmark and its architectural features.",
expected_output="Detailed description of the landmark's architecture and significance",
agent=vision_agent,
images=["https://upload.wikimedia.org/wikipedia/commons/b/bf/Krakow_-_Kosciol_Mariacki.jpg"]
)
# Run the agents
agents = PraisonAIAgents(
agents=[vision_agent],
tasks=[task],
process="sequential",
verbose=True
)
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: analyze landmark image
roles:
vision_analyst:
name: VisionAnalyst
role: Computer Vision Specialist
goal: Analyze images and videos to extract meaningful information
backstory: |
You are an expert in computer vision and image analysis.
You excel at describing images, detecting objects, and understanding visual content.
llm: gpt-4o-mini
self_reflect: false
tasks:
analyze_landmark:
description: Describe this famous landmark and its architectural features.
expected_output: Detailed description of the landmark's architecture and significance
images:
- https://upload.wikimedia.org/wikipedia/commons/b/bf/Krakow_-_Kosciol_Mariacki.jpg
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key with vision model access
- Basic understanding of Python and media handling
</Note>
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
src="https://www.youtube.com/embed/hjAWmUT1qqY"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
></iframe>
</div>
## Understanding Multimodal Agents
<Card title="What are Multimodal Agents?" icon="question">
Multimodal agents are designed to:
- Process multiple types of data (text, images, videos)
- Understand context across different modalities
- Generate insights from diverse media sources
- Handle complex multimedia tasks
</Card>
## Features
<CardGroup cols={2}>
<Card title="Vision Processing" icon="eye">
Analyze images, detect objects, and understand visual content.
</Card>
<Card title="Video Analysis" icon="video">
Process video content for events and actions.
</Card>
<Card title="Text Extraction" icon="font">
Extract and analyze text from images and documents.
</Card>
<Card title="Cross-Modal Understanding" icon="arrows-repeat">
Integrate insights across different media types.
</Card>
</CardGroup>
## Multi-Agent Media Processing
<Tabs>
<Tab title="Code">
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create first agent for image analysis
vision_agent = Agent(
role="Image Analyst",
goal="Analyze visual content and extract key information",
backstory="Expert in visual analysis and image understanding",
llm="gpt-4o-mini",
self_reflect=False
)
# Create second agent for content writing
writer_agent = Agent(
role="Content Writer",
goal="Create engaging content based on image analysis",
backstory="Expert in creating compelling content from visual insights",
llm="gpt-4o-mini"
)
# Create tasks for different media types
document_task = Task(
description="Extract and summarize text from this document image",
expected_output="Structured text content with key information highlighted",
agent=vision_agent,
images=["document.jpg"]
)
writing_task = Task(
description="Create engaging content based on image analysis",
expected_output="Compelling article incorporating visual insights",
agent=writer_agent
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[vision_agent, writer_agent],
tasks=[document_task, writing_task],
process="sequential"
)
result = agents.start()
```
</Tab>
<Tab title="No Code">
```yaml
framework: praisonai
process: sequential
topic: document analysis and content creation
roles:
vision_analyst:
role: Image Analyst
goal: Analyze visual content and extract key information
backstory: Expert in visual analysis and image understanding
llm: gpt-4o-mini
self_reflect: false
tasks:
document_task:
description: Extract and summarize text from this document image
expected_output: Structured text content with key information highlighted
images:
- document.jpg
content_writer:
role: Content Writer
goal: Create engaging content based on image analysis
backstory: Expert in creating compelling content from visual insights
llm: gpt-4o-mini
tasks:
writing_task:
description: Create engaging content based on image analysis
expected_output: Compelling article incorporating visual insights
```
</Tab>
</Tabs>
### Configuration Options
```python
# Create an agent with multimodal configuration
agent = Agent(
role="Media Analyst",
goal="Process multiple types of media",
backstory="Expert in multimedia analysis",
llm="gpt-4o-mini", # Must support vision capabilities
verbose=True, # Enable detailed logging
self_reflect=False # Optional: disable self-reflection
)
# Task with media requirements
task = Task(
description="Analyze media content",
expected_output="Comprehensive analysis",
agent=agent,
images=[ # Support multiple media sources
"https://example.com/image1.jpg",
"path/to/local/image.jpg",
"path/to/video.mp4"
]
)
```
## Best Practices
<CardGroup cols={2}>
<Card title="Media Handling" icon="image">
- Use supported formats (JPEG, PNG)
- Keep reasonable file sizes
- Provide high-quality media
- Validate files before processing
</Card>
<Card title="Task Design" icon="list-check">
- Write clear descriptions
- Break down complex analyses
- Specify expected outputs
- Handle errors gracefully
</Card>
</CardGroup>
## Example Use Cases
<CardGroup cols={2}>
<Card title="Document Analysis" icon="file-lines">
Extract and analyze text from document images.
</Card>
<Card title="Security Monitoring" icon="camera-cctv">
Monitor security feeds for suspicious activity.
</Card>
<Card title="Medical Imaging" icon="microscope">
Analyze medical scans for abnormalities.
</Card>
<Card title="Architectural Analysis" icon="building">
Study architectural features and designs.
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
For optimal results, ensure your media files are in supported formats and sizes for processing.
</Note>
+248
View File
@@ -0,0 +1,248 @@
---
title: "Agentic Prompt Chaining"
description: "Learn how to create AI agents with sequential prompt chaining for complex workflows."
icon: "link"
---
## Quick Start
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
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}")
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of Python
</Note>
## Understanding Prompt Chaining
<Card title="What is Prompt Chaining?" icon="question">
Prompt chaining enables:
- Sequential execution of prompts
- Data flow between agents
- Conditional branching in workflows
- Step-by-step processing of complex tasks
</Card>
## Features
<CardGroup cols={2}>
<Card title="Sequential Processing" icon="arrow-right">
Execute tasks in a defined sequence with data passing between steps.
</Card>
<Card title="Decision Points" icon="code-branch">
Implement conditional logic to control workflow progression.
</Card>
<Card title="Data Flow" icon="arrows-up-down">
Pass data seamlessly between agents in the chain.
</Card>
<Card title="Process Control" icon="sliders">
Monitor and control the execution of each step in the chain.
</Card>
</CardGroup>
## Configuration Options to exit the chain
```python
# Task with chaining configuration
task = Task(
name="time_check",
description="Check time and make decision",
expected_output="Time check result",
agent=agent,
is_start=True,
task_type="decision",
next_tasks=["next_step"],
condition={
"even": ["next_step"],
"odd": ""
}
)
```
```python
task = Task(
name="time_check",
description="Check time and make decision",
expected_output="Time check result",
agent=agent,
is_start=True,
task_type="decision",
next_tasks=["next_step"],
condition={
"even": ["next_step"],
"odd": "exit"
}
)
```
```python
task = Task(
name="time_check",
description="Check time and make decision",
expected_output="Time check result",
agent=agent,
is_start=True,
task_type="decision",
next_tasks=["next_step"],
condition={
"even": ["next_step"],
"odd": ["exit"]
}
)
```
```python
task = Task(
name="time_check",
description="Check time and make decision",
expected_output="Time check result",
agent=agent,
is_start=True,
task_type="decision",
next_tasks=["next_step"],
condition={
"even": ["next_step"],
"odd": [""]
}
)
```
## Troubleshooting
<CardGroup cols={2}>
<Card title="Chain Issues" icon="triangle-exclamation">
If chain execution fails:
- Verify task connections
- Check condition logic
- Enable verbose mode for debugging
</Card>
<Card title="Data Flow" icon="diagram-project">
If data flow is incorrect:
- Review task outputs
- Check agent configurations
- Verify task dependencies
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
For optimal results, ensure your chain is properly configured with clear task dependencies and conditions for branching logic.
</Note>
+305 -209
View File
@@ -1,10 +1,121 @@
---
title: "Reasoning AI Agents"
sidebarTitle: "Reasoning"
description: "Reasoning enables AI agents to think logically, solve complex problems autonomously, and make human-like decisions through multi-step logical reasoning."
icon: "brain-circuit"
title: "Reasoning Agents"
description: "Learn how to create AI agents with advanced reasoning capabilities for complex problem-solving."
icon: "brain"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
# Create reasoning agent
reasoner = Agent(
role="Problem Solver",
goal="Solve complex problems using logical reasoning",
backstory="Expert in logical analysis and problem-solving",
tools=[Tools.internet_search],
verbose=True
)
# Create task
task = Task(
description="Analyze and solve a complex business problem",
expected_output="Detailed solution with reasoning steps",
agent=reasoner
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[reasoner],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
result = agents.start()
print(result)
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: solve complex business problem
roles:
reasoner:
backstory: Expert in logical analysis and problem-solving.
goal: Solve complex problems using logical reasoning
role: Problem Solver
tools:
- internet_search
tasks:
analysis_task:
description: Analyze and solve a complex business problem.
expected_output: Detailed solution with reasoning steps.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of Python
</Note>
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
@@ -15,225 +126,210 @@ icon: "brain-circuit"
></iframe>
</div>
## Overview
## Understanding Reasoning
Reasoning in PraisonAI agents enables:
- Autonomous problem solving
- Multi-step logical reasoning
- Human-like decision making
- Deep comprehension of tasks
- Error handling with alternative solutions
<Card title="What is Reasoning?" icon="question">
Reasoning agents are designed to:
- Break down complex problems into manageable steps
- Apply logical analysis to find solutions
- Explain their thought process and decisions
- Handle uncertainty and incomplete information
</Card>
## Usage
## Features
Reasoning is controlled through reflection cycles using `min_reflect` and `max_reflect` parameters:
<CardGroup cols={2}>
<Card title="Problem Decomposition" icon="puzzle-piece">
Break complex problems into smaller, manageable parts.
</Card>
<Card title="Logical Analysis" icon="brain">
Apply structured thinking to solve problems.
</Card>
<Card title="Error Recovery" icon="shield-check">
Handle edge cases and recover from errors.
</Card>
<Card title="Explanation" icon="comment">
Provide detailed reasoning for decisions.
</Card>
</CardGroup>
## Multi-Agent Reasoning
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
# Create first agent for analysis
analyst = Agent(
role="Business Analyst",
goal="Analyze business problems and identify key issues",
backstory="Expert in business analysis and problem identification",
tools=[Tools.internet_search],
verbose=True
)
# Create second agent for solution development
solver = Agent(
role="Solution Architect",
goal="Develop comprehensive solutions to business problems",
backstory="Expert in solution design and implementation",
verbose=True
)
# Create first task
analysis_task = Task(
description="Analyze the current market challenges",
expected_output="Detailed analysis of key issues",
agent=analyst
)
# Create second task
solution_task = Task(
description="Develop solutions for identified challenges",
expected_output="Comprehensive solution strategy",
agent=solver
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[analyst, solver],
tasks=[analysis_task, solution_task],
process="sequential"
)
# Start execution
result = agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: solve market challenges
roles:
analyst:
backstory: Expert in business analysis and problem identification.
goal: Analyze business problems and identify key issues
role: Business Analyst
tools:
- internet_search
tasks:
analysis_task:
description: Analyze the current market challenges.
expected_output: Detailed analysis of key issues.
solver:
backstory: Expert in solution design and implementation.
goal: Develop comprehensive solutions to business problems
role: Solution Architect
tasks:
solution_task:
description: Develop solutions for identified challenges.
expected_output: Comprehensive solution strategy.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
from praisonaiagents import Agent
# Create an agent with reasoning configuration
agent = Agent(
name="SystemOps",
role="System Administrator",
goal="Manage and monitor system operations",
backstory="You are an expert system administrator",
min_reflect=6, # Minimum reflection cycles
max_reflect=10 # Maximum reflection cycles
)
```
## Benefits
- **Deep Comprehension**: Instead of rule-based responses, agents understand tasks deeply
- **Error Recovery**: Agents can reason through errors and find alternative solutions
- **Step-by-Step Processing**: Complex tasks are broken down and processed systematically
- **Quality Control**: Multiple reflection cycles ensure high-quality outputs
- **Adaptive Problem Solving**: Agents can adjust their approach based on reasoning
## Example
Here's a complete example showing reasoning in action:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
import subprocess
import os
def run_terminal_command(command: str):
"""
Run a terminal command and return its output.
"""
try:
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(f"Command output: {result}")
return {"stdout": result.stdout, "stderr": result.stderr}
except subprocess.CalledProcessError as e:
return {"error": str(e), "stdout": e.stdout, "stderr": e.stderr}
def save_to_file(file_path: str, content: str):
"""
Save the given content to the specified file path. Create the folder/file if it doesn't exist.
"""
# Ensure the directory exists
os.makedirs(os.path.dirname(file_path), exist_ok=True)
# Write the content to the file
with open(file_path, 'w') as file:
file.write(content)
return file_path
# Create System Operations Agent
system_ops_agent = Agent(
name="SystemOps",
role="System Operations Specialist",
goal="Execute and manage complex system operations and commands",
backstory="""You are an expert system administrator with deep knowledge of Unix/Linux systems.
You excel at running complex system commands, managing processes, and handling system operations.
You always validate commands before execution and ensure they are safe to run.""",
min_reflect=6,
max_reflect=10,
tools=[run_terminal_command, save_to_file],
llm="gpt-4o-mini"
role="Problem Solver",
goal="Solve complex problems",
backstory="Expert in problem-solving",
tools=[Tools.internet_search],
verbose=True, # Enable detailed logging
llm="gpt-4o" # Language model to use
)
# Create a complex task that tests various system operations
# Task with reasoning requirements
task = Task(
name="system_analysis_task",
description="""Perform a comprehensive system analysis by executing the following operations in sequence:
1. Get system information (OS, kernel version)
2. List 5 running processes and sort them by CPU usage
3. Check disk space usage and list directories over 1GB
4. Display current system load and memory usage
5. List 5 network connections
6. Create a summary report with all findings in a text file called system_report.txt
Do it step by step. One task at a time.
Save only the Summary report in the file.
Use appropriate commands for each step and ensure proper error handling.""",
expected_output="A comprehensive system report containing all requested information saved in system_report.txt",
agent=system_ops_agent,
description="Solve complex problem",
expected_output="Detailed solution",
agent=agent
)
agents = PraisonAIAgents(
agents=[system_ops_agent],
tasks=[task],
process="sequential"
)
result = agents.start()
```
## e2b Tool Integration Example
## Troubleshooting
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, error_logs
import json
from e2b_code_interpreter import Sandbox
<CardGroup cols={2}>
<Card title="Reasoning Errors" icon="triangle-exclamation">
If reasoning seems incorrect:
- Check problem description clarity
- Enable verbose mode for debugging
- Review agent configuration
</Card>
def code_interpret(code: str):
"""
A function to demonstrate running Python code dynamically using e2b_code_interpreter.
"""
print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
exec_result = Sandbox().run_code(code)
if exec_result.error:
print("[Code Interpreter error]", exec_result.error)
return {"error": str(exec_result.error)}
else:
results = []
for result in exec_result.results:
if hasattr(result, '__iter__'):
results.extend(list(result))
else:
results.append(str(result))
logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
return json.dumps({"results": results, "logs": logs})
<Card title="Process Flow" icon="code">
If process flow is unclear:
- Verify task dependencies
- Check agent roles and goals
- Review task descriptions
</Card>
</CardGroup>
# 1) Create Agents
web_scraper_agent = Agent(
name="WebScraper",
role="Web Scraper",
goal="Extract URLs from https://quotes.toscrape.com/",
backstory="An expert in data extraction from websites, adept at navigating and retrieving detailed information.",
tools=[code_interpret],
llm="gpt-4o"
)
## Next Steps
url_data_extractor_agent = Agent(
name="URLDataExtractor",
role="URL Data Extractor",
goal="Crawl each URL for data extraction",
backstory="Specializes in crawling websites to gather comprehensive data, ensuring nothing is missed from each link.",
tools=[code_interpret],
llm="gpt-4o"
)
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
blog_writer_agent = Agent(
name="BlogWriter",
role="Creative Content Writer",
goal="Create engaging and insightful blog posts from provided data",
backstory="An experienced content creator and storyteller with a knack for weaving compelling narratives. Skilled at analyzing quotes and creating meaningful connections that resonate with readers.",
tools=[code_interpret],
llm="gpt-4o"
)
# 2) Create Tasks
task_url_extraction = Task(
name="url_extraction_task",
description="""Use code_interpret to run Python code that fetches https://quotes.toscrape.com/
and extracts 5 URLs from the page, then outputs them as a list. Only first 5 urls""",
expected_output="A list of URLs extracted from the source page.",
agent=web_scraper_agent,
output_file="urls.txt"
)
task_data_extraction = Task(
name="data_extraction_task",
description="""Take the URLs from url_extraction_task, crawl each URL and extract all pertinent
data using code_interpret to run Python code, then output raw txt, not the html.""",
expected_output="Raw data collected from each crawled URL.",
agent=url_data_extractor_agent,
context=[task_url_extraction]
)
blog_writing_task = Task(
name="blog_writing_task",
description="""Write an engaging blog post using the extracted quotes data. The blog post should:
1. Have a catchy title
2. Include relevant quotes from the data
3. Provide commentary and insights about the quotes
4. Be well-structured with introduction, body, and conclusion
5. Be approximately 500 words long""",
expected_output="A well-written blog post incorporating the extracted quotes with analysis",
agent=blog_writer_agent,
context=[task_data_extraction],
output_file="blog_post.txt"
)
# 3) Create and run Agents manager
agents_manager = PraisonAIAgents(
agents=[web_scraper_agent, url_data_extractor_agent, blog_writer_agent],
tasks=[task_url_extraction, task_data_extraction, blog_writing_task],
verbose=True,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents_manager.start()
```
## Integration with Tools
Reasoning works particularly well with tools that require complex decision-making:
- **Code Interpreter**: For dynamic code generation and execution
- **System Operations**: For multi-step system management tasks
- **Data Analysis**: For complex data processing workflows
- **Web Scraping**: For intelligent data extraction and processing
## Performance Considerations
- Higher reflection cycles mean more thorough reasoning but longer processing time
- `min_reflect` ensures minimum quality standards
- `max_reflect` prevents excessive processing
- Balance based on task complexity and quality requirements
<Note>
For optimal results, ensure your problem descriptions are clear and provide sufficient context for the reasoning agents.
</Note>
+309 -73
View File
@@ -1,10 +1,125 @@
---
title: "Self Reflection AI Agents"
sidebarTitle: "Self-Reflection"
description: "Self-reflection is a powerful feature in PraisonAI that enables agents to evaluate and improve their own responses before delivering them. This leads to higher quality outputs and more reliable task completion."
sidebarTitle: "Self Reflection Agents"
description: "Self-reflection enables agents to evaluate and improve their own responses before delivering them."
icon: "brain"
---
## Quick Start
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent with self-reflection
agent = Agent(
role="Senior Research Analyst",
goal="Analyze and provide insights on given topics",
backstory="You are an expert analyst with strong critical thinking skills",
self_reflect=True # Enable self-reflection
)
# Create a task
task = Task(
description="Analyze recent developments in AI",
expected_output="A detailed analysis report",
agent=agent
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: create movie script about cat in mars
roles:
scriptwriter:
backstory: Expert in dialogue and script structure, translating concepts into
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
self_reflect: true
min_reflect: 1
max_reflect: 2
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
including dialogue and scene details.
expected_output: Final movie script with dialogue and scene details.
tools:
- search_tool
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
</Note>
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
@@ -15,88 +130,209 @@ icon: "brain"
></iframe>
</div>
## Overview
## Understanding Self-Reflection
When an agent with self-reflection enabled processes a task:
1. It generates an initial response using its tools and capabilities
2. The agent then reflects on its own response, evaluating:
- Completeness
- Accuracy
- Relevance
- Quality
3. Based on this reflection, the agent may revise and improve its response
4. This cycle can repeat until the agent is satisfied with the quality
<Card title="What is Self-Reflection?" icon="question">
Self-reflection enables agents to:
- Evaluate their own responses before delivery
- Check for completeness and accuracy
- Improve output quality through iteration
- Ensure task requirements are met
- Make conscious decisions about their responses
</Card>
## Usage
## Features
Self-reflection is enabled by default for all agents. You can explicitly control it using the `self_reflect` parameter:
<CardGroup cols={2}>
<Card title="Quality Assurance" icon="check-double">
Evaluates and improves response quality through self-review.
</Card>
<Card title="Iterative Improvement" icon="rotate">
Refines responses through multiple reflection cycles.
</Card>
<Card title="Task Validation" icon="list-check">
Ensures all aspects of the task are properly addressed.
</Card>
<Card title="Conscious Decision Making" icon="brain">
Enables thoughtful evaluation of responses.
</Card>
</CardGroup>
## Multi-Agent Self-Reflection
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create first agent with self-reflection
researcher = Agent(
role="Senior Research Analyst",
goal="Research and analyze AI developments",
backstory="Expert analyst specializing in AI trends and impacts",
self_reflect=True,
verbose=True
)
# Create second agent with self-reflection
writer = Agent(
role="Technical Writer",
goal="Transform research into clear documentation",
backstory="Experienced in creating technical content and documentation",
self_reflect=True,
verbose=True
)
# Create first task
research_task = Task(
description="Research and analyze recent AI developments",
expected_output="Comprehensive analysis of AI trends",
agent=researcher
)
# Create second task
documentation_task = Task(
description="Create technical documentation from research findings",
expected_output="Well-structured technical documentation",
agent=writer
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[research_task, documentation_task],
process="sequential"
)
# Start execution
agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai duckduckgo_search
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: create technical documentation about AI trends
roles:
researcher:
backstory: Expert analyst specializing in AI trends and their implications.
goal: Research and analyze current AI developments
role: Senior Research Analyst
self_reflect: true
min_reflect: 1
max_reflect: 2
tasks:
research_task:
description: Research and analyze recent developments in AI technology.
expected_output: Comprehensive analysis of current AI trends.
tools:
- duckduckgo
writer:
backstory: Experienced technical writer skilled in creating clear documentation.
goal: Transform research into accessible documentation
role: Technical Writer
tasks:
documentation_task:
description: Create technical documentation from research findings.
expected_output: Well-structured technical documentation.
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
from praisonaiagents import Agent
# Create an agent with advanced self-reflection configuration
agent = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at analyzing tech trends",
self_reflect=True # Enable self-reflection (default)
role="Research Analyst",
goal="Provide comprehensive analysis",
backstory="Expert analyst with critical thinking skills",
self_reflect=True, # Enable self-reflection
verbose=True, # Enable detailed logging
llm="gpt-4o", # Language model to use
allow_delegation=True # Allow task delegation
)
```
## Benefits
## Troubleshooting
- **Higher Quality Outputs**: Agents can catch and correct potential issues before delivering responses
- **More Reliable Task Completion**: Self-reflection helps ensure all aspects of a task are properly addressed
- **Conscious Decision Making**: Agents evaluate their own work, leading to more thoughtful responses
- **Improved Accuracy**: Multiple reflection cycles help refine and perfect the output
<CardGroup cols={2}>
<Card title="Response Issues" icon="rotate">
If responses are not meeting expectations:
- Enable verbose mode for debugging
- Review agent configuration
- Check task description clarity
</Card>
## Integration with Processes
<Card title="Quality Issues" icon="triangle-exclamation">
If output quality is insufficient:
- Enable verbose mode
- Review agent role and goal
- Clarify expected output
</Card>
</CardGroup>
Self-reflection works seamlessly with both sequential and hierarchical processes:
## Next Steps
- **Sequential**: Each agent self-reflects on their individual tasks
- **Hierarchical**: Agents self-reflect before reporting to the manager agent, ensuring high-quality inputs for decision-making
<CardGroup cols={2}>
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
## Example
Here's a complete example showing self-reflection in action:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at analyzing tech trends",
self_reflect=True,
llm="gpt-4o"
)
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report on AI trends",
agent=researcher
)
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
process="sequential"
)
result = agents.start()
```
In this example, the researcher agent will:
1. Research AI advancements
2. Generate an initial report
3. Self-reflect on the report's quality
4. Make improvements if needed
5. Deliver the final, refined output
## Performance Impact
While self-reflection adds additional processing time, the improvement in output quality often justifies the extra computation.
The number of reflection cycles need to be manually optimized to balance quality and performance.
<Note>
For optimal results, configure reflection parameters based on your specific use case requirements.
</Note>
+311 -264
View File
@@ -4,62 +4,133 @@ description: "Learn how to create AI agents that return structured, type-safe ou
icon: "cube"
---
<Note>
**Prerequisites**
- Python 3.10 or higher
- PraisonAI Agents package installed
- Basic understanding of Python and Pydantic
</Note>
## Quick Start
Create AI agents that return structured, validated outputs in just a few lines of code!
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Import Required Components">
Import the necessary components:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
from pydantic import BaseModel
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
from pydantic import BaseModel
<Step title="Define Your Data Model">
Create a Pydantic model to define your output structure:
```python
class AnalysisReport(BaseModel):
title: str
findings: str
summary: str
```
</Step>
# Define your data structure
class AnalysisReport(BaseModel):
title: str
findings: str
summary: str
<Step title="Create Structured Tasks">
Create tasks with structured outputs:
```python
research_task = Task(
description="Research recent AI developments",
agent=researcher,
expected_output="Research findings"
)
# Create agent
analyst = Agent(
role="Data Analyst",
goal="Analyze data and provide structured insights",
backstory="Expert in data analysis and insights generation",
tools=[Tools.internet_search],
verbose=True
)
analysis_task = Task(
description="Analyze research findings",
agent=analyst,
output_pydantic=AnalysisReport, # For Pydantic output
# or output_json=AnalysisReport # For JSON output
expected_output="Structured analysis report"
)
```
</Step>
</Steps>
# Create task with structured output
task = Task(
description="Analyze recent AI developments",
expected_output="Structured analysis report",
agent=analyst,
output_pydantic=AnalysisReport
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[analyst],
tasks=[task],
process="sequential",
verbose=2
)
# Start execution
result = agents.start()
print(result.pydantic.title)
print(result.pydantic.findings)
print(result.pydantic.summary)
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: analyze AI developments with structured output
roles:
analyst:
backstory: Expert in data analysis and insights generation.
goal: Analyze data and provide structured insights
role: Data Analyst
tools:
- internet_search
tasks:
analysis_task:
description: Analyze recent AI developments.
expected_output: Structured analysis report.
output_structure:
type: pydantic
model:
title: str
findings: str
summary: str
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
<Note>
**Requirements**
- Python 3.10 or higher
- OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
- Basic understanding of Python and Pydantic
</Note>
## Understanding Structured Outputs
@@ -71,221 +142,196 @@ Create AI agents that return structured, validated outputs in just a few lines o
- Ensure consistent output format across agent responses
</Card>
## Key Components
## Features
<CardGroup cols={2}>
<Card title="Pydantic Models" icon="cube">
Define your data structure
```python
class ResearchReport(BaseModel):
title: str
content: str
```
</Card>
<Card title="Task Configuration" icon="gear">
Configure output format
```python
Task(output_pydantic=Model)
# or
Task(output_json=Model)
```
</Card>
<Card title="Output Validation" icon="check">
Automatic validation of responses against your model
</Card>
<Card title="Format Options" icon="code">
Choose between Pydantic objects or JSON output
</Card>
</CardGroup>
## Output Types
<CardGroup cols={2}>
<Card title="Pydantic Output" icon="cube">
```python
task = Task(
output_pydantic=AnalysisReport
)
# Returns Pydantic object
```
Get type-safe Pydantic model instances
Define exact data structures with validation.
</Card>
<Card title="JSON Output" icon="brackets-curly">
```python
task = Task(
output_json=AnalysisReport
)
# Returns JSON dict
```
Get JSON dictionary matching model schema
Get structured JSON responses.
</Card>
<Card title="Type Safety" icon="shield-check">
Ensure type-safe, validated outputs.
</Card>
<Card title="Format Options" icon="code">
Choose between Pydantic or JSON.
</Card>
</CardGroup>
## Examples
## Multi-Agent Structured Analysis
### Research Analysis with Structured Output
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install Package">
First, install the PraisonAI Agents package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `app.py` with the basic setup:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
from pydantic import BaseModel
# Define output structures
class ResearchReport(BaseModel):
topic: str
findings: str
sources: list[str]
class Analysis(BaseModel):
key_points: list[str]
implications: str
recommendations: str
# Create first agent for research
researcher = Agent(
role="Research Analyst",
goal="Gather and structure research data",
backstory="Expert in research and data collection",
tools=[Tools.internet_search],
verbose=True
)
# Create second agent for analysis
analyst = Agent(
role="Data Analyst",
goal="Analyze research and provide structured insights",
backstory="Expert in data analysis and insights generation",
verbose=True
)
# Create first task
research_task = Task(
description="Research quantum computing developments",
expected_output="Structured research findings",
agent=researcher,
output_pydantic=ResearchReport
)
# Create second task
analysis_task = Task(
description="Analyze research implications",
expected_output="Structured analysis report",
agent=analyst,
output_pydantic=Analysis
)
# Create and start the agents
agents = PraisonAIAgents(
agents=[researcher, analyst],
tasks=[research_task, analysis_task],
process="sequential"
)
# Start execution
result = agents.start()
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install Package">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_api_key_here
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
process: sequential
topic: research and analyze quantum computing
roles:
researcher:
backstory: Expert in research and data collection.
goal: Gather and structure research data
role: Research Analyst
tools:
- internet_search
tasks:
research_task:
description: Research quantum computing developments.
expected_output: Structured research findings.
output_structure:
type: pydantic
model:
topic: str
findings: str
sources: list[str]
analyst:
backstory: Expert in data analysis and insights generation.
goal: Analyze research and provide structured insights
role: Data Analyst
tasks:
analysis_task:
description: Analyze research implications.
expected_output: Structured analysis report.
output_structure:
type: pydantic
model:
key_points: list[str]
implications: str
recommendations: str
```
</Step>
<Step title="Start Agents">
Type this in your terminal to run your agents:
```bash
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
### Configuration Options
```python
from praisonaiagents import Agent, Task, PraisonAIAgents, Tools
from pydantic import BaseModel
# Define output structure
class AnalysisReport(BaseModel):
title: str
findings: str
summary: str
# Create agents
researcher = Agent(
name="AIResearcher",
role="Technology Research Analyst",
tools=[Tools.internet_search]
# Create an agent with structured output configuration
agent = Agent(
role="Data Analyst",
goal="Provide structured analysis",
backstory="Expert in data analysis",
tools=[Tools.internet_search],
verbose=True, # Enable detailed logging
llm="gpt-4o" # Language model to use
)
analyst = Agent(
name="DataAnalyst",
role="Data Insights Specialist"
)
# Define structured tasks
research_task = Task(
name="gather_research",
description="Research recent AI developments in 2024",
agent=researcher,
expected_output="Research findings"
)
analysis_task = Task(
name="analyze_findings",
description="Analyze research findings and create structured report",
agent=analyst,
output_pydantic=AnalysisReport,
expected_output="Structured analysis report"
)
# Initialize and run agents
agents = PraisonAIAgents(
agents=[researcher, analyst],
tasks=[research_task, analysis_task]
)
result = agents.start()
```
### Different Output Formats
<CodeGroup>
```python Pydantic
# Get Pydantic model instance
analysis_task = Task(
description="Analyze findings",
agent=analyst,
output_pydantic=AnalysisReport
)
# Result will be AnalysisReport instance
result.pydantic.title
result.pydantic.findings
result.pydantic.summary
```
```python JSON
# Get JSON dictionary
analysis_task = Task(
description="Analyze findings",
agent=analyst,
output_json=AnalysisReport
)
# Result will be dictionary
result.json_dict['title']
result.json_dict['findings']
result.json_dict['summary']
```
</CodeGroup>
## Best Practices
<AccordionGroup>
<Accordion title="Model Design">
Design clear, focused Pydantic models:
```python
# Good
class ResearchReport(BaseModel):
title: str
findings: str
summary: str
# Too complex
class Report(BaseModel):
title: str
subtitle: Optional[str]
findings: List[Dict[str, Any]]
```
</Accordion>
<Accordion title="Output Format Selection">
Choose the right output format:
```python
# Use Pydantic when you need:
# - Type validation
# - Model methods
task = Task(output_pydantic=Model)
# Use JSON when you need:
# - Simple dictionary access
# - Serialization
task = Task(output_json=Model)
```
</Accordion>
</AccordionGroup>
## Common Patterns
### Research and Structured Analysis
```python
class ResearchReport(BaseModel):
topic: str
findings: str
sources: list[str]
class Analysis(BaseModel):
key_points: list[str]
implications: str
recommendations: str
research_task = Task(
description="Research quantum computing",
agent=researcher,
output_pydantic=ResearchReport
)
analysis_task = Task(
description="Analyze research implications",
agent=analyst,
output_pydantic=Analysis
)
```
### Data Collection and Summary
```python
class DataPoint(BaseModel):
timestamp: str
value: float
source: str
class Summary(BaseModel):
average: float
trends: str
insights: str
collection_task = Task(
description="Collect data points",
output_pydantic=DataPoint
)
summary_task = Task(
description="Summarize data",
output_pydantic=Summary
# Task with Pydantic output
task = Task(
description="Analyze data",
expected_output="Structured report",
agent=agent,
output_pydantic=AnalysisReport # Use Pydantic model
# or output_json=AnalysisReport # Use JSON output
)
```
@@ -293,30 +339,31 @@ summary_task = Task(
<CardGroup cols={2}>
<Card title="Validation Errors" icon="triangle-exclamation">
Check if your model matches the expected output structure
```python
# Print model schema
print(Model.schema_json())
```
If model validation fails:
- Check data types match model
- Verify required fields
- Enable verbose mode for debugging
</Card>
<Card title="Output Format" icon="code">
Verify you're accessing the right output field:
- `result.pydantic` for Pydantic
- `result.json_dict` for JSON
If output format is incorrect:
- Verify model definition
- Check output_pydantic vs output_json
- Review field specifications
</Card>
</CardGroup>
## Next Steps
<CardGroup cols={2}>
<Card title="Advanced Models" icon="graduation-cap" href="./advanced-models">
Learn about advanced Pydantic model configurations
<Card title="AutoAgents" icon="robot" href="./autoagents">
Learn about automatically created and managed AI agents
</Card>
<Card title="Custom Validation" icon="check-double" href="./validation">
Add custom validation to your models
<Card title="Mini Agents" icon="microchip" href="./mini">
Explore lightweight, focused AI agents
</Card>
</CardGroup>
<Note>
Structured outputs make your AI agents more reliable and their responses more predictable. They're perfect for production systems where data consistency is crucial.
For optimal results, ensure your Pydantic models accurately represent your desired output structure.
</Note>
-198
View File
@@ -1,198 +0,0 @@
---
title: "Multimodal AI Agents"
description: "Guide for creating and using multimodal AI agents in PraisonAI for processing images, videos, and other media types"
icon: "images"
---
<div className="relative w-full aspect-video">
<iframe
className="absolute top-0 left-0 w-full h-full"
src="https://www.youtube.com/embed/hjAWmUT1qqY"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
></iframe>
</div>
# Multimodal AI Agents
PraisonAI supports multimodal AI agents capable of processing various types of media including images and videos. This enables you to create agents that can understand and analyze visual content alongside text.
## Installation
```bash
pip install praisonaiagents
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
```
## Quick Start
Create `multimodal_app.py` and add the following code:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create Vision Analysis Agent
vision_agent = Agent(
name="VisionAnalyst",
role="Computer Vision Specialist",
goal="Analyze images and videos to extract meaningful information",
backstory="""You are an expert in computer vision and image analysis.
You excel at describing images, detecting objects, and understanding visual content.""",
llm="gpt-4o-mini",
self_reflect=False
)
# Create tasks with different media types
task = Task(
name="analyze_landmark",
description="Describe this famous landmark and its architectural features.",
expected_output="Detailed description of the landmark's architecture and significance",
agent=vision_agent,
images=["https://upload.wikimedia.org/wikipedia/commons/b/bf/Krakow_-_Kosciol_Mariacki.jpg"]
)
# Run the agents
agents = PraisonAIAgents(
agents=[vision_agent],
tasks=[task],
process="sequential",
verbose=True
)
result = agents.start()
print(result)
```
## Agent Configuration
### Core Attributes
- All standard agent attributes apply (role, goal, backstory, etc.)
- `llm`: Must be a model that supports vision capabilities (e.g., "gpt-4o-mini")
- `verbose`: Enable detailed logs (default: False)
## Task Configuration
### Media Support
Tasks can include various types of media through the `images` parameter:
1. **Image URLs**
```python
task = Task(
description="Analyze the image",
images=["https://example.com/image.jpg"]
)
```
2. **Local Image Files**
```python
task = Task(
description="Analyze the local image",
images=["path/to/local/image.jpg"]
)
```
3. **Video Files**
```python
task = Task(
description="Analyze the video content",
images=["path/to/video.mp4"]
)
```
### Task Types
1. **Image Analysis**
- Object detection
- Scene description
- Architectural analysis
- Text extraction from images
2. **Video Analysis**
- Event summarization
- Object tracking
- Action recognition
- Text and caption extraction
## Advanced Features
### Multiple Media Sources
Tasks can process multiple media sources simultaneously:
```python
task = Task(
description="Compare these images and describe the differences",
images=[
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
]
)
```
### Combining Text and Visual Analysis
Create tasks that leverage both text and visual understanding:
```python
task = Task(
description="""Analyze this technical diagram and:
1. Explain the components
2. Describe their relationships
3. Identify any text or labels
4. Provide technical insights""",
images=["diagram.jpg"]
)
```
## Best Practices
1. **Media Handling**
- Ensure images are in supported formats (JPEG, PNG, etc.)
- Keep video files within reasonable size limits
- Provide clear, high-quality media for best results
2. **Task Design**
- Write clear, specific descriptions
- Break complex analyses into subtasks
- Include expected output format
3. **Performance Optimization**
- Use appropriate model versions for your needs
- Consider caching for repeated analyses
- Monitor token usage with visual content
4. **Error Handling**
- Validate media files before processing
- Handle missing or corrupted files gracefully
- Implement retry logic for failed analyses
## Example Use Cases
1. **Document Analysis**
```python
document_task = Task(
description="Extract and summarize text from this document image",
expected_output="Structured text content with key information highlighted",
agent=vision_agent,
images=["document.jpg"]
)
```
2. **Security Monitoring**
```python
security_task = Task(
description="Monitor this security camera feed and report any suspicious activity",
expected_output="Detailed report of detected events and anomalies",
agent=vision_agent,
images=["security_feed.mp4"]
)
```
3. **Medical Imaging**
```python
medical_task = Task(
description="Analyze this medical scan and identify any abnormalities",
expected_output="Professional medical image analysis report",
agent=vision_agent,
images=["medical_scan.jpg"]
)
```
+97 -46
View File
@@ -1,64 +1,115 @@
---
title: "Installation"
description: "Guide for installing PraisonAI and its various components including framework support and additional features"
icon: "download"
title: Installation
description: Set up PraisonAI in your development environment
icon: download
---
## Basic Installation
```bash
<RequestExample>
```bash Code
pip install praisonaiagents
```
```bash No Code
pip install praisonai
```
</RequestExample>
## Framework-specific Installation
<Note>
**Requirements**
- Python 3.10 or higher
- pip package manager
- Virtual environment (recommended)
</Note>
### PraisonAI Agents Support (Default)
# Installing PraisonAI
```bash
pip install praisonai
```
Follow these steps to set up PraisonAI in your development environment.
<Tabs>
<Tab title="Code">
### CrewAI Support
```bash
pip install "praisonai[crewai]"
```
<Steps>
<Step title="Create Virtual Environment (Optional)">
First, create and activate a virtual environment:
<CodeGroup>
```bash Mac/Linux
python -m venv praisonai-env
source praisonai-env/bin/activate
```
Includes:
- CrewAI framework
- PraisonAI tools integration
- Task delegation capabilities
- Sequential and parallel task execution
```bash Windows
python -m venv praisonai-env
.\praisonai-env\Scripts\activate
```
</CodeGroup>
</Step>
### AutoGen Support
```bash
pip install "praisonai[autogen]"
```
<Step title="Install PraisonAI Agents">
Install the core PraisonAI Package:
```bash Terminal
pip install praisonaiagents
```
</Step>
Includes:
- AutoGen framework
- PraisonAI tools integration
- Multi-agent conversation capabilities
- Code execution environment
<Step title="Configure Environment">
Set your OpenAI API key as an environment variable in your terminal:
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
</Step>
### Both Frameworks
```bash
pip install "praisonai[crewai,autogen]"
```
</Steps>
</Tab>
<Tab title="No Code">
## Additional Features
<Steps>
<Step title="Create Virtual Environment (Optional)">
First, create and activate a virtual environment:
<CodeGroup>
```bash Mac/Linux
python -m venv praisonai-env
source praisonai-env/bin/activate
```
```bash
# UI support
pip install "praisonai[ui]"
```bash Windows
python -m venv praisonai-env
.\praisonai-env\Scripts\activate
```
</CodeGroup>
</Step>
<Step title="Install PraisonAI">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash
export OPENAI_API_KEY=your_openai_key
```
</Step>
</Steps>
</Tab>
</Tabs>
# Chat interface
pip install "praisonai[chat]"
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
You can also use other LLM providers like Anthropic, Google, etc. Please refer to the [Models](/models) for more information.
# Code interface
pip install "praisonai[code]"
## Next Steps
# Realtime voice interaction
pip install "praisonai[realtime]"
# Call feature
pip install "praisonai[call]"
```
<CardGroup cols={2}>
<Card
title="Quick Start Guide"
icon="bolt"
href="/code/quickstart"
>
Build your first AI agent
</Card>
<Card
title="API Reference"
icon="code"
href="/api/praisonaiagents/index"
>
Explore the API documentation
</Card>
</CardGroup>
+343 -58
View File
@@ -1,76 +1,361 @@
---
title: 'Introduction'
description: 'Get started with PraisonAI, an intelligent AI-powered tool'
icon: "hand-wave"
title: "Introduction"
description: "Welcome to PraisonAI - The Next Generation AI Agent Framework"
icon: "book-open"
---
## What is PraisonAI?
<Note>
PraisonAI is a low-code, centralized framework designed to simplify the creation and orchestration of multi-agent systems for various LLM applications.
PraisonAI is a powerful Multi-Agent Framework for building and deploying AI agents that can understand, reason, and execute complex tasks autonomously.
</Note>
# Welcome to PraisonAI
<Frame caption="PraisonAI Framework Overview">
![PraisonAI Overview](/images/praisonai-overview.png)
</Frame>
## Core Features
<CardGroup cols={2}>
<Card title="Multi-Agent Systems" icon="users">
Create and orchestrate sophisticated multi-agent systems with ease
</Card>
<Card title="Framework Integration" icon="puzzle-piece">
Seamlessly integrates AutoGen and CrewAI capabilities
</Card>
<Card title="Low-Code Design" icon="code">
Build complex AI systems with minimal coding required
</Card>
<Card title="Human-AI Interaction" icon="people-arrows">
Enhanced support for human-agent collaboration
<CardGroup cols={1}>
<Card title="Welcome to PraisonAI" icon="wand-magic-sparkles">
Build powerful autonomous agents that understand, decide, and execute with unprecedented capability.
</Card>
</CardGroup>
## Getting Started
# Core Components
<br />
```mermaid
graph LR
%% Define the main flow
Start([▶ Start]) --> Agent1
Agent1 --> Process[⚙ Process]
Process --> Agent2
Agent2 --> Output([✓ Output])
Process -.-> Agent1
%% Styling for main elements
style Start fill:#2D3047,stroke:#7C90A0,color:#fff
style Output fill:#2D3047,stroke:#7C90A0,color:#fff
style Process fill:#5C80BC,stroke:#7C90A0,color:#fff
%% Define subgraphs for agents and their tasks
subgraph Agent1[" "]
Task1[📋 Task]
AgentIcon1[🤖 AI Agent]
Tools1[🔧 Tools]
Task1 --- AgentIcon1
AgentIcon1 --- Tools1
style Task1 fill:#2D3047,stroke:#7C90A0,color:#fff
style AgentIcon1 fill:#5C80BC,stroke:#7C90A0,color:#fff
style Tools1 fill:#2D3047,stroke:#7C90A0,color:#fff
end
subgraph Agent2[" "]
Task2[📋 Task]
AgentIcon2[🤖 AI Agent]
Tools2[🔧 Tools]
Task2 --- AgentIcon2
AgentIcon2 --- Tools2
style Task2 fill:#2D3047,stroke:#7C90A0,color:#fff
style AgentIcon2 fill:#5C80BC,stroke:#7C90A0,color:#fff
style Tools2 fill:#2D3047,stroke:#7C90A0,color:#fff
end
%% Style for agent containers
style Agent1 fill:#1B1F3B,stroke:#7C90A0,color:#fff
style Agent2 fill:#1B1F3B,stroke:#7C90A0,color:#fff
%% Global styles
classDef default fill:#2D3047,stroke:#7C90A0,color:#fff
linkStyle default stroke:#7C90A0,stroke-width:2px
```
<br />
<CardGroup cols={2}>
<Card title="Agents" icon="user-robot">
Autonomous AI entities that can understand context and execute tasks
</Card>
<Card title="Tasks" icon="list-check">
Units of work that agents can execute independently or collaboratively
</Card>
<Card title="Process" icon="diagram-project">
Workflow management for coordinating agent activities
</Card>
<Card title="Tools" icon="toolbox">
Extensible capabilities that agents can leverage
</Card>
</CardGroup>
<Steps>
<Step title="Installation">
Follow our [Installation Guide](./installation) to set up PraisonAI
</Step>
<Step title="Quick Overview">
Check out our [TL;DR](./tldr) for a rapid introduction
</Step>
<Step title="First Project">
Create your first project following our [Quick Start](./quickstart) guide
</Step>
</Steps>
## Key Components
| Component | Description | Benefits |
|:----------|:------------|:---------|
| **UI Tools** | Intuitive interface components | Easy system interaction and monitoring |
| **Model Integration** | Support for various LLM models | Flexibility in AI model selection |
| **Tool System** | Extensible tool framework | Custom capability integration |
| **API Layer** | Comprehensive API access | Full programmatic control |
## Explore Further
## Use Cases
<CardGroup cols={2}>
<Card title="User Interface" icon="desktop" href="./ui/ui">
Learn about our intuitive UI components
<Card title="Customer Service" icon="headset">
Build intelligent support agents that can handle customer inquiries and resolve issues autonomously.
</Card>
<Card title="Models" icon="robot" href="./models">
Explore supported AI models
<Card title="Data Analysis" icon="chart-line">
Create agents that can process, analyze, and derive insights from complex datasets.
</Card>
<Card title="Tools" icon="wrench" href="./tools">
Discover available tools and integrations
<Card title="Content Creation" icon="pen-nib">
Deploy agents that can generate, edit, and optimize content across various formats.
</Card>
<Card title="API Reference" icon="code" href="./api-reference">
Access comprehensive API documentation
<Card title="Process Automation" icon="gears">
Automate complex workflows with intelligent agents that can coordinate and execute tasks.
</Card>
</CardGroup>
## Getting Started is Easy
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Create Agent">
Create `app.py`:
<CodeGroup>
```python Single Agent
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
```python Multiple Agents
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install PraisonAI">
Install the PraisonAI package:
```bash
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Start AutoAgents">
Run your AutoAgents:
```bash
praisonai --auto "Create a movie script about a robot in Mars"
```
</Step>
</Steps>
</Tab>
</Tabs>
## Key Features
<CardGroup cols={3}>
<Card title="Autonomous Agents" icon="robot">
Create intelligent agents that can:
- Understand natural language instructions
- Make decisions based on context
- Execute complex tasks autonomously
</Card>
<Card title="Flexible Architecture" icon="cubes">
Build with:
- Modular components
- Extensible tools
- Customizable workflows
</Card>
<Card title="Advanced Capabilities" icon="wand-sparkles">
Leverage:
- Multi-agent collaboration
- Memory and context management
- Tool integration
</Card>
</CardGroup>
<Card title="Why Choose PraisonAI?" icon="stars">
<CardGroup cols={2}>
## Developer First
Build with modern tools and frameworks you already love. Our SDK is designed to be intuitive and powerful.
## Production Ready
Enterprise-grade reliability with built-in security features and scalable architecture.
## Open Source
PraisonAI is open source and available on [GitHub](https://github.com/MervinPraison/PraisonAI).
## Low Code User Friendly
PraisonAI is designed to be easy to learn and use for non technical users.
</CardGroup>
</Card>
## Chat with One Agent
<Tabs>
<Tab title="Basic Agent">
<Steps>
<Step title="Install PraisonAI">
Get started with a simple pip install:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Initialize Your Agent">
Create your first autonomous agent in just a few lines:
```python
from praisonaiagents import Agent
agent = Agent(instructions="Your are a helpful AI assistant")
agent.chat("Write a movie script about a robot in Mars")
```
</Step>
<Step title="Build Amazing Things">
Start building powerful AI applications with autonomous capabilities!
</Step>
</Steps>
</Tab>
<Tab title="Agent with Tools">
<Steps>
<Step title="Install PraisonAI">
Get started with a simple pip install:
```bash
pip install praisonaiagents duckduckgo_search
```
</Step>
<Step title="Initialize Your Agent">
Create your first autonomous agent in just a few lines:
```python
from praisonaiagents import Agent, Tools
agent = Agent(instructions="Your are a helpful AI assistant", tools=[Tools.internet_search])
agent.chat("Write a movie script about a robot in Mars")
```
</Step>
<Step title="Build Amazing Things">
Start building powerful AI applications with autonomous capabilities!
</Step>
</Steps>
</Tab>
</Tabs>
## Next Steps
<CardGroup cols={2}>
<Card
title="Installation"
icon="download"
href="/code/installation"
>
Complete installation guide and setup instructions
</Card>
<Card
title="Quick Start"
icon="play"
href="/code/quickstart"
>
Build your first AI agent in minutes
</Card>
</CardGroup>
<Tip>
Start with our no-code interface to learn the basics, then transition to code-based development for more advanced use cases.
</Tip>
Join our community on [Discord](https://www.youtube.com/redirect?event=channel_description&redir_token=QUFFLUhqbUpiZE00YlF1MjB1LVNIeTFxTnV6MWRRc18wZ3xBQ3Jtc0ttQVZ0aFJ4RXRDanlBQU1Vd3lHU3BBcml3bzhoek1pTmlkZkFkWjlNVDBpRkxZd1dTOWhPc20xbHJOb1NibmNYeTEydklJT0pZOVJFQW93MmJOQXU0WkJNVzNsd29SZ3FBNFVwMTFGNThNSHBWVUhjSQ&q=https%3A%2F%2Fdiscord.gg%2FnNZu5gGT59) to connect with other developers and get help!
</Tip>
+23 -18
View File
@@ -60,11 +60,11 @@
"pages": ["index"]
},
{
"group": "Getting Started (Code)",
"group": "Getting Started",
"pages": [
"code/introduction",
"code/installation",
"code/quickstart"
"introduction",
"installation",
"quickstart"
]
},
{
@@ -73,32 +73,25 @@
"concepts/agents",
"concepts/tasks",
"concepts/process",
"concepts/tools"
"concepts/tools",
"concepts/memory"
]
},
{
"group": "Features",
"pages": [
"features/autoagents",
"features/selfreflection",
"features/reasoning",
"framework/multimodalagents",
"features/multimodal",
"features/langchain",
"features/async",
"features/mini",
"features/codeagent",
"features/mathagent",
"features/structured",
"features/callbacks"
]
},
{
"group": "Getting Started (No Code)",
"pages": [
"introduction",
"tldr",
"installation",
"initialise",
"run",
"auto"
"features/callbacks",
"features/agenticrouting"
]
},
{
@@ -187,12 +180,24 @@
"deploy"
]
},
{
"group": "Getting Started (No Code)",
"pages": [
"nocode/introduction",
"nocode/tldr",
"nocode/installation",
"nocode/initialise",
"nocode/run",
"nocode/auto"
]
},
{
"group": "API Reference",
"pages": [
"api/praisonaiagents/index",
"api/praisonaiagents/agent/agent",
"api/praisonaiagents/agents/agents",
"api/praisonaiagents/agents/autoagents",
"api/praisonaiagents/task/task",
"api/praisonaiagents/process/process"
]
File renamed without changes.
File renamed without changes.
+64
View File
@@ -0,0 +1,64 @@
---
title: "Installation"
description: "Guide for installing PraisonAI and its various components including framework support and additional features"
icon: "download"
---
## Basic Installation
```bash
pip install praisonai
```
## Framework-specific Installation
### PraisonAI Agents Support (Default)
```bash
pip install praisonai
```
### CrewAI Support
```bash
pip install "praisonai[crewai]"
```
Includes:
- CrewAI framework
- PraisonAI tools integration
- Task delegation capabilities
- Sequential and parallel task execution
### AutoGen Support
```bash
pip install "praisonai[autogen]"
```
Includes:
- AutoGen framework
- PraisonAI tools integration
- Multi-agent conversation capabilities
- Code execution environment
### Both Frameworks
```bash
pip install "praisonai[crewai,autogen]"
```
## Additional Features
```bash
# UI support
pip install "praisonai[ui]"
# Chat interface
pip install "praisonai[chat]"
# Code interface
pip install "praisonai[code]"
# Realtime voice interaction
pip install "praisonai[realtime]"
# Call feature
pip install "praisonai[call]"
```
+76
View File
@@ -0,0 +1,76 @@
---
title: 'Introduction'
description: 'Get started with PraisonAI, an intelligent AI-powered tool'
icon: "hand-wave"
---
<Note>
PraisonAI is a low-code, centralized framework designed to simplify the creation and orchestration of multi-agent systems for various LLM applications.
</Note>
# Welcome to PraisonAI
<Frame caption="PraisonAI Framework Overview">
![PraisonAI Overview](/images/praisonai-overview.png)
</Frame>
## Core Features
<CardGroup cols={2}>
<Card title="Multi-Agent Systems" icon="users">
Create and orchestrate sophisticated multi-agent systems with ease
</Card>
<Card title="Framework Integration" icon="puzzle-piece">
Seamlessly integrates AutoGen and CrewAI capabilities
</Card>
<Card title="Low-Code Design" icon="code">
Build complex AI systems with minimal coding required
</Card>
<Card title="Human-AI Interaction" icon="people-arrows">
Enhanced support for human-agent collaboration
</Card>
</CardGroup>
## Getting Started
<Steps>
<Step title="Installation">
Follow our [Installation Guide](./installation) to set up PraisonAI
</Step>
<Step title="Quick Overview">
Check out our [TL;DR](./tldr) for a rapid introduction
</Step>
<Step title="First Project">
Create your first project following our [Quick Start](./quickstart) guide
</Step>
</Steps>
## Key Components
| Component | Description | Benefits |
|:----------|:------------|:---------|
| **UI Tools** | Intuitive interface components | Easy system interaction and monitoring |
| **Model Integration** | Support for various LLM models | Flexibility in AI model selection |
| **Tool System** | Extensible tool framework | Custom capability integration |
| **API Layer** | Comprehensive API access | Full programmatic control |
## Explore Further
<CardGroup cols={2}>
<Card title="User Interface" icon="desktop" href="./ui/ui">
Learn about our intuitive UI components
</Card>
<Card title="Models" icon="robot" href="./models">
Explore supported AI models
</Card>
<Card title="Tools" icon="wrench" href="./tools">
Discover available tools and integrations
</Card>
<Card title="API Reference" icon="code" href="./api-reference">
Access comprehensive API documentation
</Card>
</CardGroup>
<Tip>
Start with our no-code interface to learn the basics, then transition to code-based development for more advanced use cases.
</Tip>
File renamed without changes.
File renamed without changes.
+339
View File
@@ -0,0 +1,339 @@
---
title: Quick Start
description: Build your first AI agent with PraisonAI
icon: bolt
---
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package:
```bash Terminal
pip install praisonaiagents
```
</Step>
<Step title="Configure Environment">
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
Generate your OpenAI API key from [OpenAI](https://platform.openai.com/api-keys)
Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the [Models](/models) for more information.
</Step>
<Step title="Create Agent">
Create `app.py`:
<CodeGroup>
```python Single Agent
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create an agent
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o"
)
# Define a task
task = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
# Run the agents
agents = PraisonAIAgents(
agents=[researcher],
tasks=[task],
verbose=False
)
result = agents.start()
```
```python Multiple Agents
from praisonaiagents import Agent, Task, PraisonAIAgents
# Create multiple agents
researcher = Agent(
name="Researcher",
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI",
backstory="You are an expert at a technology research group",
verbose=True,
llm="gpt-4o",
markdown=True
)
writer = Agent(
name="Writer",
role="Tech Content Strategist",
goal="Craft compelling content on tech advancements",
backstory="You are a content strategist",
llm="gpt-4o",
markdown=True
)
# Define multiple tasks
task1 = Task(
name="research_task",
description="Analyze 2024's AI advancements",
expected_output="A detailed report",
agent=researcher
)
task2 = Task(
name="writing_task",
description="Create a blog post about AI advancements",
expected_output="A blog post",
agent=writer
)
# Run with hierarchical process
agents = PraisonAIAgents(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=False,
process="hierarchical",
manager_llm="gpt-4o"
)
result = agents.start()
```
</CodeGroup>
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
python app.py
```
You should see:
- Agent initialization
- Agents progress
- Final results
- Generated report
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install No Code PraisonAI">
Install the No Code PraisonAI Package:
```bash Terminal
pip install praisonai
```
</Step>
<Step title="Set API Key">
Set your OpenAI API key as an environment variable in your terminal:
```bash Terminal
export OPENAI_API_KEY=your_openai_key
```
</Step>
<Step title="Create a file">
Create a new file `agents.yaml` with the basic setup:
```yaml
framework: praisonai
topic: create movie script about cat in mars
roles:
scriptwriter:
backstory: Expert in dialogue and script structure, translating concepts into
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
including dialogue and scene details.
expected_output: Final movie script with dialogue and scene details.
```
<Note>
You can automatically create `agents.yaml` file using
```bash Terminal
praisonai --init create movie script about cat in mars
```
</Note>
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
## Creating Custom Tool for Agents
<Info>
Tools makes the Agent powerful.
</Info>
More information about tools can be found in the [Tools](/concepts/tools) section.
<Tabs>
<Tab title="Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package and duckduckgo_search package:
```bash Terminal
pip install praisonai duckduckgo_search
```
</Step>
<Step title="Create Tools and Agents">
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from duckduckgo_search import DDGS
from typing import List, Dict
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
# 2. Agent
data_agent = Agent(
name="DataCollector",
role="Search Specialist",
goal="Perform internet searches to collect relevant information.",
backstory="Expert in finding and organising internet data.",
tools=[internet_search_tool],
self_reflect=False
)
# 3. Tasks
collect_task = Task(
description="Perform an internet search using the query: 'AI job trends in 2024'. Return results as a list of title, URL, and snippet.",
expected_output="List of search results with titles, URLs, and snippets.",
agent=data_agent,
name="collect_data",
)
# 4. Start Agents
agents = PraisonAIAgents(
agents=[data_agent],
tasks=[collect_task],
process="sequential"
)
agents.start()
```
</Step>
<Step title="Start Agents">
Run your script:
```bash Terminal
python app.py
```
</Step>
</Steps>
</Tab>
<Tab title="No Code">
<Steps>
<Step title="Install PraisonAI">
Install the core package and duckduckgo_search package:
```bash Terminal
pip install praisonai duckduckgo_search
```
</Step>
<Step title="Create Custom Tool">
<Info>
To add additional tools/features you need some coding which can be generated using ChatGPT or any LLM
</Info>
Create a new file `tools.py` with the following content:
```python
from duckduckgo_search import DDGS
from typing import List, Dict
# 1. Tool
def internet_search_tool(query: str) -> List[Dict]:
"""
Perform Internet Search
"""
results = []
ddgs = DDGS()
for result in ddgs.text(keywords=query, max_results=5):
results.append({
"title": result.get("title", ""),
"url": result.get("href", ""),
"snippet": result.get("body", "")
})
return results
```
</Step>
<Step title="Create Agent">
Create a new file `agents.yaml` with the following content:
```yaml
framework: praisonai
topic: create movie script about cat in mars
roles:
scriptwriter:
backstory: Expert in dialogue and script structure, translating concepts into
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
tools:
- internet_search_tool # <-- Tool assigned to Agent here
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
including dialogue and scene details.
expected_output: Final movie script with dialogue and scene details.
```
</Step>
<Step title="Start Agents">
Execute your script:
```bash Terminal
praisonai agents.yaml
```
</Step>
</Steps>
</Tab>
</Tabs>
## Next Steps
<CardGroup cols={2}>
<Card
title="Core Concepts"
icon="book"
href="/concepts/agents"
>
Learn about agents, tasks, and processes
</Card>
<Card
title="API Reference"
icon="code"
href="/api/praisonaiagents/index"
>
Explore detailed API documentation
</Card>
</CardGroup>
+1 -1
View File
@@ -1,7 +1,7 @@
---
title: "JSON Agent"
description: "JSON data processing tools for AI agents."
icon: "braces"
icon: "brackets-curly"
---
<Note>
+1 -1
View File
@@ -3,7 +3,7 @@ class Praisonai < Formula
desc "AI tools for various AI applications"
homepage "https://github.com/MervinPraison/PraisonAI"
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/2.0.36.tar.gz"
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/2.0.42.tar.gz"
sha256 "1828fb9227d10f991522c3f24f061943a254b667196b40b1a3e4a54a8d30ce32" # Replace with actual SHA256 checksum
license "MIT"
+15 -9
View File
@@ -212,9 +212,9 @@ class AgentsGenerator:
try:
# Try to import tools.py from current directory
spec = importlib.util.spec_from_file_location("tools", "tools.py")
self.logger.info(f"Spec: {spec}")
self.logger.debug(f"Spec: {spec}")
if spec is None:
self.logger.info("tools.py not found in current directory")
self.logger.debug("tools.py not found in current directory")
return tools_list
module = importlib.util.module_from_spec(spec)
@@ -229,11 +229,13 @@ class AgentsGenerator:
globals()[name] = obj
# Add to tools list
tools_list.append(obj)
self.logger.info(f"Loaded and globalized tool function: {name}")
self.logger.debug(f"Loaded and globalized tool function: {name}")
self.logger.info(f"Loaded {len(tools_list)} tool functions from tools.py")
self.logger.info(f"Tools list: {tools_list}")
self.logger.debug(f"Loaded {len(tools_list)} tool functions from tools.py")
self.logger.debug(f"Tools list: {tools_list}")
except FileNotFoundError:
self.logger.debug("tools.py not found in current directory")
except Exception as e:
self.logger.warning(f"Error loading tools from tools.py: {e}")
@@ -539,7 +541,7 @@ class AgentsGenerator:
# Load tools once at the beginning
tools_list = self.load_tools_from_tools_py()
self.logger.info(f"Loaded tools: {tools_list}")
self.logger.debug(f"Loaded tools: {tools_list}")
# Create agents from config
for role, details in config['roles'].items():
@@ -574,7 +576,7 @@ class AgentsGenerator:
agent.step_callback = self.agent_callback
agents[role] = agent
self.logger.info(f"Created agent {role_filled} with tools: {agent.tools}")
self.logger.debug(f"Created agent {role_filled} with tools: {agent.tools}")
# Create tasks for the agent
for task_name, task_details in details.get('tasks', {}).items():
@@ -596,7 +598,7 @@ class AgentsGenerator:
create_directory=task_details.get('create_directory', False)
)
self.logger.info(f"Created task {task_name} with tools: {task.tools}")
self.logger.debug(f"Created task {task_name} with tools: {task.tools}")
if self.task_callback:
task.callback = self.task_callback
@@ -613,6 +615,8 @@ class AgentsGenerator:
task.context = context_tasks
# Create and run the PraisonAI agents
memory = config.get('memory', False)
self.logger.debug(f"Memory: {memory}")
if config.get('process') == 'hierarchical':
agents = PraisonAIAgents(
agents=list(agents.values()),
@@ -620,12 +624,14 @@ class AgentsGenerator:
verbose=True,
process="hierarchical",
manager_llm=config.get('manager_llm', 'gpt-4o'),
memory=memory
)
else:
agents = PraisonAIAgents(
agents=list(agents.values()),
tasks=tasks,
verbose=2
verbose=2,
memory=memory
)
self.logger.debug("Final Configuration:")
+3 -3
View File
@@ -50,7 +50,7 @@ logger.handlers = []
# Try to import tools from the root directory
tools = []
tools_path = os.path.join(os.getcwd(), 'tools.py')
logger.info(f"Tools path: {tools_path}")
logger.debug(f"Tools path: {tools_path}")
def import_tools_from_file(file_path):
spec = importlib.util.spec_from_file_location("custom_tools", file_path)
@@ -63,9 +63,9 @@ try:
if os.path.exists(tools_path):
# tools.py exists in the root directory, import from file
custom_tools_module = import_tools_from_file(tools_path)
logger.info("Successfully imported custom tools from root tools.py")
logger.debug("Successfully imported custom tools from root tools.py")
else:
logger.info("No custom tools.py file found in the root directory")
logger.debug("No custom tools.py file found in the root directory")
custom_tools_module = None
if custom_tools_module:
+6
View File
@@ -73,6 +73,12 @@ except ImportError:
pass
logging.basicConfig(level=os.environ.get('LOGLEVEL', 'INFO'), format='%(asctime)s - %(levelname)s - %(message)s')
logging.getLogger('alembic').setLevel(logging.ERROR)
logging.getLogger('gradio').setLevel(logging.ERROR)
logging.getLogger('gradio').setLevel(os.environ.get('GRADIO_LOGLEVEL', 'WARNING'))
logging.getLogger('rust_logger').setLevel(logging.WARNING)
logging.getLogger('duckduckgo').setLevel(logging.ERROR)
logging.getLogger('_client').setLevel(logging.ERROR)
def stream_subprocess(command, env=None):
"""
+1 -1
View File
@@ -56,7 +56,7 @@ class CloudDeployer:
file.write("FROM python:3.11-slim\n")
file.write("WORKDIR /app\n")
file.write("COPY . .\n")
file.write("RUN pip install flask praisonai==2.0.36 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==2.0.42 gunicorn markdown\n")
file.write("EXPOSE 8080\n")
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
+61 -6
View File
@@ -186,11 +186,42 @@ try:
if custom_tools_module:
# Update the tools list with custom tools
if hasattr(custom_tools_module, 'tools') and isinstance(custom_tools_module.tools, list):
tools.extend(custom_tools_module.tools)
# Only add tools that have proper function definitions
for tool in custom_tools_module.tools:
if isinstance(tool, tuple) and len(tool) == 2:
tool_def, handler = tool
if isinstance(tool_def, dict) and "type" in tool_def and tool_def["type"] == "function":
# Convert class/function to proper tool definition
if "function" in tool_def:
func = tool_def["function"]
if hasattr(func, "__name__"):
tool_def = {
"name": func.__name__,
"description": func.__doc__ or f"Execute {func.__name__}",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
tools.append((tool_def, handler))
else:
# Tool definition is already properly formatted
tools.append(tool)
else:
# Process individual functions/classes
for name, obj in custom_tools_module.__dict__.items():
if callable(obj) and not name.startswith("__"):
tools.append(({"type": "function", "function": obj}, obj))
tool_def = {
"name": name,
"description": obj.__doc__ or f"Execute {name}",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
tools.append((tool_def, obj))
except Exception as e:
logger.warning(f"Error importing custom tools: {str(e)}. Continuing without custom tools.")
@@ -287,14 +318,30 @@ async def setup_openai_realtime():
logger.error(event)
await cl.Message(content=f"Error: {event}", author="System").send()
# Register event handlers
openai_realtime.on('conversation.updated', handle_conversation_updated)
openai_realtime.on('conversation.item.completed', handle_item_completed)
openai_realtime.on('conversation.interrupted', handle_conversation_interrupt)
openai_realtime.on('error', handle_error)
cl.user_session.set("openai_realtime", openai_realtime)
coros = [openai_realtime.add_tool(tool_def, tool_handler) for tool_def, tool_handler in tools]
await asyncio.gather(*coros)
# Filter out invalid tools and add valid ones
valid_tools = []
for tool_def, tool_handler in tools:
try:
if isinstance(tool_def, dict) and "name" in tool_def:
valid_tools.append((tool_def, tool_handler))
else:
logger.warning(f"Skipping invalid tool definition: {tool_def}")
except Exception as e:
logger.warning(f"Error processing tool: {e}")
if valid_tools:
coros = [openai_realtime.add_tool(tool_def, tool_handler) for tool_def, tool_handler in valid_tools]
await asyncio.gather(*coros)
else:
logger.warning("No valid tools found to add")
@cl.on_settings_update
async def setup_agent(settings):
@@ -330,11 +377,19 @@ async def setup_agent(settings):
async def on_audio_start():
try:
openai_realtime: RealtimeClient = cl.user_session.get("openai_realtime")
await openai_realtime.connect()
if not openai_realtime:
await setup_openai_realtime()
openai_realtime = cl.user_session.get("openai_realtime")
if not openai_realtime.is_connected():
await openai_realtime.connect()
logger.info("Connected to OpenAI realtime")
return True
except Exception as e:
await cl.ErrorMessage(content=f"Failed to connect to OpenAI realtime: {e}").send()
error_msg = f"Failed to connect to OpenAI realtime: {str(e)}"
logger.error(error_msg)
await cl.ErrorMessage(content=error_msg).send()
return False
@cl.on_audio_chunk
+2 -1
View File
@@ -9,7 +9,8 @@ import os
import aiofiles
import aiohttp
from chainlit.data.base import BaseDataLayer, BaseStorageClient
from chainlit.data.base import BaseDataLayer
from chainlit.data.storage_clients.base import EXPIRY_TIME, BaseStorageClient
from chainlit.data.utils import queue_until_user_message
from chainlit.element import ElementDict
from chainlit.logger import logger
+8 -8
View File
@@ -1,6 +1,6 @@
[project]
name = "PraisonAI"
version = "2.0.36"
version = "2.0.42"
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human–agent collaboration."
readme = "README.md"
license = ""
@@ -12,7 +12,7 @@ dependencies = [
"rich>=13.7",
"markdown>=3.5",
"pyparsing>=3.0.0",
"praisonaiagents>=0.0.27",
"praisonaiagents>=0.0.32",
"python-dotenv>=0.19.0",
"instructor>=1.3.3",
"PyYAML>=6.0",
@@ -20,7 +20,7 @@ dependencies = [
[project.optional-dependencies]
ui = [
"chainlit==1.3.2",
"chainlit==2.0.1",
"sqlalchemy>=2.0.36",
"aiosqlite>=0.20.0",
"greenlet>=3.0.3",
@@ -34,7 +34,7 @@ openai = ["langchain-openai>=0.1.7"]
anthropic = ["langchain-anthropic>=0.1.13"]
cohere = ["langchain-cohere>=0.1.4"]
chat = [
"chainlit==1.3.2",
"chainlit==2.0.1",
"litellm>=1.41.8",
"aiosqlite>=0.20.0",
"greenlet>=3.0.3",
@@ -46,7 +46,7 @@ chat = [
"pydantic<=2.10.1"
]
code = [
"chainlit==1.3.2",
"chainlit==2.0.1",
"litellm>=1.41.8",
"aiosqlite>=0.20.0",
"greenlet>=3.0.3",
@@ -57,7 +57,7 @@ code = [
"pydantic<=2.10.1"
]
realtime = [
"chainlit==1.3.2",
"chainlit==2.0.1",
"litellm>=1.41.8",
"aiosqlite>=0.20.0",
"greenlet>=3.0.3",
@@ -84,7 +84,7 @@ autogen = ["pyautogen>=0.2.19", "praisonai-tools>=0.0.7", "crewai"]
[tool.poetry]
name = "PraisonAI"
version = "2.0.36"
version = "2.0.42"
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human–agent collaboration."
authors = ["Mervin Praison"]
license = ""
@@ -102,7 +102,7 @@ python = ">=3.10,<3.13"
rich = ">=13.7"
markdown = ">=3.5"
pyparsing = ">=3.0.0"
praisonaiagents = ">=0.0.27"
praisonaiagents = ">=0.0.32"
python-dotenv = ">=0.19.0"
instructor = ">=1.3.3"
PyYAML = ">=6.0"
Generated
+15 -16
View File
@@ -482,7 +482,7 @@ wheels = [
[[package]]
name = "chainlit"
version = "1.3.2"
version = "2.0.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "aiofiles" },
@@ -495,7 +495,6 @@ dependencies = [
{ name = "lazify" },
{ name = "literalai" },
{ name = "nest-asyncio" },
{ name = "numpy" },
{ name = "packaging" },
{ name = "pydantic" },
{ name = "pyjwt" },
@@ -509,9 +508,9 @@ dependencies = [
{ name = "uvicorn" },
{ name = "watchfiles" },
]
sdist = { url = "https://files.pythonhosted.org/packages/6a/57/8c3c354b3e82fae0791b58304634b356aeea8791dd0b6f817a8d8787cad6/chainlit-1.3.2.tar.gz", hash = "sha256:5562246ca42c858a42e86efdd5a46c27fd93481706f6fe47416109c47ac41b77", size = 4271845 }
sdist = { url = "https://files.pythonhosted.org/packages/e0/15/26dc5f957c6344813b2ae8c6f52cc820a7074088509ea947da0cf76ffc5f/chainlit-2.0.1.tar.gz", hash = "sha256:9fb7728aa5704e823c5b5d51f570dcfabafdcc97c23a73e6047f65eb72c938e7", size = 4637433 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/ca/d8/2a0c00b2236c93f31df429642de6414c56510d39392101b1b102074520d0/chainlit-1.3.2-py3-none-any.whl", hash = "sha256:c0926e8b157a67aede511a8220307889d04e9458ff3f94176e004fe533662d74", size = 4332216 },
{ url = "https://files.pythonhosted.org/packages/ca/99/c63fa2e1d7b949c034b7fc838a0c00de22cd2cec30245e379c9dd15dedfd/chainlit-2.0.1-py3-none-any.whl", hash = "sha256:84982902c6f42a91ac341ea9b6d52e6b1348e53a60ee49b4ffe0e5e5be02f4ba", size = 4703745 },
]
[[package]]
@@ -3061,7 +3060,7 @@ wheels = [
[[package]]
name = "praisonai"
version = "2.0.36"
version = "2.0.42"
source = { editable = "." }
dependencies = [
{ name = "instructor" },
@@ -3165,10 +3164,10 @@ requires-dist = [
{ name = "aiosqlite", marker = "extra == 'code'", specifier = ">=0.20.0" },
{ name = "aiosqlite", marker = "extra == 'realtime'", specifier = ">=0.20.0" },
{ name = "aiosqlite", marker = "extra == 'ui'", specifier = ">=0.20.0" },
{ name = "chainlit", marker = "extra == 'chat'", specifier = "==1.3.2" },
{ name = "chainlit", marker = "extra == 'code'", specifier = "==1.3.2" },
{ name = "chainlit", marker = "extra == 'realtime'", specifier = "==1.3.2" },
{ name = "chainlit", marker = "extra == 'ui'", specifier = "==1.3.2" },
{ name = "chainlit", marker = "extra == 'chat'", specifier = "==2.0.1" },
{ name = "chainlit", marker = "extra == 'code'", specifier = "==2.0.1" },
{ name = "chainlit", marker = "extra == 'realtime'", specifier = "==2.0.1" },
{ name = "chainlit", marker = "extra == 'ui'", specifier = "==2.0.1" },
{ name = "crawl4ai", marker = "extra == 'chat'", specifier = "==0.3.4" },
{ name = "crawl4ai", marker = "extra == 'code'", specifier = "==0.3.4" },
{ name = "crawl4ai", marker = "extra == 'realtime'", specifier = "==0.3.4" },
@@ -3198,7 +3197,7 @@ requires-dist = [
{ name = "plotly", marker = "extra == 'realtime'", specifier = ">=5.24.0" },
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.7" },
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.7" },
{ name = "praisonaiagents", specifier = ">=0.0.27" },
{ name = "praisonaiagents", specifier = ">=0.0.32" },
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = ">=0.2.19" },
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
@@ -3251,16 +3250,16 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.27"
version = "0.0.32"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "openai" },
{ name = "pydantic" },
{ name = "rich" },
]
sdist = { url = "https://files.pythonhosted.org/packages/05/c9/b368dc9c23e9d0df0b2d427055b71e842beebc15e3d005520b52baf05481/praisonaiagents-0.0.27.tar.gz", hash = "sha256:a195a8ea902ef437cc2ba8a6cac4ff76d7c4a208ef387ff69e752343e53ce104", size = 61683 }
sdist = { url = "https://files.pythonhosted.org/packages/96/b7/3de3e4cff09a2b21f7b8c1555a7f3de8e4a00ff99e63bce778fe9429dc23/praisonaiagents-0.0.32.tar.gz", hash = "sha256:466f43cf9638baac3f1196870c88d02718c9a2f870fe712ee6cbbc8d6c61c6bc", size = 68653 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/0d/63/b4b45c943c78b1818cec8b2dd92b3fdea36075eb5e323c8ca22e917ca75e/praisonaiagents-0.0.27-py3-none-any.whl", hash = "sha256:3404a186f2be2dfed0b56efec613896f4306f5589dc65d4b3f7034b0401d7593", size = 80590 },
{ url = "https://files.pythonhosted.org/packages/50/a6/32e90fe57973b9ebdfcca1ff80fa0142c935517456aef8bdd8ef2118e311/praisonaiagents-0.0.32-py3-none-any.whl", hash = "sha256:f0175e2a8560d1bdc7c06b788d821422d403255fa3dd964ff5857f713e4972a1", size = 85596 },
]
[[package]]
@@ -3839,11 +3838,11 @@ wheels = [
[[package]]
name = "python-multipart"
version = "0.0.9"
version = "0.0.18"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/5c/0f/9c55ac6c84c0336e22a26fa84ca6c51d58d7ac3a2d78b0dfa8748826c883/python_multipart-0.0.9.tar.gz", hash = "sha256:03f54688c663f1b7977105f021043b0793151e4cb1c1a9d4a11fc13d622c4026", size = 31516 }
sdist = { url = "https://files.pythonhosted.org/packages/b4/86/b6b38677dec2e2e7898fc5b6f7e42c2d011919a92d25339451892f27b89c/python_multipart-0.0.18.tar.gz", hash = "sha256:7a68db60c8bfb82e460637fa4750727b45af1d5e2ed215593f917f64694d34fe", size = 36622 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/3d/47/444768600d9e0ebc82f8e347775d24aef8f6348cf00e9fa0e81910814e6d/python_multipart-0.0.9-py3-none-any.whl", hash = "sha256:97ca7b8ea7b05f977dc3849c3ba99d51689822fab725c3703af7c866a0c2b215", size = 22299 },
{ url = "https://files.pythonhosted.org/packages/13/6b/b60f47101ba2cac66b4a83246630e68ae9bbe2e614cbae5f4465f46dee13/python_multipart-0.0.18-py3-none-any.whl", hash = "sha256:efe91480f485f6a361427a541db4796f9e1591afc0fb8e7a4ba06bfbc6708996", size = 24389 },
]
[[package]]