mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-09 17:31:52 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abcc5b3b04 | ||
|
|
e584f56099 | ||
|
|
b6b01af066 | ||
|
|
9aae3eff86 | ||
|
|
f90582c66b | ||
|
|
67a8d65d8e | ||
|
|
d60c31a86d | ||
|
|
fcb7b2a0d0 | ||
|
|
81ad7db679 | ||
|
|
0482c3ebb0 | ||
|
|
3107458016 | ||
|
|
44c8ceebae | ||
|
|
20c0b02528 | ||
|
|
cfc886b591 | ||
|
|
77ac4b7a09 | ||
|
|
5897cedc45 | ||
|
|
a4d1079813 | ||
|
|
4eed985c4d | ||
|
|
0658491072 | ||
|
|
9f82368b4c |
No files matched your search
@@ -1,45 +1,54 @@
|
||||
name: Daily Publish Docker
|
||||
name: Publish Docker
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
release:
|
||||
types: [published]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
description: 'Docker image tag (default: latest)'
|
||||
required: false
|
||||
default: 'latest'
|
||||
|
||||
jobs:
|
||||
check-and-build:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Check last commit time
|
||||
id: check_commit
|
||||
run: |
|
||||
last_commit_time=$(git log -1 --format=%ct)
|
||||
current_time=$(date +%s)
|
||||
time_diff=$((current_time - last_commit_time))
|
||||
if [ $time_diff -ge 120 ] && [ $time_diff -lt 240 ]; then
|
||||
echo "should_run=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "should_run=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
if: steps.check_commit.outputs.should_run == 'true'
|
||||
uses: docker/setup-buildx-action@v1
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to the GitHub Container Registry
|
||||
if: steps.check_commit.outputs.should_run == 'true'
|
||||
run: echo ${{ secrets.GH_TOKEN }} | docker login ghcr.io -u USERNAME --password-stdin
|
||||
- name: Log in to GitHub Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Extract metadata for Docker
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ghcr.io/mervinpraison/praisonai
|
||||
tags: |
|
||||
type=raw,value=latest,enable=${{ github.event_name == 'release' }}
|
||||
type=semver,pattern={{version}},enable=${{ github.event_name == 'release' }}
|
||||
type=semver,pattern={{major}}.{{minor}},enable=${{ github.event_name == 'release' }}
|
||||
type=raw,value=${{ github.event.inputs.tag || 'latest' }},enable=${{ github.event_name == 'workflow_dispatch' }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
if: steps.check_commit.outputs.should_run == 'true'
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./docker/Dockerfile
|
||||
push: true
|
||||
tags: ghcr.io/mervinpraison/praisonai:latest
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
@@ -30,7 +30,113 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## Key Features
|
||||
---
|
||||
|
||||
## 📑 Table of Contents
|
||||
|
||||
- [🚀 Quick Start](#-quick-start)
|
||||
- [📦 Installation](#-installation)
|
||||
- [Python SDK](#python-sdk)
|
||||
- [JavaScript SDK](#javascript-sdk)
|
||||
- [💻 Usage](#-usage)
|
||||
- [Python Code Examples](#python-code-examples)
|
||||
- [CLI / No-Code Interface](#cli--no-code-interface)
|
||||
- [JavaScript Code Examples](#javascript-code-examples)
|
||||
- [✨ Key Features](#-key-features)
|
||||
- [🌐 Supported Providers](#-supported-providers)
|
||||
- [🛠️ Custom Tools](#️-custom-tools)
|
||||
- [🔧 Configuration & Integration](#-configuration--integration)
|
||||
- [Ollama Integration](#ollama-integration)
|
||||
- [Groq Integration](#groq-integration)
|
||||
- [100+ Models Support](#100-models-support)
|
||||
- [📋 Workflows](#-workflows)
|
||||
- [Workflow Patterns](#workflow-patterns)
|
||||
- [YAML Configuration](#yaml-configuration)
|
||||
- [Agents Playbook](#agents-playbook)
|
||||
- [🧠 Memory & Context](#-memory--context)
|
||||
- [🔬 Advanced Features](#-advanced-features)
|
||||
- [Research & Intelligence](#research--intelligence)
|
||||
- [Planning & Workflows](#planning--workflows)
|
||||
- [Specialised Agents](#specialised-agents)
|
||||
- [MCP Protocol](#mcp-protocol)
|
||||
- [Safety & Control](#safety--control)
|
||||
- [Developer Tools](#developer-tools)
|
||||
- [📊 Process Types & Patterns](#-process-types--patterns)
|
||||
- [Sequential Process](#sequential-process)
|
||||
- [Hierarchical Process](#hierarchical-process)
|
||||
- [Workflow Process](#workflow-process)
|
||||
- [Agentic Patterns](#agentic-patterns)
|
||||
- [🎓 Video Tutorials](#-video-tutorials)
|
||||
- [⭐ Star History](#-star-history)
|
||||
- [👥 Contributing](#-contributing)
|
||||
- [🔧 Development](#-development)
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
Get started with PraisonAI in under 1 minute:
|
||||
|
||||
```bash
|
||||
# Install
|
||||
pip install praisonaiagents
|
||||
|
||||
# Set API key
|
||||
export OPENAI_API_KEY=your_key_here
|
||||
|
||||
# Create a simple agent
|
||||
python -c "from praisonaiagents import Agent; Agent(instructions='You are a helpful AI assistant').start('Write a haiku about AI')"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 Installation
|
||||
|
||||
### Python SDK
|
||||
|
||||
Lightweight package dedicated for coding:
|
||||
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
```
|
||||
|
||||
For the full framework with CLI support:
|
||||
|
||||
```bash
|
||||
pip install praisonai
|
||||
```
|
||||
|
||||
### JavaScript SDK
|
||||
|
||||
```bash
|
||||
npm install praisonai
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 💻 Usage
|
||||
|
||||
### Python Code Examples
|
||||
|
||||
- [📘 Complete Python Examples](#using-python-code)
|
||||
- [🔧 Custom Tools](#️-custom-tools)
|
||||
- [🧠 Memory Configuration](#-memory--context)
|
||||
|
||||
### CLI / No-Code Interface
|
||||
|
||||
- [🎯 Auto Mode](#auto-mode)
|
||||
- [🔬 Deep Research](#deep-research-cli)
|
||||
- [📋 Planning Mode](#planning-mode-cli)
|
||||
- [💾 Memory Management](#memory-cli)
|
||||
- [🔄 Workflow Execution](#workflow-cli)
|
||||
|
||||
### JavaScript Code Examples
|
||||
|
||||
- [💻 JavaScript Usage](#using-javascript-code)
|
||||
|
||||
---
|
||||
|
||||
## ✨ Key Features
|
||||
|
||||
| Feature | Code | Docs |
|
||||
|---------|:----:|:----:|
|
||||
@@ -114,7 +220,11 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
|
||||
## Supported Providers
|
||||
---
|
||||
|
||||
## 🌐 Supported Providers
|
||||
|
||||
PraisonAI supports 100+ LLM providers through seamless integration:
|
||||
|
||||
| Provider | Example |
|
||||
|----------|:-------:|
|
||||
@@ -143,14 +253,14 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
| Moonshot | [Example](examples/python/providers/moonshot/moonshot_example.py) |
|
||||
| vLLM | [Example](examples/python/providers/vllm/vllm_example.py) |
|
||||
|
||||
## Using Python Code
|
||||
---
|
||||
|
||||
## 📘 Using Python Code
|
||||
|
||||
Light weight package dedicated for coding:
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
```
|
||||
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
```
|
||||
|
||||
@@ -605,6 +715,77 @@ result = manager.execute_yaml(
|
||||
)
|
||||
```
|
||||
|
||||
### Complete workflow.yaml Reference
|
||||
|
||||
```yaml
|
||||
# workflow.yaml - Full feature reference
|
||||
name: Complete Workflow
|
||||
description: Demonstrates all workflow.yaml features
|
||||
framework: praisonai # praisonai, crewai, autogen
|
||||
process: workflow # sequential, hierarchical, workflow
|
||||
|
||||
workflow:
|
||||
planning: true
|
||||
planning_llm: gpt-4o
|
||||
reasoning: true
|
||||
verbose: true
|
||||
memory_config:
|
||||
provider: chroma
|
||||
persist: true
|
||||
|
||||
variables:
|
||||
topic: AI trends
|
||||
items: [ML, NLP, Vision]
|
||||
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics thoroughly
|
||||
instructions: "Provide detailed research findings"
|
||||
backstory: "Expert researcher with 10 years experience" # alias for instructions
|
||||
llm: gpt-4o-mini
|
||||
function_calling_llm: gpt-4o # For tool calls
|
||||
max_rpm: 10 # Rate limiting
|
||||
max_execution_time: 300 # Timeout in seconds
|
||||
reflect_llm: gpt-4o # For self-reflection
|
||||
min_reflect: 1
|
||||
max_reflect: 3
|
||||
system_template: "You are a helpful assistant"
|
||||
tools:
|
||||
- tavily_search
|
||||
|
||||
writer:
|
||||
name: Writer
|
||||
role: Content Writer
|
||||
goal: Write clear content
|
||||
instructions: "Write engaging content"
|
||||
|
||||
steps:
|
||||
- name: research_step
|
||||
agent: researcher
|
||||
action: "Research {{topic}}"
|
||||
expected_output: "Comprehensive research report"
|
||||
output_file: "output/research.md"
|
||||
create_directory: true
|
||||
|
||||
- name: writing_step
|
||||
agent: writer
|
||||
action: "Write article based on research"
|
||||
context: # Task dependencies
|
||||
- research_step
|
||||
output_json: # Structured output
|
||||
type: object
|
||||
properties:
|
||||
title: { type: string }
|
||||
content: { type: string }
|
||||
|
||||
callbacks:
|
||||
on_workflow_start: log_start
|
||||
on_step_complete: log_step
|
||||
on_workflow_complete: log_complete
|
||||
```
|
||||
|
||||
### 9. Hooks
|
||||
|
||||
Configure in `.praison/hooks.json`:
|
||||
@@ -621,9 +802,50 @@ hooks.register("pre_write_code", lambda ctx: print(f"Writing {ctx['file']}"))
|
||||
result = hooks.execute("pre_write_code", {"file": "main.py"})
|
||||
```
|
||||
|
||||
### 10. Extended agents.yaml with Workflow Patterns
|
||||
### 10. Field Names Reference (A-I-G-S)
|
||||
|
||||
You can now use advanced workflow patterns directly in agents.yaml by setting `process: workflow`:
|
||||
PraisonAI accepts both old (agents.yaml) and new (workflow.yaml) field names. Use the **canonical names** for new projects:
|
||||
|
||||
| Canonical (Recommended) | Alias (Also Works) | Purpose |
|
||||
|-------------------------|-------------------|---------|
|
||||
| `agents` | `roles` | Define agent personas |
|
||||
| `instructions` | `backstory` | Agent behavior/persona |
|
||||
| `action` | `description` | What the step does |
|
||||
| `steps` | `tasks` (nested) | Define work items |
|
||||
| `name` | `topic` | Workflow identifier |
|
||||
|
||||
**A-I-G-S Mnemonic** - Easy to remember:
|
||||
- **A**gents - Who does the work
|
||||
- **I**nstructions - How they behave
|
||||
- **G**oal - What they achieve
|
||||
- **S**teps - What they do
|
||||
|
||||
```yaml
|
||||
# Quick Reference - Canonical Format
|
||||
name: My Workflow # Workflow name (not 'topic')
|
||||
agents: # Define agents (not 'roles')
|
||||
my_agent:
|
||||
role: Job Title # Agent's role
|
||||
goal: What to achieve # Agent's goal
|
||||
instructions: How to act # Agent's behavior (not 'backstory')
|
||||
|
||||
steps: # Define steps (not 'tasks')
|
||||
- agent: my_agent
|
||||
action: What to do # Step action (not 'description')
|
||||
```
|
||||
|
||||
> **Note:** The parser accepts both old and new names. Run `praisonai workflow validate <file.yaml>` to see suggestions for canonical names.
|
||||
|
||||
### 11. Extended agents.yaml with Workflow Patterns
|
||||
|
||||
**Feature Parity:** Both `agents.yaml` and `workflow.yaml` now support the same features:
|
||||
- All workflow patterns (route, parallel, loop, repeat)
|
||||
- All agent fields (function_calling_llm, max_rpm, max_execution_time, reflect_llm, templates)
|
||||
- All step fields (expected_output, context, output_json, create_directory, callback)
|
||||
- Framework support (praisonai, crewai, autogen)
|
||||
- Process types (sequential, hierarchical, workflow)
|
||||
|
||||
You can use advanced workflow patterns directly in agents.yaml by setting `process: workflow`:
|
||||
|
||||
```yaml
|
||||
# agents.yaml with workflow patterns
|
||||
@@ -639,15 +861,15 @@ workflow:
|
||||
variables:
|
||||
topic: AI trends
|
||||
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
classifier:
|
||||
role: Request Classifier
|
||||
backstory: "Classify requests into categories"
|
||||
instructions: "Classify requests into categories" # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Classify requests
|
||||
|
||||
researcher:
|
||||
role: Research Analyst
|
||||
backstory: "Expert researcher"
|
||||
instructions: "Expert researcher" # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Research topics
|
||||
tools:
|
||||
- tavily_search
|
||||
@@ -690,9 +912,13 @@ Run with the same simple command:
|
||||
praisonai agents.yaml
|
||||
```
|
||||
|
||||
## Using No Code
|
||||
---
|
||||
|
||||
### Auto Mode:
|
||||
## 🎯 CLI / No-Code Interface
|
||||
|
||||
PraisonAI provides a powerful CLI for no-code automation and quick prototyping.
|
||||
|
||||
### Auto Mode
|
||||
```bash
|
||||
pip install praisonai
|
||||
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
@@ -839,10 +1065,43 @@ praisonai "GPT-5" --workflow "Research:Search for info,Write:Write blog" --tools
|
||||
praisonai workflow help
|
||||
```
|
||||
|
||||
#### YAML Workflow Files:
|
||||
```bash
|
||||
# Run a YAML workflow file
|
||||
praisonai workflow run research.yaml
|
||||
|
||||
# Run with variables
|
||||
praisonai workflow run research.yaml --var topic="AI trends"
|
||||
|
||||
# Validate a YAML workflow
|
||||
praisonai workflow validate research.yaml
|
||||
|
||||
# Create from template (simple, routing, parallel, loop, evaluator-optimizer)
|
||||
praisonai workflow template routing --output my_workflow.yaml
|
||||
```
|
||||
|
||||
#### Auto-Generate Workflows:
|
||||
```bash
|
||||
# Auto-generate a sequential workflow from topic
|
||||
praisonai workflow auto "Research AI trends"
|
||||
|
||||
# Generate parallel workflow (multiple agents work concurrently)
|
||||
praisonai workflow auto "Research AI trends" --pattern parallel
|
||||
|
||||
# Generate routing workflow (classifier routes to specialists)
|
||||
praisonai workflow auto "Build a chatbot" --pattern routing
|
||||
|
||||
# Specify output file
|
||||
praisonai workflow auto "Research AI" --pattern sequential --output my_workflow.yaml
|
||||
```
|
||||
|
||||
**Workflow CLI Options:**
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `--workflow-var key=value` | Set workflow variable (can be repeated) |
|
||||
| `--var key=value` | Set variable for YAML workflows |
|
||||
| `--pattern <pattern>` | Pattern for auto-generation (sequential, parallel, routing, loop) |
|
||||
| `--output <file>` | Output file for auto-generation |
|
||||
| `--llm <model>` | LLM model (e.g., openai/gpt-4o-mini) |
|
||||
| `--tools <tools>` | Tools (comma-separated, e.g., tavily) |
|
||||
| `--planning` | Enable planning mode |
|
||||
@@ -1264,6 +1523,21 @@ agent = Agent(
|
||||
env={"BRAVE_API_KEY": "your-key"}
|
||||
)
|
||||
)
|
||||
|
||||
# Multiple MCP servers + regular functions
|
||||
def my_custom_tool(query: str) -> str:
|
||||
"""Custom tool function."""
|
||||
return f"Result: {query}"
|
||||
|
||||
agent = Agent(
|
||||
name="MultiToolAgent",
|
||||
instructions="Agent with multiple MCP servers",
|
||||
tools=[
|
||||
MCP("uvx mcp-server-time"), # Time tools
|
||||
MCP("npx @modelcontextprotocol/server-memory"), # Memory tools
|
||||
my_custom_tool # Regular function
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
### MCP Server (Expose Tools as MCP Server)
|
||||
@@ -1328,7 +1602,9 @@ server.run() # stdio for Claude Desktop
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/cli/prompt-caching) |
|
||||
|
||||
## Using JavaScript Code
|
||||
---
|
||||
|
||||
## 💻 Using JavaScript Code
|
||||
|
||||
```bash
|
||||
npm install praisonai
|
||||
@@ -1343,11 +1619,17 @@ agent.start('Write a movie script about a robot in Mars');
|
||||
|
||||

|
||||
|
||||
## Star History
|
||||
---
|
||||
|
||||
## ⭐ Star History
|
||||
|
||||
[](https://docs.praison.ai)
|
||||
|
||||
## AI Agents Flow
|
||||
---
|
||||
|
||||
## 📊 Process Types & Patterns
|
||||
|
||||
### AI Agents Flow
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
@@ -1686,31 +1968,40 @@ flowchart LR
|
||||
style Out fill:#8B0000,color:#fff
|
||||
```
|
||||
|
||||
## Adding Models
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai/models">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/%F0%9F%93%9A_Models-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Models" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
## 🔧 Configuration & Integration
|
||||
|
||||
### Ollama Integration
|
||||
|
||||
## Ollama Integration
|
||||
```bash
|
||||
export OPENAI_BASE_URL=http://localhost:11434/v1
|
||||
```
|
||||
|
||||
## Groq Integration
|
||||
### Groq Integration
|
||||
|
||||
Replace xxxx with Groq API KEY:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY=xxxxxxxxxxx
|
||||
export OPENAI_BASE_URL=https://api.groq.com/openai/v1
|
||||
```
|
||||
|
||||
## No Code Options
|
||||
### 100+ Models Support
|
||||
|
||||
## Agents Playbook
|
||||
PraisonAI supports 100+ LLM models from various providers. Visit our [models documentation](https://docs.praison.ai/models/) for the complete list.
|
||||
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai/models">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/📚_Models_Documentation-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Models Documentation" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
---
|
||||
|
||||
## 📋 Agents Playbook
|
||||
|
||||
### Simple Playbook Example
|
||||
|
||||
@@ -1719,9 +2010,9 @@ Create `agents.yaml` file and add the code below:
|
||||
```yaml
|
||||
framework: praisonai
|
||||
topic: Artificial Intelligence
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
screenwriter:
|
||||
backstory: "Skilled in crafting scripts with engaging dialogue about {topic}."
|
||||
instructions: "Skilled in crafting scripts with engaging dialogue about {topic}." # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Create scripts from concepts.
|
||||
role: Screenwriter
|
||||
tasks:
|
||||
@@ -1735,18 +2026,11 @@ roles:
|
||||
praisonai agents.yaml
|
||||
```
|
||||
|
||||
## Use 100+ Models
|
||||
---
|
||||
|
||||
- https://docs.praison.ai/models/
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/📚_Documentation-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Documentation" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
## 🛠️ Custom Tools / Create Plugins
|
||||
|
||||
## Custom Tools
|
||||
PraisonAI supports multiple ways to create and integrate custom tools (plugins) into your agents.
|
||||
|
||||
### Using `@tool` Decorator
|
||||
|
||||
@@ -1819,276 +2103,96 @@ After `pip install`, tools are auto-discovered:
|
||||
agent = Agent(tools=["my_tool"]) # Works automatically!
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Prompt Expansion
|
||||
## 🧠 Memory & Context
|
||||
|
||||
Expand short prompts into detailed, actionable prompts:
|
||||
PraisonAI provides zero-dependency persistent memory for agents. For detailed examples, see [section 6. Agent Memory](#6-agent-memory-zero-dependencies) in the Python Code Examples.
|
||||
|
||||
### CLI Usage
|
||||
---
|
||||
|
||||
```bash
|
||||
# Expand a short prompt into detailed prompt
|
||||
praisonai "write a movie script in 3 lines" --expand-prompt
|
||||
## 🔬 Advanced Features
|
||||
|
||||
# With verbose output
|
||||
praisonai "blog about AI" --expand-prompt -v
|
||||
### Research & Intelligence
|
||||
|
||||
# With tools for context gathering
|
||||
praisonai "latest AI trends" --expand-prompt --expand-tools tools.py
|
||||
- 🔬 **Deep Research Agents** - OpenAI & Gemini support for automated research
|
||||
- 🔄 **Query Rewriter Agent** - HyDE, Step-back, Multi-query strategies for RAG optimization
|
||||
- 🌐 **Native Web Search** - Real-time search via OpenAI, Gemini, Anthropic, xAI, Perplexity
|
||||
- 📥 **Web Fetch** - Retrieve full content from URLs (Anthropic)
|
||||
- 📝 **Prompt Expander Agent** - Expand short prompts into detailed instructions
|
||||
|
||||
# Combine with query rewrite
|
||||
praisonai "AI news" --query-rewrite --expand-prompt
|
||||
```
|
||||
### Memory & Caching
|
||||
|
||||
### Programmatic Usage
|
||||
- 💾 **Prompt Caching** - Reduce costs & latency (OpenAI, Anthropic, Bedrock, Deepseek)
|
||||
- 🧠 **Claude Memory Tool** - Persistent cross-conversation memory (Anthropic Beta)
|
||||
- 💾 **File-Based Memory** - Zero-dependency persistent memory for all agents
|
||||
- 🔍 **Built-in Search Tools** - Tavily, You.com, Exa for web search, news, content extraction
|
||||
|
||||
```python
|
||||
from praisonaiagents import PromptExpanderAgent, ExpandStrategy
|
||||
### Planning & Workflows
|
||||
|
||||
# Basic usage
|
||||
agent = PromptExpanderAgent()
|
||||
result = agent.expand("write a movie script in 3 lines")
|
||||
print(result.expanded_prompt)
|
||||
- 📋 **Planning Mode** - Plan before execution for agents & multi-agent systems
|
||||
- 🔧 **Planning Tools** - Research with tools during planning phase
|
||||
- 🧠 **Planning Reasoning** - Chain-of-thought planning for complex tasks
|
||||
- ⛓️ **Prompt Chaining** - Sequential prompt workflows with conditional gates
|
||||
- 🔍 **Evaluator Optimiser** - Generate and optimize through iterative feedback
|
||||
- 👷 **Orchestrator Workers** - Distribute tasks among specialised workers
|
||||
- ⚡ **Parallelisation** - Execute tasks in parallel for improved performance
|
||||
- 🔁 **Repetitive Agents** - Handle repetitive tasks through automated loops
|
||||
- 🤖 **Autonomous Workflow** - Monitor, act, adapt based on environment feedback
|
||||
|
||||
# With specific strategy
|
||||
result = agent.expand("blog about AI", strategy=ExpandStrategy.DETAILED)
|
||||
### Specialised Agents
|
||||
|
||||
# Available strategies: BASIC, DETAILED, STRUCTURED, CREATIVE, AUTO
|
||||
```
|
||||
- 🖼️ **Image Generation Agent** - Create images from text descriptions
|
||||
- 📷 **Image to Text Agent** - Extract text and descriptions from images
|
||||
- 🎬 **Video Agent** - Analyse and process video content
|
||||
- 📊 **Data Analyst Agent** - Analyse data and generate insights
|
||||
- 💰 **Finance Agent** - Financial analysis and recommendations
|
||||
- 🛒 **Shopping Agent** - Price comparison and shopping assistance
|
||||
- ⭐ **Recommendation Agent** - Personalised recommendations
|
||||
- 📖 **Wikipedia Agent** - Search and extract Wikipedia information
|
||||
- 💻 **Programming Agent** - Code development and analysis
|
||||
- 📝 **Markdown Agent** - Generate and format Markdown content
|
||||
- 🔀 **Router Agent** - Dynamic task routing with cost optimisation
|
||||
|
||||
**Key Difference:**
|
||||
- `--query-rewrite`: Optimizes queries for **search/retrieval** (RAG)
|
||||
- `--expand-prompt`: Expands prompts for **detailed task execution**
|
||||
### MCP Protocol
|
||||
|
||||
- 🔌 **MCP Transports** - stdio, Streamable HTTP, WebSocket, SSE (Protocol 2025-11-25)
|
||||
- 🌐 **WebSocket MCP** - Real-time bidirectional connections with auto-reconnect
|
||||
- 🔐 **MCP Security** - Origin validation, DNS rebinding prevention, secure sessions
|
||||
- 🔄 **MCP Resumability** - SSE stream recovery via Last-Event-ID
|
||||
|
||||
## Web Search, Web Fetch & Prompt Caching
|
||||
### Safety & Control
|
||||
|
||||
### CLI Usage
|
||||
- 🤝 **Agent Handoffs** - Transfer context between specialised agents
|
||||
- 🛡️ **Guardrails** - Input/output validation and safety checks
|
||||
- ✅ **Human Approval** - Require human confirmation for critical actions
|
||||
- 💬 **Sessions Management** - Isolated conversation contexts
|
||||
- 🔄 **Stateful Agents** - Maintain state across interactions
|
||||
|
||||
```bash
|
||||
# Web Search - Get real-time information
|
||||
praisonai "What are the latest AI news today?" --web-search --llm openai/gpt-4o-search-preview
|
||||
### Developer Tools
|
||||
|
||||
# Web Fetch - Retrieve and analyze URL content (Anthropic only)
|
||||
praisonai "Summarize https://docs.praison.ai" --web-fetch --llm anthropic/claude-sonnet-4-20250514
|
||||
- ⚡ **Fast Context** - Rapid parallel code search (10-20x faster)
|
||||
- 📜 **Rules & Instructions** - Auto-discover CLAUDE.md, AGENTS.md, GEMINI.md
|
||||
- 🪝 **Hooks** - Pre/post operation hooks for custom logic
|
||||
- 📈 **Telemetry** - Track agent performance and usage
|
||||
- 📹 **Camera Integration** - Capture and analyse camera input
|
||||
|
||||
# Prompt Caching - Reduce costs for repeated prompts
|
||||
praisonai "Analyze this document..." --prompt-caching --llm anthropic/claude-sonnet-4-20250514
|
||||
```
|
||||
### Other Features
|
||||
|
||||
### Programmatic Usage
|
||||
- 🔄 **CrewAI & AG2 Integration** - Use CrewAI or AG2 (Formerly AutoGen) Framework
|
||||
- 💻 **Codebase Chat** - Chat with entire codebase
|
||||
- 🎨 **Interactive UIs** - Multiple interactive interfaces
|
||||
- 📄 **YAML Configuration** - YAML-based agent and workflow configuration
|
||||
- 🛠️ **Custom Tools** - Easy custom tool integration
|
||||
- 🔍 **Internet Search** - Multiple providers (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
|
||||
- 🖼️ **VLM Support** - Vision Language Model support
|
||||
- 🎙️ **Voice Interaction** - Real-time voice interaction
|
||||
|
||||
```python
|
||||
from praisonaiagents import Agent
|
||||
---
|
||||
|
||||
# Web Search
|
||||
agent = Agent(
|
||||
instructions="You are a research assistant",
|
||||
llm="openai/gpt-4o-search-preview",
|
||||
web_search=True
|
||||
)
|
||||
## 🎓 Video Tutorials
|
||||
|
||||
# Web Fetch (Anthropic only)
|
||||
agent = Agent(
|
||||
instructions="You are a content analyzer",
|
||||
llm="anthropic/claude-sonnet-4-20250514",
|
||||
web_fetch=True
|
||||
)
|
||||
|
||||
# Prompt Caching
|
||||
agent = Agent(
|
||||
instructions="You are an AI assistant..." * 50, # Long system prompt
|
||||
llm="anthropic/claude-sonnet-4-20250514",
|
||||
prompt_caching=True
|
||||
)
|
||||
```
|
||||
|
||||
**Supported Providers:**
|
||||
| Feature | Providers |
|
||||
|---------|-----------|
|
||||
| Web Search | OpenAI, Gemini, Anthropic, xAI, Perplexity |
|
||||
| Web Fetch | Anthropic |
|
||||
| Prompt Caching | OpenAI (auto), Anthropic, Bedrock, Deepseek |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## MCP (Model Context Protocol)
|
||||
|
||||
PraisonAI supports MCP Protocol Revision 2025-11-25 with multiple transports.
|
||||
|
||||
### MCP Client (Consume MCP Servers)
|
||||
|
||||
```python
|
||||
from praisonaiagents import Agent, MCP
|
||||
|
||||
# stdio - Local NPX/Python servers
|
||||
agent = Agent(tools=MCP("npx @modelcontextprotocol/server-memory"))
|
||||
|
||||
# Streamable HTTP - Production servers
|
||||
agent = Agent(tools=MCP("https://api.example.com/mcp"))
|
||||
|
||||
# WebSocket - Real-time bidirectional
|
||||
agent = Agent(tools=MCP("wss://api.example.com/mcp", auth_token="token"))
|
||||
|
||||
# SSE (Legacy) - Backward compatibility
|
||||
agent = Agent(tools=MCP("http://localhost:8080/sse"))
|
||||
|
||||
# With environment variables
|
||||
agent = Agent(
|
||||
tools=MCP(
|
||||
command="npx",
|
||||
args=["-y", "@modelcontextprotocol/server-brave-search"],
|
||||
env={"BRAVE_API_KEY": "your-key"}
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### MCP Server (Expose Tools as MCP Server)
|
||||
|
||||
Expose your Python functions as MCP tools for Claude Desktop, Cursor, and other MCP clients:
|
||||
|
||||
```python
|
||||
from praisonaiagents.mcp import ToolsMCPServer
|
||||
|
||||
def search_web(query: str, max_results: int = 5) -> dict:
|
||||
"""Search the web for information."""
|
||||
return {"results": [f"Result for {query}"]}
|
||||
|
||||
def calculate(expression: str) -> dict:
|
||||
"""Evaluate a mathematical expression."""
|
||||
return {"result": eval(expression)}
|
||||
|
||||
# Create and run MCP server
|
||||
server = ToolsMCPServer(name="my-tools")
|
||||
server.register_tools([search_web, calculate])
|
||||
server.run() # stdio for Claude Desktop
|
||||
# server.run_sse(host="0.0.0.0", port=8080) # SSE for web clients
|
||||
```
|
||||
|
||||
### MCP Features
|
||||
|
||||
| Feature | Description |
|
||||
|---------|-------------|
|
||||
| **Session Management** | Automatic `Mcp-Session-Id` handling |
|
||||
| **Protocol Versioning** | `Mcp-Protocol-Version` header |
|
||||
| **Resumability** | SSE stream recovery via `Last-Event-ID` |
|
||||
| **Security** | Origin validation, DNS rebinding prevention |
|
||||
| **WebSocket** | Auto-reconnect with exponential backoff |
|
||||
|
||||
|
||||
## Development:
|
||||
|
||||
Below is used for development only.
|
||||
|
||||
### Using uv
|
||||
```bash
|
||||
# Install uv if you haven't already
|
||||
pip install uv
|
||||
|
||||
# Install from requirements
|
||||
uv pip install -r pyproject.toml
|
||||
|
||||
# Install with extras
|
||||
uv pip install -r pyproject.toml --extra code
|
||||
uv pip install -r pyproject.toml --extra "crewai,autogen"
|
||||
```
|
||||
|
||||
### Bump and Release
|
||||
|
||||
```bash
|
||||
# From project root - bumps version and releases in one command
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99
|
||||
|
||||
# With praisonaiagents dependency
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99 --agents 0.0.169
|
||||
|
||||
# Then publish
|
||||
cd src/praisonai && uv publish
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
- Fork on GitHub: Use the "Fork" button on the repository page.
|
||||
- Clone your fork: `git clone https://github.com/yourusername/praisonAI.git`
|
||||
- Create a branch: `git checkout -b new-feature`
|
||||
- Make changes and commit: `git commit -am "Add some feature"`
|
||||
- Push to your fork: `git push origin new-feature`
|
||||
- Submit a pull request via GitHub's web interface.
|
||||
- Await feedback from project maintainers.
|
||||
|
||||
## Advanced Features
|
||||
|
||||
**Research & Intelligence:**
|
||||
- 🔬 **Deep Research Agents** (OpenAI & Gemini)
|
||||
- 🔄 **Query Rewriter Agent** (HyDE, Step-back, Multi-query)
|
||||
- 🌐 **Native Web Search** (OpenAI, Gemini, Anthropic, xAI, Perplexity)
|
||||
- 📥 **Web Fetch** (Retrieve full content from URLs - Anthropic)
|
||||
- 📝 **Prompt Expander Agent** (Expand short prompts into detailed instructions)
|
||||
|
||||
**Memory & Caching:**
|
||||
- 💾 **Prompt Caching** (Reduce costs & latency - OpenAI, Anthropic, Bedrock, Deepseek)
|
||||
- 🧠 **Claude Memory Tool** (Persistent cross-conversation memory - Anthropic Beta)
|
||||
- 💾 **File-Based Memory** (Zero-dependency persistent memory for all agents)
|
||||
- 🔍 **Built-in Search Tools** (Tavily, You.com, Exa - web search, news, content extraction)
|
||||
|
||||
**Planning & Workflows:**
|
||||
- 📋 **Planning Mode** (Plan before execution - Agent & Multi-Agent)
|
||||
- 🔧 **Planning Tools** (Research with tools during planning)
|
||||
- 🧠 **Planning Reasoning** (Chain-of-thought planning)
|
||||
- ⛓️ **Prompt Chaining** (Sequential prompt workflows with gates)
|
||||
- 🔍 **Evaluator Optimiser** (Generate and optimize through iterative feedback)
|
||||
- 👷 **Orchestrator Workers** (Distribute tasks among specialized workers)
|
||||
- ⚡ **Parallelisation** (Execute tasks in parallel for improved performance)
|
||||
- 🔁 **Repetitive Agents** (Handle repetitive tasks through automated loops)
|
||||
- 🤖 **Autonomous Workflow** (Monitor, act, adapt based on environment feedback)
|
||||
|
||||
**Agent Types:**
|
||||
- 🖼️ **Image Generation Agent** (Create images from text descriptions)
|
||||
- 📷 **Image to Text Agent** (Extract text and descriptions from images)
|
||||
- 🎬 **Video Agent** (Analyze and process video content)
|
||||
- 📊 **Data Analyst Agent** (Analyze data and generate insights)
|
||||
- 💰 **Finance Agent** (Financial analysis and recommendations)
|
||||
- 🛒 **Shopping Agent** (Price comparison and shopping assistance)
|
||||
- ⭐ **Recommendation Agent** (Personalized recommendations)
|
||||
- 📖 **Wikipedia Agent** (Search and extract Wikipedia information)
|
||||
- 💻 **Programming Agent** (Code development and analysis)
|
||||
- 📝 **Markdown Agent** (Generate and format Markdown content)
|
||||
- 🔀 **Router Agent** (Dynamic task routing with cost optimization)
|
||||
|
||||
**MCP Protocol:**
|
||||
- 🔌 **MCP Transports** (stdio, Streamable HTTP, WebSocket, SSE - Protocol 2025-11-25)
|
||||
- 🌐 **WebSocket MCP** (Real-time bidirectional connections with auto-reconnect)
|
||||
- 🔐 **MCP Security** (Origin validation, DNS rebinding prevention, secure sessions)
|
||||
- 🔄 **MCP Resumability** (SSE stream recovery via Last-Event-ID)
|
||||
|
||||
**Safety & Control:**
|
||||
- 🤝 **Agent Handoffs** (Transfer context between specialized agents)
|
||||
- 🛡️ **Guardrails** (Input/output validation and safety checks)
|
||||
- ✅ **Human Approval** (Require human confirmation for critical actions)
|
||||
- 💬 **Sessions Management** (Isolated conversation contexts)
|
||||
- 🔄 **Stateful Agents** (Maintain state across interactions)
|
||||
|
||||
**Developer Tools:**
|
||||
- ⚡ **Fast Context** (Rapid parallel code search - 10-20x faster than traditional methods)
|
||||
- 📜 **Rules & Instructions** (Auto-discover CLAUDE.md, AGENTS.md, GEMINI.md)
|
||||
- 🪝 **Hooks** (Pre/post operation hooks for custom logic)
|
||||
- 📈 **Telemetry** (Track agent performance and usage)
|
||||
- 📹 **Camera Integration** (Capture and analyze camera input)
|
||||
|
||||
## Other Features
|
||||
|
||||
- 🔄 Use CrewAI or AG2 (Formerly AutoGen) Framework
|
||||
- 💻 Chat with ENTIRE Codebase
|
||||
- 🎨 Interactive UIs
|
||||
- 📄 YAML-based Configuration
|
||||
- 🛠️ Custom Tool Integration
|
||||
- 🔍 Internet Search Capability (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
|
||||
- 🖼️ Vision Language Model (VLM) Support
|
||||
- 🎙️ Real-time Voice Interaction
|
||||
|
||||
## Video Tutorials
|
||||
Learn PraisonAI through our comprehensive video series:
|
||||
|
||||
| Topic | Video |
|
||||
|-------|--------|
|
||||
@@ -2115,3 +2219,58 @@ cd src/praisonai && uv publish
|
||||
| Call Interface | [](https://www.youtube.com/watch?v=m1cwrUG2iAk) |
|
||||
| Reasoning Extract Agents | [](https://www.youtube.com/watch?v=2PPamsADjJA) |
|
||||
|
||||
---
|
||||
|
||||
## 👥 Contributing
|
||||
|
||||
We welcome contributions from the community! Here's how you can contribute:
|
||||
|
||||
1. **Fork on GitHub** - Use the "Fork" button on the [repository page](https://github.com/MervinPraison/PraisonAI)
|
||||
2. **Clone your fork** - `git clone https://github.com/yourusername/praisonAI.git`
|
||||
3. **Create a branch** - `git checkout -b new-feature`
|
||||
4. **Make changes and commit** - `git commit -am "Add some feature"`
|
||||
5. **Push to your fork** - `git push origin new-feature`
|
||||
6. **Submit a pull request** - Via GitHub's web interface
|
||||
7. **Await feedback** - From project maintainers
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Development
|
||||
|
||||
### Using uv
|
||||
|
||||
```bash
|
||||
# Install uv if you haven't already
|
||||
pip install uv
|
||||
|
||||
# Install from requirements
|
||||
uv pip install -r pyproject.toml
|
||||
|
||||
# Install with extras
|
||||
uv pip install -r pyproject.toml --extra code
|
||||
uv pip install -r pyproject.toml --extra "crewai,autogen"
|
||||
```
|
||||
|
||||
### Bump and Release
|
||||
|
||||
```bash
|
||||
# From project root - bumps version and releases in one command
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99
|
||||
|
||||
# With praisonaiagents dependency
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99 --agents 0.0.169
|
||||
|
||||
# Then publish
|
||||
cd src/praisonai && uv publish
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
<p><strong>Made with ❤️ by the PraisonAI Team</strong></p>
|
||||
<p>
|
||||
<a href="https://docs.praison.ai">Documentation</a> •
|
||||
<a href="https://github.com/MervinPraison/PraisonAI">GitHub</a> •
|
||||
<a href="https://github.com/MervinPraison/PraisonAI/issues">Issues</a>
|
||||
</p>
|
||||
</div>
|
||||
+2
-1
@@ -14,9 +14,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
RUN mkdir -p /root/.praison
|
||||
|
||||
# Install Python packages (using latest versions)
|
||||
# Note: praisonai is installed from PyPI - remove version pin to use latest available
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
"praisonai>=2.3.26" \
|
||||
praisonai \
|
||||
"praisonai[api]" \
|
||||
gunicorn \
|
||||
markdown
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.26" \
|
||||
"praisonai>=2.3.31" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.26" \
|
||||
"praisonai>=2.3.31" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.3.26" \
|
||||
"praisonai>=2.3.31" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
framework: crewai
|
||||
topic: "Technology Research and Content Creation using AWS Bedrock"
|
||||
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
research_specialist:
|
||||
role: "Research Specialist"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are an experienced technology researcher with deep expertise in analyzing
|
||||
emerging technologies and market trends. You excel at gathering comprehensive
|
||||
information from various sources and synthesizing it into actionable insights.
|
||||
@@ -35,7 +35,7 @@ roles:
|
||||
|
||||
content_creator:
|
||||
role: "Technical Content Creator"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a skilled technical writer and content creator who specializes in
|
||||
translating complex technology concepts into engaging, accessible content
|
||||
for various audiences including developers, business leaders, and end users.
|
||||
@@ -64,7 +64,7 @@ roles:
|
||||
|
||||
quality_reviewer:
|
||||
role: "Content Quality Reviewer"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a meticulous editor and quality assurance specialist with extensive
|
||||
experience in technical content review. You ensure accuracy, clarity, and
|
||||
consistency while maintaining the intended tone and message.
|
||||
|
||||
@@ -8,10 +8,10 @@ variables:
|
||||
industry: "Electric Vehicles"
|
||||
research_focus: "comprehensive market analysis"
|
||||
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
market_overview_specialist:
|
||||
role: "Market Overview Specialist"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a seasoned market research analyst with 10+ years of experience
|
||||
in analyzing market dynamics, trends, and growth patterns across various
|
||||
industries. You excel at synthesizing complex market data into clear,
|
||||
@@ -64,7 +64,7 @@ roles:
|
||||
|
||||
competitive_intelligence_analyst:
|
||||
role: "Competitive Intelligence Analyst"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a competitive intelligence expert specializing in analyzing
|
||||
competitive landscapes, market positioning, and strategic intelligence.
|
||||
You have deep expertise in competitor analysis, market share assessment,
|
||||
@@ -119,7 +119,7 @@ roles:
|
||||
|
||||
financial_performance_analyst:
|
||||
role: "Financial Performance Analyst"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a financial analyst with expertise in corporate finance,
|
||||
financial statement analysis, and industry benchmarking. You specialize
|
||||
in analyzing financial performance metrics, trends, and comparative
|
||||
@@ -173,7 +173,7 @@ roles:
|
||||
|
||||
growth_opportunities_researcher:
|
||||
role: "Growth Opportunities Researcher"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a strategic business analyst specializing in identifying growth
|
||||
opportunities, market expansion strategies, and emerging business models.
|
||||
You excel at spotting trends, gaps, and untapped potential in markets
|
||||
@@ -228,7 +228,7 @@ roles:
|
||||
|
||||
risk_assessment_specialist:
|
||||
role: "Risk Assessment Specialist"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a risk management expert with extensive experience in identifying,
|
||||
analyzing, and assessing business risks across various industries. You
|
||||
specialize in market risks, competitive threats, regulatory challenges,
|
||||
@@ -287,7 +287,7 @@ roles:
|
||||
|
||||
research_report_synthesizer:
|
||||
role: "Research Report Synthesizer"
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are an expert business report writer and analyst with extensive
|
||||
experience in synthesizing complex research findings into clear,
|
||||
actionable business intelligence reports. You excel at creating
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
"""
|
||||
Multiple MCP Servers Example
|
||||
|
||||
This example demonstrates how to use multiple MCP servers with a single agent.
|
||||
You can combine different MCP servers (time, memory, filesystem, etc.) along
|
||||
with regular Python functions as tools.
|
||||
|
||||
Requirements:
|
||||
- pip install praisonaiagents
|
||||
- uvx (for mcp-server-time)
|
||||
- npx (for @modelcontextprotocol servers)
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.mcp import MCP
|
||||
|
||||
|
||||
def my_custom_tool(query: str) -> str:
|
||||
"""
|
||||
A custom Python function that can be used alongside MCP tools.
|
||||
|
||||
Args:
|
||||
query: The search query
|
||||
|
||||
Returns:
|
||||
A formatted result string
|
||||
"""
|
||||
return f"Custom tool result for: {query}"
|
||||
|
||||
|
||||
def main():
|
||||
# Example 1: Single MCP server (basic usage)
|
||||
print("=" * 60)
|
||||
print("Example 1: Single MCP Server")
|
||||
print("=" * 60)
|
||||
|
||||
agent_single = Agent(
|
||||
name="TimeAgent",
|
||||
instructions="You are a helpful assistant that can tell time.",
|
||||
tools=MCP("uvx mcp-server-time")
|
||||
)
|
||||
|
||||
response = agent_single.start("What time is it in Tokyo?")
|
||||
print(f"Response: {response}\n")
|
||||
|
||||
# Example 2: Multiple MCP servers
|
||||
print("=" * 60)
|
||||
print("Example 2: Multiple MCP Servers")
|
||||
print("=" * 60)
|
||||
|
||||
agent_multi = Agent(
|
||||
name="MultiMCPAgent",
|
||||
instructions="""You are a helpful assistant with access to multiple tools:
|
||||
- Time tools for getting current time and converting timezones
|
||||
- Memory tools for storing and retrieving information
|
||||
Use the appropriate tool based on the user's request.""",
|
||||
tools=[
|
||||
MCP("uvx mcp-server-time"), # Time tools
|
||||
MCP("npx @modelcontextprotocol/server-memory"), # Memory tools
|
||||
]
|
||||
)
|
||||
|
||||
response = agent_multi.start("What time is it in New York and London?")
|
||||
print(f"Response: {response}\n")
|
||||
|
||||
# Example 3: Multiple MCP servers + custom functions
|
||||
print("=" * 60)
|
||||
print("Example 3: MCP Servers + Custom Functions")
|
||||
print("=" * 60)
|
||||
|
||||
agent_mixed = Agent(
|
||||
name="MixedToolsAgent",
|
||||
instructions="""You are a helpful assistant with access to:
|
||||
- Time tools for timezone operations
|
||||
- A custom search tool for general queries
|
||||
Choose the right tool for each task.""",
|
||||
tools=[
|
||||
MCP("uvx mcp-server-time"), # MCP server
|
||||
my_custom_tool # Regular Python function
|
||||
]
|
||||
)
|
||||
|
||||
response = agent_mixed.start("What time is it in Paris?")
|
||||
print(f"Response: {response}\n")
|
||||
|
||||
# Example 4: MCP with environment variables
|
||||
print("=" * 60)
|
||||
print("Example 4: MCP with Environment Variables")
|
||||
print("=" * 60)
|
||||
|
||||
# Note: Replace with your actual API key
|
||||
# agent_with_env = Agent(
|
||||
# name="SearchAgent",
|
||||
# instructions="You can search the web for information.",
|
||||
# tools=[
|
||||
# MCP(
|
||||
# command="npx",
|
||||
# args=["-y", "@modelcontextprotocol/server-brave-search"],
|
||||
# env={"BRAVE_API_KEY": "your-api-key-here"}
|
||||
# ),
|
||||
# MCP("uvx mcp-server-time")
|
||||
# ]
|
||||
# )
|
||||
print("(Skipped - requires BRAVE_API_KEY)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,8 +1,8 @@
|
||||
framework: praisonai
|
||||
topic: crawl and extract all data from each url from this page https://news.ycombinator.com/
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
web_scraper:
|
||||
backstory: An expert in data extraction from websites, adept at navigating and
|
||||
instructions: # Canonical An expert in data extraction from websites, adept at navigating and
|
||||
retrieving detailed information.
|
||||
goal: Extract URLs from the source page
|
||||
role: Web Scraper
|
||||
@@ -15,7 +15,7 @@ roles:
|
||||
tools:
|
||||
- 'code_interpret'
|
||||
url_data_extractor:
|
||||
backstory: Specializes in crawling websites to gather comprehensive data, ensuring
|
||||
instructions: # Canonical Specializes in crawling websites to gather comprehensive data, ensuring
|
||||
nothing is missed from each link.
|
||||
goal: Crawl each URL for data extraction
|
||||
role: URL Data Extractor
|
||||
@@ -29,7 +29,7 @@ roles:
|
||||
tools:
|
||||
- 'code_interpret'
|
||||
data_organizer:
|
||||
backstory: Known for keen attention to detail, capable of structuring vast amounts
|
||||
instructions: # Canonical Known for keen attention to detail, capable of structuring vast amounts
|
||||
of information into usable formats efficiently.
|
||||
goal: Organize extracted data into a structured format
|
||||
role: Data Organizer
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
framework: praisonai
|
||||
process: workflow
|
||||
topic: "Research AI trends and create a comprehensive report"
|
||||
input: "Research AI trends and create a comprehensive report" # The input passed to {{input}} in steps
|
||||
|
||||
# Workflow configuration (new)
|
||||
workflow:
|
||||
@@ -31,17 +31,17 @@ variables:
|
||||
- Natural Language Processing
|
||||
|
||||
# Agent definitions using familiar 'roles' format
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
classifier:
|
||||
role: Request Classifier
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are an expert at classifying requests into categories.
|
||||
Respond with ONLY one word: 'technical', 'creative', or 'research'.
|
||||
goal: Classify incoming requests accurately
|
||||
|
||||
researcher:
|
||||
role: Research Analyst
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a skilled research analyst with expertise in technology trends.
|
||||
You provide concise, factual research findings with citations.
|
||||
goal: Research topics thoroughly and provide insights
|
||||
@@ -52,28 +52,28 @@ roles:
|
||||
|
||||
tech_expert:
|
||||
role: Technical Expert
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a technical expert with deep knowledge in AI and ML.
|
||||
You provide detailed technical explanations.
|
||||
goal: Handle technical questions with expertise
|
||||
|
||||
creative_writer:
|
||||
role: Creative Writer
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You are a creative writer who can make complex topics engaging.
|
||||
You write in a clear, accessible style.
|
||||
goal: Create engaging content from research
|
||||
|
||||
market_analyst:
|
||||
role: Market Analyst
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You analyze market trends and competitive landscapes.
|
||||
You provide actionable business insights.
|
||||
goal: Analyze market trends and opportunities
|
||||
|
||||
aggregator:
|
||||
role: Synthesizer
|
||||
backstory: |
|
||||
instructions: # Canonical |
|
||||
You combine findings from multiple sources into coherent reports.
|
||||
You identify patterns and key insights.
|
||||
goal: Synthesize all findings into comprehensive reports
|
||||
|
||||
@@ -39,4 +39,4 @@ steps:
|
||||
over: topics
|
||||
|
||||
- agent: summarizer
|
||||
action: "Create a comprehensive summary of all the analyzed topics"
|
||||
action: "Create a comprehensive summary of all the analyzed topics based on these results: {{previous_output}}"
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
name: Parallel Research Workflow
|
||||
description: Multiple agents research different aspects concurrently, then an aggregator combines results
|
||||
input: "AI-powered customer service solutions" # The input passed to {{input}} in steps
|
||||
|
||||
workflow:
|
||||
verbose: true
|
||||
@@ -43,4 +44,4 @@ steps:
|
||||
action: "Research customer needs for: {{input}}"
|
||||
|
||||
- agent: aggregator
|
||||
action: "Synthesize all the research findings into a comprehensive report"
|
||||
action: "Synthesize all the research findings into a comprehensive report: {{previous_output}}"
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
name: Agentic Routing Workflow
|
||||
description: A classifier agent routes requests to specialized handler agents
|
||||
input: "Write a poem about the ocean" # The input passed to {{input}} in steps
|
||||
|
||||
workflow:
|
||||
verbose: true
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
name: Simple Research Workflow
|
||||
description: A simple workflow where a researcher gathers info and a writer summarizes it
|
||||
input: "The benefits of renewable energy" # The input passed to {{input}} in steps
|
||||
|
||||
workflow:
|
||||
verbose: true
|
||||
|
||||
@@ -87,3 +87,4 @@ graph.py
|
||||
chroma_db/
|
||||
.qodo
|
||||
*_COMPARISON_ANALYSIS.md
|
||||
*_REPORT.md
|
||||
File diff suppressed because it is too large.
Load diff
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../README.md
|
||||
@@ -0,0 +1,363 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
PraisonAI Agents - Deep Profiling Benchmark
|
||||
|
||||
Profiles each step of agent initialization to identify performance bottlenecks.
|
||||
|
||||
Usage:
|
||||
python benchmarks/deep_profiling.py
|
||||
"""
|
||||
|
||||
import time
|
||||
import tracemalloc
|
||||
import cProfile
|
||||
import pstats
|
||||
import io
|
||||
from typing import Literal, Callable, Dict, List
|
||||
from dataclasses import dataclass
|
||||
|
||||
# ============================================================================
|
||||
# Timing Utilities
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class StepTiming:
|
||||
name: str
|
||||
duration_us: float # microseconds
|
||||
memory_kb: float # kilobytes
|
||||
|
||||
|
||||
class StepProfiler:
|
||||
"""Profile individual steps during initialization"""
|
||||
|
||||
def __init__(self):
|
||||
self.steps: List[StepTiming] = []
|
||||
self._start_time = None
|
||||
self._start_memory = None
|
||||
|
||||
def start_step(self, name: str):
|
||||
tracemalloc.start()
|
||||
self._start_time = time.perf_counter()
|
||||
self._step_name = name
|
||||
|
||||
def end_step(self):
|
||||
end_time = time.perf_counter()
|
||||
current, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
|
||||
duration_us = (end_time - self._start_time) * 1_000_000
|
||||
memory_kb = peak / 1024
|
||||
|
||||
self.steps.append(StepTiming(
|
||||
name=self._step_name,
|
||||
duration_us=duration_us,
|
||||
memory_kb=memory_kb
|
||||
))
|
||||
|
||||
def print_report(self, title: str):
|
||||
print(f"\n{'='*70}")
|
||||
print(f"STEP-BY-STEP PROFILING: {title}")
|
||||
print(f"{'='*70}")
|
||||
|
||||
total_time = sum(s.duration_us for s in self.steps)
|
||||
total_memory = sum(s.memory_kb for s in self.steps)
|
||||
|
||||
print(f"\n{'Step':<40} {'Time (μs)':<15} {'Memory (KB)':<15} {'% Time':<10}")
|
||||
print("-" * 80)
|
||||
|
||||
for step in sorted(self.steps, key=lambda x: x.duration_us, reverse=True):
|
||||
pct = (step.duration_us / total_time * 100) if total_time > 0 else 0
|
||||
print(f"{step.name:<40} {step.duration_us:<15.2f} {step.memory_kb:<15.2f} {pct:<10.1f}%")
|
||||
|
||||
print("-" * 80)
|
||||
print(f"{'TOTAL':<40} {total_time:<15.2f} {total_memory:<15.2f} {'100.0':<10}%")
|
||||
|
||||
|
||||
def time_function(func: Callable, iterations: int = 100) -> Dict:
|
||||
"""Time a function with detailed statistics"""
|
||||
times = []
|
||||
|
||||
# Warmup
|
||||
for _ in range(10):
|
||||
func()
|
||||
|
||||
# Measure
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
func()
|
||||
end = time.perf_counter()
|
||||
times.append((end - start) * 1_000_000) # Convert to microseconds
|
||||
|
||||
return {
|
||||
'avg': sum(times) / len(times),
|
||||
'min': min(times),
|
||||
'max': max(times),
|
||||
'median': sorted(times)[len(times)//2]
|
||||
}
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Profile PraisonAI Agent Initialization - Step by Step
|
||||
# ============================================================================
|
||||
|
||||
def profile_praisonai_detailed():
|
||||
"""Profile each step of PraisonAI Agent initialization"""
|
||||
print("\n" + "="*70)
|
||||
print("DETAILED PROFILING: PraisonAI Agent")
|
||||
print("="*70)
|
||||
|
||||
profiler = StepProfiler()
|
||||
|
||||
# Step 1: Import
|
||||
profiler.start_step("1. Import praisonaiagents")
|
||||
from praisonaiagents import Agent as PraisonAgent
|
||||
profiler.end_step()
|
||||
|
||||
# Step 2: Define tool
|
||||
profiler.start_step("2. Define tool function")
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
tools = [get_weather]
|
||||
profiler.end_step()
|
||||
|
||||
# Step 3: Full instantiation
|
||||
profiler.start_step("3. Full Agent instantiation")
|
||||
agent = PraisonAgent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=tools,
|
||||
verbose=False
|
||||
)
|
||||
profiler.end_step()
|
||||
|
||||
profiler.print_report("PraisonAI Agent")
|
||||
|
||||
# Now profile the __init__ internals using cProfile
|
||||
print("\n" + "="*70)
|
||||
print("cProfile ANALYSIS: PraisonAI Agent.__init__")
|
||||
print("="*70)
|
||||
|
||||
pr = cProfile.Profile()
|
||||
pr.enable()
|
||||
|
||||
for _ in range(100):
|
||||
agent = PraisonAgent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=tools,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
pr.disable()
|
||||
|
||||
# Print top 30 functions by cumulative time
|
||||
s = io.StringIO()
|
||||
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
|
||||
ps.print_stats(30)
|
||||
print(s.getvalue())
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Profile Agno Agent Initialization - Step by Step
|
||||
# ============================================================================
|
||||
|
||||
def profile_agno_detailed():
|
||||
"""Profile each step of Agno Agent initialization"""
|
||||
print("\n" + "="*70)
|
||||
print("DETAILED PROFILING: Agno Agent")
|
||||
print("="*70)
|
||||
|
||||
profiler = StepProfiler()
|
||||
|
||||
# Step 1: Import
|
||||
profiler.start_step("1. Import agno")
|
||||
from agno.agent import Agent as AgnoAgent
|
||||
from agno.models.openai import OpenAIChat
|
||||
profiler.end_step()
|
||||
|
||||
# Step 2: Define tool
|
||||
profiler.start_step("2. Define tool function")
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
tools = [get_weather]
|
||||
profiler.end_step()
|
||||
|
||||
# Step 3: Full instantiation
|
||||
profiler.start_step("3. Full Agent instantiation")
|
||||
agent = AgnoAgent(model=OpenAIChat(id="gpt-4o-mini"), tools=tools)
|
||||
profiler.end_step()
|
||||
|
||||
profiler.print_report("Agno Agent")
|
||||
|
||||
# Now profile the __init__ internals using cProfile
|
||||
print("\n" + "="*70)
|
||||
print("cProfile ANALYSIS: Agno Agent.__init__")
|
||||
print("="*70)
|
||||
|
||||
pr = cProfile.Profile()
|
||||
pr.enable()
|
||||
|
||||
for _ in range(100):
|
||||
agent = AgnoAgent(model=OpenAIChat(id="gpt-4o-mini"), tools=tools)
|
||||
|
||||
pr.disable()
|
||||
|
||||
# Print top 30 functions by cumulative time
|
||||
s = io.StringIO()
|
||||
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
|
||||
ps.print_stats(30)
|
||||
print(s.getvalue())
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Profile Individual Components of PraisonAI
|
||||
# ============================================================================
|
||||
|
||||
def profile_praisonai_components():
|
||||
"""Profile individual components of PraisonAI Agent initialization"""
|
||||
print("\n" + "="*70)
|
||||
print("COMPONENT-LEVEL PROFILING: PraisonAI Agent")
|
||||
print("="*70)
|
||||
|
||||
from praisonaiagents import Agent as PraisonAgent
|
||||
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
tools = [get_weather]
|
||||
|
||||
# Test different configurations
|
||||
configs = [
|
||||
("Minimal (no tools, no llm)", {"name": "Test", "verbose": False}),
|
||||
("With name only", {"name": "Test Agent", "verbose": False}),
|
||||
("With instructions", {"name": "Test", "instructions": "Be concise", "verbose": False}),
|
||||
("With llm (gpt-4o-mini)", {"name": "Test", "llm": "gpt-4o-mini", "verbose": False}),
|
||||
("With llm (openai/gpt-4o-mini)", {"name": "Test", "llm": "openai/gpt-4o-mini", "verbose": False}),
|
||||
("With tools", {"name": "Test", "tools": tools, "verbose": False}),
|
||||
("Full config (OpenAI SDK)", {"name": "Test", "instructions": "Be concise", "llm": "gpt-4o-mini", "tools": tools, "verbose": False}),
|
||||
("Full config (LiteLLM)", {"name": "Test", "instructions": "Be concise", "llm": "openai/gpt-4o-mini", "tools": tools, "verbose": False}),
|
||||
]
|
||||
|
||||
print(f"\n{'Configuration':<40} {'Avg Time (μs)':<15} {'Min (μs)':<15} {'Max (μs)':<15}")
|
||||
print("-" * 85)
|
||||
|
||||
for name, config in configs:
|
||||
def create_agent():
|
||||
return PraisonAgent(**config)
|
||||
|
||||
stats = time_function(create_agent, iterations=100)
|
||||
print(f"{name:<40} {stats['avg']:<15.2f} {stats['min']:<15.2f} {stats['max']:<15.2f}")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Compare Specific Operations
|
||||
# ============================================================================
|
||||
|
||||
def compare_specific_operations():
|
||||
"""Compare specific operations between PraisonAI and Agno"""
|
||||
print("\n" + "="*70)
|
||||
print("OPERATION COMPARISON: PraisonAI vs Agno")
|
||||
print("="*70)
|
||||
|
||||
from praisonaiagents import Agent as PraisonAgent
|
||||
from agno.agent import Agent as AgnoAgent
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
tools = [get_weather]
|
||||
|
||||
operations = []
|
||||
|
||||
# Operation 1: Bare minimum instantiation
|
||||
def praisonai_minimal():
|
||||
return PraisonAgent(name="Test", verbose=False)
|
||||
|
||||
def agno_minimal():
|
||||
return AgnoAgent(model=OpenAIChat(id="gpt-4o-mini"))
|
||||
|
||||
praison_stats = time_function(praisonai_minimal, 100)
|
||||
agno_stats = time_function(agno_minimal, 100)
|
||||
operations.append(("Minimal instantiation", praison_stats['avg'], agno_stats['avg']))
|
||||
|
||||
# Operation 2: With tools
|
||||
def praisonai_with_tools():
|
||||
return PraisonAgent(name="Test", tools=tools, verbose=False)
|
||||
|
||||
def agno_with_tools():
|
||||
return AgnoAgent(model=OpenAIChat(id="gpt-4o-mini"), tools=tools)
|
||||
|
||||
praison_stats = time_function(praisonai_with_tools, 100)
|
||||
agno_stats = time_function(agno_with_tools, 100)
|
||||
operations.append(("With tools", praison_stats['avg'], agno_stats['avg']))
|
||||
|
||||
# Operation 3: Full config
|
||||
def praisonai_full():
|
||||
return PraisonAgent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise",
|
||||
llm="gpt-4o-mini",
|
||||
tools=tools,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
def agno_full():
|
||||
return AgnoAgent(
|
||||
model=OpenAIChat(id="gpt-4o-mini"),
|
||||
tools=tools,
|
||||
instructions=["Be concise"]
|
||||
)
|
||||
|
||||
praison_stats = time_function(praisonai_full, 100)
|
||||
agno_stats = time_function(agno_full, 100)
|
||||
operations.append(("Full config", praison_stats['avg'], agno_stats['avg']))
|
||||
|
||||
print(f"\n{'Operation':<30} {'PraisonAI (μs)':<18} {'Agno (μs)':<18} {'Ratio':<10}")
|
||||
print("-" * 76)
|
||||
|
||||
for op_name, praison_time, agno_time in operations:
|
||||
ratio = praison_time / agno_time if agno_time > 0 else 0
|
||||
print(f"{op_name:<30} {praison_time:<18.2f} {agno_time:<18.2f} {ratio:<10.1f}x")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Main
|
||||
# ============================================================================
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
os.environ.setdefault("OPENAI_API_KEY", "sk-test")
|
||||
|
||||
print("="*70)
|
||||
print("PraisonAI Agents - Deep Profiling Benchmark")
|
||||
print("="*70)
|
||||
print("\nThis analysis identifies performance bottlenecks in agent initialization.\n")
|
||||
|
||||
# Run all profiling
|
||||
profile_praisonai_detailed()
|
||||
profile_agno_detailed()
|
||||
profile_praisonai_components()
|
||||
compare_specific_operations()
|
||||
|
||||
print("\n" + "="*70)
|
||||
print("PROFILING COMPLETE")
|
||||
print("="*70)
|
||||
@@ -0,0 +1,416 @@
|
||||
"""
|
||||
PraisonAI Agents - Comprehensive Performance Benchmark
|
||||
|
||||
Measures agent instantiation time and memory footprint across
|
||||
multiple AI agent frameworks using industry-standard methodology.
|
||||
|
||||
Usage:
|
||||
python benchmarks/performance_benchmark.py
|
||||
"""
|
||||
|
||||
import gc
|
||||
import time
|
||||
import tracemalloc
|
||||
import statistics
|
||||
from typing import Literal, List, Callable
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
@dataclass
|
||||
class PerformanceResult:
|
||||
"""Performance measurement results"""
|
||||
run_times: List[float] = field(default_factory=list)
|
||||
memory_usages: List[float] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def avg_run_time(self) -> float:
|
||||
return statistics.mean(self.run_times) if self.run_times else 0
|
||||
|
||||
@property
|
||||
def avg_memory_usage(self) -> float:
|
||||
return statistics.mean(self.memory_usages) if self.memory_usages else 0
|
||||
|
||||
@property
|
||||
def min_run_time(self) -> float:
|
||||
return min(self.run_times) if self.run_times else 0
|
||||
|
||||
@property
|
||||
def max_run_time(self) -> float:
|
||||
return max(self.run_times) if self.run_times else 0
|
||||
|
||||
@property
|
||||
def std_dev_run_time(self) -> float:
|
||||
return statistics.stdev(self.run_times) if len(self.run_times) > 1 else 0
|
||||
|
||||
@property
|
||||
def median_run_time(self) -> float:
|
||||
return statistics.median(self.run_times) if self.run_times else 0
|
||||
|
||||
|
||||
def compute_tracemalloc_baseline(samples: int = 3) -> float:
|
||||
"""Compute baseline memory usage for tracemalloc"""
|
||||
def empty_func():
|
||||
return
|
||||
|
||||
results = []
|
||||
for _ in range(samples):
|
||||
gc.collect()
|
||||
tracemalloc.start()
|
||||
empty_func()
|
||||
_, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
results.append(peak / 1024 / 1024) # Convert to MiB
|
||||
|
||||
return sum(results) / len(results) if results else 0
|
||||
|
||||
|
||||
def measure_time(func: Callable, iterations: int = 1000, warmup: int = 10) -> List[float]:
|
||||
"""Measure execution time for a function"""
|
||||
# Warmup runs
|
||||
for _ in range(warmup):
|
||||
func()
|
||||
|
||||
# Measured runs
|
||||
times = []
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
func()
|
||||
end = time.perf_counter()
|
||||
times.append(end - start)
|
||||
|
||||
return times
|
||||
|
||||
|
||||
def measure_memory(func: Callable, iterations: int = 1000, warmup: int = 10) -> List[float]:
|
||||
"""Measure memory usage for a function"""
|
||||
baseline = compute_tracemalloc_baseline()
|
||||
|
||||
# Warmup runs
|
||||
for _ in range(warmup):
|
||||
func()
|
||||
|
||||
# Measured runs
|
||||
memory_usages = []
|
||||
for _ in range(iterations):
|
||||
gc.collect()
|
||||
tracemalloc.start()
|
||||
func()
|
||||
_, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
|
||||
peak_mib = peak / 1024 / 1024
|
||||
adjusted = max(0, peak_mib - baseline)
|
||||
memory_usages.append(adjusted)
|
||||
|
||||
return memory_usages
|
||||
|
||||
|
||||
def run_benchmark(func: Callable, name: str, iterations: int = 1000, warmup: int = 10) -> PerformanceResult:
|
||||
"""Run full benchmark for a function"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Benchmarking: {name}")
|
||||
print(f"{'='*60}")
|
||||
print(f"Warmup runs: {warmup}")
|
||||
print(f"Measured iterations: {iterations}")
|
||||
|
||||
print("\nMeasuring runtime...")
|
||||
run_times = measure_time(func, iterations, warmup)
|
||||
|
||||
print("Measuring memory...")
|
||||
memory_usages = measure_memory(func, iterations, warmup)
|
||||
|
||||
result = PerformanceResult(run_times=run_times, memory_usages=memory_usages)
|
||||
|
||||
print(f"\nResults for {name}:")
|
||||
print(f" Average Time: {result.avg_run_time:.6f} seconds ({result.avg_run_time * 1_000_000:.2f} μs)")
|
||||
print(f" Min Time: {result.min_run_time:.6f} seconds")
|
||||
print(f" Max Time: {result.max_run_time:.6f} seconds")
|
||||
print(f" Std Dev: {result.std_dev_run_time:.6f} seconds")
|
||||
print(f" Median Time: {result.median_run_time:.6f} seconds")
|
||||
print(f" Avg Memory: {result.avg_memory_usage:.6f} MiB ({result.avg_memory_usage * 1024:.2f} KiB)")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Framework Imports
|
||||
# ============================================================================
|
||||
from praisonaiagents import Agent as PraisonAgent
|
||||
from agno.agent import Agent as AgnoAgent
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
# Additional frameworks for comparison
|
||||
try:
|
||||
from pydantic_ai import Agent as PydanticAgent
|
||||
PYDANTIC_AVAILABLE = True
|
||||
except ImportError:
|
||||
PYDANTIC_AVAILABLE = False
|
||||
|
||||
try:
|
||||
from crewai.agent import Agent as CrewAgent
|
||||
from crewai.tools import tool as crewai_tool
|
||||
CREWAI_AVAILABLE = True
|
||||
except ImportError:
|
||||
CREWAI_AVAILABLE = False
|
||||
|
||||
try:
|
||||
from langchain_core.tools import tool as langchain_tool
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
LANGGRAPH_AVAILABLE = True
|
||||
except ImportError:
|
||||
LANGGRAPH_AVAILABLE = False
|
||||
|
||||
try:
|
||||
from agents import Agent as OpenAIAgent, function_tool
|
||||
OPENAI_AGENTS_AVAILABLE = True
|
||||
except ImportError:
|
||||
OPENAI_AGENTS_AVAILABLE = False
|
||||
|
||||
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
|
||||
|
||||
tools = [get_weather]
|
||||
|
||||
|
||||
def instantiate_praisonai_agent():
|
||||
"""Instantiate a PraisonAI agent with one tool."""
|
||||
return PraisonAgent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=tools,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
|
||||
def instantiate_praisonai_litellm_agent():
|
||||
"""Instantiate a PraisonAI agent with LiteLLM backend."""
|
||||
return PraisonAgent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=tools,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
|
||||
def instantiate_agno_agent():
|
||||
"""Instantiate an Agno agent with one tool."""
|
||||
return AgnoAgent(model=OpenAIChat(id="gpt-4o-mini"), tools=tools)
|
||||
|
||||
|
||||
def instantiate_pydantic_agent():
|
||||
"""Instantiate a PydanticAI agent with one tool."""
|
||||
agent = PydanticAgent("openai:gpt-4o-mini", system_prompt="Be concise, reply with one sentence.")
|
||||
@agent.tool_plain
|
||||
def get_weather_pydantic(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
return agent
|
||||
|
||||
|
||||
def instantiate_openai_agents():
|
||||
"""Instantiate an OpenAI Agents SDK agent with one tool."""
|
||||
return OpenAIAgent(
|
||||
name="Test agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
model="gpt-4o-mini",
|
||||
tools=[function_tool(get_weather)],
|
||||
)
|
||||
|
||||
|
||||
def instantiate_langgraph_agent():
|
||||
"""Instantiate a LangGraph agent with one tool."""
|
||||
@langchain_tool
|
||||
def get_weather_lg(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
return create_react_agent(model=ChatOpenAI(model="gpt-4o-mini"), tools=[get_weather_lg])
|
||||
|
||||
|
||||
def instantiate_crewai_agent():
|
||||
"""Instantiate a CrewAI agent with one tool."""
|
||||
@crewai_tool("Weather Tool")
|
||||
def get_weather_crew(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
return CrewAgent(
|
||||
llm="gpt-4o-mini",
|
||||
role="Test Agent",
|
||||
goal="Be concise, reply with one sentence.",
|
||||
tools=[get_weather_crew],
|
||||
backstory="Test",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("="*70)
|
||||
print("PraisonAI Agents - Comprehensive Performance Benchmark")
|
||||
print("="*70)
|
||||
print("\nThis benchmark measures:")
|
||||
print(" 1. Agent instantiation time (how fast an agent object is created)")
|
||||
print(" 2. Memory footprint (how much memory an agent uses)")
|
||||
print("\nMethodology:")
|
||||
print(" - 10 warmup runs (not measured)")
|
||||
print(" - 1000 measured iterations")
|
||||
print(" - tracemalloc for memory measurement")
|
||||
print(" - Baseline subtraction for accuracy")
|
||||
print("\nFrameworks available:")
|
||||
print(" - PraisonAI: ✅")
|
||||
print(f" - Agno: ✅")
|
||||
print(f" - PydanticAI: {'✅' if PYDANTIC_AVAILABLE else '❌'}")
|
||||
print(f" - OpenAI Agents SDK: {'✅' if OPENAI_AGENTS_AVAILABLE else '❌'}")
|
||||
print(f" - LangGraph: {'✅' if LANGGRAPH_AVAILABLE else '❌'}")
|
||||
print(f" - CrewAI: {'✅' if CREWAI_AVAILABLE else '❌'}")
|
||||
|
||||
results = {}
|
||||
|
||||
# Test PraisonAI first
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing PraisonAI Agents")
|
||||
print("="*70)
|
||||
results['praisonai'] = run_benchmark(
|
||||
instantiate_praisonai_agent,
|
||||
"PraisonAI",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing PraisonAI: {e}")
|
||||
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing PraisonAI Agents (LiteLLM)")
|
||||
print("="*70)
|
||||
results['praisonai_litellm'] = run_benchmark(
|
||||
instantiate_praisonai_litellm_agent,
|
||||
"PraisonAI (LiteLLM)",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing PraisonAI (LiteLLM): {e}")
|
||||
|
||||
# Test Agno
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing Agno")
|
||||
print("="*70)
|
||||
results['agno'] = run_benchmark(
|
||||
instantiate_agno_agent,
|
||||
"Agno",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing Agno: {e}")
|
||||
|
||||
# Test PydanticAI
|
||||
if PYDANTIC_AVAILABLE:
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing PydanticAI")
|
||||
print("="*70)
|
||||
results['pydantic'] = run_benchmark(
|
||||
instantiate_pydantic_agent,
|
||||
"PydanticAI Agent Instantiation",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing PydanticAI: {e}")
|
||||
|
||||
# Test OpenAI Agents SDK
|
||||
if OPENAI_AGENTS_AVAILABLE:
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing OpenAI Agents SDK")
|
||||
print("="*70)
|
||||
results['openai_agents'] = run_benchmark(
|
||||
instantiate_openai_agents,
|
||||
"OpenAI Agents SDK Instantiation",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing OpenAI Agents SDK: {e}")
|
||||
|
||||
# Test LangGraph
|
||||
if LANGGRAPH_AVAILABLE:
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing LangGraph")
|
||||
print("="*70)
|
||||
results['langgraph'] = run_benchmark(
|
||||
instantiate_langgraph_agent,
|
||||
"LangGraph Agent Instantiation",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing LangGraph: {e}")
|
||||
|
||||
# Test CrewAI
|
||||
if CREWAI_AVAILABLE:
|
||||
try:
|
||||
print("\n" + "="*70)
|
||||
print("Testing CrewAI")
|
||||
print("="*70)
|
||||
results['crewai'] = run_benchmark(
|
||||
instantiate_crewai_agent,
|
||||
"CrewAI Agent Instantiation",
|
||||
iterations=1000,
|
||||
warmup=10
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error testing CrewAI: {e}")
|
||||
|
||||
# Final Comparison Table
|
||||
print("\n" + "="*70)
|
||||
print("RESULTS SUMMARY")
|
||||
print("="*70)
|
||||
|
||||
if 'praisonai' in results:
|
||||
baseline = results['praisonai']
|
||||
|
||||
print(f"\n{'Framework':<25} {'Avg Time (μs)':<15} {'Avg Memory (KiB)':<18} {'Relative':<15}")
|
||||
print("-" * 73)
|
||||
|
||||
# Sort by time
|
||||
sorted_results = sorted(results.items(), key=lambda x: x[1].avg_run_time)
|
||||
|
||||
for name, result in sorted_results:
|
||||
time_ratio = result.avg_run_time / baseline.avg_run_time if baseline.avg_run_time > 0 else 0
|
||||
|
||||
time_str = f"{time_ratio:.2f}x"
|
||||
|
||||
display_name = {
|
||||
'agno': 'Agno',
|
||||
'praisonai': 'PraisonAI',
|
||||
'praisonai_litellm': 'PraisonAI (LiteLLM)',
|
||||
'pydantic': 'PydanticAI',
|
||||
'openai_agents': 'OpenAI Agents SDK',
|
||||
'langgraph': 'LangGraph',
|
||||
'crewai': 'CrewAI'
|
||||
}.get(name, name)
|
||||
|
||||
print(f"{display_name:<25} {result.avg_run_time*1e6:<15.2f} {result.avg_memory_usage*1024:<18.2f} {time_str:<15}")
|
||||
|
||||
print("\n" + "="*70)
|
||||
@@ -0,0 +1,235 @@
|
||||
"""
|
||||
PraisonAI Agents - Response Time Benchmark
|
||||
|
||||
Measures actual LLM response time with gpt-4o-mini across frameworks.
|
||||
|
||||
Usage:
|
||||
python benchmarks/response_benchmark.py
|
||||
"""
|
||||
|
||||
import time
|
||||
import statistics
|
||||
import os
|
||||
from typing import List, Literal
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Ensure we use gpt-4o-mini
|
||||
os.environ['OPENAI_MODEL_NAME'] = 'gpt-4o-mini'
|
||||
|
||||
|
||||
@dataclass
|
||||
class ResponseResult:
|
||||
"""Response time measurement results"""
|
||||
response_times: List[float] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def avg_time(self) -> float:
|
||||
return statistics.mean(self.response_times) if self.response_times else 0
|
||||
|
||||
@property
|
||||
def min_time(self) -> float:
|
||||
return min(self.response_times) if self.response_times else 0
|
||||
|
||||
@property
|
||||
def max_time(self) -> float:
|
||||
return max(self.response_times) if self.response_times else 0
|
||||
|
||||
@property
|
||||
def std_dev(self) -> float:
|
||||
return statistics.stdev(self.response_times) if len(self.response_times) > 1 else 0
|
||||
|
||||
|
||||
def get_weather(city: Literal["nyc", "sf"]):
|
||||
"""Use this to get weather information."""
|
||||
if city == "nyc":
|
||||
return "It might be cloudy in nyc"
|
||||
elif city == "sf":
|
||||
return "It's always sunny in sf"
|
||||
|
||||
|
||||
def benchmark_praisonai_response(iterations: int = 5) -> ResponseResult:
|
||||
"""Benchmark PraisonAI agent response time"""
|
||||
from praisonaiagents import Agent
|
||||
|
||||
result = ResponseResult()
|
||||
|
||||
for i in range(iterations):
|
||||
print(f" PraisonAI iteration {i+1}/{iterations}...")
|
||||
|
||||
# Create fresh agent each time
|
||||
agent = Agent(
|
||||
name="Test Agent",
|
||||
instructions="Be concise, reply with one sentence.",
|
||||
llm="gpt-4o-mini",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
start = time.perf_counter()
|
||||
response = agent.start("What is the capital of France?")
|
||||
end = time.perf_counter()
|
||||
|
||||
result.response_times.append(end - start)
|
||||
print(f" Response time: {end - start:.3f}s")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def benchmark_agno_response(iterations: int = 5) -> ResponseResult:
|
||||
"""Benchmark Agno agent response time"""
|
||||
from agno.agent import Agent
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
result = ResponseResult()
|
||||
|
||||
for i in range(iterations):
|
||||
print(f" Agno iteration {i+1}/{iterations}...")
|
||||
|
||||
# Create fresh agent each time
|
||||
agent = Agent(
|
||||
model=OpenAIChat(id="gpt-4o-mini"),
|
||||
system_message="Be concise, reply with one sentence.",
|
||||
)
|
||||
|
||||
start = time.perf_counter()
|
||||
response = agent.run("What is the capital of France?")
|
||||
end = time.perf_counter()
|
||||
|
||||
result.response_times.append(end - start)
|
||||
print(f" Response time: {end - start:.3f}s")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def benchmark_praisonai_with_tool(iterations: int = 5) -> ResponseResult:
|
||||
"""Benchmark PraisonAI agent response time with tool calling"""
|
||||
from praisonaiagents import Agent
|
||||
|
||||
result = ResponseResult()
|
||||
|
||||
for i in range(iterations):
|
||||
print(f" PraisonAI (with tool) iteration {i+1}/{iterations}...")
|
||||
|
||||
agent = Agent(
|
||||
name="Weather Agent",
|
||||
instructions="You are a weather assistant. Use the get_weather tool to answer questions.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=[get_weather],
|
||||
verbose=False
|
||||
)
|
||||
|
||||
start = time.perf_counter()
|
||||
response = agent.start("What's the weather in NYC?")
|
||||
end = time.perf_counter()
|
||||
|
||||
result.response_times.append(end - start)
|
||||
print(f" Response time: {end - start:.3f}s")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def benchmark_agno_with_tool(iterations: int = 5) -> ResponseResult:
|
||||
"""Benchmark Agno agent response time with tool calling"""
|
||||
from agno.agent import Agent
|
||||
from agno.models.openai import OpenAIChat
|
||||
|
||||
result = ResponseResult()
|
||||
|
||||
for i in range(iterations):
|
||||
print(f" Agno (with tool) iteration {i+1}/{iterations}...")
|
||||
|
||||
agent = Agent(
|
||||
model=OpenAIChat(id="gpt-4o-mini"),
|
||||
system_message="You are a weather assistant. Use the get_weather tool to answer questions.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
start = time.perf_counter()
|
||||
response = agent.run("What's the weather in NYC?")
|
||||
end = time.perf_counter()
|
||||
|
||||
result.response_times.append(end - start)
|
||||
print(f" Response time: {end - start:.3f}s")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("="*70)
|
||||
print("PraisonAI Agents - Response Time Benchmark")
|
||||
print("="*70)
|
||||
print("\nModel: gpt-4o-mini")
|
||||
print("Measures end-to-end response time including:")
|
||||
print(" - Agent instantiation")
|
||||
print(" - API call to OpenAI")
|
||||
print(" - Response processing")
|
||||
|
||||
iterations = 5
|
||||
results = {}
|
||||
|
||||
# Test 1: Simple response (no tools)
|
||||
print("\n" + "="*70)
|
||||
print("TEST 1: Simple Response (No Tools)")
|
||||
print("="*70)
|
||||
|
||||
print("\nBenchmarking PraisonAI...")
|
||||
try:
|
||||
results['praisonai_simple'] = benchmark_praisonai_response(iterations)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print("\nBenchmarking Agno...")
|
||||
try:
|
||||
results['agno_simple'] = benchmark_agno_response(iterations)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# Test 2: Response with tool calling
|
||||
print("\n" + "="*70)
|
||||
print("TEST 2: Response with Tool Calling")
|
||||
print("="*70)
|
||||
|
||||
print("\nBenchmarking PraisonAI with tool...")
|
||||
try:
|
||||
results['praisonai_tool'] = benchmark_praisonai_with_tool(iterations)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print("\nBenchmarking Agno with tool...")
|
||||
try:
|
||||
results['agno_tool'] = benchmark_agno_with_tool(iterations)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# Summary
|
||||
print("\n" + "="*70)
|
||||
print("SUMMARY RESULTS")
|
||||
print("="*70)
|
||||
|
||||
print(f"\n{'Test':<30} {'PraisonAI (avg)':<20} {'Agno (avg)':<20} {'Ratio':<15}")
|
||||
print("-" * 85)
|
||||
|
||||
if 'praisonai_simple' in results and 'agno_simple' in results:
|
||||
p_time = results['praisonai_simple'].avg_time
|
||||
a_time = results['agno_simple'].avg_time
|
||||
ratio = p_time / a_time if a_time > 0 else float('inf')
|
||||
print(f"{'Simple Response':<30} {p_time:<20.3f}s {a_time:<20.3f}s {ratio:.2f}x")
|
||||
|
||||
if 'praisonai_tool' in results and 'agno_tool' in results:
|
||||
p_time = results['praisonai_tool'].avg_time
|
||||
a_time = results['agno_tool'].avg_time
|
||||
ratio = p_time / a_time if a_time > 0 else float('inf')
|
||||
print(f"{'With Tool Calling':<30} {p_time:<20.3f}s {a_time:<20.3f}s {ratio:.2f}x")
|
||||
|
||||
print("\n" + "="*70)
|
||||
print("NOTE")
|
||||
print("="*70)
|
||||
print("\nResponse time is dominated by OpenAI API latency.")
|
||||
print("Agent instantiation overhead is negligible in real-world usage.")
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
PraisonAI Agents - Performance Benchmark
|
||||
|
||||
Compares agent instantiation times across popular AI agent frameworks.
|
||||
|
||||
Usage:
|
||||
python benchmarks/simple_benchmark.py
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import Literal
|
||||
|
||||
|
||||
ITERATIONS = 100
|
||||
|
||||
|
||||
def sample_tool(city: Literal['nyc', 'sf']):
|
||||
"""Sample tool for benchmark testing."""
|
||||
if city == 'nyc':
|
||||
return 'cloudy'
|
||||
return 'sunny'
|
||||
|
||||
|
||||
def measure_instantiation(create_fn, iterations=ITERATIONS):
|
||||
"""Measure average instantiation time in microseconds."""
|
||||
times = []
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
create_fn()
|
||||
times.append((time.perf_counter() - start) * 1_000_000)
|
||||
return sum(times) / len(times)
|
||||
|
||||
|
||||
def run_benchmark():
|
||||
"""Run the benchmark across all available frameworks."""
|
||||
tools = [sample_tool]
|
||||
results = {}
|
||||
|
||||
print('=' * 60)
|
||||
print('PraisonAI Agents - Performance Benchmark')
|
||||
print('=' * 60)
|
||||
print(f'\nIterations: {ITERATIONS}')
|
||||
print('Metric: Agent instantiation time (microseconds)\n')
|
||||
|
||||
# PraisonAI
|
||||
print("Testing PraisonAI...")
|
||||
from praisonaiagents import Agent as PraisonAgent
|
||||
results['PraisonAI'] = measure_instantiation(
|
||||
lambda: PraisonAgent(name='Test', llm='gpt-4o-mini', tools=tools, verbose=False)
|
||||
)
|
||||
|
||||
results['PraisonAI (LiteLLM)'] = measure_instantiation(
|
||||
lambda: PraisonAgent(name='Test', llm='openai/gpt-4o-mini', tools=tools, verbose=False)
|
||||
)
|
||||
|
||||
# Other frameworks for comparison
|
||||
print("Testing other frameworks...")
|
||||
|
||||
try:
|
||||
from agno.agent import Agent as AgnoAgent
|
||||
from agno.models.openai import OpenAIChat
|
||||
results['Agno'] = measure_instantiation(
|
||||
lambda: AgnoAgent(model=OpenAIChat(id='gpt-4o-mini'), tools=tools)
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from pydantic_ai import Agent as PydanticAgent
|
||||
results['PydanticAI'] = measure_instantiation(
|
||||
lambda: PydanticAgent('openai:gpt-4o-mini')
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from agents import Agent as OpenAIAgent
|
||||
results['OpenAI Agents SDK'] = measure_instantiation(
|
||||
lambda: OpenAIAgent(name='Test', model='gpt-4o-mini')
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Print results
|
||||
print('\n' + '=' * 60)
|
||||
print('RESULTS')
|
||||
print('=' * 60)
|
||||
|
||||
baseline = results.get('PraisonAI', 1)
|
||||
print(f"\n{'Framework':<25} {'Avg Time (μs)':<15} {'Relative':<10}")
|
||||
print('-' * 50)
|
||||
|
||||
for name, avg in sorted(results.items(), key=lambda x: x[1]):
|
||||
ratio = avg / baseline
|
||||
print(f'{name:<25} {avg:<15.2f} {ratio:.2f}x')
|
||||
|
||||
print('\n' + '=' * 60)
|
||||
return results
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_benchmark()
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,9 @@
|
||||
Starting test with verbose=False
|
||||
CALLBACK EXECUTED!
|
||||
Message:
|
||||
You need to do the following task: Say hello world.
|
||||
Expected Output: A greeting.
|
||||
Please provide only the final result of your work. Do not add any conversation or extra explanation.
|
||||
Response: Hello, World!
|
||||
Other kwargs: {'markdown': True, 'generation_time': 2.685833215713501, 'agent_name': 'TestAgent', 'agent_role': 'Assistant', 'agent_tools': None, 'task_name': 'simple_task', 'task_description': 'Say hello world', 'task_id': 0}
|
||||
--------------------------------------------------
|
||||
@@ -551,8 +551,9 @@ Your Goal: {self.goal}
|
||||
self.auto_save = auto_save # Session name for auto-saving
|
||||
|
||||
# Initialize rules manager for persistent context (like Cursor/Windsurf)
|
||||
# NOTE: Lazy initialization - rules are loaded only when accessed (performance optimization)
|
||||
self._rules_manager = None
|
||||
self._init_rules_manager()
|
||||
self._rules_manager_initialized = False
|
||||
|
||||
# Handle web_search fallback: inject DuckDuckGo tool for unsupported models
|
||||
if web_search and not self._model_supports_web_search():
|
||||
@@ -785,6 +786,21 @@ Your Goal: {self.goal}
|
||||
logging.warning(f"FastContext search failed: {e}")
|
||||
return None
|
||||
|
||||
@property
|
||||
def rules_manager(self):
|
||||
"""
|
||||
Lazy-initialized RulesManager for persistent rules/instructions.
|
||||
|
||||
This property initializes the RulesManager only when first accessed,
|
||||
avoiding expensive filesystem operations during agent instantiation.
|
||||
|
||||
Returns:
|
||||
RulesManager instance or None if not available
|
||||
"""
|
||||
if not self._rules_manager_initialized:
|
||||
self._init_rules_manager()
|
||||
return self._rules_manager
|
||||
|
||||
def _init_rules_manager(self):
|
||||
"""
|
||||
Initialize RulesManager for persistent rules/instructions.
|
||||
@@ -793,7 +809,10 @@ Your Goal: {self.goal}
|
||||
- ~/.praison/rules/ (global)
|
||||
- .praison/rules/ (workspace)
|
||||
- Subdirectory rules
|
||||
|
||||
NOTE: This is called lazily via the rules_manager property for performance.
|
||||
"""
|
||||
self._rules_manager_initialized = True
|
||||
try:
|
||||
from ..memory.rules_manager import RulesManager
|
||||
import os
|
||||
@@ -828,10 +847,10 @@ Your Goal: {self.goal}
|
||||
Returns:
|
||||
Formatted rules context string
|
||||
"""
|
||||
if not self._rules_manager:
|
||||
if not self.rules_manager:
|
||||
return ""
|
||||
|
||||
return self._rules_manager.build_rules_context(
|
||||
return self.rules_manager.build_rules_context(
|
||||
file_path=file_path,
|
||||
include_manual=include_manual
|
||||
)
|
||||
@@ -1239,8 +1258,8 @@ Your Goal: {self.goal}
|
||||
Your Role: {self.role}\n
|
||||
Your Goal: {self.goal}"""
|
||||
|
||||
# Add rules context if rules manager is enabled
|
||||
if self._rules_manager:
|
||||
# Add rules context if rules manager is enabled (lazy initialization)
|
||||
if self._rules_manager_initialized and self._rules_manager:
|
||||
rules_context = self.get_rules_context()
|
||||
if rules_context:
|
||||
system_prompt += f"\n\n## Rules (Guidelines you must follow)\n{rules_context}"
|
||||
@@ -1539,23 +1558,54 @@ Your Goal: {self.goal}"""
|
||||
# Special handling for MCP tools
|
||||
# Check if tools is an MCP instance with the requested function name
|
||||
from ..mcp.mcp import MCP
|
||||
|
||||
# Helper function to execute MCP tool
|
||||
def _execute_mcp_tool(mcp_instance, func_name, args):
|
||||
"""Execute a tool from an MCP instance."""
|
||||
# Handle SSE MCP client
|
||||
if hasattr(mcp_instance, 'is_sse') and mcp_instance.is_sse:
|
||||
if hasattr(mcp_instance, 'sse_client'):
|
||||
for tool in mcp_instance.sse_client.tools:
|
||||
if tool.name == func_name:
|
||||
logging.debug(f"Found matching SSE MCP tool: {func_name}")
|
||||
return True, tool(**args)
|
||||
# Handle HTTP Stream MCP client
|
||||
if hasattr(mcp_instance, 'is_http_stream') and mcp_instance.is_http_stream:
|
||||
if hasattr(mcp_instance, 'http_stream_client'):
|
||||
for tool in mcp_instance.http_stream_client.tools:
|
||||
if tool.name == func_name:
|
||||
logging.debug(f"Found matching HTTP Stream MCP tool: {func_name}")
|
||||
return True, tool(**args)
|
||||
# Handle WebSocket MCP client
|
||||
if hasattr(mcp_instance, 'is_websocket') and mcp_instance.is_websocket:
|
||||
if hasattr(mcp_instance, 'websocket_client'):
|
||||
for tool in mcp_instance.websocket_client.tools:
|
||||
if tool.name == func_name:
|
||||
logging.debug(f"Found matching WebSocket MCP tool: {func_name}")
|
||||
return True, tool(**args)
|
||||
# Handle stdio MCP client
|
||||
if hasattr(mcp_instance, 'runner'):
|
||||
for mcp_tool in mcp_instance.runner.tools:
|
||||
if hasattr(mcp_tool, 'name') and mcp_tool.name == func_name:
|
||||
logging.debug(f"Found matching MCP tool: {func_name}")
|
||||
return True, mcp_instance.runner.call_tool(func_name, args)
|
||||
return False, None
|
||||
|
||||
# Check if tools is a single MCP instance
|
||||
if isinstance(self.tools, MCP):
|
||||
logging.debug(f"Looking for MCP tool {function_name}")
|
||||
|
||||
# Handle SSE MCP client
|
||||
if hasattr(self.tools, 'is_sse') and self.tools.is_sse:
|
||||
if hasattr(self.tools, 'sse_client'):
|
||||
for tool in self.tools.sse_client.tools:
|
||||
if tool.name == function_name:
|
||||
logging.debug(f"Found matching SSE MCP tool: {function_name}")
|
||||
return tool(**arguments)
|
||||
# Handle stdio MCP client
|
||||
elif hasattr(self.tools, 'runner'):
|
||||
# Check if any of the MCP tools match the function name
|
||||
for mcp_tool in self.tools.runner.tools:
|
||||
if hasattr(mcp_tool, 'name') and mcp_tool.name == function_name:
|
||||
logging.debug(f"Found matching MCP tool: {function_name}")
|
||||
return self.tools.runner.call_tool(function_name, arguments)
|
||||
found, result = _execute_mcp_tool(self.tools, function_name, arguments)
|
||||
if found:
|
||||
return result
|
||||
|
||||
# Check if tools is a list that may contain MCP instances
|
||||
if isinstance(self.tools, (list, tuple)):
|
||||
for tool in self.tools:
|
||||
if isinstance(tool, MCP):
|
||||
logging.debug(f"Looking for MCP tool {function_name} in MCP instance")
|
||||
found, result = _execute_mcp_tool(tool, function_name, arguments)
|
||||
if found:
|
||||
return result
|
||||
|
||||
# Try to find the function in the agent's tools list first
|
||||
func = None
|
||||
@@ -1870,7 +1920,8 @@ Your Goal: {self.goal}"""
|
||||
if tool_param is not None:
|
||||
from ..mcp.mcp import MCP
|
||||
if isinstance(tool_param, MCP) and hasattr(tool_param, 'to_openai_tool'):
|
||||
logging.debug("Converting MCP tool to OpenAI format")
|
||||
# Single MCP instance
|
||||
logging.debug("Converting single MCP tool to OpenAI format")
|
||||
openai_tool = tool_param.to_openai_tool()
|
||||
if openai_tool:
|
||||
# Handle both single tool and list of tools
|
||||
@@ -1879,6 +1930,22 @@ Your Goal: {self.goal}"""
|
||||
else:
|
||||
tool_param = [openai_tool]
|
||||
logging.debug(f"Converted MCP tool: {tool_param}")
|
||||
elif isinstance(tool_param, (list, tuple)):
|
||||
# List that may contain MCP instances - convert each MCP to OpenAI format
|
||||
converted_tools = []
|
||||
for t in tool_param:
|
||||
if isinstance(t, MCP) and hasattr(t, 'to_openai_tool'):
|
||||
logging.debug("Converting MCP instance in list to OpenAI format")
|
||||
openai_tools = t.to_openai_tool()
|
||||
if isinstance(openai_tools, list):
|
||||
converted_tools.extend(openai_tools)
|
||||
elif openai_tools:
|
||||
converted_tools.append(openai_tools)
|
||||
else:
|
||||
# Keep non-MCP tools as-is
|
||||
converted_tools.append(t)
|
||||
tool_param = converted_tools
|
||||
logging.debug(f"Converted {len(converted_tools)} tools from list")
|
||||
|
||||
# Store chat history length for potential rollback
|
||||
chat_history_length = len(self.chat_history)
|
||||
|
||||
@@ -440,7 +440,7 @@ class Workflow:
|
||||
previous_output = output
|
||||
|
||||
if verbose:
|
||||
print(f"✅ {step.name}: {str(output)[:100]}...")
|
||||
print(f"✅ {step.name}: {str(output)}")
|
||||
|
||||
# Handle early stop
|
||||
if stop:
|
||||
@@ -571,7 +571,7 @@ class Workflow:
|
||||
output = f"Error: {e}"
|
||||
|
||||
if verbose:
|
||||
print(f"✅ {normalized.name}: {str(output)[:100]}...")
|
||||
print(f"✅ {normalized.name}: {str(output)}")
|
||||
|
||||
return {
|
||||
"step": normalized.name,
|
||||
|
||||
@@ -286,6 +286,7 @@ class Workflow:
|
||||
planning_llm: Optional[str] = None # LLM for planning
|
||||
reasoning: bool = False # Enable chain-of-thought reasoning
|
||||
verbose: bool = False # Enable verbose output
|
||||
stream: bool = True # Enable streaming responses (default True like PraisonAIAgents)
|
||||
|
||||
# Callbacks (like process="workflow")
|
||||
on_workflow_start: Optional[Callable[['Workflow', str], None]] = None # (workflow, input)
|
||||
@@ -316,7 +317,8 @@ class Workflow:
|
||||
self,
|
||||
input: str = "",
|
||||
llm: Optional[str] = None,
|
||||
verbose: bool = False
|
||||
verbose: bool = False,
|
||||
stream: bool = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Run the workflow with the given input.
|
||||
@@ -340,16 +342,22 @@ class Workflow:
|
||||
input: The input text/prompt for the workflow
|
||||
llm: LLM model to use (default: gpt-4o-mini)
|
||||
verbose: Print step outputs
|
||||
stream: Enable streaming responses (default: use workflow's stream setting)
|
||||
|
||||
Returns:
|
||||
Dict with 'output' (final result) and 'steps' (all step results)
|
||||
"""
|
||||
# Use default LLM if not specified
|
||||
model = llm or self.default_llm or "gpt-4o-mini"
|
||||
logger.debug(f"Workflow using model: {model} (llm={llm}, default_llm={self.default_llm})")
|
||||
|
||||
# Use workflow verbose setting if not overridden
|
||||
verbose = verbose or self.verbose
|
||||
|
||||
# Use workflow stream setting if not overridden
|
||||
if stream is None:
|
||||
stream = self.stream
|
||||
|
||||
# Add input to variables
|
||||
all_variables = {**self.variables, "input": input}
|
||||
|
||||
@@ -382,7 +390,7 @@ class Workflow:
|
||||
if isinstance(step, Route):
|
||||
# Decision-based routing
|
||||
route_result = self._execute_route(
|
||||
step, previous_output, input, all_variables, model, verbose
|
||||
step, previous_output, input, all_variables, model, verbose, stream
|
||||
)
|
||||
results.extend(route_result["steps"])
|
||||
previous_output = route_result["output"]
|
||||
@@ -393,7 +401,7 @@ class Workflow:
|
||||
elif isinstance(step, Parallel):
|
||||
# Parallel execution
|
||||
parallel_result = self._execute_parallel(
|
||||
step, previous_output, input, all_variables, model, verbose
|
||||
step, previous_output, input, all_variables, model, verbose, stream
|
||||
)
|
||||
results.extend(parallel_result["steps"])
|
||||
previous_output = parallel_result["output"]
|
||||
@@ -404,7 +412,7 @@ class Workflow:
|
||||
elif isinstance(step, Loop):
|
||||
# Loop over items
|
||||
loop_result = self._execute_loop(
|
||||
step, previous_output, input, all_variables, model, verbose
|
||||
step, previous_output, input, all_variables, model, verbose, stream
|
||||
)
|
||||
results.extend(loop_result["steps"])
|
||||
previous_output = loop_result["output"]
|
||||
@@ -415,7 +423,7 @@ class Workflow:
|
||||
elif isinstance(step, Repeat):
|
||||
# Repeat until condition
|
||||
repeat_result = self._execute_repeat(
|
||||
step, previous_output, input, all_variables, model, verbose
|
||||
step, previous_output, input, all_variables, model, verbose, stream
|
||||
)
|
||||
results.extend(repeat_result["steps"])
|
||||
previous_output = repeat_result["output"]
|
||||
@@ -494,7 +502,11 @@ class Workflow:
|
||||
for key, value in all_variables.items():
|
||||
action = action.replace(f"{{{{{key}}}}}", str(value))
|
||||
if previous_output:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
if "{{previous_output}}" in action:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
else:
|
||||
# Auto-append context if not explicitly referenced
|
||||
action = f"{action}\n\nContext from previous step:\n{previous_output}"
|
||||
action = action.replace("{{input}}", input)
|
||||
|
||||
# Add reasoning prompt if enabled
|
||||
@@ -508,9 +520,9 @@ class Workflow:
|
||||
# Handle images if present
|
||||
step_images = getattr(step, 'images', None)
|
||||
if step_images:
|
||||
output = step.agent.chat(action, images=step_images)
|
||||
output = step.agent.chat(action, images=step_images, stream=stream)
|
||||
else:
|
||||
output = step.agent.chat(action)
|
||||
output = step.agent.chat(action, stream=stream)
|
||||
|
||||
# Handle output_pydantic if present
|
||||
output_pydantic = getattr(step, 'output_pydantic', None)
|
||||
@@ -538,14 +550,19 @@ class Workflow:
|
||||
llm=config.get("llm", model),
|
||||
tools=step_tools if step_tools else None,
|
||||
verbose=verbose,
|
||||
reasoning=self.reasoning
|
||||
reasoning=self.reasoning,
|
||||
stream=stream
|
||||
)
|
||||
# Substitute variables in action
|
||||
action = step.action
|
||||
for key, value in all_variables.items():
|
||||
action = action.replace(f"{{{{{key}}}}}", str(value))
|
||||
if previous_output:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
if "{{previous_output}}" in action:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
else:
|
||||
# Auto-append context if not explicitly referenced
|
||||
action = f"{action}\n\nContext from previous step:\n{previous_output}"
|
||||
action = action.replace("{{input}}", input)
|
||||
|
||||
# Add reasoning prompt if enabled
|
||||
@@ -554,7 +571,8 @@ class Workflow:
|
||||
|
||||
if validation_feedback:
|
||||
action = f"{action}\n\nPrevious attempt feedback: {validation_feedback}"
|
||||
output = temp_agent.chat(action)
|
||||
|
||||
output = temp_agent.chat(action, stream=stream)
|
||||
|
||||
except Exception as e:
|
||||
step_error = e
|
||||
@@ -629,7 +647,7 @@ class Workflow:
|
||||
previous_output = output
|
||||
|
||||
if verbose:
|
||||
print(f"✅ {step.name}: {str(output)[:100]}...")
|
||||
print(f"✅ {step.name}: {str(output)}")
|
||||
|
||||
# Handle early stop
|
||||
if stop:
|
||||
@@ -798,11 +816,16 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
elif hasattr(step, 'chat'):
|
||||
# It's an Agent - wrap with agent reference and preserve tools
|
||||
agent_tools = getattr(step, 'tools', None)
|
||||
# Check for _yaml_action from YAML parser (canonical: action, alias: description)
|
||||
yaml_action = getattr(step, '_yaml_action', None)
|
||||
# Use yaml_action if set, otherwise fall back to previous_output/input
|
||||
default_action = "{{previous_output}}" if index > 0 else "{{input}}"
|
||||
action = yaml_action if yaml_action else default_action
|
||||
return WorkflowStep(
|
||||
name=getattr(step, 'name', f'agent_{index+1}'),
|
||||
agent=step,
|
||||
tools=agent_tools,
|
||||
action="{{previous_output}}" if index > 0 else "{{input}}"
|
||||
action=action
|
||||
)
|
||||
elif callable(step):
|
||||
return WorkflowStep(
|
||||
@@ -823,7 +846,8 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
all_variables: Dict[str, Any],
|
||||
model: str,
|
||||
verbose: bool,
|
||||
index: int = 0
|
||||
index: int = 0,
|
||||
stream: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Execute a single step and return result."""
|
||||
normalized = self._normalize_single_step(step, index)
|
||||
@@ -857,9 +881,13 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
for key, value in all_variables.items():
|
||||
action = action.replace(f"{{{{{key}}}}}", str(value))
|
||||
if previous_output:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
if "{{previous_output}}" in action:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
else:
|
||||
# Auto-append context if not explicitly referenced
|
||||
action = f"{action}\n\nContext from previous step:\n{previous_output}"
|
||||
action = action.replace("{{input}}", input)
|
||||
output = normalized.agent.chat(action)
|
||||
output = normalized.agent.chat(action, stream=stream)
|
||||
except Exception as e:
|
||||
output = f"Error: {e}"
|
||||
elif normalized.action:
|
||||
@@ -871,19 +899,25 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
role=config.get("role", "Assistant"),
|
||||
goal=config.get("goal", "Complete the task"),
|
||||
llm=config.get("llm", model),
|
||||
verbose=verbose
|
||||
verbose=verbose,
|
||||
stream=stream
|
||||
)
|
||||
action = normalized.action
|
||||
for key, value in all_variables.items():
|
||||
action = action.replace(f"{{{{{key}}}}}", str(value))
|
||||
if previous_output:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
output = temp_agent.chat(action)
|
||||
if "{{previous_output}}" in action:
|
||||
action = action.replace("{{previous_output}}", str(previous_output))
|
||||
else:
|
||||
# Auto-append context if not explicitly referenced
|
||||
action = f"{action}\n\nContext from previous step:\n{previous_output}"
|
||||
|
||||
output = temp_agent.chat(action, stream=stream)
|
||||
except Exception as e:
|
||||
output = f"Error: {e}"
|
||||
|
||||
if verbose:
|
||||
print(f"✅ {normalized.name}: {str(output)[:100]}...")
|
||||
print(f"✅ {normalized.name}: {str(output)}")
|
||||
|
||||
return {
|
||||
"step": normalized.name,
|
||||
@@ -899,7 +933,8 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
input: str,
|
||||
all_variables: Dict[str, Any],
|
||||
model: str,
|
||||
verbose: bool
|
||||
verbose: bool,
|
||||
stream: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Execute routing based on previous output."""
|
||||
results = []
|
||||
@@ -925,7 +960,7 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
# Execute matched route steps
|
||||
for idx, step in enumerate(matched_route):
|
||||
step_result = self._execute_single_step_internal(
|
||||
step, output, input, all_variables, model, verbose, idx
|
||||
step, output, input, all_variables, model, verbose, idx, stream=stream
|
||||
)
|
||||
results.append({"step": step_result["step"], "output": step_result["output"]})
|
||||
output = step_result["output"]
|
||||
@@ -943,7 +978,8 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
input: str,
|
||||
all_variables: Dict[str, Any],
|
||||
model: str,
|
||||
verbose: bool
|
||||
verbose: bool,
|
||||
stream: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Execute steps in parallel (simulated with sequential for now)."""
|
||||
import concurrent.futures
|
||||
@@ -960,7 +996,7 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
for idx, step in enumerate(parallel_step.steps):
|
||||
future = executor.submit(
|
||||
self._execute_single_step_internal,
|
||||
step, previous_output, input, all_variables.copy(), model, False, idx
|
||||
step, previous_output, input, all_variables.copy(), model, False, idx, stream
|
||||
)
|
||||
futures.append((idx, future))
|
||||
|
||||
@@ -989,7 +1025,8 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
input: str,
|
||||
all_variables: Dict[str, Any],
|
||||
model: str,
|
||||
verbose: bool
|
||||
verbose: bool,
|
||||
stream: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Execute step for each item in loop."""
|
||||
import csv
|
||||
@@ -1029,7 +1066,7 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
loop_vars["loop_index"] = idx
|
||||
|
||||
step_result = self._execute_single_step_internal(
|
||||
loop_step.step, previous_output, input, loop_vars, model, verbose, idx
|
||||
loop_step.step, previous_output, input, loop_vars, model, verbose, idx, stream=stream
|
||||
)
|
||||
results.append({"step": f"{step_result['step']}_{idx}", "output": step_result["output"]})
|
||||
outputs.append(step_result["output"])
|
||||
@@ -1047,7 +1084,8 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
input: str,
|
||||
all_variables: Dict[str, Any],
|
||||
model: str,
|
||||
verbose: bool
|
||||
verbose: bool,
|
||||
stream: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Repeat step until condition is met."""
|
||||
results = []
|
||||
@@ -1058,7 +1096,7 @@ Create a brief execution plan (2-3 sentences) describing how to best accomplish
|
||||
|
||||
for iteration in range(repeat_step.max_iterations):
|
||||
step_result = self._execute_single_step_internal(
|
||||
repeat_step.step, output, input, all_variables, model, verbose, iteration
|
||||
repeat_step.step, output, input, all_variables, model, verbose, iteration, stream=stream
|
||||
)
|
||||
results.append({"step": f"{step_result['step']}_{iteration}", "output": step_result["output"]})
|
||||
output = step_result["output"]
|
||||
|
||||
@@ -102,7 +102,112 @@ class YAMLWorkflowParser:
|
||||
Workflow object ready for execution
|
||||
"""
|
||||
data = yaml.safe_load(yaml_content)
|
||||
return self._parse_workflow_data(data)
|
||||
# Normalize to canonical format (accept both old and new field names)
|
||||
normalized_data = self._normalize_yaml_config(data)
|
||||
return self._parse_workflow_data(normalized_data)
|
||||
|
||||
def _normalize_yaml_config(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Normalize YAML config to canonical format.
|
||||
|
||||
Accepts both old (agents.yaml) and new (workflow.yaml) field names.
|
||||
This implements "Accept liberally, suggest canonically" principle.
|
||||
|
||||
Canonical names (preferred):
|
||||
- agents (not roles)
|
||||
- instructions (not backstory)
|
||||
- action (not description)
|
||||
- steps (not tasks)
|
||||
- name (not topic)
|
||||
|
||||
Args:
|
||||
data: Raw parsed YAML data
|
||||
|
||||
Returns:
|
||||
Normalized data with canonical field names
|
||||
"""
|
||||
if not data:
|
||||
return data
|
||||
|
||||
normalized = data.copy()
|
||||
|
||||
# 1. Normalize workflow name: topic -> name (only if topic is used as name, not as input)
|
||||
# Note: 'topic' is legacy, 'input' is canonical for workflow input data
|
||||
if 'topic' in normalized and 'name' not in normalized and 'input' not in normalized:
|
||||
# Legacy behavior: topic was used as both name and input
|
||||
normalized['name'] = normalized.get('topic', 'Unnamed Workflow')
|
||||
|
||||
# 2. Normalize workflow input: 'input' is canonical, 'topic' is alias
|
||||
# Priority: input > topic (input takes precedence if both exist)
|
||||
if 'input' not in normalized and 'topic' in normalized:
|
||||
normalized['input'] = normalized['topic']
|
||||
|
||||
# 3. Normalize container: roles -> agents
|
||||
# Note: _convert_roles_to_agents handles backstory -> instructions
|
||||
if 'roles' in normalized and 'agents' not in normalized:
|
||||
normalized['agents'] = self._convert_roles_to_agents(normalized['roles'])
|
||||
# Extract steps from tasks nested in roles if no steps defined
|
||||
if 'steps' not in normalized:
|
||||
normalized['steps'] = self._extract_steps_from_roles(normalized['roles'])
|
||||
|
||||
# 4. Normalize agent fields: backstory -> instructions (for agents section)
|
||||
if 'agents' in normalized:
|
||||
for agent_id, agent_config in normalized['agents'].items():
|
||||
if isinstance(agent_config, dict):
|
||||
# backstory -> instructions
|
||||
if 'backstory' in agent_config and 'instructions' not in agent_config:
|
||||
agent_config['instructions'] = agent_config['backstory']
|
||||
|
||||
# 5. Normalize step fields: description -> action
|
||||
if 'steps' in normalized:
|
||||
for step in normalized['steps']:
|
||||
if isinstance(step, dict):
|
||||
# description -> action
|
||||
if 'description' in step and 'action' not in step:
|
||||
step['action'] = step['description']
|
||||
|
||||
# Handle parallel steps
|
||||
if 'parallel' in step:
|
||||
for parallel_step in step['parallel']:
|
||||
if isinstance(parallel_step, dict):
|
||||
if 'description' in parallel_step and 'action' not in parallel_step:
|
||||
parallel_step['action'] = parallel_step['description']
|
||||
|
||||
return normalized
|
||||
|
||||
def _extract_steps_from_roles(self, roles: Dict[str, Dict]) -> List[Dict]:
|
||||
"""
|
||||
Extract steps from tasks nested within roles (agents.yaml format).
|
||||
|
||||
Args:
|
||||
roles: The roles section from agents.yaml
|
||||
|
||||
Returns:
|
||||
List of step definitions
|
||||
"""
|
||||
steps = []
|
||||
for role_id, role_config in roles.items():
|
||||
if 'tasks' in role_config:
|
||||
for task_id, task_config in role_config['tasks'].items():
|
||||
step = {
|
||||
'name': task_id,
|
||||
'agent': role_id,
|
||||
}
|
||||
# description -> action
|
||||
if 'description' in task_config:
|
||||
step['action'] = task_config['description']
|
||||
elif 'action' in task_config:
|
||||
step['action'] = task_config['action']
|
||||
|
||||
# Copy other task fields
|
||||
for field in ['expected_output', 'context', 'output_file',
|
||||
'output_json', 'create_directory', 'callback',
|
||||
'async_execution', 'guardrail', 'max_retries']:
|
||||
if field in task_config:
|
||||
step[field] = task_config[field]
|
||||
|
||||
steps.append(step)
|
||||
return steps
|
||||
|
||||
def _parse_workflow_data(self, data: Dict[str, Any]) -> Workflow:
|
||||
"""
|
||||
@@ -127,6 +232,7 @@ class YAMLWorkflowParser:
|
||||
workflow_config = data.get('workflow', {})
|
||||
planning = workflow_config.get('planning', False)
|
||||
planning_llm = workflow_config.get('planning_llm')
|
||||
default_llm = workflow_config.get('default_llm')
|
||||
reasoning = workflow_config.get('reasoning', False)
|
||||
verbose = workflow_config.get('verbose', False)
|
||||
memory_config = workflow_config.get('memory_config')
|
||||
@@ -156,6 +262,7 @@ class YAMLWorkflowParser:
|
||||
variables=variables,
|
||||
planning=planning,
|
||||
planning_llm=planning_llm,
|
||||
default_llm=default_llm,
|
||||
reasoning=reasoning,
|
||||
verbose=verbose,
|
||||
memory_config=memory_config,
|
||||
@@ -170,6 +277,10 @@ class YAMLWorkflowParser:
|
||||
workflow.process = process
|
||||
workflow.manager_llm = manager_llm
|
||||
|
||||
# Store workflow input (from 'input' or 'topic' field)
|
||||
# This is the default input passed to workflow.start() if no input is provided
|
||||
workflow.default_input = data.get('input', data.get('topic', ''))
|
||||
|
||||
return workflow
|
||||
|
||||
def _convert_roles_to_agents(self, roles: Dict[str, Dict]) -> Dict[str, Dict]:
|
||||
|
||||
@@ -4,8 +4,9 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.4"
|
||||
version = "0.1.12"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
authors = [
|
||||
{ name="Mervin Praison" }
|
||||
|
||||
@@ -493,17 +493,22 @@ class TestAgentIntegration:
|
||||
|
||||
def test_agent_store_memory(self):
|
||||
"""Test Agent store_memory method."""
|
||||
import uuid
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Use unique user_id to avoid stale data from previous test runs
|
||||
unique_user_id = f"test_user_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
agent = Agent(
|
||||
name="Test Agent",
|
||||
memory=True
|
||||
memory=True,
|
||||
user_id=unique_user_id
|
||||
)
|
||||
|
||||
agent.store_memory("User prefers dark mode", memory_type="short_term")
|
||||
|
||||
items = agent._memory_instance.get_short_term()
|
||||
assert len(items) == 1
|
||||
assert len(items) == 1, f"Expected 1 item, got {len(items)} items"
|
||||
assert items[0].content == "User prefers dark mode"
|
||||
|
||||
def test_agent_get_memory_context(self):
|
||||
@@ -658,10 +663,12 @@ class TestMultiAgentMemorySharing:
|
||||
|
||||
def test_agents_share_memory_with_same_user_id(self):
|
||||
"""Test that agents with same user_id share memory."""
|
||||
import uuid
|
||||
from praisonaiagents import Agent
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
user_id = "shared_user"
|
||||
# Use unique user_id to avoid stale data from previous test runs
|
||||
user_id = f"shared_user_{uuid.uuid4().hex[:8]}"
|
||||
base_path = f"{tmpdir}/memory"
|
||||
|
||||
# Agent 1 stores memories
|
||||
@@ -676,9 +683,9 @@ class TestMultiAgentMemorySharing:
|
||||
agent1._memory_instance.add_entity("Alice", "person", {"role": "developer"})
|
||||
|
||||
# Verify Agent1 stored memories
|
||||
assert len(agent1._memory_instance.get_short_term()) == 1
|
||||
assert len(agent1._memory_instance.get_long_term()) == 1
|
||||
assert len(agent1._memory_instance.get_all_entities()) == 1
|
||||
assert len(agent1._memory_instance.get_short_term()) == 1, f"Expected 1 short_term, got {len(agent1._memory_instance.get_short_term())}"
|
||||
assert len(agent1._memory_instance.get_long_term()) == 1, f"Expected 1 long_term, got {len(agent1._memory_instance.get_long_term())}"
|
||||
assert len(agent1._memory_instance.get_all_entities()) == 1, f"Expected 1 entity, got {len(agent1._memory_instance.get_all_entities())}"
|
||||
|
||||
# Agent 2 with same user_id should load memories
|
||||
agent2 = Agent(
|
||||
|
||||
@@ -6,6 +6,8 @@ The agent should:
|
||||
3. Return the final result (200)
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import os
|
||||
from praisonaiagents import Agent
|
||||
|
||||
def get_stock_price(company_name: str) -> str:
|
||||
@@ -35,41 +37,60 @@ def multiply(a: int, b: int) -> int:
|
||||
print(f"[Tool Called] multiply({a}, {b})")
|
||||
return a * b
|
||||
|
||||
# Test with Gemini
|
||||
print("=" * 60)
|
||||
print("Testing with Gemini model")
|
||||
print("=" * 60)
|
||||
|
||||
agent_gemini = Agent(
|
||||
instructions="You are a helpful assistant. You can use the tools provided to you to help the user. When asked to multiply a stock price, first get the stock price, then multiply it.",
|
||||
llm="gemini/gemini-2.5-pro",
|
||||
tools=[get_stock_price, multiply],
|
||||
verbose=True
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OPENAI_API_KEY"),
|
||||
reason="OPENAI_API_KEY not set - skipping integration test"
|
||||
)
|
||||
def test_sequential_tool_calling_gpt():
|
||||
"""Test sequential tool calling with GPT model"""
|
||||
print("=" * 60)
|
||||
print("Testing with GPT model")
|
||||
print("=" * 60)
|
||||
|
||||
result_gemini = agent_gemini.start("what is the stock price of Google? multiply the Google stock price with 2")
|
||||
print(f"\nFinal Result (Gemini): {result_gemini}")
|
||||
agent_gpt = Agent(
|
||||
instructions="You are a helpful assistant. When asked to multiply a stock price, first get the stock price, then multiply it.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=[get_stock_price, multiply],
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Test with GPT-4
|
||||
print("\n" + "=" * 60)
|
||||
print("Testing with GPT-4 model")
|
||||
print("=" * 60)
|
||||
result = agent_gpt.start("what is the stock price of Google? multiply the Google stock price with 2")
|
||||
print(f"\nFinal Result (GPT): {result}")
|
||||
|
||||
# Verify result
|
||||
assert result, "GPT returned empty result"
|
||||
print(f"GPT result contains '200': {'200' in str(result)}")
|
||||
|
||||
agent_gpt4 = Agent(
|
||||
instructions="You are a helpful assistant. You can use the tools provided to you to help the user. When asked to multiply a stock price, first get the stock price, then multiply it.",
|
||||
llm="gpt-5-nano",
|
||||
tools=[get_stock_price, multiply],
|
||||
verbose=True
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("GOOGLE_API_KEY"),
|
||||
reason="GOOGLE_API_KEY not set - skipping integration test"
|
||||
)
|
||||
def test_sequential_tool_calling_gemini():
|
||||
"""Test sequential tool calling with Gemini model"""
|
||||
print("=" * 60)
|
||||
print("Testing with Gemini model")
|
||||
print("=" * 60)
|
||||
|
||||
result_gpt4 = agent_gpt4.start("what is the stock price of Google? multiply the Google stock price with 2")
|
||||
print(f"\nFinal Result (GPT-4): {result_gpt4}")
|
||||
agent_gemini = Agent(
|
||||
instructions="You are a helpful assistant. When asked to multiply a stock price, first get the stock price, then multiply it.",
|
||||
llm="gemini/gemini-2.0-flash",
|
||||
tools=[get_stock_price, multiply],
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Verify results
|
||||
print("\n" + "=" * 60)
|
||||
print("Test Results Summary")
|
||||
print("=" * 60)
|
||||
print(f"Gemini result contains '200': {'200' in str(result_gemini) if result_gemini else False}")
|
||||
print(f"GPT-4 result contains '200': {'200' in str(result_gpt4) if result_gpt4 else False}")
|
||||
print(f"Gemini returned empty: {not result_gemini or result_gemini == ''}")
|
||||
print(f"GPT-4 returned empty: {not result_gpt4 or result_gpt4 == ''}")
|
||||
result = agent_gemini.start("what is the stock price of Google? multiply the Google stock price with 2")
|
||||
print(f"\nFinal Result (Gemini): {result}")
|
||||
|
||||
# Verify result
|
||||
assert result, "Gemini returned empty result"
|
||||
print(f"Gemini result contains '200': {'200' in str(result)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run tests manually
|
||||
if os.environ.get("OPENAI_API_KEY"):
|
||||
test_sequential_tool_calling_gpt()
|
||||
if os.environ.get("GOOGLE_API_KEY"):
|
||||
test_sequential_tool_calling_gemini()
|
||||
@@ -2,7 +2,23 @@
|
||||
"""Test LLM class directly to verify self-reflection fix"""
|
||||
|
||||
from praisonaiagents.llm import LLM
|
||||
from praisonaiagents.tools import calculator
|
||||
|
||||
# Define calculator tool locally to avoid import issues
|
||||
def calculator(expression: str) -> str:
|
||||
"""
|
||||
Evaluate a mathematical expression.
|
||||
|
||||
Args:
|
||||
expression: A mathematical expression to evaluate (e.g., "2 + 2", "15 * 23")
|
||||
|
||||
Returns:
|
||||
The result of the calculation as a string
|
||||
"""
|
||||
try:
|
||||
result = eval(expression)
|
||||
return str(result)
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
def test_llm_direct():
|
||||
"""Test LLM class directly with self-reflection and tools"""
|
||||
|
||||
@@ -0,0 +1,391 @@
|
||||
"""
|
||||
Test-Driven Development tests for Multiple MCP Tools support.
|
||||
|
||||
This test file verifies that:
|
||||
1. Single MCP instance works (baseline)
|
||||
2. Multiple MCP instances in a list work for tool definition conversion
|
||||
3. Multiple MCP instances in a list work for tool execution
|
||||
4. Mixed tools (MCP + regular functions) work together
|
||||
|
||||
Uses mcp-server-time as the test MCP server (simple, no API keys needed).
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import os
|
||||
import sys
|
||||
from unittest.mock import Mock
|
||||
|
||||
# Add the package to path for testing
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||
|
||||
from praisonaiagents.mcp.mcp import MCP
|
||||
from praisonaiagents.agent.agent import Agent
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test Fixtures
|
||||
# ============================================================================
|
||||
|
||||
@pytest.fixture
|
||||
def time_mcp():
|
||||
"""Create a Time MCP server instance."""
|
||||
return MCP("uvx mcp-server-time", timeout=30)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_mcp_with_tools():
|
||||
"""Create a mock MCP instance with predefined tools."""
|
||||
mock = Mock(spec=MCP)
|
||||
mock.is_sse = False
|
||||
mock.is_http_stream = False
|
||||
mock.is_websocket = False
|
||||
|
||||
# Create mock runner with tools
|
||||
mock.runner = Mock()
|
||||
mock_tool1 = Mock()
|
||||
mock_tool1.name = "get_current_time"
|
||||
mock_tool1.description = "Get current time in a timezone"
|
||||
mock_tool1.inputSchema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"timezone": {"type": "string", "description": "IANA timezone name"}
|
||||
},
|
||||
"required": ["timezone"]
|
||||
}
|
||||
|
||||
mock_tool2 = Mock()
|
||||
mock_tool2.name = "convert_time"
|
||||
mock_tool2.description = "Convert time between timezones"
|
||||
mock_tool2.inputSchema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"source_timezone": {"type": "string"},
|
||||
"time": {"type": "string"},
|
||||
"target_timezone": {"type": "string"}
|
||||
},
|
||||
"required": ["source_timezone", "time", "target_timezone"]
|
||||
}
|
||||
|
||||
mock.runner.tools = [mock_tool1, mock_tool2]
|
||||
mock._tools = []
|
||||
|
||||
# Mock to_openai_tool method
|
||||
def to_openai_tool():
|
||||
return [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_current_time",
|
||||
"description": "Get current time in a timezone",
|
||||
"parameters": mock_tool1.inputSchema
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "convert_time",
|
||||
"description": "Convert time between timezones",
|
||||
"parameters": mock_tool2.inputSchema
|
||||
}
|
||||
}
|
||||
]
|
||||
mock.to_openai_tool = to_openai_tool
|
||||
|
||||
# Mock __iter__ to return tool functions
|
||||
def get_current_time(timezone: str) -> str:
|
||||
return f"Time in {timezone}: 12:00"
|
||||
def convert_time(source_timezone: str, time: str, target_timezone: str) -> str:
|
||||
return f"Converted {time} from {source_timezone} to {target_timezone}"
|
||||
get_current_time.__name__ = "get_current_time"
|
||||
convert_time.__name__ = "convert_time"
|
||||
mock._tools = [get_current_time, convert_time]
|
||||
mock.__iter__ = lambda self: iter(self._tools)
|
||||
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_mcp_filesystem():
|
||||
"""Create a mock MCP instance simulating filesystem server."""
|
||||
mock = Mock(spec=MCP)
|
||||
mock.is_sse = False
|
||||
mock.is_http_stream = False
|
||||
mock.is_websocket = False
|
||||
|
||||
mock.runner = Mock()
|
||||
mock_tool = Mock()
|
||||
mock_tool.name = "read_file"
|
||||
mock_tool.description = "Read a file"
|
||||
mock_tool.inputSchema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string"}
|
||||
},
|
||||
"required": ["path"]
|
||||
}
|
||||
mock.runner.tools = [mock_tool]
|
||||
mock._tools = []
|
||||
|
||||
def to_openai_tool():
|
||||
return [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "read_file",
|
||||
"description": "Read a file",
|
||||
"parameters": mock_tool.inputSchema
|
||||
}
|
||||
}
|
||||
]
|
||||
mock.to_openai_tool = to_openai_tool
|
||||
|
||||
def read_file(path: str) -> str:
|
||||
return f"Contents of {path}"
|
||||
read_file.__name__ = "read_file"
|
||||
mock._tools = [read_file]
|
||||
mock.__iter__ = lambda self: iter(self._tools)
|
||||
|
||||
return mock
|
||||
|
||||
|
||||
def sample_function(query: str) -> str:
|
||||
"""A sample regular function tool."""
|
||||
return f"Result for: {query}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 1: Single MCP Instance (Baseline)
|
||||
# ============================================================================
|
||||
|
||||
class TestSingleMCPInstance:
|
||||
"""Tests for single MCP instance - baseline functionality."""
|
||||
|
||||
def test_single_mcp_creates_tools(self, mock_mcp_with_tools):
|
||||
"""Test that a single MCP instance properly exposes tools."""
|
||||
tools = mock_mcp_with_tools.to_openai_tool()
|
||||
assert isinstance(tools, list)
|
||||
assert len(tools) == 2
|
||||
assert tools[0]["function"]["name"] == "get_current_time"
|
||||
assert tools[1]["function"]["name"] == "convert_time"
|
||||
|
||||
def test_single_mcp_is_iterable(self, mock_mcp_with_tools):
|
||||
"""Test that MCP instance is iterable."""
|
||||
tools_list = mock_mcp_with_tools._tools
|
||||
assert len(tools_list) == 2
|
||||
assert callable(tools_list[0])
|
||||
assert callable(tools_list[1])
|
||||
|
||||
def test_agent_accepts_single_mcp(self, mock_mcp_with_tools):
|
||||
"""Test that Agent accepts a single MCP instance as tools."""
|
||||
# This should not raise an error
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=mock_mcp_with_tools
|
||||
)
|
||||
# Agent wraps tools in a list if not already a list
|
||||
assert mock_mcp_with_tools in agent.tools or agent.tools == mock_mcp_with_tools
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 2: Multiple MCP Instances - Tool Definition Conversion
|
||||
# ============================================================================
|
||||
|
||||
class TestMultipleMCPToolDefinition:
|
||||
"""Tests for multiple MCP instances - tool definition conversion."""
|
||||
|
||||
def test_agent_accepts_multiple_mcp_in_list(self, mock_mcp_with_tools, mock_mcp_filesystem):
|
||||
"""Test that Agent accepts multiple MCP instances in a list."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, mock_mcp_filesystem]
|
||||
)
|
||||
assert isinstance(agent.tools, list)
|
||||
assert len(agent.tools) == 2
|
||||
|
||||
def test_format_tools_handles_multiple_mcp(self, mock_mcp_with_tools, mock_mcp_filesystem):
|
||||
"""Test that _format_tools_for_llm handles multiple MCP instances."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, mock_mcp_filesystem]
|
||||
)
|
||||
|
||||
# Call the internal method to format tools
|
||||
formatted = agent._format_tools_for_completion(agent.tools)
|
||||
|
||||
# Should have 3 tools total (2 from time + 1 from filesystem)
|
||||
assert isinstance(formatted, list)
|
||||
assert len(formatted) == 3
|
||||
|
||||
# Check tool names
|
||||
tool_names = [t["function"]["name"] for t in formatted]
|
||||
assert "get_current_time" in tool_names
|
||||
assert "convert_time" in tool_names
|
||||
assert "read_file" in tool_names
|
||||
|
||||
def test_system_prompt_includes_all_mcp_tools(self, mock_mcp_with_tools, mock_mcp_filesystem):
|
||||
"""Test that system prompt includes tools from all MCP instances."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, mock_mcp_filesystem]
|
||||
)
|
||||
|
||||
system_prompt = agent._build_system_prompt(agent.tools)
|
||||
|
||||
# Should mention all tools
|
||||
assert "get_current_time" in system_prompt
|
||||
assert "convert_time" in system_prompt
|
||||
assert "read_file" in system_prompt
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 3: Multiple MCP Instances - Tool Execution
|
||||
# ============================================================================
|
||||
|
||||
class TestMultipleMCPToolExecution:
|
||||
"""Tests for multiple MCP instances - tool execution."""
|
||||
|
||||
def test_execute_tool_finds_mcp_tool_in_list(self, mock_mcp_with_tools, mock_mcp_filesystem):
|
||||
"""Test that execute_tool_call finds tools from MCP instances in a list."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, mock_mcp_filesystem]
|
||||
)
|
||||
|
||||
# Mock the runner.call_tool method
|
||||
mock_mcp_with_tools.runner.call_tool = Mock(return_value='{"timezone": "UTC", "datetime": "2024-01-01T12:00:00Z"}')
|
||||
|
||||
# This should find and execute the tool from the first MCP instance
|
||||
result = agent.execute_tool("get_current_time", {"timezone": "UTC"})
|
||||
|
||||
# Verify the tool was called
|
||||
mock_mcp_with_tools.runner.call_tool.assert_called_once_with("get_current_time", {"timezone": "UTC"})
|
||||
|
||||
def test_execute_tool_finds_tool_from_second_mcp(self, mock_mcp_with_tools, mock_mcp_filesystem):
|
||||
"""Test that execute_tool_call finds tools from the second MCP instance."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, mock_mcp_filesystem]
|
||||
)
|
||||
|
||||
# Mock the runner.call_tool method
|
||||
mock_mcp_filesystem.runner.call_tool = Mock(return_value='Contents of /test/file.txt')
|
||||
|
||||
# This should find and execute the tool from the second MCP instance
|
||||
result = agent.execute_tool("read_file", {"path": "/test/file.txt"})
|
||||
|
||||
# Verify the tool was called
|
||||
mock_mcp_filesystem.runner.call_tool.assert_called_once_with("read_file", {"path": "/test/file.txt"})
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 4: Mixed Tools (MCP + Regular Functions)
|
||||
# ============================================================================
|
||||
|
||||
class TestMixedTools:
|
||||
"""Tests for mixed tools - MCP instances and regular functions."""
|
||||
|
||||
def test_agent_accepts_mixed_tools(self, mock_mcp_with_tools):
|
||||
"""Test that Agent accepts a mix of MCP instances and regular functions."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, sample_function]
|
||||
)
|
||||
assert isinstance(agent.tools, list)
|
||||
assert len(agent.tools) == 2
|
||||
|
||||
def test_format_tools_handles_mixed(self, mock_mcp_with_tools):
|
||||
"""Test that _format_tools_for_llm handles mixed tools."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, sample_function]
|
||||
)
|
||||
|
||||
formatted = agent._format_tools_for_completion(agent.tools)
|
||||
|
||||
# Should have 3 tools (2 from MCP + 1 regular function)
|
||||
assert isinstance(formatted, list)
|
||||
assert len(formatted) == 3
|
||||
|
||||
tool_names = [t["function"]["name"] for t in formatted]
|
||||
assert "get_current_time" in tool_names
|
||||
assert "convert_time" in tool_names
|
||||
assert "sample_function" in tool_names
|
||||
|
||||
def test_execute_mixed_tools(self, mock_mcp_with_tools):
|
||||
"""Test executing both MCP tools and regular functions."""
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
instructions="Test agent",
|
||||
tools=[mock_mcp_with_tools, sample_function]
|
||||
)
|
||||
|
||||
# Execute regular function
|
||||
result = agent.execute_tool("sample_function", {"query": "test"})
|
||||
assert "Result for: test" in str(result)
|
||||
|
||||
# Mock and execute MCP tool
|
||||
mock_mcp_with_tools.runner.call_tool = Mock(return_value='{"timezone": "UTC"}')
|
||||
result = agent.execute_tool("get_current_time", {"timezone": "UTC"})
|
||||
mock_mcp_with_tools.runner.call_tool.assert_called_once()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Integration Tests (requires actual MCP server)
|
||||
# ============================================================================
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.skipif(
|
||||
os.environ.get("SKIP_MCP_INTEGRATION") == "1",
|
||||
reason="MCP integration tests skipped"
|
||||
)
|
||||
class TestMCPIntegration:
|
||||
"""Integration tests using actual MCP servers."""
|
||||
|
||||
def test_real_time_mcp_server(self):
|
||||
"""Test with real mcp-server-time."""
|
||||
try:
|
||||
mcp = MCP("uvx mcp-server-time", timeout=30)
|
||||
tools = mcp.to_openai_tool()
|
||||
|
||||
assert isinstance(tools, list)
|
||||
assert len(tools) >= 1
|
||||
|
||||
tool_names = [t["function"]["name"] for t in tools]
|
||||
assert "get_current_time" in tool_names
|
||||
except Exception as e:
|
||||
pytest.skip(f"MCP server not available: {e}")
|
||||
|
||||
def test_multiple_real_mcp_servers(self):
|
||||
"""Test with multiple real MCP servers."""
|
||||
try:
|
||||
time_mcp = MCP("uvx mcp-server-time", timeout=30)
|
||||
|
||||
# Create agent with multiple MCP instances
|
||||
agent = Agent(
|
||||
name="MultiMCPAgent",
|
||||
instructions="Test agent with multiple MCP tools",
|
||||
tools=[time_mcp]
|
||||
)
|
||||
|
||||
# Verify tools are available
|
||||
formatted = agent._format_tools_for_completion(agent.tools)
|
||||
assert len(formatted) >= 1
|
||||
|
||||
except Exception as e:
|
||||
pytest.skip(f"MCP servers not available: {e}")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Run tests
|
||||
# ============================================================================
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v", "--tb=short"])
|
||||
@@ -5,7 +5,9 @@ Test script to verify Ollama empty response fix for both sync and async methods.
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents, TaskOutput
|
||||
import os
|
||||
import pytest
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from typing import Dict, Any
|
||||
|
||||
# Enable debug logging
|
||||
@@ -25,8 +27,8 @@ def search_tool(query: str) -> Dict[str, Any]:
|
||||
logging.debug(f"[TEST] Search tool returning: {results}")
|
||||
return results
|
||||
|
||||
# Test with both sync and async workflows
|
||||
async def test_model_async(model_name: str):
|
||||
# Helper async function to test a model
|
||||
async def _test_model_async_helper(model_name: str):
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Testing ASYNC with model: {model_name}")
|
||||
print('='*60)
|
||||
@@ -78,7 +80,7 @@ async def test_model_async(model_name: str):
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_model_sync(model_name: str):
|
||||
def _test_model_sync_helper(model_name: str):
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Testing SYNC with model: {model_name}")
|
||||
print('='*60)
|
||||
@@ -130,18 +132,60 @@ def test_model_sync(model_name: str):
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OPENAI_API_KEY"),
|
||||
reason="OPENAI_API_KEY not set - skipping integration test"
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_ollama_async_openai():
|
||||
"""Test async with OpenAI model as baseline."""
|
||||
result = await _test_model_async_helper("gpt-4o-mini")
|
||||
assert result, "OpenAI async model test failed"
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OPENAI_API_KEY"),
|
||||
reason="OPENAI_API_KEY not set - skipping integration test"
|
||||
)
|
||||
def test_ollama_sync_openai():
|
||||
"""Test sync with OpenAI model as baseline."""
|
||||
result = _test_model_sync_helper("gpt-4o-mini")
|
||||
assert result, "OpenAI sync model test failed"
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OLLAMA_HOST", "").strip(),
|
||||
reason="OLLAMA_HOST not set - skipping Ollama integration test"
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_ollama_async_ollama():
|
||||
"""Test async with Ollama model."""
|
||||
result = await _test_model_async_helper("ollama/llama3.2")
|
||||
assert result, "Ollama async model test failed"
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OLLAMA_HOST", "").strip(),
|
||||
reason="OLLAMA_HOST not set - skipping Ollama integration test"
|
||||
)
|
||||
def test_ollama_sync_ollama():
|
||||
"""Test sync with Ollama model."""
|
||||
result = _test_model_sync_helper("ollama/llama3.2")
|
||||
assert result, "Ollama sync model test failed"
|
||||
|
||||
|
||||
async def main():
|
||||
print("Testing Ollama empty response fix for both sync and async...")
|
||||
|
||||
# Test sync methods
|
||||
print("\n1. Testing SYNC methods:")
|
||||
openai_sync_success = test_model_sync("openai/gpt-5-nano")
|
||||
ollama_sync_success = test_model_sync("ollama/llama3.2")
|
||||
openai_sync_success = _test_model_sync_helper("gpt-4o-mini")
|
||||
ollama_sync_success = _test_model_sync_helper("ollama/llama3.2")
|
||||
|
||||
# Test async methods
|
||||
print("\n2. Testing ASYNC methods:")
|
||||
openai_async_success = await test_model_async("openai/gpt-5-nano")
|
||||
ollama_async_success = await test_model_async("ollama/llama3.2")
|
||||
openai_async_success = await _test_model_async_helper("gpt-4o-mini")
|
||||
ollama_async_success = await _test_model_async_helper("ollama/llama3.2")
|
||||
|
||||
# Summary
|
||||
print(f"\n{'='*60}")
|
||||
|
||||
@@ -4,7 +4,9 @@ Test script to verify Ollama empty response fix.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents, TaskOutput
|
||||
import os
|
||||
import pytest
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from typing import Dict, Any
|
||||
|
||||
# Enable debug logging
|
||||
@@ -24,8 +26,8 @@ def search_tool(query: str) -> Dict[str, Any]:
|
||||
logging.debug(f"[TEST] Search tool returning: {results}")
|
||||
return results
|
||||
|
||||
# Test with both OpenAI and Ollama models
|
||||
def test_model(model_name: str):
|
||||
# Helper function to test a model
|
||||
def _test_model_helper(model_name: str):
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Testing with model: {model_name}")
|
||||
print('='*60)
|
||||
@@ -77,16 +79,36 @@ def test_model(model_name: str):
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OPENAI_API_KEY"),
|
||||
reason="OPENAI_API_KEY not set - skipping integration test"
|
||||
)
|
||||
def test_ollama_fix_openai():
|
||||
"""Test with OpenAI model as baseline."""
|
||||
result = _test_model_helper("gpt-4o-mini")
|
||||
assert result, "OpenAI model test failed"
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("OLLAMA_HOST", "").strip(),
|
||||
reason="OLLAMA_HOST not set - skipping Ollama integration test"
|
||||
)
|
||||
def test_ollama_fix_ollama():
|
||||
"""Test with Ollama model."""
|
||||
result = _test_model_helper("ollama/llama3.2")
|
||||
assert result, "Ollama model test failed"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Testing Ollama empty response fix...")
|
||||
|
||||
# Test with OpenAI first (as baseline)
|
||||
print("\n1. Testing with OpenAI (baseline):")
|
||||
openai_success = test_model("openai/gpt-5-nano")
|
||||
openai_success = _test_model_helper("gpt-4o-mini")
|
||||
|
||||
# Test with Ollama
|
||||
print("\n2. Testing with Ollama:")
|
||||
ollama_success = test_model("ollama/llama3.2")
|
||||
ollama_success = _test_model_helper("ollama/llama3.2")
|
||||
|
||||
# Summary
|
||||
print(f"\n{'='*60}")
|
||||
|
||||
@@ -53,11 +53,14 @@ def test_ollama_argument_validation():
|
||||
print("\n2. Testing mixed arguments (the main issue):")
|
||||
mixed_args = {"a": "get_stock_price", "company_name": "Google", "b": "2"}
|
||||
filtered_args = llm._validate_and_filter_ollama_arguments("multiply", mixed_args, tools)
|
||||
expected_filtered = {"a": "get_stock_price", "b": "2"} # Should remove 'company_name'
|
||||
# Note: The method may coerce "2" to int 2 based on type hints - both are acceptable
|
||||
expected_filtered_str = {"a": "get_stock_price", "b": "2"} # String version
|
||||
expected_filtered_int = {"a": "get_stock_price", "b": 2} # Int version (coerced)
|
||||
print(f"Original: {mixed_args}")
|
||||
print(f"Filtered: {filtered_args}")
|
||||
print(f"Expected: {expected_filtered}")
|
||||
assert filtered_args == expected_filtered, f"Expected {expected_filtered}, got {filtered_args}"
|
||||
print(f"Expected: {expected_filtered_str} or {expected_filtered_int}")
|
||||
assert filtered_args == expected_filtered_str or filtered_args == expected_filtered_int, \
|
||||
f"Expected {expected_filtered_str} or {expected_filtered_int}, got {filtered_args}"
|
||||
print("✅ Mixed arguments filtering test passed")
|
||||
|
||||
# Test case 3: All invalid arguments
|
||||
|
||||
@@ -704,7 +704,8 @@ class TestPlanningAgent:
|
||||
planner = PlanningAgent()
|
||||
|
||||
mock_llm = MagicMock()
|
||||
mock_llm.achat = AsyncMock(return_value=json.dumps({
|
||||
# Mock get_response_async which is the method actually called
|
||||
mock_llm.get_response_async = AsyncMock(return_value=json.dumps({
|
||||
"name": "Test Plan",
|
||||
"description": "A test plan",
|
||||
"steps": [
|
||||
@@ -733,7 +734,8 @@ class TestPlanningAgent:
|
||||
planner = PlanningAgent()
|
||||
|
||||
mock_llm = MagicMock()
|
||||
mock_llm.achat = AsyncMock(return_value=json.dumps({
|
||||
# Mock get_response_async which is the method actually called
|
||||
mock_llm.get_response_async = AsyncMock(return_value=json.dumps({
|
||||
"name": "Refined Plan",
|
||||
"description": "Updated plan",
|
||||
"steps": [
|
||||
@@ -765,7 +767,8 @@ class TestPlanningAgent:
|
||||
planner = PlanningAgent()
|
||||
|
||||
mock_llm = MagicMock()
|
||||
mock_llm.achat = AsyncMock(return_value="Analysis: Python project with FastAPI")
|
||||
# Mock get_response_async which is the method actually called
|
||||
mock_llm.get_response_async = AsyncMock(return_value="Analysis: Python project with FastAPI")
|
||||
|
||||
planner._llm = mock_llm
|
||||
|
||||
|
||||
@@ -2,7 +2,29 @@
|
||||
"""Comprehensive test script to verify self-reflection works with tools after the fix"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.tools import duckduckgo_search
|
||||
|
||||
# Define duckduckgo_search tool locally to avoid import issues
|
||||
def duckduckgo_search(query: str) -> str:
|
||||
"""
|
||||
Search the web using DuckDuckGo.
|
||||
|
||||
Args:
|
||||
query: The search query string
|
||||
|
||||
Returns:
|
||||
Search results as a string
|
||||
"""
|
||||
try:
|
||||
from duckduckgo_search import DDGS
|
||||
with DDGS() as ddgs:
|
||||
results = list(ddgs.text(query, max_results=3))
|
||||
if results:
|
||||
return "\n".join([f"- {r.get('title', '')}: {r.get('body', '')}" for r in results])
|
||||
return "No results found"
|
||||
except ImportError:
|
||||
return f"Mock search results for: {query}"
|
||||
except Exception as e:
|
||||
return f"Search error: {str(e)}"
|
||||
|
||||
def test_self_reflection_with_tools():
|
||||
"""Test self-reflection with tools - should work after the fix"""
|
||||
|
||||
@@ -2,7 +2,23 @@
|
||||
"""Simple test to verify the self-reflection fix works"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.tools import calculator
|
||||
|
||||
# Define calculator tool locally to avoid import issues
|
||||
def calculator(expression: str) -> str:
|
||||
"""
|
||||
Evaluate a mathematical expression.
|
||||
|
||||
Args:
|
||||
expression: A mathematical expression to evaluate (e.g., "2 + 2", "25 * 17")
|
||||
|
||||
Returns:
|
||||
The result of the calculation as a string
|
||||
"""
|
||||
try:
|
||||
result = eval(expression)
|
||||
return str(result)
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
def test_self_reflection_fix():
|
||||
"""Test that self-reflection works with tools after the fix"""
|
||||
|
||||
@@ -2,7 +2,23 @@
|
||||
"""Test to verify the self-reflection fix works with tools"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.tools import calculator
|
||||
|
||||
# Define calculator tool locally to avoid import issues
|
||||
def calculator(expression: str) -> str:
|
||||
"""
|
||||
Evaluate a mathematical expression.
|
||||
|
||||
Args:
|
||||
expression: A mathematical expression to evaluate (e.g., "123 * 456")
|
||||
|
||||
Returns:
|
||||
The result of the calculation as a string
|
||||
"""
|
||||
try:
|
||||
result = eval(expression)
|
||||
return str(result)
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
def test_self_reflection_fix_verification():
|
||||
"""Test that self-reflection now works with tools"""
|
||||
|
||||
@@ -319,7 +319,14 @@ class TestExecuteWithAgents:
|
||||
result = manager.execute("test_workflow")
|
||||
|
||||
assert not result["success"]
|
||||
assert "No executor available" in result.get("error", "")
|
||||
# Error can be at top level or in results array
|
||||
error_found = "No executor available" in result.get("error", "")
|
||||
if not error_found and "results" in result:
|
||||
error_found = any(
|
||||
"No executor available" in r.get("error", "")
|
||||
for r in result.get("results", [])
|
||||
)
|
||||
assert error_found, f"Expected 'No executor available' error, got: {result}"
|
||||
|
||||
|
||||
class TestWorkflowLevelDefaults:
|
||||
|
||||
@@ -1032,7 +1032,7 @@ class TestAgentWorkflows:
|
||||
self.name = name
|
||||
self.tools = tools or []
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return f"Response from {self.name}: {message}"
|
||||
|
||||
agent = MockAgent("TestAgent", tools=["tool1", "tool2"])
|
||||
@@ -1051,7 +1051,7 @@ class TestAgentWorkflows:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return f"Processed: {message}"
|
||||
|
||||
agent = MockAgent("Processor")
|
||||
@@ -1069,7 +1069,7 @@ class TestAgentWorkflows:
|
||||
self.name = name
|
||||
self.prefix = prefix
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return f"{self.prefix}: {message}"
|
||||
|
||||
agent1 = MockAgent("Agent1", "First")
|
||||
@@ -1090,7 +1090,7 @@ class TestAgentWorkflows:
|
||||
self.name = name
|
||||
self.output = output
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return self.output
|
||||
|
||||
agent1 = MockAgent("Agent1", "Result A")
|
||||
@@ -1116,7 +1116,7 @@ class TestAgentWorkflows:
|
||||
self.name = name
|
||||
self.output = output
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return self.output
|
||||
|
||||
tech_agent = MockAgent("TechAgent", "Technical response")
|
||||
@@ -1149,7 +1149,7 @@ class TestAgentWorkflows:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return f"Processed: {message}"
|
||||
|
||||
processor = MockAgent("Processor")
|
||||
@@ -1463,7 +1463,7 @@ class TestToolsPerStep:
|
||||
self.name = name
|
||||
self.tools = tools or []
|
||||
|
||||
def chat(self, message):
|
||||
def chat(self, message, **kwargs):
|
||||
return f"Response with {len(self.tools)} tools"
|
||||
|
||||
def mock_tool():
|
||||
|
||||
@@ -1339,3 +1339,308 @@ steps:
|
||||
assert researcher is not None
|
||||
# backstory should be used as instructions
|
||||
assert researcher.backstory == "You are an experienced researcher with 10 years of experience."
|
||||
|
||||
|
||||
class TestFieldNameNormalization:
|
||||
"""Tests for field name normalization (Accept liberally, suggest canonically)."""
|
||||
|
||||
def test_roles_normalized_to_agents(self):
|
||||
"""Test that 'roles' is normalized to 'agents'."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Roles Test
|
||||
roles:
|
||||
researcher:
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
backstory: "Expert researcher"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research AI"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert 'researcher' in parser._agents
|
||||
|
||||
def test_topic_normalized_to_name(self):
|
||||
"""Test that 'topic' is normalized to 'name'."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
topic: My Topic Workflow
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.name == "My Topic Workflow"
|
||||
|
||||
def test_input_field_canonical(self):
|
||||
"""Test that 'input' field is the canonical way to specify workflow input."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Input Test Workflow
|
||||
input: "This is the workflow input"
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research {{input}}"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.default_input == "This is the workflow input"
|
||||
|
||||
def test_topic_as_input_alias(self):
|
||||
"""Test that 'topic' works as an alias for 'input' (backward compatibility)."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Topic Alias Test
|
||||
topic: "This is the topic as input"
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research {{input}}"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.default_input == "This is the topic as input"
|
||||
|
||||
def test_input_takes_precedence_over_topic(self):
|
||||
"""Test that 'input' takes precedence over 'topic' when both are present."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Precedence Test
|
||||
input: "Input value wins"
|
||||
topic: "Topic value loses"
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research {{input}}"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.default_input == "Input value wins"
|
||||
|
||||
def test_backstory_normalized_to_instructions(self):
|
||||
"""Test that 'backstory' is normalized to 'instructions'."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Backstory Test
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
backstory: "Expert researcher with 10 years experience"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research AI"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
researcher = parser._agents.get('researcher')
|
||||
assert researcher is not None
|
||||
|
||||
def test_description_normalized_to_action(self):
|
||||
"""Test that 'description' is normalized to 'action'."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Description Test
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
description: "Research AI trends"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
# The step should have been parsed with action from description
|
||||
researcher = parser._agents.get('researcher')
|
||||
assert researcher is not None
|
||||
assert hasattr(researcher, '_yaml_action')
|
||||
assert researcher._yaml_action == "Research AI trends"
|
||||
|
||||
def test_full_agents_yaml_format(self):
|
||||
"""Test that full agents.yaml format with roles, backstory, tasks is normalized."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
topic: AI Research
|
||||
framework: praisonai
|
||||
process: workflow
|
||||
|
||||
workflow:
|
||||
planning: true
|
||||
verbose: true
|
||||
|
||||
roles:
|
||||
researcher:
|
||||
role: Research Analyst
|
||||
backstory: "Expert researcher"
|
||||
goal: Research topics
|
||||
|
||||
writer:
|
||||
role: Content Writer
|
||||
backstory: "Expert writer"
|
||||
goal: Write content
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
description: "Research AI trends"
|
||||
- agent: writer
|
||||
description: "Write summary"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.name == "AI Research"
|
||||
assert 'researcher' in parser._agents
|
||||
assert 'writer' in parser._agents
|
||||
|
||||
def test_parallel_steps_description_normalized(self):
|
||||
"""Test that description in parallel steps is normalized to action."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Parallel Test
|
||||
agents:
|
||||
researcher1:
|
||||
name: Researcher1
|
||||
role: Market Analyst
|
||||
goal: Research market
|
||||
instructions: "Provide market insights"
|
||||
researcher2:
|
||||
name: Researcher2
|
||||
role: Tech Analyst
|
||||
goal: Research tech
|
||||
instructions: "Provide tech insights"
|
||||
|
||||
steps:
|
||||
- name: parallel_research
|
||||
parallel:
|
||||
- agent: researcher1
|
||||
description: "Research market trends"
|
||||
- agent: researcher2
|
||||
description: "Research tech trends"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
# Both agents should have their actions set
|
||||
researcher1 = parser._agents.get('researcher1')
|
||||
researcher2 = parser._agents.get('researcher2')
|
||||
assert researcher1 is not None
|
||||
assert researcher2 is not None
|
||||
|
||||
def test_canonical_names_work(self):
|
||||
"""Test that canonical names (agents, instructions, action, steps, name) work."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Canonical Test
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: "Provide detailed research"
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research AI trends"
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert workflow.name == "Canonical Test"
|
||||
researcher = parser._agents.get('researcher')
|
||||
assert researcher is not None
|
||||
assert researcher._yaml_action == "Research AI trends"
|
||||
|
||||
def test_mixed_old_and_new_names(self):
|
||||
"""Test that mixing old and new names works."""
|
||||
from praisonaiagents.workflows import YAMLWorkflowParser
|
||||
|
||||
yaml_content = """
|
||||
name: Mixed Test
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
backstory: "Expert researcher" # Old name
|
||||
|
||||
writer:
|
||||
name: Writer
|
||||
role: Content Writer
|
||||
goal: Write content
|
||||
instructions: "Write clearly" # New name
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research AI" # New name
|
||||
- agent: writer
|
||||
description: "Write summary" # Old name
|
||||
"""
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
assert workflow is not None
|
||||
assert 'researcher' in parser._agents
|
||||
assert 'writer' in parser._agents
|
||||
Generated
+1
-1
@@ -2422,7 +2422,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.4"
|
||||
version = "0.1.12"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
|
||||
+446
-287
@@ -30,7 +30,113 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## Key Features
|
||||
---
|
||||
|
||||
## 📑 Table of Contents
|
||||
|
||||
- [🚀 Quick Start](#-quick-start)
|
||||
- [📦 Installation](#-installation)
|
||||
- [Python SDK](#python-sdk)
|
||||
- [JavaScript SDK](#javascript-sdk)
|
||||
- [💻 Usage](#-usage)
|
||||
- [Python Code Examples](#python-code-examples)
|
||||
- [CLI / No-Code Interface](#cli--no-code-interface)
|
||||
- [JavaScript Code Examples](#javascript-code-examples)
|
||||
- [✨ Key Features](#-key-features)
|
||||
- [🌐 Supported Providers](#-supported-providers)
|
||||
- [🛠️ Custom Tools](#️-custom-tools)
|
||||
- [🔧 Configuration & Integration](#-configuration--integration)
|
||||
- [Ollama Integration](#ollama-integration)
|
||||
- [Groq Integration](#groq-integration)
|
||||
- [100+ Models Support](#100-models-support)
|
||||
- [📋 Workflows](#-workflows)
|
||||
- [Workflow Patterns](#workflow-patterns)
|
||||
- [YAML Configuration](#yaml-configuration)
|
||||
- [Agents Playbook](#agents-playbook)
|
||||
- [🧠 Memory & Context](#-memory--context)
|
||||
- [🔬 Advanced Features](#-advanced-features)
|
||||
- [Research & Intelligence](#research--intelligence)
|
||||
- [Planning & Workflows](#planning--workflows)
|
||||
- [Specialised Agents](#specialised-agents)
|
||||
- [MCP Protocol](#mcp-protocol)
|
||||
- [Safety & Control](#safety--control)
|
||||
- [Developer Tools](#developer-tools)
|
||||
- [📊 Process Types & Patterns](#-process-types--patterns)
|
||||
- [Sequential Process](#sequential-process)
|
||||
- [Hierarchical Process](#hierarchical-process)
|
||||
- [Workflow Process](#workflow-process)
|
||||
- [Agentic Patterns](#agentic-patterns)
|
||||
- [🎓 Video Tutorials](#-video-tutorials)
|
||||
- [⭐ Star History](#-star-history)
|
||||
- [👥 Contributing](#-contributing)
|
||||
- [🔧 Development](#-development)
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
Get started with PraisonAI in under 1 minute:
|
||||
|
||||
```bash
|
||||
# Install
|
||||
pip install praisonaiagents
|
||||
|
||||
# Set API key
|
||||
export OPENAI_API_KEY=your_key_here
|
||||
|
||||
# Create a simple agent
|
||||
python -c "from praisonaiagents import Agent; Agent(instructions='You are a helpful AI assistant').start('Write a haiku about AI')"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 Installation
|
||||
|
||||
### Python SDK
|
||||
|
||||
Lightweight package dedicated for coding:
|
||||
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
```
|
||||
|
||||
For the full framework with CLI support:
|
||||
|
||||
```bash
|
||||
pip install praisonai
|
||||
```
|
||||
|
||||
### JavaScript SDK
|
||||
|
||||
```bash
|
||||
npm install praisonai
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 💻 Usage
|
||||
|
||||
### Python Code Examples
|
||||
|
||||
- [📘 Complete Python Examples](#using-python-code)
|
||||
- [🔧 Custom Tools](#️-custom-tools)
|
||||
- [🧠 Memory Configuration](#-memory--context)
|
||||
|
||||
### CLI / No-Code Interface
|
||||
|
||||
- [🎯 Auto Mode](#auto-mode)
|
||||
- [🔬 Deep Research](#deep-research-cli)
|
||||
- [📋 Planning Mode](#planning-mode-cli)
|
||||
- [💾 Memory Management](#memory-cli)
|
||||
- [🔄 Workflow Execution](#workflow-cli)
|
||||
|
||||
### JavaScript Code Examples
|
||||
|
||||
- [💻 JavaScript Usage](#using-javascript-code)
|
||||
|
||||
---
|
||||
|
||||
## ✨ Key Features
|
||||
|
||||
| Feature | Code | Docs |
|
||||
|---------|:----:|:----:|
|
||||
@@ -114,7 +220,11 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
| 💾 Auto-Save Sessions | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
| 📜 History in Context | [Example](#session-management-python) | [📖](https://docs.praison.ai/cli/session) |
|
||||
|
||||
## Supported Providers
|
||||
---
|
||||
|
||||
## 🌐 Supported Providers
|
||||
|
||||
PraisonAI supports 100+ LLM providers through seamless integration:
|
||||
|
||||
| Provider | Example |
|
||||
|----------|:-------:|
|
||||
@@ -143,14 +253,14 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
|
||||
| Moonshot | [Example](examples/python/providers/moonshot/moonshot_example.py) |
|
||||
| vLLM | [Example](examples/python/providers/vllm/vllm_example.py) |
|
||||
|
||||
## Using Python Code
|
||||
---
|
||||
|
||||
## 📘 Using Python Code
|
||||
|
||||
Light weight package dedicated for coding:
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
```
|
||||
|
||||
```bash
|
||||
pip install praisonaiagents
|
||||
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
```
|
||||
|
||||
@@ -605,6 +715,77 @@ result = manager.execute_yaml(
|
||||
)
|
||||
```
|
||||
|
||||
### Complete workflow.yaml Reference
|
||||
|
||||
```yaml
|
||||
# workflow.yaml - Full feature reference
|
||||
name: Complete Workflow
|
||||
description: Demonstrates all workflow.yaml features
|
||||
framework: praisonai # praisonai, crewai, autogen
|
||||
process: workflow # sequential, hierarchical, workflow
|
||||
|
||||
workflow:
|
||||
planning: true
|
||||
planning_llm: gpt-4o
|
||||
reasoning: true
|
||||
verbose: true
|
||||
memory_config:
|
||||
provider: chroma
|
||||
persist: true
|
||||
|
||||
variables:
|
||||
topic: AI trends
|
||||
items: [ML, NLP, Vision]
|
||||
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics thoroughly
|
||||
instructions: "Provide detailed research findings"
|
||||
backstory: "Expert researcher with 10 years experience" # alias for instructions
|
||||
llm: gpt-4o-mini
|
||||
function_calling_llm: gpt-4o # For tool calls
|
||||
max_rpm: 10 # Rate limiting
|
||||
max_execution_time: 300 # Timeout in seconds
|
||||
reflect_llm: gpt-4o # For self-reflection
|
||||
min_reflect: 1
|
||||
max_reflect: 3
|
||||
system_template: "You are a helpful assistant"
|
||||
tools:
|
||||
- tavily_search
|
||||
|
||||
writer:
|
||||
name: Writer
|
||||
role: Content Writer
|
||||
goal: Write clear content
|
||||
instructions: "Write engaging content"
|
||||
|
||||
steps:
|
||||
- name: research_step
|
||||
agent: researcher
|
||||
action: "Research {{topic}}"
|
||||
expected_output: "Comprehensive research report"
|
||||
output_file: "output/research.md"
|
||||
create_directory: true
|
||||
|
||||
- name: writing_step
|
||||
agent: writer
|
||||
action: "Write article based on research"
|
||||
context: # Task dependencies
|
||||
- research_step
|
||||
output_json: # Structured output
|
||||
type: object
|
||||
properties:
|
||||
title: { type: string }
|
||||
content: { type: string }
|
||||
|
||||
callbacks:
|
||||
on_workflow_start: log_start
|
||||
on_step_complete: log_step
|
||||
on_workflow_complete: log_complete
|
||||
```
|
||||
|
||||
### 9. Hooks
|
||||
|
||||
Configure in `.praison/hooks.json`:
|
||||
@@ -621,9 +802,50 @@ hooks.register("pre_write_code", lambda ctx: print(f"Writing {ctx['file']}"))
|
||||
result = hooks.execute("pre_write_code", {"file": "main.py"})
|
||||
```
|
||||
|
||||
### 10. Extended agents.yaml with Workflow Patterns
|
||||
### 10. Field Names Reference (A-I-G-S)
|
||||
|
||||
You can now use advanced workflow patterns directly in agents.yaml by setting `process: workflow`:
|
||||
PraisonAI accepts both old (agents.yaml) and new (workflow.yaml) field names. Use the **canonical names** for new projects:
|
||||
|
||||
| Canonical (Recommended) | Alias (Also Works) | Purpose |
|
||||
|-------------------------|-------------------|---------|
|
||||
| `agents` | `roles` | Define agent personas |
|
||||
| `instructions` | `backstory` | Agent behavior/persona |
|
||||
| `action` | `description` | What the step does |
|
||||
| `steps` | `tasks` (nested) | Define work items |
|
||||
| `name` | `topic` | Workflow identifier |
|
||||
|
||||
**A-I-G-S Mnemonic** - Easy to remember:
|
||||
- **A**gents - Who does the work
|
||||
- **I**nstructions - How they behave
|
||||
- **G**oal - What they achieve
|
||||
- **S**teps - What they do
|
||||
|
||||
```yaml
|
||||
# Quick Reference - Canonical Format
|
||||
name: My Workflow # Workflow name (not 'topic')
|
||||
agents: # Define agents (not 'roles')
|
||||
my_agent:
|
||||
role: Job Title # Agent's role
|
||||
goal: What to achieve # Agent's goal
|
||||
instructions: How to act # Agent's behavior (not 'backstory')
|
||||
|
||||
steps: # Define steps (not 'tasks')
|
||||
- agent: my_agent
|
||||
action: What to do # Step action (not 'description')
|
||||
```
|
||||
|
||||
> **Note:** The parser accepts both old and new names. Run `praisonai workflow validate <file.yaml>` to see suggestions for canonical names.
|
||||
|
||||
### 11. Extended agents.yaml with Workflow Patterns
|
||||
|
||||
**Feature Parity:** Both `agents.yaml` and `workflow.yaml` now support the same features:
|
||||
- All workflow patterns (route, parallel, loop, repeat)
|
||||
- All agent fields (function_calling_llm, max_rpm, max_execution_time, reflect_llm, templates)
|
||||
- All step fields (expected_output, context, output_json, create_directory, callback)
|
||||
- Framework support (praisonai, crewai, autogen)
|
||||
- Process types (sequential, hierarchical, workflow)
|
||||
|
||||
You can use advanced workflow patterns directly in agents.yaml by setting `process: workflow`:
|
||||
|
||||
```yaml
|
||||
# agents.yaml with workflow patterns
|
||||
@@ -639,15 +861,15 @@ workflow:
|
||||
variables:
|
||||
topic: AI trends
|
||||
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
classifier:
|
||||
role: Request Classifier
|
||||
backstory: "Classify requests into categories"
|
||||
instructions: "Classify requests into categories" # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Classify requests
|
||||
|
||||
researcher:
|
||||
role: Research Analyst
|
||||
backstory: "Expert researcher"
|
||||
instructions: "Expert researcher" # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Research topics
|
||||
tools:
|
||||
- tavily_search
|
||||
@@ -690,9 +912,13 @@ Run with the same simple command:
|
||||
praisonai agents.yaml
|
||||
```
|
||||
|
||||
## Using No Code
|
||||
---
|
||||
|
||||
### Auto Mode:
|
||||
## 🎯 CLI / No-Code Interface
|
||||
|
||||
PraisonAI provides a powerful CLI for no-code automation and quick prototyping.
|
||||
|
||||
### Auto Mode
|
||||
```bash
|
||||
pip install praisonai
|
||||
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
|
||||
@@ -839,10 +1065,43 @@ praisonai "GPT-5" --workflow "Research:Search for info,Write:Write blog" --tools
|
||||
praisonai workflow help
|
||||
```
|
||||
|
||||
#### YAML Workflow Files:
|
||||
```bash
|
||||
# Run a YAML workflow file
|
||||
praisonai workflow run research.yaml
|
||||
|
||||
# Run with variables
|
||||
praisonai workflow run research.yaml --var topic="AI trends"
|
||||
|
||||
# Validate a YAML workflow
|
||||
praisonai workflow validate research.yaml
|
||||
|
||||
# Create from template (simple, routing, parallel, loop, evaluator-optimizer)
|
||||
praisonai workflow template routing --output my_workflow.yaml
|
||||
```
|
||||
|
||||
#### Auto-Generate Workflows:
|
||||
```bash
|
||||
# Auto-generate a sequential workflow from topic
|
||||
praisonai workflow auto "Research AI trends"
|
||||
|
||||
# Generate parallel workflow (multiple agents work concurrently)
|
||||
praisonai workflow auto "Research AI trends" --pattern parallel
|
||||
|
||||
# Generate routing workflow (classifier routes to specialists)
|
||||
praisonai workflow auto "Build a chatbot" --pattern routing
|
||||
|
||||
# Specify output file
|
||||
praisonai workflow auto "Research AI" --pattern sequential --output my_workflow.yaml
|
||||
```
|
||||
|
||||
**Workflow CLI Options:**
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `--workflow-var key=value` | Set workflow variable (can be repeated) |
|
||||
| `--var key=value` | Set variable for YAML workflows |
|
||||
| `--pattern <pattern>` | Pattern for auto-generation (sequential, parallel, routing, loop) |
|
||||
| `--output <file>` | Output file for auto-generation |
|
||||
| `--llm <model>` | LLM model (e.g., openai/gpt-4o-mini) |
|
||||
| `--tools <tools>` | Tools (comma-separated, e.g., tavily) |
|
||||
| `--planning` | Enable planning mode |
|
||||
@@ -1264,6 +1523,21 @@ agent = Agent(
|
||||
env={"BRAVE_API_KEY": "your-key"}
|
||||
)
|
||||
)
|
||||
|
||||
# Multiple MCP servers + regular functions
|
||||
def my_custom_tool(query: str) -> str:
|
||||
"""Custom tool function."""
|
||||
return f"Result: {query}"
|
||||
|
||||
agent = Agent(
|
||||
name="MultiToolAgent",
|
||||
instructions="Agent with multiple MCP servers",
|
||||
tools=[
|
||||
MCP("uvx mcp-server-time"), # Time tools
|
||||
MCP("npx @modelcontextprotocol/server-memory"), # Memory tools
|
||||
my_custom_tool # Regular function
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
### MCP Server (Expose Tools as MCP Server)
|
||||
@@ -1328,7 +1602,9 @@ server.run() # stdio for Claude Desktop
|
||||
| 📥 Web Fetch - URL content retrieval | [📖](https://docs.praison.ai/cli/web-fetch) |
|
||||
| 💾 Prompt Caching - Cost reduction | [📖](https://docs.praison.ai/cli/prompt-caching) |
|
||||
|
||||
## Using JavaScript Code
|
||||
---
|
||||
|
||||
## 💻 Using JavaScript Code
|
||||
|
||||
```bash
|
||||
npm install praisonai
|
||||
@@ -1343,11 +1619,17 @@ agent.start('Write a movie script about a robot in Mars');
|
||||
|
||||

|
||||
|
||||
## Star History
|
||||
---
|
||||
|
||||
## ⭐ Star History
|
||||
|
||||
[](https://docs.praison.ai)
|
||||
|
||||
## AI Agents Flow
|
||||
---
|
||||
|
||||
## 📊 Process Types & Patterns
|
||||
|
||||
### AI Agents Flow
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
@@ -1686,31 +1968,40 @@ flowchart LR
|
||||
style Out fill:#8B0000,color:#fff
|
||||
```
|
||||
|
||||
## Adding Models
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai/models">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/%F0%9F%93%9A_Models-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Models" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
## 🔧 Configuration & Integration
|
||||
|
||||
### Ollama Integration
|
||||
|
||||
## Ollama Integration
|
||||
```bash
|
||||
export OPENAI_BASE_URL=http://localhost:11434/v1
|
||||
```
|
||||
|
||||
## Groq Integration
|
||||
### Groq Integration
|
||||
|
||||
Replace xxxx with Groq API KEY:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY=xxxxxxxxxxx
|
||||
export OPENAI_BASE_URL=https://api.groq.com/openai/v1
|
||||
```
|
||||
|
||||
## No Code Options
|
||||
### 100+ Models Support
|
||||
|
||||
## Agents Playbook
|
||||
PraisonAI supports 100+ LLM models from various providers. Visit our [models documentation](https://docs.praison.ai/models/) for the complete list.
|
||||
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai/models">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/📚_Models_Documentation-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Models Documentation" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
---
|
||||
|
||||
## 📋 Agents Playbook
|
||||
|
||||
### Simple Playbook Example
|
||||
|
||||
@@ -1719,9 +2010,9 @@ Create `agents.yaml` file and add the code below:
|
||||
```yaml
|
||||
framework: praisonai
|
||||
topic: Artificial Intelligence
|
||||
roles:
|
||||
agents: # Canonical: use 'agents' instead of 'roles'
|
||||
screenwriter:
|
||||
backstory: "Skilled in crafting scripts with engaging dialogue about {topic}."
|
||||
instructions: "Skilled in crafting scripts with engaging dialogue about {topic}." # Canonical: use 'instructions' instead of 'backstory'
|
||||
goal: Create scripts from concepts.
|
||||
role: Screenwriter
|
||||
tasks:
|
||||
@@ -1735,18 +2026,11 @@ roles:
|
||||
praisonai agents.yaml
|
||||
```
|
||||
|
||||
## Use 100+ Models
|
||||
---
|
||||
|
||||
- https://docs.praison.ai/models/
|
||||
<div align="center">
|
||||
<a href="https://docs.praison.ai">
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/📚_Documentation-Visit_docs.praison.ai-blue?style=for-the-badge&logo=bookstack&logoColor=white" alt="Documentation" />
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
## 🛠️ Custom Tools / Create Plugins
|
||||
|
||||
## Custom Tools
|
||||
PraisonAI supports multiple ways to create and integrate custom tools (plugins) into your agents.
|
||||
|
||||
### Using `@tool` Decorator
|
||||
|
||||
@@ -1819,276 +2103,96 @@ After `pip install`, tools are auto-discovered:
|
||||
agent = Agent(tools=["my_tool"]) # Works automatically!
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Prompt Expansion
|
||||
## 🧠 Memory & Context
|
||||
|
||||
Expand short prompts into detailed, actionable prompts:
|
||||
PraisonAI provides zero-dependency persistent memory for agents. For detailed examples, see [section 6. Agent Memory](#6-agent-memory-zero-dependencies) in the Python Code Examples.
|
||||
|
||||
### CLI Usage
|
||||
---
|
||||
|
||||
```bash
|
||||
# Expand a short prompt into detailed prompt
|
||||
praisonai "write a movie script in 3 lines" --expand-prompt
|
||||
## 🔬 Advanced Features
|
||||
|
||||
# With verbose output
|
||||
praisonai "blog about AI" --expand-prompt -v
|
||||
### Research & Intelligence
|
||||
|
||||
# With tools for context gathering
|
||||
praisonai "latest AI trends" --expand-prompt --expand-tools tools.py
|
||||
- 🔬 **Deep Research Agents** - OpenAI & Gemini support for automated research
|
||||
- 🔄 **Query Rewriter Agent** - HyDE, Step-back, Multi-query strategies for RAG optimization
|
||||
- 🌐 **Native Web Search** - Real-time search via OpenAI, Gemini, Anthropic, xAI, Perplexity
|
||||
- 📥 **Web Fetch** - Retrieve full content from URLs (Anthropic)
|
||||
- 📝 **Prompt Expander Agent** - Expand short prompts into detailed instructions
|
||||
|
||||
# Combine with query rewrite
|
||||
praisonai "AI news" --query-rewrite --expand-prompt
|
||||
```
|
||||
### Memory & Caching
|
||||
|
||||
### Programmatic Usage
|
||||
- 💾 **Prompt Caching** - Reduce costs & latency (OpenAI, Anthropic, Bedrock, Deepseek)
|
||||
- 🧠 **Claude Memory Tool** - Persistent cross-conversation memory (Anthropic Beta)
|
||||
- 💾 **File-Based Memory** - Zero-dependency persistent memory for all agents
|
||||
- 🔍 **Built-in Search Tools** - Tavily, You.com, Exa for web search, news, content extraction
|
||||
|
||||
```python
|
||||
from praisonaiagents import PromptExpanderAgent, ExpandStrategy
|
||||
### Planning & Workflows
|
||||
|
||||
# Basic usage
|
||||
agent = PromptExpanderAgent()
|
||||
result = agent.expand("write a movie script in 3 lines")
|
||||
print(result.expanded_prompt)
|
||||
- 📋 **Planning Mode** - Plan before execution for agents & multi-agent systems
|
||||
- 🔧 **Planning Tools** - Research with tools during planning phase
|
||||
- 🧠 **Planning Reasoning** - Chain-of-thought planning for complex tasks
|
||||
- ⛓️ **Prompt Chaining** - Sequential prompt workflows with conditional gates
|
||||
- 🔍 **Evaluator Optimiser** - Generate and optimize through iterative feedback
|
||||
- 👷 **Orchestrator Workers** - Distribute tasks among specialised workers
|
||||
- ⚡ **Parallelisation** - Execute tasks in parallel for improved performance
|
||||
- 🔁 **Repetitive Agents** - Handle repetitive tasks through automated loops
|
||||
- 🤖 **Autonomous Workflow** - Monitor, act, adapt based on environment feedback
|
||||
|
||||
# With specific strategy
|
||||
result = agent.expand("blog about AI", strategy=ExpandStrategy.DETAILED)
|
||||
### Specialised Agents
|
||||
|
||||
# Available strategies: BASIC, DETAILED, STRUCTURED, CREATIVE, AUTO
|
||||
```
|
||||
- 🖼️ **Image Generation Agent** - Create images from text descriptions
|
||||
- 📷 **Image to Text Agent** - Extract text and descriptions from images
|
||||
- 🎬 **Video Agent** - Analyse and process video content
|
||||
- 📊 **Data Analyst Agent** - Analyse data and generate insights
|
||||
- 💰 **Finance Agent** - Financial analysis and recommendations
|
||||
- 🛒 **Shopping Agent** - Price comparison and shopping assistance
|
||||
- ⭐ **Recommendation Agent** - Personalised recommendations
|
||||
- 📖 **Wikipedia Agent** - Search and extract Wikipedia information
|
||||
- 💻 **Programming Agent** - Code development and analysis
|
||||
- 📝 **Markdown Agent** - Generate and format Markdown content
|
||||
- 🔀 **Router Agent** - Dynamic task routing with cost optimisation
|
||||
|
||||
**Key Difference:**
|
||||
- `--query-rewrite`: Optimizes queries for **search/retrieval** (RAG)
|
||||
- `--expand-prompt`: Expands prompts for **detailed task execution**
|
||||
### MCP Protocol
|
||||
|
||||
- 🔌 **MCP Transports** - stdio, Streamable HTTP, WebSocket, SSE (Protocol 2025-11-25)
|
||||
- 🌐 **WebSocket MCP** - Real-time bidirectional connections with auto-reconnect
|
||||
- 🔐 **MCP Security** - Origin validation, DNS rebinding prevention, secure sessions
|
||||
- 🔄 **MCP Resumability** - SSE stream recovery via Last-Event-ID
|
||||
|
||||
## Web Search, Web Fetch & Prompt Caching
|
||||
### Safety & Control
|
||||
|
||||
### CLI Usage
|
||||
- 🤝 **Agent Handoffs** - Transfer context between specialised agents
|
||||
- 🛡️ **Guardrails** - Input/output validation and safety checks
|
||||
- ✅ **Human Approval** - Require human confirmation for critical actions
|
||||
- 💬 **Sessions Management** - Isolated conversation contexts
|
||||
- 🔄 **Stateful Agents** - Maintain state across interactions
|
||||
|
||||
```bash
|
||||
# Web Search - Get real-time information
|
||||
praisonai "What are the latest AI news today?" --web-search --llm openai/gpt-4o-search-preview
|
||||
### Developer Tools
|
||||
|
||||
# Web Fetch - Retrieve and analyze URL content (Anthropic only)
|
||||
praisonai "Summarize https://docs.praison.ai" --web-fetch --llm anthropic/claude-sonnet-4-20250514
|
||||
- ⚡ **Fast Context** - Rapid parallel code search (10-20x faster)
|
||||
- 📜 **Rules & Instructions** - Auto-discover CLAUDE.md, AGENTS.md, GEMINI.md
|
||||
- 🪝 **Hooks** - Pre/post operation hooks for custom logic
|
||||
- 📈 **Telemetry** - Track agent performance and usage
|
||||
- 📹 **Camera Integration** - Capture and analyse camera input
|
||||
|
||||
# Prompt Caching - Reduce costs for repeated prompts
|
||||
praisonai "Analyze this document..." --prompt-caching --llm anthropic/claude-sonnet-4-20250514
|
||||
```
|
||||
### Other Features
|
||||
|
||||
### Programmatic Usage
|
||||
- 🔄 **CrewAI & AG2 Integration** - Use CrewAI or AG2 (Formerly AutoGen) Framework
|
||||
- 💻 **Codebase Chat** - Chat with entire codebase
|
||||
- 🎨 **Interactive UIs** - Multiple interactive interfaces
|
||||
- 📄 **YAML Configuration** - YAML-based agent and workflow configuration
|
||||
- 🛠️ **Custom Tools** - Easy custom tool integration
|
||||
- 🔍 **Internet Search** - Multiple providers (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
|
||||
- 🖼️ **VLM Support** - Vision Language Model support
|
||||
- 🎙️ **Voice Interaction** - Real-time voice interaction
|
||||
|
||||
```python
|
||||
from praisonaiagents import Agent
|
||||
---
|
||||
|
||||
# Web Search
|
||||
agent = Agent(
|
||||
instructions="You are a research assistant",
|
||||
llm="openai/gpt-4o-search-preview",
|
||||
web_search=True
|
||||
)
|
||||
## 🎓 Video Tutorials
|
||||
|
||||
# Web Fetch (Anthropic only)
|
||||
agent = Agent(
|
||||
instructions="You are a content analyzer",
|
||||
llm="anthropic/claude-sonnet-4-20250514",
|
||||
web_fetch=True
|
||||
)
|
||||
|
||||
# Prompt Caching
|
||||
agent = Agent(
|
||||
instructions="You are an AI assistant..." * 50, # Long system prompt
|
||||
llm="anthropic/claude-sonnet-4-20250514",
|
||||
prompt_caching=True
|
||||
)
|
||||
```
|
||||
|
||||
**Supported Providers:**
|
||||
| Feature | Providers |
|
||||
|---------|-----------|
|
||||
| Web Search | OpenAI, Gemini, Anthropic, xAI, Perplexity |
|
||||
| Web Fetch | Anthropic |
|
||||
| Prompt Caching | OpenAI (auto), Anthropic, Bedrock, Deepseek |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## MCP (Model Context Protocol)
|
||||
|
||||
PraisonAI supports MCP Protocol Revision 2025-11-25 with multiple transports.
|
||||
|
||||
### MCP Client (Consume MCP Servers)
|
||||
|
||||
```python
|
||||
from praisonaiagents import Agent, MCP
|
||||
|
||||
# stdio - Local NPX/Python servers
|
||||
agent = Agent(tools=MCP("npx @modelcontextprotocol/server-memory"))
|
||||
|
||||
# Streamable HTTP - Production servers
|
||||
agent = Agent(tools=MCP("https://api.example.com/mcp"))
|
||||
|
||||
# WebSocket - Real-time bidirectional
|
||||
agent = Agent(tools=MCP("wss://api.example.com/mcp", auth_token="token"))
|
||||
|
||||
# SSE (Legacy) - Backward compatibility
|
||||
agent = Agent(tools=MCP("http://localhost:8080/sse"))
|
||||
|
||||
# With environment variables
|
||||
agent = Agent(
|
||||
tools=MCP(
|
||||
command="npx",
|
||||
args=["-y", "@modelcontextprotocol/server-brave-search"],
|
||||
env={"BRAVE_API_KEY": "your-key"}
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### MCP Server (Expose Tools as MCP Server)
|
||||
|
||||
Expose your Python functions as MCP tools for Claude Desktop, Cursor, and other MCP clients:
|
||||
|
||||
```python
|
||||
from praisonaiagents.mcp import ToolsMCPServer
|
||||
|
||||
def search_web(query: str, max_results: int = 5) -> dict:
|
||||
"""Search the web for information."""
|
||||
return {"results": [f"Result for {query}"]}
|
||||
|
||||
def calculate(expression: str) -> dict:
|
||||
"""Evaluate a mathematical expression."""
|
||||
return {"result": eval(expression)}
|
||||
|
||||
# Create and run MCP server
|
||||
server = ToolsMCPServer(name="my-tools")
|
||||
server.register_tools([search_web, calculate])
|
||||
server.run() # stdio for Claude Desktop
|
||||
# server.run_sse(host="0.0.0.0", port=8080) # SSE for web clients
|
||||
```
|
||||
|
||||
### MCP Features
|
||||
|
||||
| Feature | Description |
|
||||
|---------|-------------|
|
||||
| **Session Management** | Automatic `Mcp-Session-Id` handling |
|
||||
| **Protocol Versioning** | `Mcp-Protocol-Version` header |
|
||||
| **Resumability** | SSE stream recovery via `Last-Event-ID` |
|
||||
| **Security** | Origin validation, DNS rebinding prevention |
|
||||
| **WebSocket** | Auto-reconnect with exponential backoff |
|
||||
|
||||
|
||||
## Development:
|
||||
|
||||
Below is used for development only.
|
||||
|
||||
### Using uv
|
||||
```bash
|
||||
# Install uv if you haven't already
|
||||
pip install uv
|
||||
|
||||
# Install from requirements
|
||||
uv pip install -r pyproject.toml
|
||||
|
||||
# Install with extras
|
||||
uv pip install -r pyproject.toml --extra code
|
||||
uv pip install -r pyproject.toml --extra "crewai,autogen"
|
||||
```
|
||||
|
||||
### Bump and Release
|
||||
|
||||
```bash
|
||||
# From project root - bumps version and releases in one command
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99
|
||||
|
||||
# With praisonaiagents dependency
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99 --agents 0.0.169
|
||||
|
||||
# Then publish
|
||||
cd src/praisonai && uv publish
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
- Fork on GitHub: Use the "Fork" button on the repository page.
|
||||
- Clone your fork: `git clone https://github.com/yourusername/praisonAI.git`
|
||||
- Create a branch: `git checkout -b new-feature`
|
||||
- Make changes and commit: `git commit -am "Add some feature"`
|
||||
- Push to your fork: `git push origin new-feature`
|
||||
- Submit a pull request via GitHub's web interface.
|
||||
- Await feedback from project maintainers.
|
||||
|
||||
## Advanced Features
|
||||
|
||||
**Research & Intelligence:**
|
||||
- 🔬 **Deep Research Agents** (OpenAI & Gemini)
|
||||
- 🔄 **Query Rewriter Agent** (HyDE, Step-back, Multi-query)
|
||||
- 🌐 **Native Web Search** (OpenAI, Gemini, Anthropic, xAI, Perplexity)
|
||||
- 📥 **Web Fetch** (Retrieve full content from URLs - Anthropic)
|
||||
- 📝 **Prompt Expander Agent** (Expand short prompts into detailed instructions)
|
||||
|
||||
**Memory & Caching:**
|
||||
- 💾 **Prompt Caching** (Reduce costs & latency - OpenAI, Anthropic, Bedrock, Deepseek)
|
||||
- 🧠 **Claude Memory Tool** (Persistent cross-conversation memory - Anthropic Beta)
|
||||
- 💾 **File-Based Memory** (Zero-dependency persistent memory for all agents)
|
||||
- 🔍 **Built-in Search Tools** (Tavily, You.com, Exa - web search, news, content extraction)
|
||||
|
||||
**Planning & Workflows:**
|
||||
- 📋 **Planning Mode** (Plan before execution - Agent & Multi-Agent)
|
||||
- 🔧 **Planning Tools** (Research with tools during planning)
|
||||
- 🧠 **Planning Reasoning** (Chain-of-thought planning)
|
||||
- ⛓️ **Prompt Chaining** (Sequential prompt workflows with gates)
|
||||
- 🔍 **Evaluator Optimiser** (Generate and optimize through iterative feedback)
|
||||
- 👷 **Orchestrator Workers** (Distribute tasks among specialized workers)
|
||||
- ⚡ **Parallelisation** (Execute tasks in parallel for improved performance)
|
||||
- 🔁 **Repetitive Agents** (Handle repetitive tasks through automated loops)
|
||||
- 🤖 **Autonomous Workflow** (Monitor, act, adapt based on environment feedback)
|
||||
|
||||
**Agent Types:**
|
||||
- 🖼️ **Image Generation Agent** (Create images from text descriptions)
|
||||
- 📷 **Image to Text Agent** (Extract text and descriptions from images)
|
||||
- 🎬 **Video Agent** (Analyze and process video content)
|
||||
- 📊 **Data Analyst Agent** (Analyze data and generate insights)
|
||||
- 💰 **Finance Agent** (Financial analysis and recommendations)
|
||||
- 🛒 **Shopping Agent** (Price comparison and shopping assistance)
|
||||
- ⭐ **Recommendation Agent** (Personalized recommendations)
|
||||
- 📖 **Wikipedia Agent** (Search and extract Wikipedia information)
|
||||
- 💻 **Programming Agent** (Code development and analysis)
|
||||
- 📝 **Markdown Agent** (Generate and format Markdown content)
|
||||
- 🔀 **Router Agent** (Dynamic task routing with cost optimization)
|
||||
|
||||
**MCP Protocol:**
|
||||
- 🔌 **MCP Transports** (stdio, Streamable HTTP, WebSocket, SSE - Protocol 2025-11-25)
|
||||
- 🌐 **WebSocket MCP** (Real-time bidirectional connections with auto-reconnect)
|
||||
- 🔐 **MCP Security** (Origin validation, DNS rebinding prevention, secure sessions)
|
||||
- 🔄 **MCP Resumability** (SSE stream recovery via Last-Event-ID)
|
||||
|
||||
**Safety & Control:**
|
||||
- 🤝 **Agent Handoffs** (Transfer context between specialized agents)
|
||||
- 🛡️ **Guardrails** (Input/output validation and safety checks)
|
||||
- ✅ **Human Approval** (Require human confirmation for critical actions)
|
||||
- 💬 **Sessions Management** (Isolated conversation contexts)
|
||||
- 🔄 **Stateful Agents** (Maintain state across interactions)
|
||||
|
||||
**Developer Tools:**
|
||||
- ⚡ **Fast Context** (Rapid parallel code search - 10-20x faster than traditional methods)
|
||||
- 📜 **Rules & Instructions** (Auto-discover CLAUDE.md, AGENTS.md, GEMINI.md)
|
||||
- 🪝 **Hooks** (Pre/post operation hooks for custom logic)
|
||||
- 📈 **Telemetry** (Track agent performance and usage)
|
||||
- 📹 **Camera Integration** (Capture and analyze camera input)
|
||||
|
||||
## Other Features
|
||||
|
||||
- 🔄 Use CrewAI or AG2 (Formerly AutoGen) Framework
|
||||
- 💻 Chat with ENTIRE Codebase
|
||||
- 🎨 Interactive UIs
|
||||
- 📄 YAML-based Configuration
|
||||
- 🛠️ Custom Tool Integration
|
||||
- 🔍 Internet Search Capability (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
|
||||
- 🖼️ Vision Language Model (VLM) Support
|
||||
- 🎙️ Real-time Voice Interaction
|
||||
|
||||
## Video Tutorials
|
||||
Learn PraisonAI through our comprehensive video series:
|
||||
|
||||
| Topic | Video |
|
||||
|-------|--------|
|
||||
@@ -2115,3 +2219,58 @@ cd src/praisonai && uv publish
|
||||
| Call Interface | [](https://www.youtube.com/watch?v=m1cwrUG2iAk) |
|
||||
| Reasoning Extract Agents | [](https://www.youtube.com/watch?v=2PPamsADjJA) |
|
||||
|
||||
---
|
||||
|
||||
## 👥 Contributing
|
||||
|
||||
We welcome contributions from the community! Here's how you can contribute:
|
||||
|
||||
1. **Fork on GitHub** - Use the "Fork" button on the [repository page](https://github.com/MervinPraison/PraisonAI)
|
||||
2. **Clone your fork** - `git clone https://github.com/yourusername/praisonAI.git`
|
||||
3. **Create a branch** - `git checkout -b new-feature`
|
||||
4. **Make changes and commit** - `git commit -am "Add some feature"`
|
||||
5. **Push to your fork** - `git push origin new-feature`
|
||||
6. **Submit a pull request** - Via GitHub's web interface
|
||||
7. **Await feedback** - From project maintainers
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Development
|
||||
|
||||
### Using uv
|
||||
|
||||
```bash
|
||||
# Install uv if you haven't already
|
||||
pip install uv
|
||||
|
||||
# Install from requirements
|
||||
uv pip install -r pyproject.toml
|
||||
|
||||
# Install with extras
|
||||
uv pip install -r pyproject.toml --extra code
|
||||
uv pip install -r pyproject.toml --extra "crewai,autogen"
|
||||
```
|
||||
|
||||
### Bump and Release
|
||||
|
||||
```bash
|
||||
# From project root - bumps version and releases in one command
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99
|
||||
|
||||
# With praisonaiagents dependency
|
||||
python src/praisonai/scripts/bump_and_release.py 2.2.99 --agents 0.0.169
|
||||
|
||||
# Then publish
|
||||
cd src/praisonai && uv publish
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
<p><strong>Made with ❤️ by the PraisonAI Team</strong></p>
|
||||
<p>
|
||||
<a href="https://docs.praison.ai">Documentation</a> •
|
||||
<a href="https://github.com/MervinPraison/PraisonAI">GitHub</a> •
|
||||
<a href="https://github.com/MervinPraison/PraisonAI/issues">Issues</a>
|
||||
</p>
|
||||
</div>
|
||||
+41
-36
@@ -1,44 +1,49 @@
|
||||
# Movie Script Workflow - Cat in Mars
|
||||
# Uses the modern agents + steps format
|
||||
|
||||
name: Create Movie Script About Cat in Mars
|
||||
description: A sequential workflow to research, design narrative, and write a movie script
|
||||
framework: praisonai
|
||||
process: sequential
|
||||
topic: create movie script about cat in mars
|
||||
memory: false
|
||||
roles:
|
||||
|
||||
workflow:
|
||||
verbose: true
|
||||
|
||||
agents:
|
||||
researcher:
|
||||
backstory: Skilled in research, with a focus on gathering accurate and relevant
|
||||
information.
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Gather information about Mars and cats
|
||||
role: Researcher
|
||||
min_reflect: 1
|
||||
max_reflect: 2
|
||||
tasks:
|
||||
research_task:
|
||||
description: Research about Mars, its environment, and the feasibility of
|
||||
a cat being on Mars. Also, research about cat behavior and characteristics.
|
||||
expected_output: Document with research findings on Mars and cats.
|
||||
backstory: Skilled in research, with a focus on gathering accurate and relevant information.
|
||||
instructions: |
|
||||
Research about Mars, its environment, and the feasibility of a cat being on Mars.
|
||||
Also, research about cat behavior and characteristics.
|
||||
tools:
|
||||
- search_tool
|
||||
- search_tool
|
||||
|
||||
narrative_designer:
|
||||
backstory: Skilled in narrative development, with a focus on creating engaging
|
||||
stories.
|
||||
name: Narrative Designer
|
||||
role: Story Concept Developer
|
||||
goal: Create a story concept for a movie about a cat in Mars
|
||||
role: Narrative Designer
|
||||
tasks:
|
||||
story_concept_development:
|
||||
description: Use the research findings to craft a unique story concept for
|
||||
a movie about a cat in Mars.
|
||||
expected_output: Document with narrative arcs, character bios, and settings.
|
||||
tools:
|
||||
- ''
|
||||
backstory: Skilled in narrative development, with a focus on creating engaging stories.
|
||||
instructions: |
|
||||
Use the research findings to craft a unique story concept for a movie about a cat in Mars.
|
||||
Create narrative arcs, character bios, and settings.
|
||||
|
||||
scriptwriter:
|
||||
backstory: Expert in dialogue and script structure, translating concepts into
|
||||
scripts.
|
||||
name: Scriptwriter
|
||||
role: Script Writer
|
||||
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.
|
||||
tools:
|
||||
- ''
|
||||
dependencies: []
|
||||
backstory: Expert in dialogue and script structure, translating concepts into scripts.
|
||||
instructions: |
|
||||
Turn the story concept into a production-ready movie script,
|
||||
including dialogue and scene details.
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research about Mars environment and cat behavior for the movie concept"
|
||||
|
||||
- agent: narrative_designer
|
||||
action: "Create story concept based on research: {{previous_output}}"
|
||||
|
||||
- agent: scriptwriter
|
||||
action: "Write the final movie script based on the story concept: {{previous_output}}"
|
||||
@@ -0,0 +1,106 @@
|
||||
{
|
||||
"id": "e022a3a1",
|
||||
"name": "Create Movie Script About Cat in Mars",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "trigger",
|
||||
"name": "Manual Trigger",
|
||||
"type": "n8n-nodes-base.manualTrigger",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
250,
|
||||
300
|
||||
],
|
||||
"parameters": {}
|
||||
},
|
||||
{
|
||||
"id": "agent_0",
|
||||
"name": "Researcher",
|
||||
"type": "n8n-nodes-base.executeCommand",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
450,
|
||||
300
|
||||
],
|
||||
"parameters": {
|
||||
"command": "praisonai \"Research about Mars environment and cat behavior for the movie concept\"",
|
||||
"executeOnce": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "agent_1",
|
||||
"name": "Narrative Designer",
|
||||
"type": "n8n-nodes-base.executeCommand",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
650,
|
||||
300
|
||||
],
|
||||
"parameters": {
|
||||
"command": "praisonai \"Create story concept based on research: {{previous_output}}\"",
|
||||
"executeOnce": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "agent_2",
|
||||
"name": "Scriptwriter",
|
||||
"type": "n8n-nodes-base.executeCommand",
|
||||
"typeVersion": 1,
|
||||
"position": [
|
||||
850,
|
||||
300
|
||||
],
|
||||
"parameters": {
|
||||
"command": "praisonai \"Write the final movie script based on the story concept: {{previous_output}}\"",
|
||||
"executeOnce": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"connections": {
|
||||
"Manual Trigger": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Researcher",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Researcher": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Narrative Designer",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Narrative Designer": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Scriptwriter",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"active": false,
|
||||
"settings": {
|
||||
"executionOrder": "v1"
|
||||
},
|
||||
"versionId": "1",
|
||||
"pinData": {},
|
||||
"meta": {
|
||||
"instanceId": "praisonai-export",
|
||||
"templateCredsSetupCompleted": true,
|
||||
"description": "A sequential workflow to research, design narrative, and write a movie script"
|
||||
},
|
||||
"tags": []
|
||||
}
|
||||
@@ -3,8 +3,8 @@ 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/v2.3.26.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.26.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.31.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.31.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -327,7 +327,8 @@ class AgentsGenerator:
|
||||
# Route to YAMLWorkflowParser for advanced workflow patterns
|
||||
return self._run_yaml_workflow(config)
|
||||
|
||||
topic = config.get('topic', '')
|
||||
# Get workflow input: 'input' is canonical, 'topic' is alias for backward compatibility
|
||||
topic = config.get('input', config.get('topic', ''))
|
||||
tools_dict = {}
|
||||
|
||||
# Only try to use praisonai_tools if it's available and needed
|
||||
@@ -441,6 +442,15 @@ class AgentsGenerator:
|
||||
if 'name' not in config:
|
||||
config['name'] = config.get('topic', 'Workflow')
|
||||
|
||||
# Pass model from config_list to workflow as default_llm
|
||||
if self.config_list and self.config_list[0].get('model'):
|
||||
model_from_cli = self.config_list[0]['model']
|
||||
# Set default_llm in workflow config if not already set
|
||||
if 'workflow' not in config:
|
||||
config['workflow'] = {}
|
||||
if 'default_llm' not in config['workflow']:
|
||||
config['workflow']['default_llm'] = model_from_cli
|
||||
|
||||
# Convert config back to YAML string for parser
|
||||
# Note: YAMLWorkflowParser handles 'roles' to 'agents' conversion internally
|
||||
import yaml as yaml_module
|
||||
@@ -450,11 +460,11 @@ class AgentsGenerator:
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_string(yaml_content)
|
||||
|
||||
# Get input from topic or variables
|
||||
input_data = config.get('topic', '')
|
||||
# Get input: 'input' is canonical, 'topic' is alias for backward compatibility
|
||||
input_data = config.get('input', config.get('topic', ''))
|
||||
|
||||
# Execute workflow
|
||||
self.logger.info(f"Running workflow: {workflow.name}")
|
||||
self.logger.debug(f"Running workflow: {workflow.name}")
|
||||
result = workflow.start(input_data)
|
||||
|
||||
if result.get("status") == "completed":
|
||||
|
||||
@@ -20,6 +20,7 @@ Features:
|
||||
- router: Smart model selection
|
||||
- flow_display: Visual workflow tracking
|
||||
- workflow: YAML workflow management
|
||||
- n8n: Export workflows to n8n visual editor
|
||||
"""
|
||||
|
||||
# Type hints available for IDE support
|
||||
@@ -41,6 +42,7 @@ __all__ = [
|
||||
'RouterHandler',
|
||||
'FlowDisplayHandler',
|
||||
'WorkflowHandler',
|
||||
'N8nHandler',
|
||||
]
|
||||
|
||||
def __getattr__(name):
|
||||
@@ -90,4 +92,7 @@ def __getattr__(name):
|
||||
elif name == 'WorkflowHandler':
|
||||
from .workflow import WorkflowHandler
|
||||
return WorkflowHandler
|
||||
elif name == 'N8nHandler':
|
||||
from .n8n import N8nHandler
|
||||
return N8nHandler
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
@@ -1,25 +1,31 @@
|
||||
"""
|
||||
Flow Display Handler for CLI.
|
||||
|
||||
Provides visual workflow tracking.
|
||||
Shows a visual flow diagram of the workflow WITHOUT executing it.
|
||||
Usage: praisonai agents.yaml --flow-display
|
||||
"""
|
||||
|
||||
from typing import Any, Dict, Tuple, List
|
||||
from typing import Any, Dict, Tuple
|
||||
from .base import FlagHandler
|
||||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
class FlowDisplayHandler(FlagHandler):
|
||||
"""
|
||||
Handler for --flow-display flag.
|
||||
|
||||
Enables visual workflow tracking for multi-agent executions.
|
||||
Shows a visual flow diagram/chart of agents and tasks WITHOUT executing.
|
||||
This allows users to preview the workflow structure before running it.
|
||||
|
||||
Example:
|
||||
praisonai agents.yaml --flow-display
|
||||
praisonai "Complex task" --flow-display
|
||||
"""
|
||||
|
||||
def __init__(self, verbose: bool = False):
|
||||
super().__init__(verbose)
|
||||
self._console = None
|
||||
|
||||
@property
|
||||
def feature_name(self) -> str:
|
||||
return "flow_display"
|
||||
@@ -30,160 +36,531 @@ class FlowDisplayHandler(FlagHandler):
|
||||
|
||||
@property
|
||||
def flag_help(self) -> str:
|
||||
return "Enable visual workflow tracking"
|
||||
return "Show workflow flow diagram without executing"
|
||||
|
||||
def _get_console(self):
|
||||
"""Get or create Rich Console."""
|
||||
if self._console is None:
|
||||
try:
|
||||
from rich.console import Console
|
||||
self._console = Console()
|
||||
except ImportError:
|
||||
pass
|
||||
return self._console
|
||||
|
||||
def check_dependencies(self) -> Tuple[bool, str]:
|
||||
"""Check if FlowDisplay is available."""
|
||||
"""Check if Rich is available."""
|
||||
try:
|
||||
import importlib.util
|
||||
if importlib.util.find_spec("praisonaiagents") is not None:
|
||||
return True, ""
|
||||
return False, "praisonaiagents not installed"
|
||||
rich_available = importlib.util.find_spec("rich") is not None
|
||||
if not rich_available:
|
||||
return False, "rich not installed. Install with: pip install rich"
|
||||
return True, ""
|
||||
except ImportError:
|
||||
return False, "praisonaiagents not installed. Install with: pip install praisonaiagents"
|
||||
|
||||
def _get_flow_display(self):
|
||||
"""Get FlowDisplay class lazily."""
|
||||
try:
|
||||
from praisonaiagents import FlowDisplay
|
||||
return FlowDisplay
|
||||
except ImportError:
|
||||
return None
|
||||
|
||||
def create_flow_display(self, title: str = "Workflow") -> Any:
|
||||
"""
|
||||
Create a FlowDisplay instance.
|
||||
|
||||
Args:
|
||||
title: Title for the workflow display
|
||||
|
||||
Returns:
|
||||
FlowDisplay instance or None
|
||||
"""
|
||||
FlowDisplay = self._get_flow_display()
|
||||
if not FlowDisplay:
|
||||
self.print_status(
|
||||
"FlowDisplay requires praisonaiagents. Install with: pip install praisonaiagents",
|
||||
"error"
|
||||
)
|
||||
return None
|
||||
|
||||
try:
|
||||
return FlowDisplay(title=title)
|
||||
except Exception as e:
|
||||
self.log(f"Failed to create FlowDisplay: {e}", "error")
|
||||
return None
|
||||
return False, "rich not installed. Install with: pip install rich"
|
||||
|
||||
def apply_to_agent_config(self, config: Dict[str, Any], flag_value: Any) -> Dict[str, Any]:
|
||||
"""
|
||||
Apply flow display configuration.
|
||||
|
||||
Args:
|
||||
config: Agent configuration dictionary
|
||||
flag_value: Boolean indicating flow display
|
||||
|
||||
Returns:
|
||||
Modified configuration
|
||||
"""
|
||||
"""Apply flow display configuration."""
|
||||
if flag_value:
|
||||
config['flow_display'] = True
|
||||
return config
|
||||
|
||||
def display_workflow_start(self, workflow_name: str, agents: List[str] = None):
|
||||
def display_flow_diagram(self, yaml_file: str, show_footer: bool = True) -> bool:
|
||||
"""
|
||||
Display workflow start.
|
||||
Display a visual flow diagram of the workflow from YAML file.
|
||||
|
||||
Args:
|
||||
workflow_name: Name of the workflow
|
||||
agents: List of agent names
|
||||
"""
|
||||
self.print_status(f"\n🚀 Starting Workflow: {workflow_name}", "info")
|
||||
self.print_status("=" * 50, "info")
|
||||
|
||||
if agents:
|
||||
self.print_status("Agents:", "info")
|
||||
for i, agent in enumerate(agents, 1):
|
||||
self.print_status(f" {i}. {agent}", "info")
|
||||
|
||||
self.print_status("=" * 50, "info")
|
||||
|
||||
def display_step(self, step_num: int, step_name: str, status: str = "running"):
|
||||
"""
|
||||
Display a workflow step.
|
||||
|
||||
Args:
|
||||
step_num: Step number
|
||||
step_name: Name of the step
|
||||
status: Step status (running, completed, failed)
|
||||
"""
|
||||
icons = {
|
||||
'running': '⏳',
|
||||
'completed': '✅',
|
||||
'failed': '❌',
|
||||
'pending': '⬜'
|
||||
}
|
||||
icon = icons.get(status, '•')
|
||||
self.print_status(f" {icon} Step {step_num}: {step_name}", "info")
|
||||
|
||||
def display_workflow_end(self, success: bool = True, duration: float = None):
|
||||
"""
|
||||
Display workflow end.
|
||||
|
||||
Args:
|
||||
success: Whether workflow completed successfully
|
||||
duration: Workflow duration in seconds
|
||||
"""
|
||||
self.print_status("=" * 50, "info")
|
||||
|
||||
if success:
|
||||
msg = "✅ Workflow completed successfully"
|
||||
else:
|
||||
msg = "❌ Workflow failed"
|
||||
|
||||
if duration:
|
||||
msg += f" ({duration:.2f}s)"
|
||||
|
||||
self.print_status(msg, "success" if success else "error")
|
||||
|
||||
def track_execution(self, func):
|
||||
"""
|
||||
Decorator to track function execution in flow display.
|
||||
|
||||
Args:
|
||||
func: Function to track
|
||||
yaml_file: Path to the agents.yaml file
|
||||
show_footer: Whether to show the "run without --flow-display" footer
|
||||
|
||||
Returns:
|
||||
Wrapped function
|
||||
True if displayed successfully, False otherwise
|
||||
"""
|
||||
import functools
|
||||
console = self._get_console()
|
||||
if not console:
|
||||
print("Rich library not available for flow display")
|
||||
return False
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
func_name = func.__name__
|
||||
# Load YAML config
|
||||
try:
|
||||
if not os.path.exists(yaml_file):
|
||||
console.print(f"[red]Error: File not found: {yaml_file}[/red]")
|
||||
return False
|
||||
|
||||
self.display_step(1, func_name, "running")
|
||||
with open(yaml_file, 'r') as f:
|
||||
config = yaml.safe_load(f)
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error loading YAML: {e}[/red]")
|
||||
return False
|
||||
|
||||
# Detect YAML format: 'roles' (old) or 'agents'+'steps' (new workflow)
|
||||
has_roles = 'roles' in config and config['roles']
|
||||
has_agents = 'agents' in config and config['agents']
|
||||
has_steps = 'steps' in config and config['steps']
|
||||
|
||||
if not has_roles and not has_agents:
|
||||
console.print("[red]Error: Invalid YAML format. No 'roles' or 'agents' found.[/red]")
|
||||
return False
|
||||
|
||||
try:
|
||||
from rich.panel import Panel
|
||||
from rich.table import Table
|
||||
from rich.text import Text
|
||||
from rich import box
|
||||
|
||||
# Determine format type
|
||||
is_workflow_format = has_agents and has_steps
|
||||
|
||||
# Extract workflow info based on format
|
||||
if is_workflow_format:
|
||||
name = config.get('name', 'Workflow')
|
||||
description = config.get('description', '')
|
||||
framework = config.get('framework', 'praisonai')
|
||||
workflow_config = config.get('workflow', {})
|
||||
memory = workflow_config.get('memory_config', {}).get('persist', False)
|
||||
num_agents = len(config['agents'])
|
||||
num_steps = len(config['steps'])
|
||||
else:
|
||||
name = config.get('topic', 'Workflow')
|
||||
description = ''
|
||||
framework = config.get('framework', 'praisonai')
|
||||
process = config.get('process', 'sequential')
|
||||
memory = config.get('memory', False)
|
||||
num_agents = len(config['roles'])
|
||||
num_steps = sum(len(details.get('tasks', {})) for details in config['roles'].values())
|
||||
|
||||
# Build the flow diagram
|
||||
console.print()
|
||||
|
||||
# === HEADER ===
|
||||
header = Text()
|
||||
header.append("📊 WORKFLOW FLOW DIAGRAM\n", style="bold cyan")
|
||||
header.append("─" * 40, style="dim")
|
||||
|
||||
header_panel = Panel(
|
||||
header,
|
||||
border_style="cyan",
|
||||
box=box.DOUBLE_EDGE,
|
||||
padding=(0, 2)
|
||||
)
|
||||
console.print(header_panel)
|
||||
|
||||
# === WORKFLOW INFO ===
|
||||
info_table = Table(show_header=False, box=None, padding=(0, 2))
|
||||
info_table.add_column("Key", style="bold yellow")
|
||||
info_table.add_column("Value", style="white")
|
||||
|
||||
info_table.add_row("📋 Name:", name)
|
||||
if description:
|
||||
info_table.add_row("📝 Description:", description)
|
||||
|
||||
# Show input if present ('input' is canonical, 'topic' is alias)
|
||||
workflow_input = config.get('input', config.get('topic', ''))
|
||||
if workflow_input:
|
||||
info_table.add_row("💬 Input:", workflow_input)
|
||||
|
||||
info_table.add_row("⚙️ Framework:", framework)
|
||||
|
||||
if is_workflow_format:
|
||||
info_table.add_row("🔄 Format:", "WORKFLOW (steps-based)")
|
||||
planning = workflow_config.get('planning', False)
|
||||
info_table.add_row("🧠 Planning:", "✅ Enabled" if planning else "❌ Disabled")
|
||||
else:
|
||||
info_table.add_row("🔄 Process:", process.upper())
|
||||
|
||||
info_table.add_row("💾 Memory:", "✅ Enabled" if memory else "❌ Disabled")
|
||||
info_table.add_row("👥 Agents:", str(num_agents))
|
||||
info_table.add_row("📝 Steps:", str(num_steps))
|
||||
|
||||
console.print(Panel(info_table, title="[bold white]Workflow Configuration[/bold white]",
|
||||
border_style="blue", box=box.ROUNDED))
|
||||
console.print()
|
||||
|
||||
# === FLOW DIAGRAM ===
|
||||
if is_workflow_format:
|
||||
self._display_steps_flow(console, config)
|
||||
else:
|
||||
process = config.get('process', 'sequential')
|
||||
if process == 'sequential':
|
||||
self._display_sequential_flow(console, config)
|
||||
elif process == 'hierarchical':
|
||||
self._display_hierarchical_flow(console, config)
|
||||
else:
|
||||
self._display_sequential_flow(console, config)
|
||||
|
||||
# === DETAILED AGENT INFO ===
|
||||
console.print()
|
||||
if is_workflow_format:
|
||||
self._display_workflow_agent_details(console, config)
|
||||
else:
|
||||
self._display_agent_details(console, config)
|
||||
|
||||
# === FOOTER ===
|
||||
if show_footer:
|
||||
console.print()
|
||||
footer = Text()
|
||||
footer.append("💡 ", style="bold")
|
||||
footer.append("To execute this workflow, run without --flow-display flag", style="dim italic")
|
||||
console.print(Panel(footer, border_style="dim", box=box.ROUNDED))
|
||||
console.print()
|
||||
|
||||
return True
|
||||
|
||||
except ImportError as e:
|
||||
console.print(f"[red]Error: Rich components not available: {e}[/red]")
|
||||
return False
|
||||
|
||||
def _display_sequential_flow(self, console, config: Dict):
|
||||
"""Display sequential flow diagram."""
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
from rich import box
|
||||
|
||||
roles = list(config['roles'].items())
|
||||
|
||||
# Box width (content area)
|
||||
BOX_WIDTH = 36
|
||||
|
||||
def pad_line(content: str, width: int = BOX_WIDTH) -> str:
|
||||
"""Pad content to fixed width."""
|
||||
padding = width - len(content)
|
||||
if padding > 0:
|
||||
return content + " " * padding
|
||||
return content[:width]
|
||||
|
||||
flow_text = Text()
|
||||
flow_text.append(" SEQUENTIAL EXECUTION FLOW\n\n", style="bold magenta")
|
||||
|
||||
for i, (role_key, details) in enumerate(roles):
|
||||
role_name = details.get('role', role_key)
|
||||
tasks = list(details.get('tasks', {}).keys())
|
||||
|
||||
# Agent box
|
||||
if i == 0:
|
||||
flow_text.append(" ┌── START ──┐\n", style="green")
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
|
||||
# Agent node
|
||||
flow_text.append(" ╔════════════════════════════════════╗\n", style="cyan")
|
||||
line = f" Agent {i+1}: {role_name}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="cyan")
|
||||
|
||||
# Tasks
|
||||
for task in tasks:
|
||||
line = f" -> {task}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="dim white")
|
||||
|
||||
flow_text.append(" ╚════════════════════════════════════╝\n", style="cyan")
|
||||
|
||||
# Arrow to next
|
||||
if i < len(roles) - 1:
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" output\n", style="dim italic")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
else:
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
flow_text.append(" └── END ────┘\n", style="red")
|
||||
|
||||
console.print(Panel(flow_text, border_style="magenta", box=box.DOUBLE_EDGE,
|
||||
title="[bold white]Execution Flow[/bold white]"))
|
||||
|
||||
def _display_hierarchical_flow(self, console, config: Dict):
|
||||
"""Display hierarchical flow diagram."""
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
from rich import box
|
||||
|
||||
manager_llm = config.get('manager_llm', 'default')
|
||||
|
||||
flow_text = Text()
|
||||
flow_text.append("👑 HIERARCHICAL EXECUTION FLOW\n\n", style="bold magenta")
|
||||
|
||||
# Manager at top
|
||||
flow_text.append(" ┌──────────────────────┐\n", style="yellow")
|
||||
flow_text.append(" │ 👑 MANAGER AGENT │\n", style="bold yellow")
|
||||
flow_text.append(f" │ LLM: {manager_llm:<14}│\n", style="dim yellow")
|
||||
flow_text.append(" └──────────┬───────────┘\n", style="yellow")
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ┌──────────┴───────────┐\n", style="dim")
|
||||
flow_text.append(" │ delegates to │\n", style="dim italic")
|
||||
flow_text.append(" └──────────┬───────────┘\n", style="dim")
|
||||
|
||||
roles = list(config['roles'].items())
|
||||
num_agents = len(roles)
|
||||
|
||||
# Draw branches
|
||||
if num_agents > 0:
|
||||
branch_line = " "
|
||||
for i in range(num_agents):
|
||||
if i == 0:
|
||||
branch_line += "┌" + "─" * 8
|
||||
elif i == num_agents - 1:
|
||||
branch_line += "┬" + "─" * 8 + "┐"
|
||||
else:
|
||||
branch_line += "┬" + "─" * 8
|
||||
flow_text.append(branch_line + "\n", style="dim")
|
||||
|
||||
# Agent boxes
|
||||
for i, (role_key, details) in enumerate(roles):
|
||||
flow_text.append("\n ╔═══════════════╗", style="cyan")
|
||||
flow_text.append("\n")
|
||||
|
||||
for i, (role_key, details) in enumerate(roles):
|
||||
role_name = details.get('role', role_key)[:12]
|
||||
flow_text.append(f" ║ 🤖 {role_name:<10} ║", style="cyan")
|
||||
flow_text.append("\n")
|
||||
|
||||
for i, (role_key, details) in enumerate(roles):
|
||||
flow_text.append(" ╚═══════════════╝", style="cyan")
|
||||
flow_text.append("\n")
|
||||
|
||||
console.print(Panel(flow_text, border_style="magenta", box=box.DOUBLE_EDGE,
|
||||
title="[bold white]Execution Flow[/bold white]"))
|
||||
|
||||
def _display_agent_details(self, console, config: Dict):
|
||||
"""Display detailed agent information."""
|
||||
from rich.table import Table
|
||||
from rich import box
|
||||
|
||||
table = Table(
|
||||
title="[bold cyan]📋 Agent & Task Details[/bold cyan]",
|
||||
box=box.ROUNDED,
|
||||
show_header=True,
|
||||
header_style="bold magenta",
|
||||
border_style="blue",
|
||||
padding=(0, 1),
|
||||
expand=True
|
||||
)
|
||||
|
||||
table.add_column("#", style="dim", width=3, justify="center")
|
||||
table.add_column("Agent", style="bold yellow", min_width=15)
|
||||
table.add_column("Role", style="cyan", min_width=15)
|
||||
table.add_column("Goal", style="white", min_width=25, overflow="fold")
|
||||
table.add_column("Tasks", style="green", min_width=20)
|
||||
table.add_column("Tools", style="magenta", min_width=15)
|
||||
|
||||
for i, (role_key, details) in enumerate(config['roles'].items(), 1):
|
||||
role_name = details.get('role', role_key)
|
||||
goal = details.get('goal', '-')
|
||||
if len(goal) > 50:
|
||||
goal = goal[:47] + "..."
|
||||
|
||||
tasks = list(details.get('tasks', {}).keys())
|
||||
tasks_str = "\n".join(f"• {t}" for t in tasks) if tasks else "-"
|
||||
|
||||
tools = details.get('tools', [])
|
||||
tools_str = ", ".join(t for t in tools if t) if tools else "-"
|
||||
|
||||
table.add_row(
|
||||
str(i),
|
||||
role_name,
|
||||
role_key,
|
||||
goal,
|
||||
tasks_str,
|
||||
tools_str
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
def _display_steps_flow(self, console, config: Dict):
|
||||
"""Display steps-based workflow flow diagram."""
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
from rich import box
|
||||
|
||||
steps = config.get('steps', [])
|
||||
agents = config.get('agents', {})
|
||||
|
||||
# Box width (content area)
|
||||
BOX_WIDTH = 36
|
||||
|
||||
def pad_line(content: str, width: int = BOX_WIDTH) -> str:
|
||||
"""Pad content to fixed width, accounting for emoji width."""
|
||||
# Emojis typically take 2 character widths
|
||||
emoji_count = sum(1 for c in content if ord(c) > 0x1F300)
|
||||
actual_len = len(content) + emoji_count # Add extra for emoji width
|
||||
padding = width - actual_len
|
||||
if padding > 0:
|
||||
return content + " " * padding
|
||||
return content[:width]
|
||||
|
||||
flow_text = Text()
|
||||
flow_text.append(" STEPS-BASED EXECUTION FLOW\n\n", style="bold magenta")
|
||||
|
||||
flow_text.append(" ┌── START ──┐\n", style="green")
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
|
||||
for i, step in enumerate(steps):
|
||||
# Determine step type
|
||||
agent_key = step.get('agent', '')
|
||||
step_name = step.get('name', '')
|
||||
|
||||
# Check for special step types
|
||||
has_route = 'route' in step
|
||||
has_parallel = 'parallel' in step
|
||||
has_loop = 'loop' in step
|
||||
has_repeat = 'repeat' in step
|
||||
|
||||
if has_route:
|
||||
# Route step
|
||||
flow_text.append(" ╔════════════════════════════════════╗\n", style="yellow")
|
||||
line = f" ROUTE: {step_name or 'routing'}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="yellow")
|
||||
|
||||
routes = step['route']
|
||||
for route_name, route_agents in routes.items():
|
||||
agents_str = ", ".join(route_agents) if isinstance(route_agents, list) else str(route_agents)
|
||||
line = f" -> {route_name}: {agents_str}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="yellow")
|
||||
|
||||
flow_text.append(" ╚════════════════════════════════════╝\n", style="yellow")
|
||||
|
||||
elif has_parallel:
|
||||
# Parallel step
|
||||
flow_text.append(" ╔════════════════════════════════════╗\n", style="green")
|
||||
line = f" PARALLEL: {step_name or 'parallel'}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="green")
|
||||
|
||||
for p_step in step['parallel']:
|
||||
p_agent = p_step.get('agent', '?')
|
||||
line = f" |-- {p_agent}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="green")
|
||||
|
||||
flow_text.append(" ╚════════════════════════════════════╝\n", style="green")
|
||||
|
||||
else:
|
||||
# Regular agent step
|
||||
agent_info = agents.get(agent_key, {})
|
||||
agent_name = agent_info.get('name', agent_key)
|
||||
|
||||
flow_text.append(" ╔════════════════════════════════════╗\n", style="cyan")
|
||||
line = f" Step {i+1}: {agent_name}"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="cyan")
|
||||
|
||||
# Show modifiers
|
||||
if has_loop:
|
||||
loop_over = step['loop'].get('over', '?')
|
||||
line = f" [loop over: {loop_over}]"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="dim magenta")
|
||||
|
||||
if has_repeat:
|
||||
until = step['repeat'].get('until', '?')
|
||||
max_iter = step['repeat'].get('max_iterations', '?')
|
||||
line = f" [repeat until: {until} max:{max_iter}]"
|
||||
flow_text.append(f" ║{pad_line(line)}║\n", style="dim magenta")
|
||||
|
||||
flow_text.append(" ╚════════════════════════════════════╝\n", style="cyan")
|
||||
|
||||
# Arrow to next
|
||||
if i < len(steps) - 1:
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
|
||||
flow_text.append(" │\n", style="dim")
|
||||
flow_text.append(" ▼\n", style="dim")
|
||||
flow_text.append(" └── END ────┘\n", style="red")
|
||||
|
||||
console.print(Panel(flow_text, border_style="magenta", box=box.DOUBLE_EDGE,
|
||||
title="[bold white]Execution Flow[/bold white]"))
|
||||
|
||||
def _display_workflow_agent_details(self, console, config: Dict):
|
||||
"""Display detailed agent information for workflow format."""
|
||||
from rich.table import Table
|
||||
from rich import box
|
||||
|
||||
table = Table(
|
||||
title="[bold cyan]📋 Agent Details[/bold cyan]",
|
||||
box=box.ROUNDED,
|
||||
show_header=True,
|
||||
header_style="bold magenta",
|
||||
border_style="blue",
|
||||
padding=(0, 1),
|
||||
expand=True
|
||||
)
|
||||
|
||||
table.add_column("#", style="dim", width=3, justify="center")
|
||||
table.add_column("Agent Key", style="bold yellow", min_width=15)
|
||||
table.add_column("Name", style="cyan", min_width=15)
|
||||
table.add_column("Role", style="white", min_width=20)
|
||||
table.add_column("Goal", style="green", min_width=25, overflow="fold")
|
||||
|
||||
for i, (agent_key, details) in enumerate(config['agents'].items(), 1):
|
||||
name = details.get('name', agent_key)
|
||||
role = details.get('role', '-')
|
||||
goal = details.get('goal', '-')
|
||||
if len(goal) > 40:
|
||||
goal = goal[:37] + "..."
|
||||
|
||||
table.add_row(
|
||||
str(i),
|
||||
agent_key,
|
||||
name,
|
||||
role,
|
||||
goal
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
def display_workflow_start(self, workflow_name: str, agents: list) -> None:
|
||||
"""
|
||||
Display workflow start message.
|
||||
|
||||
Args:
|
||||
workflow_name: Name of the workflow being started
|
||||
agents: List of agent names in the workflow
|
||||
"""
|
||||
console = self._get_console()
|
||||
if console:
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
self.display_step(1, func_name, "completed")
|
||||
return result
|
||||
except Exception:
|
||||
self.display_step(1, func_name, "failed")
|
||||
raise
|
||||
|
||||
return wrapper
|
||||
from rich.panel import Panel
|
||||
from rich import box
|
||||
|
||||
agents_str = ", ".join(agents) if agents else "No agents"
|
||||
content = f"🚀 Starting: {workflow_name}\n👥 Agents: {agents_str}"
|
||||
console.print(Panel(content, title="[bold cyan]Workflow Started[/bold cyan]",
|
||||
border_style="cyan", box=box.ROUNDED))
|
||||
except ImportError:
|
||||
print(f"Starting workflow: {workflow_name} with agents: {agents}")
|
||||
|
||||
def execute(self, workflow_name: str = None, **kwargs) -> Any:
|
||||
def display_workflow_end(self, success: bool = True) -> None:
|
||||
"""
|
||||
Execute flow display setup.
|
||||
Display workflow end message.
|
||||
|
||||
Args:
|
||||
workflow_name: Name of the workflow
|
||||
success: Whether the workflow completed successfully
|
||||
"""
|
||||
console = self._get_console()
|
||||
if console:
|
||||
try:
|
||||
from rich.panel import Panel
|
||||
from rich import box
|
||||
|
||||
if success:
|
||||
content = "✅ Workflow completed successfully"
|
||||
style = "green"
|
||||
else:
|
||||
content = "❌ Workflow failed"
|
||||
style = "red"
|
||||
|
||||
console.print(Panel(content, title=f"[bold {style}]Workflow Ended[/bold {style}]",
|
||||
border_style=style, box=box.ROUNDED))
|
||||
except ImportError:
|
||||
status = "successfully" if success else "with errors"
|
||||
print(f"Workflow ended {status}")
|
||||
|
||||
def execute(self, yaml_file: str = None, **kwargs) -> bool:
|
||||
"""
|
||||
Execute flow display - shows diagram without running workflow.
|
||||
|
||||
Args:
|
||||
yaml_file: Path to the YAML file
|
||||
|
||||
Returns:
|
||||
FlowDisplay instance or None
|
||||
True if displayed successfully
|
||||
"""
|
||||
if workflow_name:
|
||||
self.display_workflow_start(workflow_name)
|
||||
|
||||
return self.create_flow_display(workflow_name or "Workflow")
|
||||
if yaml_file:
|
||||
return self.display_flow_diagram(yaml_file)
|
||||
return False
|
||||
@@ -0,0 +1,539 @@
|
||||
"""
|
||||
n8n Integration Handler for PraisonAI CLI.
|
||||
|
||||
Converts PraisonAI agents.yaml workflows to n8n workflow JSON format
|
||||
and opens them in the n8n UI.
|
||||
|
||||
Usage:
|
||||
praisonai agents.yaml --n8n
|
||||
praisonai agents.yaml --n8n --n8n-url http://localhost:5678
|
||||
"""
|
||||
|
||||
import json
|
||||
import yaml
|
||||
import webbrowser
|
||||
import hashlib
|
||||
import os
|
||||
from typing import Any, Dict, List, Optional
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import requests
|
||||
REQUESTS_AVAILABLE = True
|
||||
except ImportError:
|
||||
REQUESTS_AVAILABLE = False
|
||||
|
||||
from .base import FlagHandler
|
||||
|
||||
|
||||
class N8nHandler(FlagHandler):
|
||||
"""
|
||||
Handler for n8n workflow export and visualization.
|
||||
|
||||
Converts PraisonAI YAML workflows to n8n-compatible JSON format
|
||||
and opens them in the n8n UI for visual editing.
|
||||
"""
|
||||
|
||||
def __init__(self, verbose: bool = False, n8n_url: str = "http://localhost:5678",
|
||||
use_execute_command: bool = True):
|
||||
"""
|
||||
Initialize the n8n handler.
|
||||
|
||||
Args:
|
||||
verbose: Enable verbose output
|
||||
n8n_url: Base URL of the n8n instance
|
||||
use_execute_command: Use Execute Command nodes (runs praisonai directly)
|
||||
instead of HTTP Request nodes
|
||||
"""
|
||||
super().__init__(verbose=verbose)
|
||||
self.n8n_url = n8n_url
|
||||
self.praisonai_api_url = "http://localhost:8000"
|
||||
self.use_execute_command = use_execute_command
|
||||
|
||||
@property
|
||||
def feature_name(self) -> str:
|
||||
return "n8n"
|
||||
|
||||
@property
|
||||
def flag_name(self) -> str:
|
||||
return "n8n"
|
||||
|
||||
@property
|
||||
def flag_help(self) -> str:
|
||||
return "Export workflow to n8n and open in browser"
|
||||
|
||||
def execute(self, yaml_path: str, n8n_url: Optional[str] = None,
|
||||
open_browser: bool = True, **kwargs) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute the n8n export workflow.
|
||||
|
||||
Args:
|
||||
yaml_path: Path to the agents.yaml file
|
||||
n8n_url: Optional custom n8n URL
|
||||
open_browser: Whether to open the workflow in browser
|
||||
|
||||
Returns:
|
||||
Dictionary with workflow JSON and URL
|
||||
"""
|
||||
if n8n_url:
|
||||
self.n8n_url = n8n_url
|
||||
|
||||
# Convert YAML to n8n workflow JSON
|
||||
workflow_json = self.convert_yaml_to_n8n(yaml_path)
|
||||
|
||||
# Save workflow JSON to file
|
||||
output_path = self._save_workflow_json(workflow_json, yaml_path)
|
||||
|
||||
self.print_status("✅ Workflow converted successfully!", "success")
|
||||
self.print_status(f"📄 JSON saved to: {output_path}", "info")
|
||||
|
||||
# Try to create workflow via n8n API
|
||||
api_key = os.environ.get('N8N_API_KEY')
|
||||
workflow_id = None
|
||||
|
||||
if api_key and REQUESTS_AVAILABLE:
|
||||
workflow_id = self._create_workflow_via_api(workflow_json, api_key)
|
||||
|
||||
if workflow_id:
|
||||
# Successfully created via API - open directly
|
||||
url = f"{self.n8n_url}/workflow/{workflow_id}"
|
||||
self.print_status("🚀 Workflow created in n8n!", "success")
|
||||
|
||||
if open_browser:
|
||||
self.open_in_browser(url)
|
||||
self.print_status(f"🌐 Opening: {url}", "info")
|
||||
else:
|
||||
# Fallback to manual import instructions
|
||||
url = self.generate_n8n_url(workflow_json, self.n8n_url)
|
||||
|
||||
if not api_key:
|
||||
self.print_status("", "info")
|
||||
self.print_status("💡 Tip: Set N8N_API_KEY env var for auto-import", "info")
|
||||
self.print_status(" Generate API key in n8n: Settings → API", "info")
|
||||
|
||||
self.print_status("", "info")
|
||||
self.print_status("📋 To import into n8n:", "info")
|
||||
self.print_status(" 1. Open n8n in your browser", "info")
|
||||
self.print_status(" 2. Click the three dots menu (⋮) in the top right", "info")
|
||||
self.print_status(" 3. Select 'Import from File'", "info")
|
||||
self.print_status(f" 4. Choose: {output_path}", "info")
|
||||
|
||||
if open_browser:
|
||||
self.open_in_browser(url)
|
||||
self.print_status(f"🌐 Opening n8n: {url}", "info")
|
||||
|
||||
return {
|
||||
"workflow": workflow_json,
|
||||
"url": url,
|
||||
"output_path": output_path,
|
||||
"workflow_id": workflow_id
|
||||
}
|
||||
|
||||
def _create_workflow_via_api(self, workflow_json: Dict[str, Any],
|
||||
api_key: str) -> Optional[str]:
|
||||
"""
|
||||
Create workflow in n8n via REST API.
|
||||
|
||||
Args:
|
||||
workflow_json: The n8n workflow JSON
|
||||
api_key: n8n API key
|
||||
|
||||
Returns:
|
||||
Workflow ID if successful, None otherwise
|
||||
"""
|
||||
if not REQUESTS_AVAILABLE:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Only include fields allowed by n8n API schema
|
||||
# See: n8n/packages/cli/src/public-api/v1/handlers/workflows/spec/schemas/workflow.yml
|
||||
workflow_data = {
|
||||
"name": workflow_json.get("name", "PraisonAI Workflow"),
|
||||
"nodes": workflow_json.get("nodes", []),
|
||||
"connections": workflow_json.get("connections", {}),
|
||||
"settings": workflow_json.get("settings", {"executionOrder": "v1"}),
|
||||
}
|
||||
|
||||
# Optional fields
|
||||
if "staticData" in workflow_json:
|
||||
workflow_data["staticData"] = workflow_json["staticData"]
|
||||
|
||||
response = requests.post(
|
||||
f"{self.n8n_url}/api/v1/workflows",
|
||||
headers={
|
||||
"X-N8N-API-KEY": api_key,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
json=workflow_data,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
return result.get('id')
|
||||
else:
|
||||
self.log(f"API error: {response.status_code} - {response.text}", "warning")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.log(f"Failed to create workflow via API: {e}", "warning")
|
||||
return None
|
||||
|
||||
def convert_yaml_to_n8n(self, yaml_path: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Convert a PraisonAI YAML workflow to n8n JSON format.
|
||||
|
||||
Args:
|
||||
yaml_path: Path to the agents.yaml file
|
||||
|
||||
Returns:
|
||||
n8n workflow JSON as dictionary
|
||||
"""
|
||||
# Load YAML file
|
||||
with open(yaml_path, 'r') as f:
|
||||
yaml_content = yaml.safe_load(f)
|
||||
|
||||
# Extract workflow metadata
|
||||
workflow_name = yaml_content.get('name', 'PraisonAI Workflow')
|
||||
description = yaml_content.get('description', '')
|
||||
|
||||
# Get agents and steps
|
||||
agents = yaml_content.get('agents', yaml_content.get('roles', {}))
|
||||
steps = yaml_content.get('steps', [])
|
||||
|
||||
# If no steps defined, create steps from agents
|
||||
if not steps and agents:
|
||||
steps = self._create_steps_from_agents(agents)
|
||||
|
||||
# Build n8n nodes and connections
|
||||
nodes = []
|
||||
connections = {}
|
||||
|
||||
# Add manual trigger node
|
||||
trigger_node = self._create_trigger_node()
|
||||
nodes.append(trigger_node)
|
||||
|
||||
# Track previous node for connections
|
||||
prev_node_name = trigger_node["name"]
|
||||
|
||||
# Process each step
|
||||
x_position = 450 # Start position after trigger
|
||||
|
||||
for i, step in enumerate(steps):
|
||||
if isinstance(step, dict):
|
||||
if 'parallel' in step:
|
||||
# Handle parallel steps
|
||||
parallel_nodes, parallel_connections = self._create_parallel_nodes(
|
||||
step['parallel'], x_position, agents
|
||||
)
|
||||
nodes.extend(parallel_nodes)
|
||||
connections.update(parallel_connections)
|
||||
|
||||
# Connect previous node to first parallel node
|
||||
if prev_node_name not in connections:
|
||||
connections[prev_node_name] = {"main": [[]]}
|
||||
for pnode in parallel_nodes:
|
||||
connections[prev_node_name]["main"][0].append({
|
||||
"node": pnode["name"],
|
||||
"type": "main",
|
||||
"index": 0
|
||||
})
|
||||
|
||||
x_position += 200 * len(parallel_nodes)
|
||||
prev_node_name = parallel_nodes[-1]["name"] if parallel_nodes else prev_node_name
|
||||
|
||||
elif 'route' in step:
|
||||
# Handle route/decision steps
|
||||
route_node = self._create_route_node(step, x_position, i)
|
||||
nodes.append(route_node)
|
||||
|
||||
# Connect previous node
|
||||
if prev_node_name not in connections:
|
||||
connections[prev_node_name] = {"main": [[]]}
|
||||
connections[prev_node_name]["main"][0].append({
|
||||
"node": route_node["name"],
|
||||
"type": "main",
|
||||
"index": 0
|
||||
})
|
||||
|
||||
prev_node_name = route_node["name"]
|
||||
x_position += 200
|
||||
|
||||
else:
|
||||
# Regular agent step
|
||||
agent_id = step.get('agent', '')
|
||||
action = step.get('action', step.get('description', ''))
|
||||
agent_config = agents.get(agent_id, {})
|
||||
|
||||
agent_node = self._create_agent_node(
|
||||
agent_id=agent_id,
|
||||
agent_config=agent_config,
|
||||
action=action,
|
||||
position=[x_position, 300],
|
||||
index=i
|
||||
)
|
||||
nodes.append(agent_node)
|
||||
|
||||
# Connect to previous node
|
||||
if prev_node_name not in connections:
|
||||
connections[prev_node_name] = {"main": [[]]}
|
||||
connections[prev_node_name]["main"][0].append({
|
||||
"node": agent_node["name"],
|
||||
"type": "main",
|
||||
"index": 0
|
||||
})
|
||||
|
||||
prev_node_name = agent_node["name"]
|
||||
x_position += 200
|
||||
|
||||
# Build the complete workflow
|
||||
# Generate a unique ID based on workflow name
|
||||
workflow_id = hashlib.md5(workflow_name.encode()).hexdigest()[:8]
|
||||
|
||||
workflow = {
|
||||
"id": workflow_id,
|
||||
"name": workflow_name,
|
||||
"nodes": nodes,
|
||||
"connections": connections,
|
||||
"active": False,
|
||||
"settings": {
|
||||
"executionOrder": "v1"
|
||||
},
|
||||
"versionId": "1",
|
||||
"pinData": {},
|
||||
"meta": {
|
||||
"instanceId": "praisonai-export",
|
||||
"templateCredsSetupCompleted": True
|
||||
},
|
||||
"tags": []
|
||||
}
|
||||
|
||||
if description:
|
||||
workflow["meta"]["description"] = description
|
||||
|
||||
return workflow
|
||||
|
||||
def _create_steps_from_agents(self, agents: Dict[str, Any]) -> List[Dict]:
|
||||
"""Create steps from agents when no steps are defined."""
|
||||
steps = []
|
||||
for agent_id, agent_config in agents.items():
|
||||
if isinstance(agent_config, dict):
|
||||
# Check for tasks nested in agent
|
||||
tasks = agent_config.get('tasks', {})
|
||||
if tasks:
|
||||
for task_id, task_config in tasks.items():
|
||||
steps.append({
|
||||
'agent': agent_id,
|
||||
'action': task_config.get('description', task_config.get('action', f'Execute {task_id}'))
|
||||
})
|
||||
else:
|
||||
# Create a default step for the agent
|
||||
steps.append({
|
||||
'agent': agent_id,
|
||||
'action': agent_config.get('instructions', agent_config.get('goal', f'Execute {agent_id}'))
|
||||
})
|
||||
return steps
|
||||
|
||||
def _create_trigger_node(self) -> Dict[str, Any]:
|
||||
"""Create a manual trigger node."""
|
||||
return {
|
||||
"id": "trigger",
|
||||
"name": "Manual Trigger",
|
||||
"type": "n8n-nodes-base.manualTrigger",
|
||||
"typeVersion": 1,
|
||||
"position": [250, 300],
|
||||
"parameters": {}
|
||||
}
|
||||
|
||||
def _create_agent_node(self, agent_id: str, agent_config: Dict,
|
||||
action: str, position: List[int],
|
||||
index: int) -> Dict[str, Any]:
|
||||
"""Create a node for an agent - either Execute Command or HTTP Request."""
|
||||
if self.use_execute_command:
|
||||
return self._create_execute_command_node(
|
||||
agent_id, agent_config, action, position, index
|
||||
)
|
||||
else:
|
||||
return self._create_http_request_node(
|
||||
agent_id, agent_config, action, position, index
|
||||
)
|
||||
|
||||
def _create_execute_command_node(self, agent_id: str, agent_config: Dict,
|
||||
action: str, position: List[int],
|
||||
index: int) -> Dict[str, Any]:
|
||||
"""Create an Execute Command node that runs praisonai directly."""
|
||||
agent_name = agent_config.get('name', agent_id.title())
|
||||
|
||||
# Escape quotes in the action for shell command
|
||||
escaped_action = action.replace('"', '\\"').replace("'", "\\'")
|
||||
|
||||
# Build the praisonai command
|
||||
# Uses the input from previous node if available
|
||||
command = f'praisonai "{escaped_action}"'
|
||||
|
||||
return {
|
||||
"id": f"agent_{index}",
|
||||
"name": agent_name,
|
||||
"type": "n8n-nodes-base.executeCommand",
|
||||
"typeVersion": 1,
|
||||
"position": position,
|
||||
"parameters": {
|
||||
"command": command,
|
||||
"executeOnce": True
|
||||
}
|
||||
}
|
||||
|
||||
def _create_http_request_node(self, agent_id: str, agent_config: Dict,
|
||||
action: str, position: List[int],
|
||||
index: int) -> Dict[str, Any]:
|
||||
"""Create an HTTP Request node for an agent."""
|
||||
agent_name = agent_config.get('name', agent_id.title())
|
||||
|
||||
# Build the request body
|
||||
request_body = {
|
||||
"query": action,
|
||||
"agent": agent_id
|
||||
}
|
||||
|
||||
return {
|
||||
"id": f"agent_{index}",
|
||||
"name": agent_name,
|
||||
"type": "n8n-nodes-base.httpRequest",
|
||||
"typeVersion": 4.2,
|
||||
"position": position,
|
||||
"parameters": {
|
||||
"method": "POST",
|
||||
"url": f"{self.praisonai_api_url}/agents",
|
||||
"sendBody": True,
|
||||
"specifyBody": "json",
|
||||
"jsonBody": json.dumps(request_body),
|
||||
"options": {
|
||||
"timeout": 300000 # 5 minute timeout for LLM calls
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def _create_parallel_nodes(self, parallel_steps: List[Dict],
|
||||
x_position: int,
|
||||
agents: Dict[str, Any]) -> tuple:
|
||||
"""Create nodes for parallel execution."""
|
||||
nodes = []
|
||||
connections = {}
|
||||
|
||||
for i, step in enumerate(parallel_steps):
|
||||
agent_id = step.get('agent', '')
|
||||
action = step.get('action', step.get('description', ''))
|
||||
agent_config = agents.get(agent_id, {})
|
||||
|
||||
node = self._create_agent_node(
|
||||
agent_id=agent_id,
|
||||
agent_config=agent_config,
|
||||
action=action,
|
||||
position=[x_position + (i * 200), 300 + (i * 100)],
|
||||
index=100 + i # Offset index for parallel nodes
|
||||
)
|
||||
nodes.append(node)
|
||||
|
||||
return nodes, connections
|
||||
|
||||
def _create_route_node(self, step: Dict, x_position: int,
|
||||
index: int) -> Dict[str, Any]:
|
||||
"""Create an IF node for routing decisions."""
|
||||
route_config = step.get('route', {})
|
||||
conditions = list(route_config.keys())
|
||||
|
||||
return {
|
||||
"id": f"route_{index}",
|
||||
"name": f"Route Decision {index}",
|
||||
"type": "n8n-nodes-base.if",
|
||||
"typeVersion": 2,
|
||||
"position": [x_position, 300],
|
||||
"parameters": {
|
||||
"conditions": {
|
||||
"options": {
|
||||
"caseSensitive": True,
|
||||
"leftValue": "",
|
||||
"typeValidation": "strict"
|
||||
},
|
||||
"conditions": [
|
||||
{
|
||||
"id": f"condition_{i}",
|
||||
"leftValue": "={{ $json.result }}",
|
||||
"rightValue": condition,
|
||||
"operator": {
|
||||
"type": "string",
|
||||
"operation": "contains"
|
||||
}
|
||||
}
|
||||
for i, condition in enumerate(conditions) if condition != 'default'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def generate_n8n_url(self, workflow_json: Dict[str, Any],
|
||||
base_url: str) -> str:
|
||||
"""
|
||||
Generate a URL to open n8n workflow editor.
|
||||
|
||||
Note: n8n doesn't support URL hash-based import. Users need to:
|
||||
1. Open n8n
|
||||
2. Use "Import from File" to load the saved JSON
|
||||
|
||||
Args:
|
||||
workflow_json: The n8n workflow JSON (unused, kept for API compatibility)
|
||||
base_url: Base URL of the n8n instance
|
||||
|
||||
Returns:
|
||||
URL to open n8n workflow editor
|
||||
"""
|
||||
# n8n doesn't support URL-based workflow import via hash
|
||||
# Return the new workflow URL - user will need to import manually
|
||||
return f"{base_url}/workflow/new"
|
||||
|
||||
def open_in_browser(self, url: str) -> None:
|
||||
"""
|
||||
Open a URL in the default web browser.
|
||||
|
||||
Args:
|
||||
url: URL to open
|
||||
"""
|
||||
try:
|
||||
webbrowser.open(url)
|
||||
except Exception as e:
|
||||
self.print_status(f"Could not open browser: {e}", "warning")
|
||||
self.print_status(f"Please open this URL manually: {url}", "info")
|
||||
|
||||
def _save_workflow_json(self, workflow_json: Dict[str, Any],
|
||||
yaml_path: str) -> str:
|
||||
"""
|
||||
Save the workflow JSON to a file.
|
||||
|
||||
Args:
|
||||
workflow_json: The n8n workflow JSON
|
||||
yaml_path: Original YAML file path
|
||||
|
||||
Returns:
|
||||
Path to the saved JSON file
|
||||
"""
|
||||
# Create output path based on input YAML
|
||||
yaml_file = Path(yaml_path)
|
||||
output_path = yaml_file.parent / f"{yaml_file.stem}_n8n.json"
|
||||
|
||||
with open(output_path, 'w') as f:
|
||||
json.dump(workflow_json, f, indent=2)
|
||||
|
||||
return str(output_path)
|
||||
|
||||
def get_workflow_json_for_clipboard(self, yaml_path: str) -> str:
|
||||
"""
|
||||
Get the workflow JSON as a string for clipboard copy.
|
||||
|
||||
Args:
|
||||
yaml_path: Path to the agents.yaml file
|
||||
|
||||
Returns:
|
||||
JSON string ready for clipboard
|
||||
"""
|
||||
workflow_json = self.convert_yaml_to_n8n(yaml_path)
|
||||
return json.dumps(workflow_json, indent=2)
|
||||
@@ -367,6 +367,14 @@ Example:
|
||||
self.print_status(f"Validating: {file_path}", "info")
|
||||
|
||||
try:
|
||||
# Load raw YAML to check for non-canonical names
|
||||
import yaml
|
||||
with open(file_path, 'r') as f:
|
||||
raw_data = yaml.safe_load(f)
|
||||
|
||||
# Check for non-canonical names and suggest canonical ones
|
||||
suggestions = self._get_canonical_suggestions(raw_data)
|
||||
|
||||
workflow = parser.parse_file(file_path)
|
||||
|
||||
# Print validation results
|
||||
@@ -386,12 +394,82 @@ Example:
|
||||
|
||||
rprint(table)
|
||||
self.print_status("✓ Workflow is valid!", "success")
|
||||
|
||||
# Show suggestions for canonical names
|
||||
if suggestions:
|
||||
rprint()
|
||||
rprint("[yellow]💡 Suggestions for canonical field names:[/yellow]")
|
||||
for suggestion in suggestions:
|
||||
rprint(f" [dim]•[/dim] {suggestion}")
|
||||
rprint()
|
||||
rprint("[dim]Note: Both old and new names work, but canonical names are recommended.[/dim]")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.print_status(f"✗ Validation failed: {e}", "error")
|
||||
return False
|
||||
|
||||
def _get_canonical_suggestions(self, data: Dict) -> List[str]:
|
||||
"""
|
||||
Check for non-canonical field names and return suggestions.
|
||||
|
||||
Canonical names (A-I-G-S mnemonic):
|
||||
- Agents (not roles)
|
||||
- Instructions (not backstory)
|
||||
- Goal (same)
|
||||
- Steps (not tasks)
|
||||
|
||||
Also:
|
||||
- name (not topic)
|
||||
- action (not description)
|
||||
|
||||
Args:
|
||||
data: Raw YAML data
|
||||
|
||||
Returns:
|
||||
List of suggestion strings
|
||||
"""
|
||||
suggestions = []
|
||||
|
||||
if not data:
|
||||
return suggestions
|
||||
|
||||
# Check top-level keys
|
||||
if 'roles' in data:
|
||||
suggestions.append("Use 'agents' instead of 'roles'")
|
||||
|
||||
if 'topic' in data and 'name' not in data:
|
||||
suggestions.append("Use 'name' instead of 'topic'")
|
||||
|
||||
# Check agent fields
|
||||
agents_data = data.get('agents', data.get('roles', {}))
|
||||
for agent_id, agent_config in agents_data.items():
|
||||
if isinstance(agent_config, dict):
|
||||
if 'backstory' in agent_config:
|
||||
suggestions.append(f"Agent '{agent_id}': Use 'instructions' instead of 'backstory'")
|
||||
|
||||
# Check nested tasks
|
||||
if 'tasks' in agent_config:
|
||||
suggestions.append(f"Agent '{agent_id}': Use 'steps' at top level instead of nested 'tasks'")
|
||||
|
||||
# Check step fields
|
||||
steps_data = data.get('steps', [])
|
||||
for i, step in enumerate(steps_data):
|
||||
if isinstance(step, dict):
|
||||
if 'description' in step and 'action' not in step:
|
||||
step_name = step.get('name', f'step {i+1}')
|
||||
suggestions.append(f"Step '{step_name}': Use 'action' instead of 'description'")
|
||||
|
||||
# Check parallel steps
|
||||
if 'parallel' in step:
|
||||
for j, parallel_step in enumerate(step['parallel']):
|
||||
if isinstance(parallel_step, dict):
|
||||
if 'description' in parallel_step and 'action' not in parallel_step:
|
||||
suggestions.append(f"Parallel step {j+1}: Use 'action' instead of 'description'")
|
||||
|
||||
return suggestions
|
||||
|
||||
def action_list(self, args: List[str], **kwargs) -> List[str]:
|
||||
"""
|
||||
List available workflows in .praison/workflows/ directory.
|
||||
@@ -567,8 +645,8 @@ Example:
|
||||
|
||||
return result_path
|
||||
|
||||
except ImportError as e:
|
||||
self.print_status(f"Auto-generation requires instructor: pip install instructor", "error")
|
||||
except ImportError:
|
||||
self.print_status("Auto-generation requires instructor: pip install instructor", "error")
|
||||
return None
|
||||
except Exception as e:
|
||||
self.print_status(f"Generation failed: {e}", "error")
|
||||
|
||||
@@ -210,6 +210,10 @@ class PraisonAI:
|
||||
version_string = f"PraisonAI version {__version__}"
|
||||
|
||||
self.framework = args.framework or self.framework
|
||||
|
||||
# Update config_list model if --model flag is provided
|
||||
if getattr(args, 'model', None):
|
||||
self.config_list[0]['model'] = args.model
|
||||
|
||||
# Check for piped input from stdin
|
||||
stdin_input = self.read_stdin_if_available()
|
||||
@@ -495,6 +499,31 @@ class PraisonAI:
|
||||
print(result)
|
||||
return result
|
||||
else:
|
||||
# n8n Integration - Export workflow to n8n and open in browser
|
||||
if getattr(args, 'n8n', False):
|
||||
from .features.n8n import N8nHandler
|
||||
n8n_handler = N8nHandler(
|
||||
verbose=getattr(args, 'verbose', False),
|
||||
n8n_url=getattr(args, 'n8n_url', 'http://localhost:5678')
|
||||
)
|
||||
result = n8n_handler.execute(self.agent_file)
|
||||
return result
|
||||
|
||||
# Flow Display - Show flow diagram WITHOUT executing (--flow-display flag)
|
||||
if getattr(args, 'flow_display', False):
|
||||
from .features.flow_display import FlowDisplayHandler
|
||||
flow_handler = FlowDisplayHandler(verbose=getattr(args, 'verbose', False))
|
||||
flow_handler.display_flow_diagram(self.agent_file)
|
||||
return # Exit without executing
|
||||
|
||||
# Always show flow diagram before execution (default behavior)
|
||||
try:
|
||||
from .features.flow_display import FlowDisplayHandler
|
||||
flow_handler = FlowDisplayHandler(verbose=getattr(args, 'verbose', False))
|
||||
flow_handler.display_flow_diagram(self.agent_file, show_footer=False)
|
||||
except Exception:
|
||||
pass # Continue even if flow display fails
|
||||
|
||||
agents_generator = AgentsGenerator(
|
||||
self.agent_file,
|
||||
self.framework,
|
||||
@@ -650,6 +679,10 @@ class PraisonAI:
|
||||
# Flow Display - visual workflow
|
||||
parser.add_argument("--flow-display", action="store_true", help="Enable visual workflow tracking")
|
||||
|
||||
# n8n Integration - export workflow to n8n
|
||||
parser.add_argument("--n8n", action="store_true", help="Export workflow to n8n and open in browser")
|
||||
parser.add_argument("--n8n-url", type=str, default="http://localhost:5678", help="n8n instance URL (default: http://localhost:5678)")
|
||||
|
||||
# If we're in a test environment, parse with empty args to avoid pytest interference
|
||||
if in_test_env:
|
||||
args, unknown_args = parser.parse_known_args([])
|
||||
@@ -1632,6 +1665,10 @@ class PraisonAI:
|
||||
if arg == '--output' and i + 1 < len(action_args):
|
||||
output_file = action_args[i + 1]
|
||||
self._create_workflow_from_template(template_name, output_file)
|
||||
|
||||
elif action == 'auto':
|
||||
# Auto-generate workflow from topic
|
||||
self._auto_generate_workflow(action_args)
|
||||
|
||||
elif action == 'help' or action == '--help':
|
||||
print("[bold]Workflow Commands:[/bold]")
|
||||
@@ -1642,6 +1679,7 @@ class PraisonAI:
|
||||
print(" praisonai workflow create <name> - Create a new workflow")
|
||||
print(" praisonai workflow validate <file.yaml> - Validate a YAML workflow")
|
||||
print(" praisonai workflow template <name> - Create from template")
|
||||
print(' praisonai workflow auto "topic" - Auto-generate workflow')
|
||||
print("\n[bold]Templates:[/bold]")
|
||||
print(" simple, routing, parallel, loop, evaluator-optimizer")
|
||||
print("\n[bold]Options (uses global flags):[/bold]")
|
||||
@@ -1763,6 +1801,7 @@ class PraisonAI:
|
||||
from rich import print
|
||||
from rich.table import Table
|
||||
from rich.console import Console
|
||||
import yaml
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -1772,6 +1811,13 @@ class PraisonAI:
|
||||
|
||||
print(f"[cyan]Validating: {yaml_file}[/cyan]")
|
||||
|
||||
# Load raw YAML to check for non-canonical names
|
||||
with open(yaml_file, 'r') as f:
|
||||
raw_data = yaml.safe_load(f)
|
||||
|
||||
# Check for non-canonical names and suggest canonical ones
|
||||
suggestions = self._get_canonical_suggestions(raw_data)
|
||||
|
||||
parser = YAMLWorkflowParser()
|
||||
workflow = parser.parse_file(yaml_file)
|
||||
|
||||
@@ -1790,8 +1836,77 @@ class PraisonAI:
|
||||
console.print(table)
|
||||
print("[green]✓ Workflow is valid![/green]")
|
||||
|
||||
# Show suggestions for canonical names
|
||||
if suggestions:
|
||||
print()
|
||||
print("[yellow]💡 Suggestions for canonical field names:[/yellow]")
|
||||
for suggestion in suggestions:
|
||||
print(f" [dim]•[/dim] {suggestion}")
|
||||
print()
|
||||
print("[dim]Note: Both old and new names work, but canonical names are recommended.[/dim]")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[red]✗ Validation failed: {e}[/red]")
|
||||
|
||||
def _get_canonical_suggestions(self, data: dict) -> list:
|
||||
"""
|
||||
Check for non-canonical field names and return suggestions.
|
||||
|
||||
Canonical names (A-I-G-S mnemonic):
|
||||
- Agents (not roles)
|
||||
- Instructions (not backstory)
|
||||
- Goal (same)
|
||||
- Steps (not tasks)
|
||||
|
||||
Also:
|
||||
- name (not topic)
|
||||
- action (not description)
|
||||
|
||||
Args:
|
||||
data: Raw YAML data
|
||||
|
||||
Returns:
|
||||
List of suggestion strings
|
||||
"""
|
||||
suggestions = []
|
||||
|
||||
if not data:
|
||||
return suggestions
|
||||
|
||||
# Check top-level keys
|
||||
if 'roles' in data:
|
||||
suggestions.append("Use 'agents' instead of 'roles'")
|
||||
|
||||
if 'topic' in data and 'name' not in data:
|
||||
suggestions.append("Use 'name' instead of 'topic'")
|
||||
|
||||
# Check agent fields
|
||||
agents_data = data.get('agents', data.get('roles', {}))
|
||||
for agent_id, agent_config in agents_data.items():
|
||||
if isinstance(agent_config, dict):
|
||||
if 'backstory' in agent_config:
|
||||
suggestions.append(f"Agent '{agent_id}': Use 'instructions' instead of 'backstory'")
|
||||
|
||||
# Check nested tasks
|
||||
if 'tasks' in agent_config:
|
||||
suggestions.append(f"Agent '{agent_id}': Use 'steps' at top level instead of nested 'tasks'")
|
||||
|
||||
# Check step fields
|
||||
steps_data = data.get('steps', [])
|
||||
for i, step in enumerate(steps_data):
|
||||
if isinstance(step, dict):
|
||||
if 'description' in step and 'action' not in step:
|
||||
step_name = step.get('name', f'step {i+1}')
|
||||
suggestions.append(f"Step '{step_name}': Use 'action' instead of 'description'")
|
||||
|
||||
# Check parallel steps
|
||||
if 'parallel' in step:
|
||||
for j, parallel_step in enumerate(step['parallel']):
|
||||
if isinstance(parallel_step, dict):
|
||||
if 'description' in parallel_step and 'action' not in parallel_step:
|
||||
suggestions.append(f"Parallel step {j+1}: Use 'action' instead of 'description'")
|
||||
|
||||
return suggestions
|
||||
|
||||
def _create_workflow_from_template(self, template_name: str = None, output_file: str = None):
|
||||
"""
|
||||
@@ -1839,6 +1954,76 @@ class PraisonAI:
|
||||
print(f"[green]✓ Created workflow: {output_file}[/green]")
|
||||
print(f"[cyan]Run with: praisonai workflow run {output_file}[/cyan]")
|
||||
|
||||
def _auto_generate_workflow(self, action_args: list):
|
||||
"""
|
||||
Auto-generate a workflow from a topic description.
|
||||
|
||||
Args:
|
||||
action_args: ["topic description", --pattern, <pattern>, --output, <file.yaml>]
|
||||
"""
|
||||
from rich import print
|
||||
|
||||
# Parse arguments
|
||||
topic = None
|
||||
pattern = "sequential"
|
||||
output_file = None
|
||||
|
||||
i = 0
|
||||
while i < len(action_args):
|
||||
if action_args[i] == "--pattern" and i + 1 < len(action_args):
|
||||
pattern = action_args[i + 1]
|
||||
i += 2
|
||||
elif action_args[i] == "--output" and i + 1 < len(action_args):
|
||||
output_file = action_args[i + 1]
|
||||
i += 2
|
||||
elif not action_args[i].startswith("--") and topic is None:
|
||||
topic = action_args[i]
|
||||
i += 1
|
||||
else:
|
||||
i += 1
|
||||
|
||||
if not topic:
|
||||
print('[red]Usage: praisonai workflow auto "topic" --pattern <pattern>[/red]')
|
||||
print("[cyan]Patterns: sequential, routing, parallel[/cyan]")
|
||||
return
|
||||
|
||||
# Validate pattern
|
||||
valid_patterns = ["sequential", "routing", "parallel", "loop"]
|
||||
if pattern not in valid_patterns:
|
||||
print(f"[red]Unknown pattern: {pattern}[/red]")
|
||||
print(f"[cyan]Valid patterns: {', '.join(valid_patterns)}[/cyan]")
|
||||
return
|
||||
|
||||
# Default output file
|
||||
if not output_file:
|
||||
safe_name = "".join(c if c.isalnum() else "_" for c in topic[:30]).lower()
|
||||
output_file = f"{safe_name}_workflow.yaml"
|
||||
|
||||
# Check if file exists
|
||||
if os.path.exists(output_file):
|
||||
print(f"[red]ERROR: File already exists: {output_file}[/red]")
|
||||
return
|
||||
|
||||
print(f"[cyan]Generating {pattern} workflow for: {topic}[/cyan]")
|
||||
|
||||
try:
|
||||
from praisonai.auto import WorkflowAutoGenerator
|
||||
|
||||
generator = WorkflowAutoGenerator(
|
||||
topic=topic,
|
||||
workflow_file=output_file
|
||||
)
|
||||
|
||||
result_path = generator.generate(pattern=pattern)
|
||||
|
||||
print(f"[green]✓ Created workflow: {result_path}[/green]")
|
||||
print(f"[cyan]Run with: praisonai workflow run {output_file}[/cyan]")
|
||||
|
||||
except ImportError:
|
||||
print("[red]Auto-generation requires instructor: pip install instructor[/red]")
|
||||
except Exception as e:
|
||||
print(f"[red]Generation failed: {e}[/red]")
|
||||
|
||||
def handle_hooks_command(self, action: str):
|
||||
"""
|
||||
Handle hooks subcommand actions.
|
||||
|
||||
@@ -57,7 +57,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.3.26 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.3.31 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
__version__ = "2.3.26"
|
||||
__version__ = "2.3.31"
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.1.4",
|
||||
"praisonaiagents>=0.1.12",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
"""
|
||||
Test-Driven Development tests for n8n integration.
|
||||
|
||||
Tests for converting PraisonAI agents.yaml to n8n workflow JSON format.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestN8nConverter:
|
||||
"""Tests for YAML to n8n JSON conversion."""
|
||||
|
||||
@pytest.fixture
|
||||
def sample_agents_yaml(self):
|
||||
"""Sample agents.yaml content for testing."""
|
||||
return """
|
||||
name: Test Workflow
|
||||
description: A test workflow for n8n integration
|
||||
|
||||
agents:
|
||||
researcher:
|
||||
name: Researcher
|
||||
role: Research Analyst
|
||||
goal: Research topics
|
||||
instructions: Research and gather information
|
||||
|
||||
writer:
|
||||
name: Writer
|
||||
role: Content Writer
|
||||
goal: Write content
|
||||
instructions: Write based on research
|
||||
|
||||
steps:
|
||||
- agent: researcher
|
||||
action: "Research the topic"
|
||||
|
||||
- agent: writer
|
||||
action: "Write content based on research"
|
||||
"""
|
||||
|
||||
@pytest.fixture
|
||||
def sample_yaml_file(self, sample_agents_yaml):
|
||||
"""Create a temporary YAML file for testing."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
f.write(sample_agents_yaml)
|
||||
f.flush()
|
||||
yield f.name
|
||||
os.unlink(f.name)
|
||||
|
||||
def test_n8n_handler_import(self):
|
||||
"""Test that N8nHandler can be imported."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
assert N8nHandler is not None
|
||||
|
||||
def test_n8n_handler_initialization(self):
|
||||
"""Test N8nHandler initialization."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler(verbose=True)
|
||||
assert handler.feature_name == "n8n"
|
||||
assert handler.verbose is True
|
||||
|
||||
def test_convert_yaml_to_n8n_returns_dict(self, sample_yaml_file):
|
||||
"""Test that convert_yaml_to_n8n returns a dictionary."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
assert isinstance(result, dict)
|
||||
|
||||
def test_n8n_workflow_has_required_keys(self, sample_yaml_file):
|
||||
"""Test that n8n workflow JSON has required keys."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
assert "name" in result
|
||||
assert "nodes" in result
|
||||
assert "connections" in result
|
||||
|
||||
def test_n8n_workflow_has_trigger_node(self, sample_yaml_file):
|
||||
"""Test that n8n workflow has a manual trigger node."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
nodes = result["nodes"]
|
||||
trigger_nodes = [n for n in nodes if "trigger" in n.get("type", "").lower() or "trigger" in n.get("name", "").lower()]
|
||||
assert len(trigger_nodes) >= 1
|
||||
|
||||
def test_n8n_workflow_has_agent_nodes(self, sample_yaml_file):
|
||||
"""Test that n8n workflow has agent nodes (Execute Command or HTTP Request)."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
nodes = result["nodes"]
|
||||
# Agent nodes can be either executeCommand or httpRequest
|
||||
agent_nodes = [n for n in nodes if "executeCommand" in n.get("type", "") or "httpRequest" in n.get("type", "")]
|
||||
# Should have at least 2 agent nodes (one per agent step)
|
||||
assert len(agent_nodes) >= 2
|
||||
|
||||
def test_n8n_workflow_nodes_are_connected(self, sample_yaml_file):
|
||||
"""Test that nodes are properly connected."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
connections = result["connections"]
|
||||
assert len(connections) > 0
|
||||
|
||||
def test_n8n_workflow_name_from_yaml(self, sample_yaml_file):
|
||||
"""Test that workflow name is taken from YAML."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
assert result["name"] == "Test Workflow"
|
||||
|
||||
def test_generate_n8n_url(self, sample_yaml_file):
|
||||
"""Test URL generation for n8n."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
workflow_json = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
url = handler.generate_n8n_url(workflow_json, "http://localhost:5678")
|
||||
|
||||
assert url.startswith("http://localhost:5678")
|
||||
assert "workflow" in url.lower()
|
||||
|
||||
def test_node_positions_are_sequential(self, sample_yaml_file):
|
||||
"""Test that node positions are laid out sequentially."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(sample_yaml_file)
|
||||
|
||||
nodes = result["nodes"]
|
||||
positions = [n.get("position", [0, 0]) for n in nodes]
|
||||
|
||||
# X positions should increase for sequential workflow
|
||||
x_positions = [p[0] for p in positions]
|
||||
assert x_positions == sorted(x_positions), "Nodes should be positioned left to right"
|
||||
|
||||
|
||||
class TestN8nCLIIntegration:
|
||||
"""Tests for CLI integration of n8n feature."""
|
||||
|
||||
def test_n8n_flag_in_argparser(self):
|
||||
"""Test that --n8n flag is recognized by argparser."""
|
||||
import argparse
|
||||
# This will be tested after we add the flag to main.py
|
||||
pass
|
||||
|
||||
def test_execute_method_exists(self):
|
||||
"""Test that execute method exists."""
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
handler = N8nHandler()
|
||||
assert hasattr(handler, 'execute')
|
||||
assert callable(handler.execute)
|
||||
|
||||
|
||||
class TestN8nWorkflowPatterns:
|
||||
"""Tests for different workflow patterns."""
|
||||
|
||||
@pytest.fixture
|
||||
def parallel_workflow_yaml(self):
|
||||
"""YAML with parallel steps."""
|
||||
return """
|
||||
name: Parallel Workflow
|
||||
|
||||
agents:
|
||||
agent1:
|
||||
name: Agent1
|
||||
role: Role1
|
||||
goal: Goal1
|
||||
agent2:
|
||||
name: Agent2
|
||||
role: Role2
|
||||
goal: Goal2
|
||||
|
||||
steps:
|
||||
- parallel:
|
||||
- agent: agent1
|
||||
action: "Do task 1"
|
||||
- agent: agent2
|
||||
action: "Do task 2"
|
||||
"""
|
||||
|
||||
@pytest.fixture
|
||||
def route_workflow_yaml(self):
|
||||
"""YAML with route/decision steps."""
|
||||
return """
|
||||
name: Route Workflow
|
||||
|
||||
agents:
|
||||
classifier:
|
||||
name: Classifier
|
||||
role: Classifier
|
||||
goal: Classify input
|
||||
handler_a:
|
||||
name: HandlerA
|
||||
role: Handler
|
||||
goal: Handle type A
|
||||
handler_b:
|
||||
name: HandlerB
|
||||
role: Handler
|
||||
goal: Handle type B
|
||||
|
||||
steps:
|
||||
- agent: classifier
|
||||
action: "Classify the input"
|
||||
|
||||
- route:
|
||||
type_a: [handler_a]
|
||||
type_b: [handler_b]
|
||||
"""
|
||||
|
||||
def test_parallel_workflow_creates_split_node(self, parallel_workflow_yaml):
|
||||
"""Test that parallel steps create appropriate n8n structure."""
|
||||
import tempfile
|
||||
import os
|
||||
from praisonai.cli.features.n8n import N8nHandler
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
f.write(parallel_workflow_yaml)
|
||||
f.flush()
|
||||
|
||||
handler = N8nHandler()
|
||||
result = handler.convert_yaml_to_n8n(f.name)
|
||||
|
||||
# Should have nodes for parallel execution
|
||||
assert len(result["nodes"]) >= 2
|
||||
|
||||
os.unlink(f.name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -1,15 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test script to verify Phase 2 refactoring maintains backward compatibility"""
|
||||
"""Test script to verify Phase 2 refactoring maintains backward compatibility
|
||||
|
||||
NOTE: These tests make real API calls and require valid API keys.
|
||||
They are marked with @pytest.mark.real to be excluded from CI.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
|
||||
# Add the correct path for imports
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src/praisonai-agents'))
|
||||
|
||||
from praisonaiagents.llm import LLM
|
||||
|
||||
@pytest.mark.real
|
||||
def test_sync_methods():
|
||||
"""Test synchronous methods still work correctly"""
|
||||
print("Testing synchronous methods...")
|
||||
@@ -52,6 +58,7 @@ def test_sync_methods():
|
||||
|
||||
print("✅ Synchronous methods working correctly\n")
|
||||
|
||||
@pytest.mark.real
|
||||
async def test_async_methods():
|
||||
"""Test asynchronous methods still work correctly"""
|
||||
print("Testing asynchronous methods...")
|
||||
@@ -94,6 +101,7 @@ async def test_async_methods():
|
||||
|
||||
print("✅ Asynchronous methods working correctly\n")
|
||||
|
||||
@pytest.mark.real
|
||||
def test_edge_cases():
|
||||
"""Test edge cases and special scenarios"""
|
||||
print("Testing edge cases...")
|
||||
|
||||
Generated
+4
-4
@@ -4197,7 +4197,7 @@ requires-dist = [
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen-v4'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.22" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.1.4" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.1.12" },
|
||||
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = "==0.2.29" },
|
||||
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
|
||||
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
|
||||
@@ -4252,7 +4252,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.1.4"
|
||||
version = "0.1.12"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
@@ -4261,9 +4261,9 @@ dependencies = [
|
||||
{ name = "pydantic" },
|
||||
{ name = "rich" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/ab/9d/d286e0ba5421cb8a37ea86f4714a7fbcb39fe1f38e9a202cf0cf6381ae8c/praisonaiagents-0.1.4.tar.gz", hash = "sha256:d00a326ac65701bcc66c44aba7d1616ef1317a2491034a242ad4290f0269e190", size = 560571, upload-time = "2025-12-17T11:43:41.829Z" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/06/ea/0489a0f62da0ded124c7a4fe079d3c898537e7a4ec639dc774c8420855e1/praisonaiagents-0.1.12.tar.gz", hash = "sha256:9d0dcb64fa0fd642fef60939d4547c2aae07267bf894d565e0e9a8013dc8c035", size = 620248, upload-time = "2025-12-18T12:54:12.363Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/f3/90/b6429a58f75aeec4da0034d7594863b3fd6e4bf916dbcbeb53a8597f0f4a/praisonaiagents-0.1.4-py3-none-any.whl", hash = "sha256:c7ad9b64e3d3a24b9560d695bc7447f286928e52f0979e84d02484662cb8a49d", size = 524190, upload-time = "2025-12-17T11:43:40.119Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/1b/71/fb62eb0745d56301f844c545fd66fa6f2933beacf9d4dd6a065fc38d9521/praisonaiagents-0.1.12-py3-none-any.whl", hash = "sha256:d02e676b9028b9420b49d784986812315b0f668fde0458980837de612a120791", size = 547369, upload-time = "2025-12-18T12:54:10.709Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in new issue
Block a user