mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-07 17:33:33 -10:00
Compare commits
No files matched your search
@@ -11,6 +11,7 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
@@ -10,6 +10,7 @@ jobs:
|
||||
test:
|
||||
name: Run tests and collect coverage
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
||||
@@ -26,6 +26,7 @@ on:
|
||||
jobs:
|
||||
comprehensive-test:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: ["3.11"]
|
||||
|
||||
@@ -9,6 +9,7 @@ on:
|
||||
jobs:
|
||||
test-core:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.11]
|
||||
|
||||
@@ -13,6 +13,7 @@ on:
|
||||
jobs:
|
||||
test-examples:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
@@ -43,6 +44,9 @@ jobs:
|
||||
echo "OPENAI_API_BASE=${{ secrets.OPENAI_API_BASE || 'https://api.openai.com/v1' }}" >> $GITHUB_ENV
|
||||
echo "OPENAI_MODEL_NAME=${{ secrets.OPENAI_MODEL_NAME || 'gpt-4o-mini' }}" >> $GITHUB_ENV
|
||||
echo "PYTHONPATH=${{ github.workspace }}/src/praisonai-agents:$PYTHONPATH" >> $GITHUB_ENV
|
||||
echo "PRAISONAI_TELEMETRY_DISABLED=true" >> $GITHUB_ENV
|
||||
echo "PRAISONAI_DISABLE_TELEMETRY=true" >> $GITHUB_ENV
|
||||
echo "DO_NOT_TRACK=true" >> $GITHUB_ENV
|
||||
|
||||
- name: Test Key Example Scripts
|
||||
run: |
|
||||
@@ -73,6 +77,7 @@ jobs:
|
||||
|
||||
performance-test:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
@@ -97,6 +102,12 @@ jobs:
|
||||
# Install knowledge dependencies from praisonai-agents
|
||||
uv pip install --system "praisonaiagents[knowledge]"
|
||||
|
||||
- name: Set environment variables
|
||||
run: |
|
||||
echo "PRAISONAI_TELEMETRY_DISABLED=true" >> $GITHUB_ENV
|
||||
echo "PRAISONAI_DISABLE_TELEMETRY=true" >> $GITHUB_ENV
|
||||
echo "DO_NOT_TRACK=true" >> $GITHUB_ENV
|
||||
|
||||
- name: Run Performance Benchmarks
|
||||
run: |
|
||||
echo "🏃 Running performance benchmarks..."
|
||||
@@ -161,4 +172,4 @@ jobs:
|
||||
with:
|
||||
name: performance-report
|
||||
path: performance_report.md
|
||||
retention-days: 30
|
||||
retention-days: 5
|
||||
@@ -24,6 +24,7 @@ on:
|
||||
jobs:
|
||||
framework-tests:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.11]
|
||||
|
||||
@@ -26,6 +26,7 @@ jobs:
|
||||
real-tests:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ github.event.inputs.confirm_costs == 'true' && github.event.inputs.framework != 'none' }}
|
||||
timeout-minutes: 15
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
|
||||
@@ -5,6 +5,7 @@ on: [push, pull_request]
|
||||
jobs:
|
||||
quick-test:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
|
||||
@@ -120,6 +120,10 @@ agent.start('Write a movie script about a robot in Mars');
|
||||
|
||||

|
||||
|
||||
## Star History
|
||||
|
||||
[](https://docs.praison.ai)
|
||||
|
||||
## AI Agents Flow
|
||||
|
||||
```mermaid
|
||||
@@ -557,10 +561,6 @@ uv pip install -r pyproject.toml --extra "crewai,autogen"
|
||||
- 🖼️ Vision Language Model (VLM) Support
|
||||
- 🎙️ Real-time Voice Interaction
|
||||
|
||||
## Star History
|
||||
|
||||
[](https://docs.praison.ai)
|
||||
|
||||
## Video Tutorials
|
||||
|
||||
| Topic | Video |
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
"praisonai>=2.2.66" \
|
||||
"praisonai>=2.2.78" \
|
||||
"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.2.66" \
|
||||
"praisonai>=2.2.78" \
|
||||
"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.2.66" \
|
||||
"praisonai>=2.2.78" \
|
||||
"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.2.66" \
|
||||
"praisonai>=2.2.78" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
+2
-2
@@ -121,7 +121,7 @@ healthcheck:
|
||||
## 📦 Package Versions
|
||||
|
||||
All Docker images use consistent, up-to-date versions:
|
||||
- PraisonAI: `>=2.2.66`
|
||||
- PraisonAI: `>=2.2.78`
|
||||
- PraisonAI Agents: `>=0.0.92`
|
||||
- Python: `3.11-slim`
|
||||
|
||||
@@ -218,7 +218,7 @@ docker-compose up -d
|
||||
### Version Pinning
|
||||
To use specific versions, update the Dockerfile:
|
||||
```dockerfile
|
||||
RUN pip install "praisonai==2.2.66" "praisonaiagents==0.0.92"
|
||||
RUN pip install "praisonai==2.2.78" "praisonaiagents==0.0.92"
|
||||
```
|
||||
|
||||
## 🌐 Production Deployment
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Context Agent Example - Basic Usage
|
||||
|
||||
This example demonstrates how to use the ContextAgent for automated context generation.
|
||||
The ContextAgent implements Context Engineering principles to generate comprehensive
|
||||
context for AI coding assistants, enabling first-try implementation success.
|
||||
|
||||
Key features demonstrated:
|
||||
- Basic ContextAgent instantiation
|
||||
- Codebase pattern analysis
|
||||
- Context document generation
|
||||
- Validation loop creation
|
||||
- Prompt enhancement with context
|
||||
- PRP (Product Requirements Prompt) generation
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the src directory to Python path for imports
|
||||
project_root = Path(__file__).parent.parent.parent.parent / "src" / "praisonai-agents"
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from praisonaiagents import create_context_agent
|
||||
|
||||
def demonstrate_basic_context_generation():
|
||||
"""Demonstrate basic context generation capabilities."""
|
||||
print("🔧 Context Engineering Example: Basic Usage")
|
||||
print("=" * 60)
|
||||
|
||||
# Create a ContextAgent using the factory function
|
||||
context_agent = create_context_agent(
|
||||
llm="gpt-4o-mini",
|
||||
name="Basic Context Engineer",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Example 1: Analyze codebase patterns
|
||||
print("\n📁 Example 1: Analyzing Codebase Patterns")
|
||||
print("-" * 40)
|
||||
|
||||
# Use current project as example (adjust path as needed)
|
||||
project_path = str(project_root)
|
||||
analysis = context_agent.analyze_codebase_patterns(
|
||||
project_path=project_path,
|
||||
file_patterns=["*.py"]
|
||||
)
|
||||
|
||||
print(f"✅ Analyzed project at: {project_path}")
|
||||
print(f" Project structure: {len(analysis.get('project_structure', {}).get('directories', []))} directories")
|
||||
print(f" Code patterns identified: {len(analysis.get('code_patterns', {}))}")
|
||||
print(f" Architecture: {analysis.get('architecture_insights', {}).get('primary_pattern', 'Unknown')}")
|
||||
|
||||
# Example 2: Generate context document
|
||||
print("\n📄 Example 2: Generating Context Document")
|
||||
print("-" * 40)
|
||||
|
||||
feature_request = "Add user authentication system with JWT tokens and role-based access control"
|
||||
context_doc = context_agent.generate_context_document(
|
||||
project_path=project_path,
|
||||
requirements=feature_request,
|
||||
analysis=analysis
|
||||
)
|
||||
|
||||
print(f"✅ Generated context document for: {feature_request}")
|
||||
print(f" Document length: {len(context_doc)} characters")
|
||||
print(f" Contains architecture patterns: {'Architecture Patterns' in context_doc}")
|
||||
print(f" Contains validation criteria: {'Validation Criteria' in context_doc}")
|
||||
|
||||
# Example 3: Create validation loop
|
||||
print("\n✅ Example 3: Creating Validation Loop")
|
||||
print("-" * 40)
|
||||
|
||||
success_criteria = [
|
||||
"Authentication system accepts valid JWT tokens",
|
||||
"Role-based access control blocks unauthorized users",
|
||||
"User registration and login endpoints work correctly",
|
||||
"Password hashing follows security best practices",
|
||||
"Session management handles token expiration"
|
||||
]
|
||||
|
||||
validation_config = context_agent.create_validation_loop(
|
||||
implementation_requirements=feature_request,
|
||||
success_criteria=success_criteria
|
||||
)
|
||||
|
||||
print(f"✅ Created validation loop with {len(validation_config['validation_steps'])} steps")
|
||||
print(f" Success criteria: {len(success_criteria)}")
|
||||
print(f" Executable tests: {len(validation_config['executable_tests'])}")
|
||||
print(f" Quality gates: {len(validation_config['quality_gates'])}")
|
||||
|
||||
# Example 4: Enhance prompt with context
|
||||
print("\n🚀 Example 4: Enhancing Prompt with Context")
|
||||
print("-" * 40)
|
||||
|
||||
basic_prompt = "Implement user authentication"
|
||||
enhanced_prompt = context_agent.enhance_prompt_with_context(
|
||||
base_prompt=basic_prompt,
|
||||
context_data=analysis
|
||||
)
|
||||
|
||||
print(f"✅ Enhanced prompt from {len(basic_prompt)} to {len(enhanced_prompt)} characters")
|
||||
print(f" Original: '{basic_prompt}'")
|
||||
print(" Enhanced includes: Context Engineering, Implementation Context, Quality Requirements")
|
||||
|
||||
# Example 5: Generate PRP (Product Requirements Prompt)
|
||||
print("\n📋 Example 5: Generating PRP")
|
||||
print("-" * 40)
|
||||
|
||||
documentation_links = [
|
||||
"https://jwt.io/introduction/",
|
||||
"https://fastapi.tiangolo.com/tutorial/security/",
|
||||
"https://passlib.readthedocs.io/en/stable/"
|
||||
]
|
||||
|
||||
prp = context_agent.generate_prp(
|
||||
feature_request=feature_request,
|
||||
context_analysis=analysis,
|
||||
documentation_links=documentation_links
|
||||
)
|
||||
|
||||
print("✅ Generated comprehensive PRP")
|
||||
print(f" PRP length: {len(prp)} characters")
|
||||
print(f" Contains feature request: {feature_request in prp}")
|
||||
print(f" Contains documentation links: {len(documentation_links)} links included")
|
||||
print(f" Contains implementation blueprint: {'Implementation Blueprint' in prp}")
|
||||
print(f" Confidence score: {'9/10' in prp}")
|
||||
|
||||
def demonstrate_context_agent_as_tool():
|
||||
"""Demonstrate using ContextAgent as a tool in a multi-agent workflow."""
|
||||
print("\n🔗 Context Engineering Example: Multi-Agent Integration")
|
||||
print("=" * 60)
|
||||
|
||||
# Create ContextAgent
|
||||
context_agent = create_context_agent(llm="gpt-4o-mini")
|
||||
|
||||
# Example scenario: Product planning workflow
|
||||
project_path = str(project_root)
|
||||
feature_request = "Build a real-time chat system with WebSocket support"
|
||||
|
||||
# Step 1: Context analysis
|
||||
print("\n1️⃣ Step 1: Context Analysis")
|
||||
analysis = context_agent.analyze_codebase_patterns(project_path)
|
||||
print(f"✅ Analyzed codebase architecture and patterns")
|
||||
|
||||
# Step 2: Generate implementation blueprint
|
||||
print("\n2️⃣ Step 2: Implementation Blueprint")
|
||||
blueprint = context_agent.create_implementation_blueprint(feature_request, analysis)
|
||||
print(f"✅ Created implementation blueprint with {len(blueprint['implementation_steps'])} steps")
|
||||
|
||||
# Step 3: Generate comprehensive PRP
|
||||
print("\n3️⃣ Step 3: Generate PRP")
|
||||
prp = context_agent.generate_prp(feature_request, analysis) # Generated PRP for implementation guidance
|
||||
print(f"✅ Generated PRP for implementation guidance")
|
||||
|
||||
# Step 4: Create validation framework
|
||||
print("\n4️⃣ Step 4: Validation Framework")
|
||||
criteria = [
|
||||
"WebSocket connections establish successfully",
|
||||
"Messages broadcast to all connected clients",
|
||||
"Chat history persists and loads correctly",
|
||||
"User authentication works with WebSocket",
|
||||
"Connection handling is robust with reconnection"
|
||||
]
|
||||
validation = context_agent.create_validation_loop(feature_request, criteria) # Validation framework for QA
|
||||
print(f"✅ Created validation framework with {len(criteria)} criteria")
|
||||
|
||||
print("\n🎯 Context Engineering Workflow Complete!")
|
||||
print(f" Feature: {feature_request}")
|
||||
print(" Implementation ready with comprehensive context")
|
||||
print(" Validation criteria defined for quality assurance")
|
||||
|
||||
def show_context_engineering_benefits():
|
||||
"""Demonstrate the benefits of Context Engineering vs traditional approaches."""
|
||||
print("\n📊 Context Engineering Benefits")
|
||||
print("=" * 60)
|
||||
|
||||
# Traditional prompt engineering approach
|
||||
print("\n❌ Traditional Prompt Engineering:")
|
||||
traditional_prompt = "Create a user authentication system"
|
||||
print(f" Prompt: '{traditional_prompt}'")
|
||||
print(f" Length: {len(traditional_prompt)} characters")
|
||||
print(f" Context: Minimal - relies on AI's general knowledge")
|
||||
print(f" Success Rate: Variable - depends on AI model's training")
|
||||
|
||||
# Context Engineering approach
|
||||
print("\n✅ Context Engineering Approach:")
|
||||
context_agent = create_context_agent(llm="gpt-4o-mini")
|
||||
|
||||
# Generate comprehensive context
|
||||
analysis = context_agent.analyze_codebase_patterns(str(project_root))
|
||||
enhanced_prompt = context_agent.enhance_prompt_with_context(
|
||||
traditional_prompt, analysis
|
||||
)
|
||||
|
||||
print(f" Enhanced Prompt Length: {len(enhanced_prompt)} characters")
|
||||
print(f" Context: Comprehensive - includes:")
|
||||
print(f" • Codebase architecture analysis")
|
||||
print(f" • Existing patterns and conventions")
|
||||
print(f" • Implementation guidance")
|
||||
print(f" • Quality requirements")
|
||||
print(f" • Validation criteria")
|
||||
print(f" Success Rate: Higher - AI has all necessary context")
|
||||
|
||||
print(f"\n🎯 Context Engineering provides:")
|
||||
print(" 📈 10x better than prompt engineering (comprehensive vs clever wording)")
|
||||
print(" 📈 100x better than vibe coding (structured vs ad-hoc)")
|
||||
print(" 🎯 First-try implementation success through complete context")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🚀 PraisonAI Context Engineering Examples")
|
||||
print("=========================================")
|
||||
|
||||
try:
|
||||
# Run all examples
|
||||
demonstrate_basic_context_generation()
|
||||
demonstrate_context_agent_as_tool()
|
||||
show_context_engineering_benefits()
|
||||
|
||||
print("\n🎉 All Context Engineering examples completed successfully!")
|
||||
print("\n💡 Key Takeaways:")
|
||||
print(" • ContextAgent automates comprehensive context generation")
|
||||
print(" • Context Engineering enables first-try implementation success")
|
||||
print(" • Integration with existing PraisonAI agents is seamless")
|
||||
print(" • Validation frameworks ensure quality and completeness")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error running examples: {e}")
|
||||
print(" Make sure you have the PraisonAI package installed and configured")
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,469 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Context Engineering Workflow - Advanced Multi-Agent Example
|
||||
|
||||
This example demonstrates advanced Context Engineering workflows using ContextAgent
|
||||
in combination with other PraisonAI agents. It shows how Context Engineering can
|
||||
dramatically improve AI implementation success rates through comprehensive context
|
||||
generation and validation loops.
|
||||
|
||||
Workflow demonstrated:
|
||||
1. Product Manager Agent defines requirements
|
||||
2. ContextAgent analyzes codebase and generates comprehensive context
|
||||
3. Architecture Agent designs implementation using context
|
||||
4. Developer Agent implements using context-enhanced prompts
|
||||
5. QA Agent validates using context-generated criteria
|
||||
|
||||
This represents the full Context Engineering methodology in action.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the src directory to Python path for imports
|
||||
project_root = Path(__file__).parent.parent.parent.parent / "src" / "praisonai-agents"
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents, create_context_agent
|
||||
|
||||
class ContextEngineeringWorkflow:
|
||||
"""
|
||||
Advanced Context Engineering workflow demonstrating the full methodology.
|
||||
|
||||
This class orchestrates multiple agents using Context Engineering principles
|
||||
to achieve higher success rates in AI-driven development tasks.
|
||||
"""
|
||||
|
||||
def __init__(self, project_path: str, llm: str = "gpt-4o-mini"):
|
||||
self.project_path = project_path
|
||||
self.llm = llm
|
||||
self.context_data = {}
|
||||
self.setup_agents()
|
||||
|
||||
def setup_agents(self):
|
||||
"""Setup the multi-agent team with Context Engineering support."""
|
||||
|
||||
# 1. Product Manager Agent - Defines requirements
|
||||
self.product_manager = Agent(
|
||||
name="Product Manager",
|
||||
role="Product Requirements Specialist",
|
||||
goal="Define clear, comprehensive product requirements and user stories",
|
||||
backstory="""You are an experienced product manager who specializes in
|
||||
creating detailed, actionable requirements. You understand that clear
|
||||
requirements are the foundation of successful implementation.""",
|
||||
instructions="""
|
||||
When defining requirements:
|
||||
1. Be specific and measurable
|
||||
2. Include user acceptance criteria
|
||||
3. Consider edge cases and error scenarios
|
||||
4. Define success metrics
|
||||
5. Specify technical constraints
|
||||
""",
|
||||
llm=self.llm,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# 2. Context Engineering Agent - Generates comprehensive context
|
||||
self.context_engineer = create_context_agent(
|
||||
llm=self.llm,
|
||||
name="Context Engineering Specialist",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# 3. Software Architect Agent - Designs implementation using context
|
||||
self.architect = Agent(
|
||||
name="Software Architect",
|
||||
role="System Architecture Specialist",
|
||||
goal="Design robust, scalable system architecture based on comprehensive context",
|
||||
backstory="""You are a senior software architect with expertise in
|
||||
designing systems that follow established patterns and best practices.
|
||||
You excel at creating architectures that integrate seamlessly with
|
||||
existing codebases.""",
|
||||
instructions="""
|
||||
When designing architecture:
|
||||
1. Follow the patterns identified in the context analysis
|
||||
2. Ensure compatibility with existing systems
|
||||
3. Design for scalability and maintainability
|
||||
4. Consider security and performance implications
|
||||
5. Document architectural decisions and rationale
|
||||
""",
|
||||
llm=self.llm,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# 4. Senior Developer Agent - Implements using context-enhanced guidance
|
||||
self.developer = Agent(
|
||||
name="Senior Developer",
|
||||
role="Implementation Specialist",
|
||||
goal="Implement features following context-guided best practices",
|
||||
backstory="""You are a senior developer who excels at implementing
|
||||
features that follow established codebase patterns. You understand
|
||||
that consistency and quality are paramount.""",
|
||||
instructions="""
|
||||
When implementing features:
|
||||
1. Follow the codebase patterns identified in context analysis
|
||||
2. Maintain consistency with existing code style
|
||||
3. Implement comprehensive error handling
|
||||
4. Write clean, maintainable code
|
||||
5. Follow the implementation blueprint exactly
|
||||
""",
|
||||
llm=self.llm,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# 5. QA Engineer Agent - Validates using context-generated criteria
|
||||
self.qa_engineer = Agent(
|
||||
name="QA Engineer",
|
||||
role="Quality Assurance Specialist",
|
||||
goal="Ensure implementation meets all quality criteria and requirements",
|
||||
backstory="""You are an experienced QA engineer who specializes in
|
||||
comprehensive testing and validation. You understand that quality
|
||||
is built in, not bolted on.""",
|
||||
instructions="""
|
||||
When validating implementations:
|
||||
1. Use the validation criteria from context analysis
|
||||
2. Test both happy path and edge cases
|
||||
3. Verify integration with existing systems
|
||||
4. Check for security vulnerabilities
|
||||
5. Validate performance requirements
|
||||
""",
|
||||
llm=self.llm,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
def run_context_engineering_workflow(self, feature_request: str):
|
||||
"""
|
||||
Execute the complete Context Engineering workflow.
|
||||
|
||||
Args:
|
||||
feature_request (str): The feature to be implemented
|
||||
|
||||
Returns:
|
||||
dict: Complete workflow results with context and implementation
|
||||
"""
|
||||
print(f"🚀 Starting Context Engineering Workflow")
|
||||
print(f"Feature Request: {feature_request}")
|
||||
print("=" * 80)
|
||||
|
||||
# Phase 1: Requirements Analysis
|
||||
print("\n📋 Phase 1: Product Requirements Analysis")
|
||||
print("-" * 50)
|
||||
|
||||
requirements_task = Task(
|
||||
name="requirements_analysis",
|
||||
description=f"""
|
||||
Analyze and refine the feature request: '{feature_request}'
|
||||
|
||||
Create comprehensive requirements including:
|
||||
- Detailed feature description
|
||||
- User acceptance criteria
|
||||
- Technical requirements
|
||||
- Success metrics
|
||||
- Edge cases and constraints
|
||||
""",
|
||||
expected_output="Comprehensive product requirements document",
|
||||
agent=self.product_manager
|
||||
)
|
||||
|
||||
# Phase 2: Context Engineering Analysis
|
||||
print("\n🔧 Phase 2: Context Engineering Analysis")
|
||||
print("-" * 50)
|
||||
|
||||
# Generate comprehensive context using ContextAgent
|
||||
codebase_analysis = self.context_engineer.analyze_codebase_patterns(
|
||||
project_path=self.project_path
|
||||
)
|
||||
|
||||
context_document = self.context_engineer.generate_context_document(
|
||||
project_path=self.project_path,
|
||||
requirements=feature_request,
|
||||
analysis=codebase_analysis
|
||||
)
|
||||
|
||||
validation_framework = self.context_engineer.create_validation_loop(
|
||||
implementation_requirements=feature_request,
|
||||
success_criteria=[
|
||||
"Feature implements all specified requirements",
|
||||
"Code follows existing patterns and conventions",
|
||||
"Implementation includes comprehensive error handling",
|
||||
"All tests pass and coverage meets standards",
|
||||
"Integration with existing systems is seamless"
|
||||
]
|
||||
)
|
||||
|
||||
implementation_blueprint = self.context_engineer.create_implementation_blueprint(
|
||||
feature_request=feature_request,
|
||||
context_analysis=codebase_analysis
|
||||
)
|
||||
|
||||
# Generate PRP for complete context
|
||||
prp = self.context_engineer.generate_prp(
|
||||
feature_request=feature_request,
|
||||
context_analysis=codebase_analysis,
|
||||
documentation_links=[
|
||||
"https://docs.praisonai.com/",
|
||||
"https://pydantic-docs.helpmanual.io/",
|
||||
"https://fastapi.tiangolo.com/"
|
||||
]
|
||||
)
|
||||
|
||||
print(f"✅ Context Engineering Analysis Complete:")
|
||||
print(f" • Codebase analysis: {len(str(codebase_analysis))} chars")
|
||||
print(f" • Context document: {len(context_document)} chars")
|
||||
print(f" • Validation framework: {len(validation_framework['validation_steps'])} steps")
|
||||
print(f" • Implementation blueprint: {len(implementation_blueprint['implementation_steps'])} steps")
|
||||
print(f" • PRP generated: {len(prp)} chars")
|
||||
|
||||
# Store context data for subsequent phases
|
||||
self.context_data = {
|
||||
"codebase_analysis": codebase_analysis,
|
||||
"context_document": context_document,
|
||||
"validation_framework": validation_framework,
|
||||
"implementation_blueprint": implementation_blueprint,
|
||||
"prp": prp
|
||||
}
|
||||
|
||||
# Phase 3: Architecture Design with Context
|
||||
print("\n🏗️ Phase 3: Architecture Design with Context")
|
||||
print("-" * 50)
|
||||
|
||||
architecture_task = Task(
|
||||
name="architecture_design",
|
||||
description=f"""
|
||||
Design system architecture for: '{feature_request}'
|
||||
|
||||
Use the comprehensive context provided:
|
||||
{context_document}
|
||||
|
||||
Implementation Blueprint:
|
||||
{implementation_blueprint}
|
||||
|
||||
Design architecture that:
|
||||
- Follows identified codebase patterns
|
||||
- Integrates with existing systems
|
||||
- Meets all technical requirements
|
||||
- Is scalable and maintainable
|
||||
""",
|
||||
expected_output="Detailed system architecture design with component specifications",
|
||||
agent=self.architect,
|
||||
context=[requirements_task]
|
||||
)
|
||||
|
||||
# Phase 4: Implementation with Context-Enhanced Guidance
|
||||
print("\n💻 Phase 4: Implementation with Context")
|
||||
print("-" * 50)
|
||||
|
||||
# Enhance the implementation prompt with context
|
||||
enhanced_prompt = self.context_engineer.enhance_prompt_with_context(
|
||||
base_prompt=f"Implement {feature_request}",
|
||||
context_data=codebase_analysis
|
||||
)
|
||||
|
||||
implementation_task = Task(
|
||||
name="feature_implementation",
|
||||
description=f"""
|
||||
Implement the feature using context-enhanced guidance:
|
||||
|
||||
{enhanced_prompt}
|
||||
|
||||
Product Requirements Prompt (PRP):
|
||||
{prp}
|
||||
|
||||
Architecture Design: Reference the architecture task output
|
||||
|
||||
Implementation must:
|
||||
- Follow the implementation blueprint exactly
|
||||
- Use patterns identified in codebase analysis
|
||||
- Meet all requirements from Phase 1
|
||||
- Include comprehensive error handling
|
||||
""",
|
||||
expected_output="Complete feature implementation with code and documentation",
|
||||
agent=self.developer,
|
||||
context=[requirements_task, architecture_task]
|
||||
)
|
||||
|
||||
# Phase 5: Quality Assurance with Context-Generated Criteria
|
||||
print("\n🔍 Phase 5: Quality Assurance with Context")
|
||||
print("-" * 50)
|
||||
|
||||
qa_task = Task(
|
||||
name="quality_validation",
|
||||
description=f"""
|
||||
Validate the implementation using context-generated criteria:
|
||||
|
||||
Validation Framework:
|
||||
{validation_framework}
|
||||
|
||||
Verify that implementation:
|
||||
- Meets all success criteria defined in validation framework
|
||||
- Follows codebase patterns and conventions
|
||||
- Integrates properly with existing systems
|
||||
- Handles edge cases and error scenarios
|
||||
- Meets performance and security requirements
|
||||
|
||||
Use the validation steps provided to systematically check each criterion.
|
||||
""",
|
||||
expected_output="Comprehensive quality validation report with pass/fail status",
|
||||
agent=self.qa_engineer,
|
||||
context=[requirements_task, architecture_task, implementation_task]
|
||||
)
|
||||
|
||||
# Execute the complete workflow
|
||||
print("\n⚙️ Executing Context Engineering Workflow")
|
||||
print("-" * 50)
|
||||
|
||||
agents_workflow = PraisonAIAgents(
|
||||
agents=[
|
||||
self.product_manager,
|
||||
self.architect,
|
||||
self.developer,
|
||||
self.qa_engineer
|
||||
],
|
||||
tasks=[
|
||||
requirements_task,
|
||||
architecture_task,
|
||||
implementation_task,
|
||||
qa_task
|
||||
],
|
||||
process="sequential",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute workflow
|
||||
workflow_results = agents_workflow.start()
|
||||
|
||||
# Compile complete results
|
||||
complete_results = {
|
||||
"feature_request": feature_request,
|
||||
"context_engineering": self.context_data,
|
||||
"workflow_results": workflow_results,
|
||||
"methodology": "Context Engineering - 10x better than prompt engineering"
|
||||
}
|
||||
|
||||
return complete_results
|
||||
|
||||
def demonstrate_context_engineering_benefits():
|
||||
"""
|
||||
Demonstrate the benefits of Context Engineering methodology.
|
||||
"""
|
||||
print("\n📊 Context Engineering vs Traditional Approaches")
|
||||
print("=" * 60)
|
||||
|
||||
# Traditional approach simulation
|
||||
print("\n❌ Traditional AI Coding Approach:")
|
||||
print(" 1. Write basic prompt: 'Implement user authentication'")
|
||||
print(" 2. AI uses general knowledge to implement")
|
||||
print(" 3. Result often doesn't fit existing codebase patterns")
|
||||
print(" 4. Multiple iterations needed to fix integration issues")
|
||||
print(" 5. Success rate: ~30-40% first-try success")
|
||||
|
||||
# Context Engineering approach
|
||||
print("\n✅ Context Engineering Approach:")
|
||||
print(" 1. Analyze codebase patterns and architecture")
|
||||
print(" 2. Generate comprehensive context document")
|
||||
print(" 3. Create validation framework with success criteria")
|
||||
print(" 4. Enhance prompts with rich contextual information")
|
||||
print(" 5. Generate PRP (Product Requirements Prompt)")
|
||||
print(" 6. AI implements using complete context")
|
||||
print(" 7. Success rate: ~90%+ first-try success")
|
||||
|
||||
print("\n📈 Context Engineering Advantages:")
|
||||
print(" • 10x better than prompt engineering (context vs clever wording)")
|
||||
print(" • 100x better than vibe coding (structured vs ad-hoc)")
|
||||
print(" • Enables first-try implementation success")
|
||||
print(" • Reduces development iteration cycles")
|
||||
print(" • Ensures consistency with existing codebase")
|
||||
print(" • Provides built-in quality validation")
|
||||
|
||||
def run_example_workflow():
|
||||
"""Run an example Context Engineering workflow."""
|
||||
|
||||
# Setup workflow with current project
|
||||
project_path = str(project_root)
|
||||
workflow = ContextEngineeringWorkflow(
|
||||
project_path=project_path,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Example feature request
|
||||
feature_request = """
|
||||
Implement a real-time notification system that:
|
||||
- Sends notifications via WebSocket connections
|
||||
- Supports different notification types (info, warning, error)
|
||||
- Persists notifications in database for offline users
|
||||
- Includes user preference management for notification settings
|
||||
- Provides REST API for notification management
|
||||
"""
|
||||
|
||||
print("🎯 Running Example Context Engineering Workflow")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
# Execute the complete workflow
|
||||
results = workflow.run_context_engineering_workflow(feature_request)
|
||||
|
||||
print("\n🎉 Context Engineering Workflow Completed Successfully!")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n📊 Workflow Summary:")
|
||||
print(f" • Feature: Real-time notification system")
|
||||
print(f" • Context generated: ✅ Complete")
|
||||
print(f" • Architecture designed: ✅ With context")
|
||||
print(f" • Implementation: ✅ Context-guided")
|
||||
print(f" • Quality validation: ✅ Context-criteria")
|
||||
|
||||
print(f"\n🔧 Context Engineering Data:")
|
||||
context_data = results["context_engineering"]
|
||||
print(f" • Codebase analysis: {len(str(context_data['codebase_analysis']))} chars")
|
||||
print(f" • Context document: {len(context_data['context_document'])} chars")
|
||||
print(f" • Validation framework: {len(context_data['validation_framework']['validation_steps'])} steps")
|
||||
print(f" • Implementation blueprint: {len(context_data['implementation_blueprint']['implementation_steps'])} steps")
|
||||
print(f" • PRP: {len(context_data['prp'])} chars")
|
||||
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error in workflow execution: {e}")
|
||||
print(" Note: This is a demonstration - actual execution requires proper environment setup")
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🚀 Advanced Context Engineering Workflow Example")
|
||||
print("================================================")
|
||||
|
||||
print("\n💡 This example demonstrates the complete Context Engineering methodology:")
|
||||
print(" • Comprehensive codebase analysis")
|
||||
print(" • Context document generation")
|
||||
print(" • Validation framework creation")
|
||||
print(" • Multi-agent workflow with context integration")
|
||||
print(" • Quality assurance with context-generated criteria")
|
||||
|
||||
# Show benefits comparison
|
||||
demonstrate_context_engineering_benefits()
|
||||
|
||||
# Run example workflow (demo mode)
|
||||
print("\n🎭 Demo Mode: Context Engineering Workflow Structure")
|
||||
print("(Note: Full execution requires complete environment setup)")
|
||||
|
||||
project_path = str(project_root)
|
||||
workflow = ContextEngineeringWorkflow(project_path, "gpt-4o-mini")
|
||||
|
||||
print(f"\n✅ Workflow Setup Complete:")
|
||||
print(f" • Project path: {project_path}")
|
||||
print(f" • Agents configured: 5 (PM, Context Engineer, Architect, Developer, QA)")
|
||||
print(f" • Context Engineering agent: ✅ Ready")
|
||||
print(f" • Multi-agent workflow: ✅ Configured")
|
||||
|
||||
print(f"\n🎯 Ready to execute Context Engineering workflow!")
|
||||
print(f" Use workflow.run_context_engineering_workflow(feature_request)")
|
||||
print(f" to execute the complete methodology.")
|
||||
|
||||
print(f"\n📚 Key Context Engineering Concepts Demonstrated:")
|
||||
print(f" • Pattern extraction from existing codebase")
|
||||
print(f" • Comprehensive context document generation")
|
||||
print(f" • Validation loop creation with success criteria")
|
||||
print(f" • PRP (Product Requirements Prompt) generation")
|
||||
print(f" • Context-enhanced prompt engineering")
|
||||
print(f" • Multi-agent workflow integration")
|
||||
|
||||
print(f"\n🎉 Context Engineering: 10x better than prompt engineering, 100x better than vibe coding!")
|
||||
@@ -130,9 +130,11 @@ def example_mcp_tracking():
|
||||
)
|
||||
plan = agent.chat("Plan: Search for Python documentation")
|
||||
|
||||
# Track tool usage
|
||||
# Track tool usage (this will now have actual timing)
|
||||
with tracker.track("tool_usage", request_id):
|
||||
# Simulate tool execution
|
||||
# Simulate tool execution with realistic timing
|
||||
import time
|
||||
time.sleep(0.1) # Simulate some work
|
||||
tool_result = "Found 5 relevant documentation pages"
|
||||
|
||||
# Track LLM generation
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a creative designer AI agent. "
|
||||
"Help users with graphic design concepts, branding strategies, "
|
||||
"visual communication, and creative problem-solving. Provide guidance "
|
||||
"on design principles, color theory, typography, and user experience.",
|
||||
llm="anthropic/claude-3-5-sonnet-20241022"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your creative designer assistant. "
|
||||
"How can I help you with your design projects today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an educational tutor AI agent. "
|
||||
"Help students learn various subjects, explain complex concepts, "
|
||||
"provide step-by-step solutions, and create personalized learning "
|
||||
"materials for different educational levels.",
|
||||
llm="anthropic/claude-3-5-sonnet-20241022"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your educational tutor assistant. "
|
||||
"How can I help you learn today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a financial advisor AI agent. "
|
||||
"Help users understand financial concepts, analyze investment opportunities, "
|
||||
"and provide guidance on personal finance, budgeting, and financial planning. "
|
||||
"Note: This is for educational purposes only, not financial advice.",
|
||||
llm="anthropic/claude-3-5-sonnet-20241022"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your financial advisor assistant. "
|
||||
"How can I help you understand financial concepts today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a legal advisor AI agent. "
|
||||
"Help users understand legal concepts, review contracts, "
|
||||
"analyze legal documents, and provide guidance on compliance "
|
||||
"and regulatory requirements. Note: This is for educational purposes only.",
|
||||
llm="anthropic/claude-3-5-sonnet-20241022"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your legal advisor assistant. "
|
||||
"How can I help you understand legal concepts today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a medical research AI agent. "
|
||||
"Help users understand medical concepts, analyze research papers, "
|
||||
"and provide insights on healthcare trends and developments. "
|
||||
"Note: This is for educational purposes only, not medical advice.",
|
||||
llm="anthropic/claude-3-5-sonnet-20241022"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your medical research assistant. "
|
||||
"How can I help you understand medical concepts today?")
|
||||
@@ -1,18 +1,12 @@
|
||||
import asyncio
|
||||
from praisonaiagents import Agent
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant",
|
||||
llm="gemini/gemini-1.5-flash-8b",
|
||||
self_reflect=True,
|
||||
verbose=True
|
||||
)
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant",
|
||||
llm="gemini/gemini-1.5-flash-8b",
|
||||
self_reflect=True,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Use achat instead of start/chat for async operation
|
||||
response = await agent.achat("Why sky is Blue in 1000 words?")
|
||||
print(response)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the async main function
|
||||
asyncio.run(main())
|
||||
# Use achat instead of start/chat for async operation
|
||||
response = asyncio.run(agent.achat("Why sky is Blue in 1000 words?"))
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a cybersecurity AI agent. "
|
||||
"Help users understand security concepts, analyze security threats, "
|
||||
"and provide guidance on security best practices, risk assessment, "
|
||||
"and security architecture design.",
|
||||
llm="openai/gpt-4o"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your cybersecurity assistant. "
|
||||
"How can I help you with security concepts today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a data engineer AI agent. "
|
||||
"Help users design data pipelines, optimize database performance, "
|
||||
"create ETL processes, and provide guidance on data architecture, "
|
||||
"data modeling, and big data technologies.",
|
||||
llm="openai/gpt-4o"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your data engineer assistant. "
|
||||
"How can I help you with your data engineering projects today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a marketing strategist AI agent. "
|
||||
"Help users develop marketing strategies, analyze market trends, "
|
||||
"create campaign plans, and provide guidance on digital marketing, "
|
||||
"brand positioning, and customer acquisition strategies.",
|
||||
llm="openai/gpt-4o"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your marketing strategist assistant. "
|
||||
"How can I help you with your marketing strategy today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a product manager AI agent. "
|
||||
"Help users with product strategy, feature prioritization, "
|
||||
"user research analysis, and product roadmap planning. Provide "
|
||||
"guidance on market analysis, competitive positioning, and user experience.",
|
||||
llm="openai/gpt-4o"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your product manager assistant. "
|
||||
"How can I help you with your product strategy today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a software architect AI agent. "
|
||||
"Help users design software systems, create architecture diagrams, "
|
||||
"plan system integrations, and provide guidance on scalability, "
|
||||
"security, and best practices for software development.",
|
||||
llm="openai/gpt-4o"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your software architect assistant. "
|
||||
"How can I help you design your software system today?")
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Basic example of using Grok model in PraisonAI
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Initialize Agent with Grok model
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant",
|
||||
llm="xai/grok-beta",
|
||||
)
|
||||
|
||||
# Example conversation
|
||||
response = agent.start("Hello! Can you explain quantum computing in simple terms?")
|
||||
|
||||
# Example with mathematical reasoning
|
||||
math_task = """
|
||||
Solve this problem step by step:
|
||||
If a train travels at 60 mph for 2.5 hours, how far does it travel?
|
||||
"""
|
||||
|
||||
response = agent.start(math_task)
|
||||
|
||||
# Example with creative writing
|
||||
creative_task = """
|
||||
Write a short story about a robot learning to paint.
|
||||
Make it engaging and about 100 words.
|
||||
"""
|
||||
|
||||
response = agent.start(creative_task)
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a crisis management and communication AI agent. "
|
||||
"Help users develop crisis communication strategies, "
|
||||
"manage reputation during difficult situations, and create effective messaging. "
|
||||
"Provide guidance on crisis response planning, stakeholder communication, "
|
||||
"media relations during crises, and reputation recovery strategies.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your crisis management and communication assistant. "
|
||||
"How can I help you develop effective crisis communication "
|
||||
"strategies and manage difficult situations today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a digital marketing and SEO optimization AI agent. "
|
||||
"Help users develop digital marketing strategies, optimize for search engines, "
|
||||
"and improve online visibility. Provide guidance on SEO best practices, "
|
||||
"content marketing, social media strategies, PPC campaigns, "
|
||||
"and analytics interpretation for better ROI.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your digital marketing and SEO optimization assistant. "
|
||||
"How can I help you improve your online presence, "
|
||||
"optimize for search engines, or develop marketing strategies today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an environmental impact assessment AI agent. "
|
||||
"Help users evaluate environmental impacts of projects, "
|
||||
"develop sustainability strategies, and ensure compliance with environmental regulations. "
|
||||
"Provide guidance on environmental risk assessment, sustainability reporting, "
|
||||
"carbon footprint analysis, and green technology implementation.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your environmental impact assessment assistant. "
|
||||
"How can I help you evaluate environmental impacts, "
|
||||
"develop sustainability strategies, or ensure compliance today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an event planning and management AI agent. "
|
||||
"Help users plan, organize, and manage successful events of all types. "
|
||||
"Provide guidance on venue selection, budgeting, vendor coordination, "
|
||||
"timeline management, marketing strategies, and post-event analysis. "
|
||||
"Assist with both virtual and in-person event planning.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your event planning and management assistant. "
|
||||
"How can I help you plan and organize your next successful "
|
||||
"event today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a healthcare and medical research AI agent. "
|
||||
"Help users with medical research, healthcare insights, "
|
||||
"and understanding complex medical information. Provide guidance on medical literature review, "
|
||||
"healthcare trends analysis, patient care strategies, "
|
||||
"and medical technology advancements for better healthcare outcomes.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your healthcare and medical research assistant. "
|
||||
"How can I help you with medical research, healthcare insights, "
|
||||
"or understanding complex medical information today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a language learning and translation AI agent. "
|
||||
"Help users learn new languages, translate content accurately, "
|
||||
"and understand cultural nuances. Provide guidance on language learning strategies, "
|
||||
"grammar explanations, vocabulary building, pronunciation, "
|
||||
"and cultural context for effective communication.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your language learning and translation assistant. "
|
||||
"How can I help you learn new languages, translate content, "
|
||||
"or understand cultural nuances today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a patent research and innovation AI agent. "
|
||||
"Help users conduct patent research, analyze intellectual property, "
|
||||
"identify innovation opportunities, and navigate patent filing processes. "
|
||||
"Provide guidance on patent searches, prior art analysis, "
|
||||
"innovation strategy, IP protection, and technology landscape assessment.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your patent research and innovation assistant. "
|
||||
"How can I help you with patent research, innovation strategy, "
|
||||
"and intellectual property protection today?")
|
||||
@@ -0,0 +1,19 @@
|
||||
"""
|
||||
Personal Branding & PR Agent using Groq
|
||||
Specialized agent for managing personal brand and public relations
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a personal branding and PR AI agent. "
|
||||
"Help users build their personal brand, manage public relations, "
|
||||
"create compelling content strategies, and enhance their online presence. "
|
||||
"Provide guidance on social media management, content creation, "
|
||||
"media relations, reputation management, and brand positioning.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your personal branding and PR assistant. "
|
||||
"How can I help you build your personal brand and "
|
||||
"manage your public relations today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a product development and innovation AI agent. "
|
||||
"Help users develop new products, improve existing ones, "
|
||||
"and drive innovation in their business. Provide guidance on product strategy, "
|
||||
"market research, user experience design, prototyping, "
|
||||
"and innovation management for successful product launches.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your product development and innovation assistant. "
|
||||
"How can I help you develop new products, improve existing ones, "
|
||||
"or drive innovation in your business today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a supply chain optimization AI agent. "
|
||||
"Help users optimize their supply chain operations, "
|
||||
"improve logistics efficiency, reduce costs, and enhance inventory management. "
|
||||
"Provide guidance on demand forecasting, supplier management, "
|
||||
"warehouse optimization, transportation planning, and risk mitigation.",
|
||||
llm="groq/llama3.1-8b-instant"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your supply chain optimization assistant. "
|
||||
"How can I help you optimize your supply chain operations "
|
||||
"and improve your logistics efficiency today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an aerospace engineering AI agent. "
|
||||
"Help users understand aerospace engineering concepts, "
|
||||
"aircraft design, and space technology. Provide guidance on "
|
||||
"aerodynamics, propulsion systems, materials science, "
|
||||
"and satellite technology.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your aerospace engineering assistant. "
|
||||
"How can I help you with aerospace engineering "
|
||||
"and space technology today?")
|
||||
File renamed without changes.
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a bioinformatics and genomics AI agent. "
|
||||
"Help users understand bioinformatics, genomics, "
|
||||
"and computational biology. Provide guidance on "
|
||||
"DNA sequence analysis, protein structure prediction, "
|
||||
"genetic algorithms, and biological data mining.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your bioinformatics and genomics assistant. "
|
||||
"How can I help you with bioinformatics "
|
||||
"and genomics research today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a blockchain and DeFi AI agent. "
|
||||
"Help users understand blockchain technology, "
|
||||
"decentralized finance, and cryptocurrency. Provide guidance on "
|
||||
"smart contract development, DeFi protocols, "
|
||||
"tokenomics, and blockchain security.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your blockchain and DeFi assistant. "
|
||||
"How can I help you explore blockchain technology "
|
||||
"and decentralized finance today?")
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an energy and renewables AI agent. "
|
||||
"Help users understand renewable energy technologies, "
|
||||
"energy systems, and sustainability. Provide guidance on "
|
||||
"solar power, wind energy, energy storage, "
|
||||
"and green technology solutions.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your energy and renewables assistant. "
|
||||
"How can I help you with renewable energy "
|
||||
"and sustainability today?")
|
||||
File renamed without changes.
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are an IoT and smart cities AI agent. "
|
||||
"Help users understand Internet of Things, smart city "
|
||||
"technologies, and connected systems. Provide guidance on "
|
||||
"IoT device integration, smart infrastructure, "
|
||||
"sensor networks, and urban technology solutions.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your IoT and smart cities assistant. "
|
||||
"How can I help you with IoT and smart city "
|
||||
"technologies today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a metaverse and virtual reality AI agent. "
|
||||
"Help users understand metaverse concepts, VR development, "
|
||||
"and immersive technologies. Provide guidance on "
|
||||
"3D modeling, VR programming, digital twins, "
|
||||
"and virtual world design.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your metaverse and VR assistant. "
|
||||
"How can I help you explore the metaverse "
|
||||
"and virtual reality today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a nanotechnology and materials AI agent. "
|
||||
"Help users understand nanotechnology, advanced materials, "
|
||||
"and material science. Provide guidance on "
|
||||
"nanomaterials, material properties, fabrication techniques, "
|
||||
"and applications in various industries.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your nanotechnology and materials assistant. "
|
||||
"How can I help you with nanotechnology "
|
||||
"and materials science today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a quantum AI agent. "
|
||||
"Help users understand quantum artificial intelligence, "
|
||||
"quantum machine learning, and quantum neural networks. "
|
||||
"Provide guidance on quantum algorithms for AI, "
|
||||
"quantum optimization, and hybrid quantum-classical systems.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your quantum AI assistant. "
|
||||
"How can I help you explore quantum artificial "
|
||||
"intelligence today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a quantum computing AI agent. "
|
||||
"Help users understand quantum computing concepts, "
|
||||
"quantum algorithms, and quantum programming. Provide guidance on "
|
||||
"quantum circuit design, quantum error correction, "
|
||||
"quantum machine learning, and quantum cryptography.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your quantum computing assistant. "
|
||||
"How can I help you explore quantum computing "
|
||||
"and quantum programming today?")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a robotics and automation AI agent. "
|
||||
"Help users understand robotics, automation systems, "
|
||||
"and industrial applications. Provide guidance on "
|
||||
"robot programming, automation workflows, "
|
||||
"sensor integration, and robotic process automation.",
|
||||
llm="openrouter/moonshotai/kimi-k2"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your robotics and automation assistant. "
|
||||
"How can I help you with robotics and "
|
||||
"automation systems today?")
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a business analyst AI agent. "
|
||||
"Help users analyze business performance, market trends, "
|
||||
"competitive landscape, and strategic opportunities. Provide insights "
|
||||
"on business metrics, financial analysis, and growth strategies.",
|
||||
llm="meta-llama/Llama-3.1-8B-Instruct"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your business analyst assistant. "
|
||||
"How can I help you analyze your business data and strategies today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a code review AI agent. "
|
||||
"Help developers review code for best practices, security vulnerabilities, "
|
||||
"performance issues, and maintainability. Provide constructive feedback "
|
||||
"and suggest improvements for code quality and efficiency.",
|
||||
llm="meta-llama/Llama-3.1-8B-Instruct"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your code review assistant. "
|
||||
"How can I help you improve your code quality today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a content writing AI agent. "
|
||||
"Help users create engaging blog posts, articles, marketing copy, "
|
||||
"and social media content. Provide guidance on SEO optimization, "
|
||||
"content structure, tone adjustment, and audience targeting.",
|
||||
llm="meta-llama/Llama-3.1-8B-Instruct"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your content writing assistant. "
|
||||
"How can I help you create compelling content today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a data science AI agent. "
|
||||
"Help users with data analysis, machine learning model development, "
|
||||
"statistical analysis, and data visualization. Provide guidance on "
|
||||
"data preprocessing, feature engineering, model selection, and evaluation.",
|
||||
llm="meta-llama/Llama-3.1-8B-Instruct"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your data science assistant. "
|
||||
"How can I help you with your data analysis and machine learning projects today?")
|
||||
@@ -0,0 +1,12 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a research assistant AI agent. "
|
||||
"Help users conduct comprehensive research on various topics, "
|
||||
"analyze academic papers, synthesize information, and provide "
|
||||
"evidence-based insights and recommendations.",
|
||||
llm="meta-llama/Llama-3.1-8B-Instruct"
|
||||
)
|
||||
|
||||
response = agent.start("Hello! I'm your research assistant. "
|
||||
"How can I help you with your research project today?")
|
||||
@@ -0,0 +1,315 @@
|
||||
# MongoDB Integration Examples for PraisonAI Agents
|
||||
|
||||
This directory contains comprehensive examples demonstrating how to integrate MongoDB with PraisonAI agents for memory, knowledge, and data operations.
|
||||
|
||||
## Features
|
||||
|
||||
MongoDB integration with PraisonAI provides:
|
||||
|
||||
- **Memory Provider**: Use MongoDB as a persistent memory store for agents
|
||||
- **Knowledge Store**: Store and retrieve documents with vector search capabilities
|
||||
- **Tools Integration**: Perform MongoDB operations directly from agents
|
||||
- **Vector Search**: Leverage MongoDB Atlas Vector Search for semantic similarity
|
||||
- **Scalability**: Handle large datasets with MongoDB's scalable architecture
|
||||
- **Flexibility**: Use MongoDB as both key-value store and vector database
|
||||
|
||||
## Prerequisites
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
# Install PraisonAI with MongoDB support
|
||||
pip install 'praisonaiagents[mongodb]'
|
||||
```
|
||||
|
||||
### Dependencies
|
||||
|
||||
- **MongoDB**: Local MongoDB instance or MongoDB Atlas
|
||||
- **OpenAI API Key**: For embeddings and LLM operations
|
||||
- **Python 3.10+**: Required for PraisonAI
|
||||
|
||||
### Environment Setup
|
||||
|
||||
```bash
|
||||
# Set your OpenAI API key
|
||||
export OPENAI_API_KEY="your-openai-api-key"
|
||||
|
||||
# Optional: Set MongoDB connection string
|
||||
export MONGODB_CONNECTION_STRING="mongodb://localhost:27017/"
|
||||
```
|
||||
|
||||
## Examples Overview
|
||||
|
||||
### 1. [mongodb_memory_example.py](mongodb_memory_example.py)
|
||||
|
||||
Demonstrates using MongoDB as a memory provider for PraisonAI agents.
|
||||
|
||||
**Features:**
|
||||
- MongoDB as persistent memory storage
|
||||
- Quality scoring and filtering
|
||||
- Vector search for memory retrieval
|
||||
- Multi-session memory persistence
|
||||
- Memory search and context building
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
python mongodb_memory_example.py
|
||||
```
|
||||
|
||||
### 2. [mongodb_knowledge_example.py](mongodb_knowledge_example.py)
|
||||
|
||||
Shows how to use MongoDB as a knowledge store with vector search capabilities.
|
||||
|
||||
**Features:**
|
||||
- MongoDB as knowledge vector store
|
||||
- Document processing and storage
|
||||
- Vector search for knowledge retrieval
|
||||
- Knowledge-based agent interactions
|
||||
- File processing with MongoDB storage
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
python mongodb_knowledge_example.py
|
||||
```
|
||||
|
||||
### 3. [mongodb_tools_example.py](mongodb_tools_example.py)
|
||||
|
||||
Demonstrates MongoDB tools integration for database operations.
|
||||
|
||||
**Features:**
|
||||
- MongoDB CRUD operations
|
||||
- Vector search with embeddings
|
||||
- Data analysis and aggregation
|
||||
- Collection management
|
||||
- Index creation and optimization
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
python mongodb_tools_example.py
|
||||
```
|
||||
|
||||
### 4. [mongodb_comprehensive_example.py](mongodb_comprehensive_example.py)
|
||||
|
||||
Complete business scenario simulation using all MongoDB features.
|
||||
|
||||
**Features:**
|
||||
- Full MongoDB integration (memory + knowledge + tools)
|
||||
- Multi-agent business workflow
|
||||
- Real-world e-commerce simulation
|
||||
- Business intelligence analytics
|
||||
- Customer service automation
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
python mongodb_comprehensive_example.py
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### MongoDB Memory Configuration
|
||||
|
||||
```python
|
||||
mongodb_memory_config = {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/",
|
||||
"database": "praisonai_memory",
|
||||
"use_vector_search": True, # Enable Atlas Vector Search
|
||||
"max_pool_size": 50,
|
||||
"min_pool_size": 10,
|
||||
"server_selection_timeout": 5000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### MongoDB Knowledge Configuration
|
||||
|
||||
```python
|
||||
mongodb_knowledge_config = {
|
||||
"vector_store": {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/",
|
||||
"database": "praisonai_knowledge",
|
||||
"collection": "knowledge_base",
|
||||
"use_vector_search": True
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-small"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## MongoDB Atlas Vector Search
|
||||
|
||||
For production use with advanced vector search capabilities:
|
||||
|
||||
### Setup
|
||||
|
||||
1. **Create MongoDB Atlas Cluster**
|
||||
- Sign up for MongoDB Atlas
|
||||
- Create a new cluster
|
||||
- Get your connection string
|
||||
|
||||
2. **Enable Vector Search**
|
||||
- Create vector search indexes
|
||||
- Configure embedding dimensions (1536 for OpenAI)
|
||||
- Set similarity metric (cosine, euclidean, dotProduct)
|
||||
|
||||
3. **Update Connection String**
|
||||
```python
|
||||
"connection_string": "mongodb+srv://username:[email protected]/"
|
||||
```
|
||||
|
||||
### Vector Search Index Creation
|
||||
|
||||
```python
|
||||
# Vector search index definition
|
||||
{
|
||||
"mappings": {
|
||||
"dynamic": True,
|
||||
"fields": {
|
||||
"embedding": {
|
||||
"type": "knnVector",
|
||||
"dimensions": 1536,
|
||||
"similarity": "cosine"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Connection Management
|
||||
|
||||
```python
|
||||
# Use connection pooling
|
||||
mongodb_config = {
|
||||
"connection_string": "mongodb://localhost:27017/",
|
||||
"max_pool_size": 50,
|
||||
"min_pool_size": 10,
|
||||
"maxIdleTimeMS": 30000,
|
||||
"serverSelectionTimeoutMS": 5000
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Indexing Strategy
|
||||
|
||||
```python
|
||||
# Create appropriate indexes
|
||||
collection.create_index([("content", "text")]) # Text search
|
||||
collection.create_index([("created_at", -1)]) # Time-based queries
|
||||
collection.create_index([("metadata.category", 1)]) # Category filtering
|
||||
```
|
||||
|
||||
### 3. Error Handling
|
||||
|
||||
```python
|
||||
try:
|
||||
# MongoDB operations
|
||||
result = collection.insert_one(document)
|
||||
except PyMongoError as e:
|
||||
logger.error(f"MongoDB error: {e}")
|
||||
# Implement fallback strategy
|
||||
```
|
||||
|
||||
### 4. Data Validation
|
||||
|
||||
```python
|
||||
# Validate data before storage
|
||||
def validate_document(doc):
|
||||
required_fields = ["content", "metadata", "created_at"]
|
||||
return all(field in doc for field in required_fields)
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### 1. Indexing
|
||||
|
||||
- Create indexes on frequently queried fields
|
||||
- Use compound indexes for complex queries
|
||||
- Monitor index usage with MongoDB profiler
|
||||
|
||||
### 2. Vector Search Optimization
|
||||
|
||||
- Use appropriate vector dimensions
|
||||
- Optimize numCandidates parameter
|
||||
- Consider filtering to reduce search space
|
||||
|
||||
### 3. Connection Pooling
|
||||
|
||||
- Configure appropriate pool sizes
|
||||
- Use connection pooling for high-concurrency scenarios
|
||||
- Monitor connection metrics
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
1. **Connection Errors**
|
||||
- Check MongoDB server is running
|
||||
- Verify connection string format
|
||||
- Ensure network connectivity
|
||||
|
||||
2. **Vector Search Issues**
|
||||
- Verify Atlas Vector Search indexes exist
|
||||
- Check embedding dimensions match
|
||||
- Ensure proper index configuration
|
||||
|
||||
3. **Memory Issues**
|
||||
- Monitor memory usage with large datasets
|
||||
- Use appropriate batch sizes
|
||||
- Consider data pagination
|
||||
|
||||
### Debugging
|
||||
|
||||
```python
|
||||
# Enable detailed logging
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
# Monitor MongoDB operations
|
||||
from pymongo import monitoring
|
||||
monitoring.register(CommandLogger())
|
||||
```
|
||||
|
||||
## Production Deployment
|
||||
|
||||
### 1. Security
|
||||
|
||||
```python
|
||||
# Use authentication
|
||||
connection_string = "mongodb://username:password@host:port/database"
|
||||
|
||||
# Enable SSL/TLS
|
||||
client = MongoClient(connection_string, tls=True)
|
||||
```
|
||||
|
||||
### 2. Monitoring
|
||||
|
||||
- Use MongoDB Atlas monitoring
|
||||
- Implement application-level metrics
|
||||
- Set up alerting for critical issues
|
||||
|
||||
### 3. Backup and Recovery
|
||||
|
||||
- Configure automated backups
|
||||
- Test restore procedures
|
||||
- Implement disaster recovery plans
|
||||
|
||||
## Support
|
||||
|
||||
For additional support:
|
||||
|
||||
- [PraisonAI Documentation](https://docs.praisonai.com)
|
||||
- [MongoDB Documentation](https://docs.mongodb.com)
|
||||
- [MongoDB Atlas Vector Search](https://docs.atlas.mongodb.com/atlas-vector-search/)
|
||||
|
||||
## License
|
||||
|
||||
These examples are provided under the same license as PraisonAI.
|
||||
@@ -0,0 +1,20 @@
|
||||
# PraisonAI Documentation
|
||||
|
||||
## Docs
|
||||
|
||||
- [AI Agents](https://docs.praison.ai/docs/agents/agents.md): Overview of all available PraisonAI agents and their capabilities
|
||||
- [Data Analyst Agent](https://docs.praison.ai/docs/agents/data-analyst.md): Learn how to create AI agents for data analysis and insights generation.
|
||||
- [Finance Agent](https://docs.praison.ai/docs/agents/finance.md): Learn how to create AI agents for financial analysis and investment recommendations.
|
||||
- [Image Analysis Agent](https://docs.praison.ai/docs/agents/image.md): Learn how to create AI agents for image analysis and visual content understanding.
|
||||
- [Image to Text Agent](https://docs.praison.ai/docs/agents/image-to-text.md): Learn how to create AI agents for converting images to textual descriptions and extracting text from images.
|
||||
- [Markdown Agent](https://docs.praison.ai/docs/agents/markdown.md): Learn how to create AI agents for generating and formatting content in Markdown.
|
||||
- [Planning Agent](https://docs.praison.ai/docs/agents/planning.md): Learn how to create AI agents for trip planning and itinerary generation.
|
||||
- [Programming Agent](https://docs.praison.ai/docs/agents/programming.md): Learn how to create AI agents for code development, analysis, and execution.
|
||||
- [Recommendation Agent](https://docs.praison.ai/docs/agents/recommendation.md): Learn how to create AI agents for personalized recommendations across various domains.
|
||||
- [Research Agent](https://docs.praison.ai/docs/agents/research.md): Learn how to create AI agents for conducting comprehensive research and analysis.
|
||||
- [Shopping Agent](https://docs.praison.ai/docs/agents/shopping.md): Learn how to create AI agents for price comparison and shopping assistance.
|
||||
- [Single Agent](https://docs.praison.ai/docs/agents/single.md): Learn how to create a basic single-purpose AI agent for simple tasks.
|
||||
- [Video Agent](https://docs.praison.ai/docs/agents/video.md): Learn how to create AI agents for video analysis and content understanding.
|
||||
- [Web Search Agent](https://docs.praison.ai/docs/agents/websearch.md): Learn how to create AI agents for intelligent web searching and information gathering.
|
||||
- [Wikipedia Agent](https://docs.praison.ai/docs/agents/wikipedia.md): Learn how to create AI agents for searching and extracting information from Wikipedia.
|
||||
- [Agent Module](https://docs.praison.ai/docs/api/praisonaiagents/agent/agent.md): Documentation for the praisonaiagents.agent.agent module
|
||||
@@ -0,0 +1,297 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Comprehensive MongoDB Integration Example for PraisonAI Agents
|
||||
|
||||
This example demonstrates a complete MongoDB integration with PraisonAI agents,
|
||||
combining memory, knowledge, and tools for a comprehensive data management system.
|
||||
|
||||
Prerequisites:
|
||||
- Install MongoDB dependencies: pip install 'praisonaiagents[mongodb]'
|
||||
- MongoDB server running (local or Atlas)
|
||||
- OpenAI API key for embeddings
|
||||
|
||||
Features demonstrated:
|
||||
- MongoDB as memory provider
|
||||
- MongoDB as knowledge store
|
||||
- MongoDB tools for data operations
|
||||
- Vector search capabilities
|
||||
- Multi-agent coordination with MongoDB
|
||||
- Real-world business scenario simulation
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.tools import mongodb_tools
|
||||
|
||||
# Ensure OpenAI API key is set
|
||||
if not os.environ.get("OPENAI_API_KEY"):
|
||||
raise ValueError("Please set the OPENAI_API_KEY environment variable")
|
||||
|
||||
def main():
|
||||
# MongoDB configuration for different components
|
||||
mongodb_memory_config = {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/", # Replace with your connection string
|
||||
"database": "praisonai_comprehensive",
|
||||
"use_vector_search": True,
|
||||
"max_pool_size": 50
|
||||
}
|
||||
}
|
||||
|
||||
mongodb_knowledge_config = {
|
||||
"vector_store": {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/",
|
||||
"database": "praisonai_comprehensive",
|
||||
"collection": "knowledge_base",
|
||||
"use_vector_search": True
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-small",
|
||||
"api_key": os.getenv("OPENAI_API_KEY")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Create specialized agents for different business functions
|
||||
|
||||
# 1. Data Manager Agent - handles data operations
|
||||
data_manager = Agent(
|
||||
name="Data Manager",
|
||||
role="Senior Data Operations Manager",
|
||||
goal="Manage and organize all data operations using MongoDB",
|
||||
backstory="""You are an expert data operations manager who specializes in
|
||||
MongoDB database management. You handle data ingestion, organization,
|
||||
indexing, and ensure data quality and accessibility across the organization.""",
|
||||
tools=[mongodb_tools],
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# 2. Knowledge Curator Agent - manages knowledge base
|
||||
knowledge_curator = Agent(
|
||||
name="Knowledge Curator",
|
||||
role="Knowledge Management Specialist",
|
||||
goal="Curate and maintain organizational knowledge using MongoDB",
|
||||
backstory="""You are a knowledge management specialist who excels at
|
||||
organizing, categorizing, and retrieving information from the knowledge base.
|
||||
You ensure that knowledge is properly stored, indexed, and accessible.""",
|
||||
knowledge_config=mongodb_knowledge_config,
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# 3. Business Analyst Agent - analyzes data and provides insights
|
||||
business_analyst = Agent(
|
||||
name="Business Analyst",
|
||||
role="Senior Business Intelligence Analyst",
|
||||
goal="Analyze business data and provide actionable insights",
|
||||
backstory="""You are a senior business analyst who specializes in extracting
|
||||
insights from data stored in MongoDB. You create reports, identify trends,
|
||||
and provide strategic recommendations based on data analysis.""",
|
||||
tools=[mongodb_tools],
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# 4. Customer Service Agent - handles customer interactions
|
||||
customer_service = Agent(
|
||||
name="Customer Service Agent",
|
||||
role="Customer Service Representative",
|
||||
goal="Provide excellent customer service using stored knowledge and data",
|
||||
backstory="""You are a customer service representative who uses the
|
||||
knowledge base and customer data to provide excellent support. You can
|
||||
access customer history, product information, and troubleshooting guides.""",
|
||||
knowledge_config=mongodb_knowledge_config,
|
||||
tools=[mongodb_tools],
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create comprehensive business tasks
|
||||
business_tasks = [
|
||||
Task(
|
||||
description="""Set up comprehensive business data infrastructure:
|
||||
1. Create collections for customers, products, orders, and support tickets
|
||||
2. Insert sample business data for an e-commerce platform
|
||||
3. Set up appropriate indexes for performance optimization
|
||||
4. Create vector indexes for product descriptions and customer support
|
||||
5. Implement data validation and business rules
|
||||
|
||||
Focus on creating a realistic business data structure.
|
||||
""",
|
||||
expected_output="Complete business data infrastructure with sample data",
|
||||
agent=data_manager
|
||||
),
|
||||
Task(
|
||||
description="""Build organizational knowledge base:
|
||||
1. Store product documentation and specifications
|
||||
2. Create customer service guidelines and FAQs
|
||||
3. Add troubleshooting guides and best practices
|
||||
4. Include company policies and procedures
|
||||
5. Organize knowledge by categories and tags
|
||||
|
||||
Ensure knowledge is searchable and well-organized.
|
||||
""",
|
||||
expected_output="Comprehensive knowledge base with categorized information",
|
||||
agent=knowledge_curator
|
||||
),
|
||||
Task(
|
||||
description="""Perform business intelligence analysis:
|
||||
1. Analyze customer purchase patterns and behavior
|
||||
2. Calculate key business metrics (revenue, conversion rates, etc.)
|
||||
3. Identify top-selling products and categories
|
||||
4. Analyze customer support ticket trends
|
||||
5. Generate insights for business optimization
|
||||
|
||||
Use MongoDB aggregation and analytics capabilities.
|
||||
""",
|
||||
expected_output="Business intelligence report with actionable insights",
|
||||
agent=business_analyst
|
||||
),
|
||||
Task(
|
||||
description="""Handle customer service scenarios:
|
||||
1. Process customer inquiries using knowledge base
|
||||
2. Look up customer order history and preferences
|
||||
3. Provide product recommendations based on customer data
|
||||
4. Access troubleshooting guides for technical issues
|
||||
5. Update customer records and interaction history
|
||||
|
||||
Demonstrate excellent customer service with data-driven support.
|
||||
""",
|
||||
expected_output="Customer service interactions with knowledge-based responses",
|
||||
agent=customer_service
|
||||
),
|
||||
Task(
|
||||
description="""Generate comprehensive business report:
|
||||
1. Combine insights from all data sources
|
||||
2. Create executive summary with key findings
|
||||
3. Provide recommendations for business improvement
|
||||
4. Include data visualizations and metrics
|
||||
5. Store report in knowledge base for future reference
|
||||
|
||||
Create a comprehensive business intelligence report.
|
||||
""",
|
||||
expected_output="Executive business report with comprehensive insights",
|
||||
agent=business_analyst
|
||||
)
|
||||
]
|
||||
|
||||
# Initialize the comprehensive business system
|
||||
print("🚀 Starting Comprehensive MongoDB Business System...")
|
||||
print("=" * 60)
|
||||
|
||||
business_system = PraisonAIAgents(
|
||||
agents=[data_manager, knowledge_curator, business_analyst, customer_service],
|
||||
tasks=business_tasks,
|
||||
memory=True,
|
||||
memory_config=mongodb_memory_config,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute the comprehensive business pipeline
|
||||
try:
|
||||
results = business_system.start()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Comprehensive Business System Results:")
|
||||
print("=" * 60)
|
||||
|
||||
# Handle results structure properly
|
||||
if isinstance(results, (list, tuple)):
|
||||
for i, result in enumerate(results, 1):
|
||||
print(f"\n{i}. Task Result:")
|
||||
if hasattr(result, 'raw') and hasattr(result, 'agent'):
|
||||
print(f" Output: {result.raw[:200]}...")
|
||||
print(f" Agent: {result.agent}")
|
||||
else:
|
||||
print(f" Output: {str(result)[:200]}...")
|
||||
else:
|
||||
print(f"\n1. Task Result:")
|
||||
print(f" Output: {str(results)[:200]}...")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("💾 Comprehensive MongoDB Integration Complete!")
|
||||
print("=" * 60)
|
||||
|
||||
# Demonstrate system integration
|
||||
print("\n🔍 Demonstrating System Integration:")
|
||||
print("-" * 40)
|
||||
|
||||
# Access different components
|
||||
# Note: Direct memory access not available through business_system.memory
|
||||
print("📋 Memory integration enabled through memory_config...")
|
||||
print("Memory operations are handled internally by the agents")
|
||||
|
||||
# Access memory system directly
|
||||
from praisonaiagents.memory import Memory
|
||||
memory_system = Memory(config=mongodb_memory_config)
|
||||
|
||||
# Search business memories
|
||||
print("\n📋 Searching business memories...")
|
||||
business_memories = memory_system.search_long_term("business analysis", limit=3)
|
||||
print(f"Found {len(business_memories)} business-related memories")
|
||||
|
||||
# Access knowledge system
|
||||
from praisonaiagents.knowledge import Knowledge
|
||||
knowledge_system = Knowledge(config=mongodb_knowledge_config)
|
||||
|
||||
# Search knowledge base
|
||||
print("\n📚 Searching knowledge base...")
|
||||
knowledge_results = knowledge_system.search("customer service", limit=3)
|
||||
print(f"Found {len(knowledge_results)} knowledge entries")
|
||||
|
||||
# Direct MongoDB operations
|
||||
print("\n🔧 Direct MongoDB operations...")
|
||||
mongo_client = mongodb_tools.connect_mongodb(
|
||||
"mongodb://localhost:27017/",
|
||||
"praisonai_comprehensive"
|
||||
)
|
||||
|
||||
# Get database statistics
|
||||
stats = mongo_client.get_stats("customers")
|
||||
print(f"Customer collection stats: {stats}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("🎉 Comprehensive MongoDB Example Completed Successfully!")
|
||||
print("=" * 60)
|
||||
|
||||
print("\n📈 System Overview:")
|
||||
print("- Memory System: MongoDB-based persistent memory")
|
||||
print("- Knowledge Base: MongoDB vector search enabled")
|
||||
print("- Tools Integration: Full MongoDB CRUD operations")
|
||||
print("- Multi-Agent Coordination: Business workflow simulation")
|
||||
print("- Vector Search: Semantic search capabilities")
|
||||
print("- Data Analytics: Business intelligence insights")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error during execution: {e}")
|
||||
print("Please check your MongoDB connection and API keys.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Comprehensive MongoDB Integration Example")
|
||||
print("=" * 50)
|
||||
print("This example demonstrates complete MongoDB integration with PraisonAI")
|
||||
print("Features:")
|
||||
print("- MongoDB Memory Provider")
|
||||
print("- MongoDB Knowledge Store")
|
||||
print("- MongoDB Tools Integration")
|
||||
print("- Vector Search Capabilities")
|
||||
print("- Multi-Agent Business Simulation")
|
||||
print("=" * 50)
|
||||
print("Make sure you have:")
|
||||
print("1. MongoDB server running (local or Atlas)")
|
||||
print("2. OpenAI API key set in environment variables")
|
||||
print("3. MongoDB dependencies installed: pip install 'praisonaiagents[mongodb]'")
|
||||
print("=" * 50)
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MongoDB Knowledge Example for PraisonAI Agents
|
||||
|
||||
This example demonstrates how to use MongoDB as a knowledge store for PraisonAI agents.
|
||||
MongoDB can store and retrieve knowledge documents with vector search capabilities.
|
||||
|
||||
Prerequisites:
|
||||
- Install MongoDB dependencies: pip install 'praisonaiagents[mongodb]'
|
||||
- MongoDB server running (local or Atlas)
|
||||
- OpenAI API key for embeddings
|
||||
|
||||
Features demonstrated:
|
||||
- MongoDB as knowledge vector store
|
||||
- Document processing and storage
|
||||
- Vector search for knowledge retrieval
|
||||
- Knowledge-based agent interactions
|
||||
- File processing with MongoDB storage
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
|
||||
# Ensure OpenAI API key is set
|
||||
if not os.environ.get("OPENAI_API_KEY"):
|
||||
raise ValueError("Please set the OPENAI_API_KEY environment variable")
|
||||
|
||||
def main():
|
||||
# MongoDB knowledge configuration
|
||||
mongodb_knowledge_config = {
|
||||
"vector_store": {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/", # Replace with your MongoDB connection string
|
||||
"database": "praisonai_knowledge",
|
||||
"collection": "knowledge_base",
|
||||
"use_vector_search": True # Enable Atlas Vector Search
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": "text-embedding-3-small",
|
||||
"api_key": os.getenv("OPENAI_API_KEY")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Create a knowledge agent with MongoDB knowledge store
|
||||
knowledge_agent = Agent(
|
||||
name="MongoDB Knowledge Agent",
|
||||
role="Knowledge Specialist",
|
||||
goal="Provide accurate information from MongoDB knowledge base",
|
||||
backstory="""You are an expert knowledge specialist who can access and
|
||||
retrieve information from a comprehensive MongoDB knowledge base. You excel
|
||||
at finding relevant information, synthesizing knowledge from multiple sources,
|
||||
and providing accurate, context-aware responses.""",
|
||||
knowledge_config=mongodb_knowledge_config,
|
||||
knowledge=[os.path.join(os.path.dirname(__file__), "llms.md")],
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create a research assistant agent
|
||||
research_agent = Agent(
|
||||
name="Research Assistant",
|
||||
role="Research Assistant",
|
||||
goal="Gather information and store it in the knowledge base",
|
||||
backstory="""You are a research assistant who specializes in gathering
|
||||
information from various sources and organizing it for storage in the
|
||||
knowledge base. You ensure information is accurate, well-structured,
|
||||
and properly categorized.""",
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks for knowledge management
|
||||
knowledge_tasks = [
|
||||
Task(
|
||||
description="""Research and store information about MongoDB Atlas Vector Search:
|
||||
1. Gather comprehensive information about MongoDB Atlas Vector Search
|
||||
2. Include technical specifications, use cases, and best practices
|
||||
3. Store the information in the MongoDB knowledge base
|
||||
4. Organize information by categories (features, performance, integration)
|
||||
""",
|
||||
expected_output="MongoDB Atlas Vector Search information stored in knowledge base",
|
||||
agent=research_agent
|
||||
),
|
||||
Task(
|
||||
description="""Research and store information about AI agent frameworks:
|
||||
1. Research popular AI agent frameworks (LangChain, AutoGen, etc.)
|
||||
2. Compare their features, capabilities, and use cases
|
||||
3. Store comparative analysis in the knowledge base
|
||||
4. Include code examples and best practices
|
||||
""",
|
||||
expected_output="AI agent framework comparison stored in knowledge base",
|
||||
agent=research_agent
|
||||
),
|
||||
Task(
|
||||
description="""Query the knowledge base for MongoDB information:
|
||||
1. Search for information about MongoDB Atlas Vector Search
|
||||
2. Extract key features and capabilities
|
||||
3. Provide a comprehensive summary
|
||||
4. Include technical recommendations
|
||||
""",
|
||||
expected_output="Comprehensive MongoDB Atlas Vector Search summary from knowledge base",
|
||||
agent=knowledge_agent
|
||||
),
|
||||
Task(
|
||||
description="""Query the knowledge base for AI agent framework information:
|
||||
1. Search for information about AI agent frameworks
|
||||
2. Compare different frameworks based on stored knowledge
|
||||
3. Provide recommendations for different use cases
|
||||
4. Include best practices and examples
|
||||
""",
|
||||
expected_output="AI agent framework comparison and recommendations from knowledge base",
|
||||
agent=knowledge_agent
|
||||
)
|
||||
]
|
||||
|
||||
# Initialize the multi-agent system with MongoDB knowledge
|
||||
print("🚀 Starting MongoDB Knowledge Management System...")
|
||||
print("=" * 60)
|
||||
|
||||
knowledge_system = PraisonAIAgents(
|
||||
agents=[research_agent, knowledge_agent],
|
||||
tasks=knowledge_tasks,
|
||||
memory=True,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute the knowledge management pipeline
|
||||
try:
|
||||
results = knowledge_system.start()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Knowledge Management System Results:")
|
||||
print("=" * 60)
|
||||
|
||||
# Handle results structure properly
|
||||
if isinstance(results, (list, tuple)):
|
||||
for i, result in enumerate(results, 1):
|
||||
print(f"\n{i}. Task Result:")
|
||||
if hasattr(result, 'raw') and hasattr(result, 'agent'):
|
||||
print(f" Output: {result.raw[:200]}...")
|
||||
print(f" Agent: {result.agent}")
|
||||
else:
|
||||
print(f" Output: {str(result)[:200]}...")
|
||||
else:
|
||||
print(f"\n1. Task Result:")
|
||||
print(f" Output: {str(results)[:200]}...")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("💾 MongoDB Knowledge Integration Complete!")
|
||||
print("=" * 60)
|
||||
|
||||
# Demonstrate direct knowledge access
|
||||
print("\n🔍 Demonstrating Direct Knowledge Access:")
|
||||
print("-" * 40)
|
||||
|
||||
# Access knowledge system directly
|
||||
from praisonaiagents.knowledge import Knowledge
|
||||
|
||||
knowledge_system = Knowledge(config=mongodb_knowledge_config)
|
||||
|
||||
# Store a sample document
|
||||
sample_doc = """
|
||||
MongoDB is a document-oriented database that provides high performance,
|
||||
high availability, and easy scalability. It works on the concept of
|
||||
collections and documents, using a flexible, JSON-like document model.
|
||||
"""
|
||||
|
||||
print("📄 Storing sample document...")
|
||||
knowledge_system.store(sample_doc, metadata={"category": "database", "type": "overview"})
|
||||
|
||||
# Search for stored knowledge
|
||||
print("\n🔍 Searching knowledge base...")
|
||||
results = knowledge_system.search("MongoDB document database", limit=3)
|
||||
|
||||
print(f"Found {len(results)} knowledge entries:")
|
||||
for result in results:
|
||||
print(f" - {result.get('memory', '')[:100]}...")
|
||||
print(f" Score: {result.get('score', 'N/A')}")
|
||||
|
||||
print("\n✅ MongoDB Knowledge Example Completed Successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error during execution: {e}")
|
||||
print("Please check your MongoDB connection and API keys.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("MongoDB Knowledge Example for PraisonAI Agents")
|
||||
print("=" * 50)
|
||||
print("This example demonstrates MongoDB integration for knowledge management")
|
||||
print("Make sure you have:")
|
||||
print("1. MongoDB server running (local or Atlas)")
|
||||
print("2. OpenAI API key set in environment variables")
|
||||
print("3. MongoDB dependencies installed: pip install 'praisonaiagents[mongodb]'")
|
||||
print("=" * 50)
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,178 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MongoDB Memory Example for PraisonAI Agents
|
||||
|
||||
This example demonstrates how to use MongoDB as a memory provider for PraisonAI agents.
|
||||
MongoDB can be used as both a key-value store and a vector database for memory operations.
|
||||
|
||||
Prerequisites:
|
||||
- Install MongoDB dependencies: pip install 'praisonaiagents[mongodb]'
|
||||
- MongoDB server running (local or Atlas)
|
||||
- OpenAI API key for embeddings
|
||||
|
||||
Features demonstrated:
|
||||
- MongoDB as memory provider
|
||||
- Vector search with Atlas Vector Search
|
||||
- Quality scoring and filtering
|
||||
- User-specific memory storage
|
||||
- Persistent memory across sessions
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
|
||||
# Ensure OpenAI API key is set
|
||||
if not os.environ.get("OPENAI_API_KEY"):
|
||||
raise ValueError("Please set the OPENAI_API_KEY environment variable")
|
||||
|
||||
def main():
|
||||
# MongoDB memory configuration
|
||||
mongodb_memory_config = {
|
||||
"provider": "mongodb",
|
||||
"config": {
|
||||
"connection_string": "mongodb://localhost:27017/", # Replace with your MongoDB connection string
|
||||
"database": "praisonai_memory",
|
||||
"use_vector_search": True, # Enable Atlas Vector Search (requires MongoDB Atlas)
|
||||
"max_pool_size": 50,
|
||||
"min_pool_size": 10,
|
||||
"server_selection_timeout": 5000
|
||||
}
|
||||
}
|
||||
|
||||
# Create a research agent with MongoDB memory
|
||||
research_agent = Agent(
|
||||
name="MongoDB Research Agent",
|
||||
role="Senior Research Specialist",
|
||||
goal="Research topics and maintain persistent memory using MongoDB",
|
||||
backstory="""You are an expert researcher who specializes in collecting,
|
||||
analyzing, and storing information using advanced MongoDB-based memory systems.
|
||||
You excel at maintaining context across multiple research sessions and can
|
||||
retrieve relevant information from your persistent memory store.""",
|
||||
memory=True,
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create research tasks
|
||||
research_tasks = [
|
||||
Task(
|
||||
description="""Research the latest developments in AI/ML for 2024:
|
||||
1. Identify major AI breakthroughs and trends
|
||||
2. Analyze the impact on various industries
|
||||
3. Store key findings in MongoDB memory with quality scores
|
||||
4. Identify emerging technologies and their potential applications
|
||||
""",
|
||||
expected_output="Comprehensive AI/ML research report with key findings stored in MongoDB",
|
||||
agent=research_agent
|
||||
),
|
||||
Task(
|
||||
description="""Research MongoDB Atlas Vector Search capabilities:
|
||||
1. Explore vector search features and use cases
|
||||
2. Compare with other vector databases
|
||||
3. Analyze performance and scalability
|
||||
4. Store technical insights in MongoDB memory
|
||||
""",
|
||||
expected_output="Technical analysis of MongoDB Atlas Vector Search with stored insights",
|
||||
agent=research_agent
|
||||
),
|
||||
Task(
|
||||
description="""Research AI agent frameworks and tools:
|
||||
1. Compare popular AI agent frameworks
|
||||
2. Analyze their strengths and weaknesses
|
||||
3. Identify best practices for agent development
|
||||
4. Store comparative analysis in MongoDB memory
|
||||
""",
|
||||
expected_output="Comparative analysis of AI agent frameworks with stored insights",
|
||||
agent=research_agent
|
||||
)
|
||||
]
|
||||
|
||||
# Initialize the multi-agent system with MongoDB memory
|
||||
print("🚀 Starting MongoDB Memory Research System...")
|
||||
print("=" * 60)
|
||||
|
||||
research_system = PraisonAIAgents(
|
||||
agents=[research_agent],
|
||||
tasks=research_tasks,
|
||||
memory=True,
|
||||
memory_config=mongodb_memory_config,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute the research pipeline
|
||||
try:
|
||||
results = research_system.start()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Research System Results:")
|
||||
print("=" * 60)
|
||||
|
||||
# Handle results structure properly
|
||||
if isinstance(results, (list, tuple)):
|
||||
for i, result in enumerate(results, 1):
|
||||
print(f"\n{i}. Task Result:")
|
||||
if hasattr(result, 'agent'):
|
||||
print(f" Output: {str(result)[:200]}...")
|
||||
print(f" Agent: {result.agent}")
|
||||
else:
|
||||
print(f" Output: {str(result)[:200]}...")
|
||||
else:
|
||||
print(f"\n1. Task Result:")
|
||||
print(f" Output: {str(results)[:200]}...")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("💾 MongoDB Memory Integration Complete!")
|
||||
print("=" * 60)
|
||||
|
||||
# Demonstrate memory retrieval
|
||||
print("\n🔍 Demonstrating Memory Retrieval:")
|
||||
print("-" * 40)
|
||||
|
||||
# Note: Direct memory access not available through research_system.memory
|
||||
# Instead, use Memory class directly for verification
|
||||
try:
|
||||
from praisonaiagents.memory import Memory
|
||||
|
||||
# Create Memory instance with MongoDB config for verification
|
||||
memory_system = Memory(mongodb_memory_config)
|
||||
|
||||
# Search for AI-related memories
|
||||
ai_memories = memory_system.search_long_term("AI machine learning", limit=3)
|
||||
print(f"Found {len(ai_memories)} AI-related memories:")
|
||||
for memory in ai_memories:
|
||||
if hasattr(memory, 'get'):
|
||||
print(f" - {memory.get('text', '')[:100]}...")
|
||||
else:
|
||||
print(f" - {str(memory)[:100]}...")
|
||||
|
||||
# Search for MongoDB-related memories
|
||||
mongo_memories = memory_system.search_long_term("MongoDB vector search", limit=3)
|
||||
print(f"\nFound {len(mongo_memories)} MongoDB-related memories:")
|
||||
for memory in mongo_memories:
|
||||
if hasattr(memory, 'get'):
|
||||
print(f" - {memory.get('text', '')[:100]}...")
|
||||
else:
|
||||
print(f" - {str(memory)[:100]}...")
|
||||
|
||||
except ImportError:
|
||||
print("Memory features not available - install with: pip install 'praisonaiagents[memory]'")
|
||||
except Exception as mem_error:
|
||||
print(f"Memory verification error: {mem_error}")
|
||||
|
||||
print("\n✅ MongoDB Memory Example Completed Successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error during execution: {e}")
|
||||
print("Please check your MongoDB connection and API keys.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("MongoDB Memory Example for PraisonAI Agents")
|
||||
print("=" * 50)
|
||||
print("This example demonstrates MongoDB integration for persistent memory")
|
||||
print("Make sure you have:")
|
||||
print("1. MongoDB server running (local or Atlas)")
|
||||
print("2. OpenAI API key set in environment variables")
|
||||
print("3. MongoDB dependencies installed: pip install 'praisonaiagents[mongodb]'")
|
||||
print("=" * 50)
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,205 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MongoDB Tools Example for PraisonAI Agents
|
||||
|
||||
This example demonstrates how to use MongoDB tools with PraisonAI agents.
|
||||
Agents can perform MongoDB operations like inserting, querying, and vector search.
|
||||
|
||||
Prerequisites:
|
||||
- Install MongoDB dependencies: pip install 'praisonaiagents[mongodb]'
|
||||
- MongoDB server running (local or Atlas)
|
||||
- OpenAI API key for embeddings
|
||||
|
||||
Features demonstrated:
|
||||
- MongoDB tools integration
|
||||
- Document CRUD operations
|
||||
- Vector search with embeddings
|
||||
- Data analysis with MongoDB
|
||||
- Agent-driven database operations
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.tools import mongodb_tools
|
||||
|
||||
# Ensure OpenAI API key is set
|
||||
if not os.environ.get("OPENAI_API_KEY"):
|
||||
raise ValueError("Please set the OPENAI_API_KEY environment variable")
|
||||
|
||||
def main():
|
||||
# Create a MongoDB Database Agent with MongoDB tools
|
||||
db_agent = Agent(
|
||||
name="MongoDB Database Agent",
|
||||
role="Database Operations Specialist",
|
||||
goal="Manage MongoDB operations and data analysis",
|
||||
backstory="""You are an expert database administrator who specializes in
|
||||
MongoDB operations. You can perform complex database queries, manage
|
||||
collections, and analyze data patterns. You excel at using MongoDB tools
|
||||
for data management and retrieval.""",
|
||||
tools=[mongodb_tools],
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create a Data Analysis Agent
|
||||
analysis_agent = Agent(
|
||||
name="Data Analysis Agent",
|
||||
role="Data Analyst",
|
||||
goal="Analyze data stored in MongoDB and provide insights",
|
||||
backstory="""You are a data analyst who specializes in extracting insights
|
||||
from MongoDB databases. You can query data, perform aggregations, and
|
||||
identify patterns and trends in the stored information.""",
|
||||
tools=[mongodb_tools],
|
||||
verbose=True,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks for MongoDB operations
|
||||
mongodb_tasks = [
|
||||
Task(
|
||||
description="""Set up MongoDB collections and insert sample data:
|
||||
1. Create a collection called 'products' for e-commerce data
|
||||
2. Insert sample product documents with fields: name, price, category, description
|
||||
3. Create a collection called 'users' for user data
|
||||
4. Insert sample user documents with fields: name, email, age, preferences
|
||||
5. Create appropriate indexes for better query performance
|
||||
|
||||
Use the MongoDB tools to perform these operations.
|
||||
""",
|
||||
expected_output="MongoDB collections created with sample data and indexes",
|
||||
agent=db_agent
|
||||
),
|
||||
Task(
|
||||
description="""Perform vector search operations:
|
||||
1. Create a vector index for product descriptions
|
||||
2. Store products with text embeddings for vector search
|
||||
3. Demonstrate vector similarity search for product recommendations
|
||||
4. Show how to find similar products based on description embeddings
|
||||
|
||||
Use the MongoDB vector search tools for these operations.
|
||||
""",
|
||||
expected_output="Vector search implementation with product recommendations",
|
||||
agent=db_agent
|
||||
),
|
||||
Task(
|
||||
description="""Query and analyze the stored data:
|
||||
1. Find all products in a specific category
|
||||
2. Calculate average price by category
|
||||
3. Find users with specific preferences
|
||||
4. Perform text search on product descriptions
|
||||
5. Generate insights about the data patterns
|
||||
|
||||
Use MongoDB query tools to perform these analyses.
|
||||
""",
|
||||
expected_output="Data analysis report with insights from MongoDB queries",
|
||||
agent=analysis_agent
|
||||
),
|
||||
Task(
|
||||
description="""Demonstrate advanced MongoDB operations:
|
||||
1. Update product prices based on specific criteria
|
||||
2. Perform aggregation operations for statistical analysis
|
||||
3. Create reports on user demographics and preferences
|
||||
4. Implement data validation and error handling
|
||||
5. Show MongoDB best practices for data management
|
||||
|
||||
Use MongoDB tools for these advanced operations.
|
||||
""",
|
||||
expected_output="Advanced MongoDB operations report with best practices",
|
||||
agent=db_agent
|
||||
)
|
||||
]
|
||||
|
||||
# Initialize the multi-agent system with MongoDB tools
|
||||
print("🚀 Starting MongoDB Tools Demo System...")
|
||||
print("=" * 60)
|
||||
|
||||
mongodb_system = PraisonAIAgents(
|
||||
agents=[db_agent, analysis_agent],
|
||||
tasks=mongodb_tasks,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Execute the MongoDB operations pipeline
|
||||
try:
|
||||
results = mongodb_system.start(return_dict=True)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 MongoDB Tools System Results:")
|
||||
print("=" * 60)
|
||||
print(results)
|
||||
|
||||
# Handle results structure properly
|
||||
if isinstance(results, dict) and 'task_results' in results:
|
||||
task_results = results['task_results']
|
||||
for i, result in task_results.items():
|
||||
print(f"\n{i}. Task Result:")
|
||||
if hasattr(result, 'raw') and hasattr(result, 'agent'):
|
||||
print(f" Output: {result.raw[:200]}...")
|
||||
print(f" Agent: {result.agent}")
|
||||
else:
|
||||
print(f" Result: {str(result)[:200]}...")
|
||||
else:
|
||||
print(f"\n1. Task Result:")
|
||||
print(f" Output: {str(results)[:200]}...")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("💾 MongoDB Tools Integration Complete!")
|
||||
print("=" * 60)
|
||||
|
||||
# Demonstrate direct MongoDB tools usage
|
||||
print("\n🔍 Demonstrating Direct MongoDB Tools Usage:")
|
||||
print("-" * 40)
|
||||
|
||||
# Connect to MongoDB
|
||||
mongo_client = mongodb_tools.connect_mongodb(
|
||||
"mongodb://localhost:27017/", # Replace with your connection string
|
||||
"praisonai_tools_demo"
|
||||
)
|
||||
|
||||
# Insert a sample document
|
||||
print("📄 Inserting sample document...")
|
||||
result = mongo_client.insert_document(
|
||||
"demo_collection",
|
||||
{
|
||||
"title": "MongoDB Tools Demo",
|
||||
"description": "Demonstration of MongoDB tools with PraisonAI",
|
||||
"category": "database",
|
||||
"tags": ["mongodb", "praisonai", "tools", "demo"]
|
||||
},
|
||||
metadata={"created_by": "demo", "version": "1.0"}
|
||||
)
|
||||
print(f" Result: {result}")
|
||||
|
||||
# Query documents
|
||||
print("\n🔍 Querying documents...")
|
||||
documents = mongo_client.find_documents(
|
||||
"demo_collection",
|
||||
{"category": "database"},
|
||||
limit=5
|
||||
)
|
||||
print(f" Found {len(documents)} documents")
|
||||
for doc in documents:
|
||||
print(f" - {doc.get('title', 'N/A')}: {doc.get('description', '')[:50]}...")
|
||||
|
||||
# Get collection statistics
|
||||
print("\n📈 Getting collection statistics...")
|
||||
stats = mongo_client.get_stats("demo_collection")
|
||||
print(f" Collection stats: {stats}")
|
||||
|
||||
print("\n✅ MongoDB Tools Example Completed Successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error during execution: {e}")
|
||||
print("Please check your MongoDB connection and API keys.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("MongoDB Tools Example for PraisonAI Agents")
|
||||
print("=" * 50)
|
||||
print("This example demonstrates MongoDB tools integration")
|
||||
print("Make sure you have:")
|
||||
print("1. MongoDB server running (local or Atlas)")
|
||||
print("2. OpenAI API key set in environment variables")
|
||||
print("3. MongoDB dependencies installed: pip install 'praisonaiagents[mongodb]'")
|
||||
print("=" * 50)
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,143 @@
|
||||
"""
|
||||
Multi-Agent Workflow with Performance Monitoring - Example 2
|
||||
|
||||
Demonstrates performance monitoring in a multi-agent workflow:
|
||||
- Multiple agents with different roles
|
||||
- Inter-agent communication tracking
|
||||
- Workflow performance analysis
|
||||
- Task delegation timing
|
||||
|
||||
Shows how to monitor complex multi-agent systems.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import (
|
||||
monitor_function, track_api_call, get_function_stats,
|
||||
analyze_function_flow, generate_comprehensive_report
|
||||
)
|
||||
import time
|
||||
|
||||
@monitor_function("research_task")
|
||||
def setup_research_task():
|
||||
"""Set up research task with monitoring."""
|
||||
return Task(
|
||||
name="research_task",
|
||||
description="Research the topic of renewable energy trends",
|
||||
expected_output="A comprehensive research summary"
|
||||
)
|
||||
|
||||
@monitor_function("analysis_task")
|
||||
def setup_analysis_task():
|
||||
"""Set up analysis task with monitoring."""
|
||||
return Task(
|
||||
name="analysis_task",
|
||||
description="Analyze the research data and identify key trends",
|
||||
expected_output="Key insights and trend analysis"
|
||||
)
|
||||
|
||||
@monitor_function("report_task")
|
||||
def setup_report_task():
|
||||
"""Set up report generation task with monitoring."""
|
||||
return Task(
|
||||
name="report_task",
|
||||
description="Create a final report combining research and analysis",
|
||||
expected_output="A well-structured final report"
|
||||
)
|
||||
|
||||
@monitor_function("agent_creation")
|
||||
def create_agents():
|
||||
"""Create specialized agents with monitoring."""
|
||||
|
||||
researcher = Agent(
|
||||
name="researcher",
|
||||
role="Research Specialist",
|
||||
goal="Gather comprehensive information on renewable energy",
|
||||
backstory="Expert researcher with deep knowledge of energy sector",
|
||||
instructions="You are a thorough researcher. Provide detailed, fact-based information.",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
analyst = Agent(
|
||||
name="analyst",
|
||||
role="Data Analyst",
|
||||
goal="Analyze trends and patterns in renewable energy data",
|
||||
backstory="Experienced data analyst specializing in energy markets",
|
||||
instructions="You are an analytical thinker. Focus on trends, patterns, and insights.",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
writer = Agent(
|
||||
name="writer",
|
||||
role="Report Writer",
|
||||
goal="Create clear, compelling reports",
|
||||
backstory="Professional technical writer with expertise in energy sector",
|
||||
instructions="You are a skilled writer. Create clear, well-structured reports.",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
return researcher, analyst, writer
|
||||
|
||||
@monitor_function("workflow_execution")
|
||||
def main():
|
||||
"""Main function demonstrating multi-agent workflow with performance monitoring."""
|
||||
print("=" * 70)
|
||||
print("EXAMPLE 2: Multi-Agent Workflow with Performance Monitoring")
|
||||
print("=" * 70)
|
||||
|
||||
# Create agents with monitoring
|
||||
researcher, analyst, writer = create_agents()
|
||||
|
||||
# Set up tasks with monitoring
|
||||
research_task = setup_research_task()
|
||||
analysis_task = setup_analysis_task()
|
||||
report_task = setup_report_task()
|
||||
|
||||
# Assign agents to tasks
|
||||
research_task.agent = researcher
|
||||
analysis_task.agent = analyst
|
||||
report_task.agent = writer
|
||||
|
||||
# Create workflow
|
||||
with track_api_call("multi_agent_workflow"):
|
||||
print("\n🚀 Starting multi-agent workflow...")
|
||||
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[researcher, analyst, writer],
|
||||
tasks=[research_task, analysis_task, report_task],
|
||||
process="sequential",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
result = workflow.start()
|
||||
|
||||
# Display comprehensive performance analysis
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 MULTI-AGENT PERFORMANCE ANALYSIS")
|
||||
print("=" * 70)
|
||||
|
||||
# Function statistics
|
||||
print("\n📈 Function Performance Statistics:")
|
||||
stats = get_function_stats()
|
||||
for func_name, data in stats.items():
|
||||
print(f" {func_name}:")
|
||||
print(f" Calls: {data['call_count']}")
|
||||
avg_time = data['total_time'] / data['call_count'] if data['call_count'] > 0 else 0
|
||||
print(f" Avg Time: {avg_time:.3f}s")
|
||||
print(f" Total Time: {data['total_time']:.3f}s")
|
||||
|
||||
# Flow analysis
|
||||
print("\n🔄 Execution Flow Analysis:")
|
||||
flow_analysis = analyze_function_flow()
|
||||
print(f" Total Functions Monitored: {len(flow_analysis.get('functions', []))}")
|
||||
print(f" Execution Chains: {len(flow_analysis.get('call_chains', []))}")
|
||||
|
||||
# Comprehensive report
|
||||
print("\n📋 Comprehensive Performance Report:")
|
||||
report = generate_comprehensive_report()
|
||||
print(report[:500] + "..." if len(report) > 500 else report)
|
||||
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = main()
|
||||
print(f"\n✅ Multi-agent workflow completed successfully!")
|
||||
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Agent with Tools Performance Monitoring - Example 3
|
||||
|
||||
Demonstrates performance monitoring for agents using tools:
|
||||
- Tool execution timing
|
||||
- Search operation performance
|
||||
- Tool call success/failure tracking
|
||||
- Tool usage analysis
|
||||
|
||||
Shows how to monitor agent performance when using external tools.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.tools import duckduckgo, wikipedia
|
||||
from praisonaiagents.telemetry import (
|
||||
monitor_function, track_api_call, get_api_stats,
|
||||
get_slowest_functions, performance_monitor
|
||||
)
|
||||
import time
|
||||
|
||||
@monitor_function("search_with_duckduckgo")
|
||||
def perform_web_search(query):
|
||||
"""Perform web search with performance monitoring."""
|
||||
print(f"🔍 Searching web for: {query}")
|
||||
with track_api_call("duckduckgo_search"):
|
||||
# Simulate search time
|
||||
time.sleep(0.2)
|
||||
return f"Search results for: {query}"
|
||||
|
||||
@monitor_function("wikipedia_lookup")
|
||||
def perform_wikipedia_search(topic):
|
||||
"""Perform Wikipedia search with performance monitoring."""
|
||||
print(f"📚 Looking up Wikipedia for: {topic}")
|
||||
with track_api_call("wikipedia_search"):
|
||||
# Simulate Wikipedia lookup
|
||||
time.sleep(0.15)
|
||||
return f"Wikipedia information about: {topic}"
|
||||
|
||||
@monitor_function("agent_with_tools_execution")
|
||||
def main():
|
||||
"""Main function demonstrating agent with tools and performance monitoring."""
|
||||
print("=" * 70)
|
||||
print("EXAMPLE 3: Agent with Tools Performance Monitoring")
|
||||
print("=" * 70)
|
||||
|
||||
# Create agent with tools
|
||||
agent = Agent(
|
||||
instructions="""You are a research assistant that helps find information.
|
||||
Use the available tools to search for information and provide comprehensive answers.
|
||||
Be thorough in your research and cite your sources.""",
|
||||
llm="gpt-4o-mini",
|
||||
tools=[duckduckgo, wikipedia] # Add search tools
|
||||
)
|
||||
|
||||
# Test queries to demonstrate tool performance monitoring
|
||||
queries = [
|
||||
"What are the latest developments in artificial intelligence?",
|
||||
"Tell me about renewable energy innovations in 2024",
|
||||
"How do neural networks work?"
|
||||
]
|
||||
|
||||
results = []
|
||||
|
||||
for i, query in enumerate(queries, 1):
|
||||
print(f"\n🎯 Query {i}: {query}")
|
||||
|
||||
# Monitor the search operations
|
||||
perform_web_search(query)
|
||||
perform_wikipedia_search(query)
|
||||
|
||||
# Track the full agent execution
|
||||
with track_api_call(f"agent_query_{i}"):
|
||||
print(f"\n🚀 Processing with agent...")
|
||||
|
||||
# Note: In a real implementation, the agent would use the tools automatically
|
||||
# For demo purposes, we're simulating the tool usage
|
||||
result = agent.start(f"Research and answer: {query}")
|
||||
results.append(result)
|
||||
|
||||
print(f"📝 Agent Response: {result[:200]}..." if len(result) > 200 else f"📝 Agent Response: {result}")
|
||||
|
||||
# Performance Analysis
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 TOOLS PERFORMANCE ANALYSIS")
|
||||
print("=" * 70)
|
||||
|
||||
# API call statistics
|
||||
print("\n📈 API Call Performance:")
|
||||
api_stats = get_api_stats()
|
||||
for api_name, stats in api_stats.items():
|
||||
print(f" {api_name}:")
|
||||
print(f" Total Calls: {stats['call_count']}")
|
||||
print(f" Avg Response Time: {stats['total_time']/stats['call_count']:.3f}s")
|
||||
print(f" Success Rate: {stats['success_count']/stats['call_count']*100:.1f}%")
|
||||
print(f" Total Time: {stats['total_time']:.3f}s")
|
||||
|
||||
# Slowest functions analysis
|
||||
print("\n🐌 Slowest Functions:")
|
||||
slowest = get_slowest_functions()
|
||||
for func_name, avg_time in slowest:
|
||||
print(f" {func_name}: {avg_time:.3f}s average")
|
||||
|
||||
# Real-time performance data
|
||||
print("\n⚡ Real-time Performance Data:")
|
||||
current_stats = performance_monitor.get_function_performance()
|
||||
print(f" Functions Monitored: {len(current_stats)}")
|
||||
|
||||
total_function_calls = sum(stats['call_count'] for stats in current_stats.values())
|
||||
total_execution_time = sum(stats['total_time'] for stats in current_stats.values())
|
||||
|
||||
print(f" Total Function Calls: {total_function_calls}")
|
||||
print(f" Total Execution Time: {total_execution_time:.3f}s")
|
||||
print(f" Average Time per Call: {total_execution_time/total_function_calls:.3f}s" if total_function_calls > 0 else " No calls recorded")
|
||||
|
||||
return results
|
||||
|
||||
if __name__ == "__main__":
|
||||
results = main()
|
||||
print(f"\n✅ Tool monitoring example completed! Processed {len(results)} queries.")
|
||||
@@ -0,0 +1,167 @@
|
||||
"""
|
||||
Async Agent with Performance Monitoring - Example 4
|
||||
|
||||
Demonstrates performance monitoring for asynchronous agent operations:
|
||||
- Async function execution timing
|
||||
- Concurrent task performance
|
||||
- Async API call tracking
|
||||
- Parallel operation analysis
|
||||
|
||||
Shows how to monitor performance in async/await workflows.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.telemetry import (
|
||||
monitor_function, track_api_call, get_performance_report,
|
||||
get_function_stats, visualize_execution_flow
|
||||
)
|
||||
|
||||
@monitor_function("async_data_processing")
|
||||
async def process_data_async(data_id: int):
|
||||
"""Async data processing with performance monitoring."""
|
||||
print(f"⚙️ Processing data {data_id} asynchronously...")
|
||||
await asyncio.sleep(0.1) # Simulate async processing
|
||||
return f"Processed data {data_id}"
|
||||
|
||||
@monitor_function("async_api_call")
|
||||
async def simulate_api_call(endpoint: str, delay: float = 0.2):
|
||||
"""Simulate async API call with performance monitoring."""
|
||||
print(f"🌐 Calling API endpoint: {endpoint}")
|
||||
await asyncio.sleep(delay) # Simulate network delay
|
||||
return f"Response from {endpoint}"
|
||||
|
||||
@monitor_function("batch_processing")
|
||||
async def process_batch(batch_size: int = 3):
|
||||
"""Process multiple items concurrently with monitoring."""
|
||||
print(f"📦 Processing batch of {batch_size} items...")
|
||||
|
||||
# Create concurrent tasks
|
||||
tasks = []
|
||||
for i in range(batch_size):
|
||||
tasks.append(process_data_async(i + 1))
|
||||
|
||||
# Wait for all tasks to complete
|
||||
results = await asyncio.gather(*tasks)
|
||||
print(f"✅ Batch processing completed: {len(results)} items processed")
|
||||
return results
|
||||
|
||||
@monitor_function("async_agent_execution")
|
||||
async def run_async_agent(query: str):
|
||||
"""Run agent with async operations and monitoring."""
|
||||
|
||||
# Create agent
|
||||
agent = Agent(
|
||||
instructions="You are an efficient AI assistant that processes requests quickly",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Track the agent execution
|
||||
with track_api_call("async_agent_request"):
|
||||
print(f"🚀 Running async agent for query: {query}")
|
||||
|
||||
# For demo purposes, we simulate async agent execution
|
||||
# In real scenarios, you'd use agent.astart() or similar async method when available
|
||||
await asyncio.sleep(0.1) # Simulate async processing time
|
||||
result = agent.start(query)
|
||||
|
||||
# Simulate some async post-processing
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
return result
|
||||
|
||||
@monitor_function("concurrent_agents")
|
||||
async def run_concurrent_agents():
|
||||
"""Run multiple agents concurrently with monitoring."""
|
||||
print("🔄 Running multiple agents concurrently...")
|
||||
|
||||
queries = [
|
||||
"What is machine learning?",
|
||||
"Explain quantum computing",
|
||||
"How does blockchain work?"
|
||||
]
|
||||
|
||||
# Create concurrent agent tasks
|
||||
agent_tasks = []
|
||||
for i, query in enumerate(queries):
|
||||
task = run_async_agent(query)
|
||||
agent_tasks.append(task)
|
||||
|
||||
# Execute all agents concurrently
|
||||
results = await asyncio.gather(*agent_tasks)
|
||||
return results
|
||||
|
||||
async def main():
|
||||
"""Main async function demonstrating async agent performance monitoring."""
|
||||
print("=" * 70)
|
||||
print("EXAMPLE 4: Async Agent Performance Monitoring")
|
||||
print("=" * 70)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
# Run async operations with monitoring
|
||||
print("\n📊 Phase 1: Batch Processing")
|
||||
batch_results = await process_batch(3)
|
||||
|
||||
print("\n🌐 Phase 2: API Calls")
|
||||
api_tasks = [
|
||||
simulate_api_call("user-service", 0.1),
|
||||
simulate_api_call("data-service", 0.15),
|
||||
simulate_api_call("analytics-service", 0.2)
|
||||
]
|
||||
api_results = await asyncio.gather(*api_tasks)
|
||||
|
||||
print("\n🤖 Phase 3: Concurrent Agents")
|
||||
agent_results = await run_concurrent_agents()
|
||||
|
||||
total_time = time.time() - start_time
|
||||
|
||||
# Performance Analysis
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 ASYNC PERFORMANCE ANALYSIS")
|
||||
print("=" * 70)
|
||||
|
||||
print(f"\n⏱️ Total Execution Time: {total_time:.3f}s")
|
||||
|
||||
# Function statistics
|
||||
print("\n📈 Function Performance:")
|
||||
stats = get_function_stats()
|
||||
for func_name, data in stats.items():
|
||||
avg_time = data['total_time'] / data['call_count'] if data['call_count'] > 0 else 0
|
||||
print(f" {func_name}:")
|
||||
print(f" Calls: {data['call_count']}")
|
||||
print(f" Avg Time: {avg_time:.3f}s")
|
||||
print(f" Total Time: {data['total_time']:.3f}s")
|
||||
|
||||
# Execution flow visualization
|
||||
print("\n🔄 Execution Flow:")
|
||||
try:
|
||||
flow_viz = visualize_execution_flow()
|
||||
print(" Flow visualization generated successfully")
|
||||
print(f" Flow data length: {len(str(flow_viz))}")
|
||||
except Exception as e:
|
||||
print(f" Flow visualization: {str(e)}")
|
||||
|
||||
# Performance report
|
||||
print("\n📋 Performance Report:")
|
||||
report = get_performance_report()
|
||||
print(report[:300] + "..." if len(report) > 300 else report)
|
||||
|
||||
# Results summary
|
||||
print(f"\n✅ Results Summary:")
|
||||
print(f" Batch items processed: {len(batch_results)}")
|
||||
print(f" API calls completed: {len(api_results)}")
|
||||
print(f" Agent queries processed: {len(agent_results)}")
|
||||
|
||||
return {
|
||||
'batch_results': batch_results,
|
||||
'api_results': api_results,
|
||||
'agent_results': agent_results,
|
||||
'total_time': total_time
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the async main function
|
||||
result = asyncio.run(main())
|
||||
print(f"\n🎉 Async monitoring example completed successfully!")
|
||||
@@ -0,0 +1,195 @@
|
||||
"""
|
||||
Error Handling with Performance Monitoring - Example 5
|
||||
|
||||
Demonstrates performance monitoring for error scenarios:
|
||||
- Error rate tracking
|
||||
- Failed operation timing
|
||||
- Recovery mechanism performance
|
||||
- Exception handling with monitoring
|
||||
|
||||
Shows how to monitor performance when things go wrong.
|
||||
"""
|
||||
|
||||
import random
|
||||
import time
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.telemetry import (
|
||||
monitor_function, track_api_call, get_function_stats,
|
||||
get_api_stats, performance_monitor
|
||||
)
|
||||
|
||||
@monitor_function("unreliable_operation")
|
||||
def simulate_unreliable_operation(operation_id: int, failure_rate: float = 0.3):
|
||||
"""Simulate an operation that sometimes fails."""
|
||||
print(f"⚙️ Running operation {operation_id}...")
|
||||
|
||||
# Simulate processing time
|
||||
time.sleep(random.uniform(0.05, 0.2))
|
||||
|
||||
# Random failure simulation
|
||||
if random.random() < failure_rate:
|
||||
raise RuntimeError(f"Operation {operation_id} failed!")
|
||||
|
||||
return f"Operation {operation_id} succeeded"
|
||||
|
||||
@monitor_function("retry_with_backoff")
|
||||
def retry_operation(operation_id: int, max_retries: int = 3):
|
||||
"""Retry an operation with exponential backoff and monitoring."""
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
print(f"🔄 Attempt {attempt + 1} for operation {operation_id}")
|
||||
result = simulate_unreliable_operation(operation_id)
|
||||
print(f"✅ Operation {operation_id} succeeded on attempt {attempt + 1}")
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"❌ Attempt {attempt + 1} failed: {e}")
|
||||
if attempt < max_retries - 1:
|
||||
# Exponential backoff
|
||||
sleep_time = 2 ** attempt * 0.1
|
||||
print(f"⏳ Waiting {sleep_time:.1f}s before retry...")
|
||||
time.sleep(sleep_time)
|
||||
else:
|
||||
print(f"💥 All {max_retries} attempts failed for operation {operation_id}")
|
||||
raise
|
||||
|
||||
@monitor_function("robust_agent_execution")
|
||||
def run_robust_agent(query: str, with_error_simulation: bool = True):
|
||||
"""Run agent with error handling and performance monitoring."""
|
||||
|
||||
agent = Agent(
|
||||
instructions="""You are a resilient AI assistant.
|
||||
Even when facing challenges, you provide helpful responses.
|
||||
If you encounter errors, acknowledge them and offer alternatives.""",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
try:
|
||||
with track_api_call("robust_agent_request"):
|
||||
print(f"🚀 Processing query: {query}")
|
||||
|
||||
if with_error_simulation and random.random() < 0.2:
|
||||
# Simulate API error
|
||||
raise Exception("Simulated API error")
|
||||
|
||||
result = agent.start(query)
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Error during agent execution: {e}")
|
||||
|
||||
# Fallback response with monitoring
|
||||
with track_api_call("fallback_response"):
|
||||
print("🔄 Using fallback response mechanism...")
|
||||
time.sleep(0.1) # Simulate fallback processing
|
||||
return f"I encountered an error while processing '{query}', but I'm designed to be resilient. Let me provide a general response about this topic."
|
||||
|
||||
@monitor_function("batch_processing_with_errors")
|
||||
def process_batch_with_error_handling(batch_size: int = 5):
|
||||
"""Process a batch of operations with comprehensive error handling."""
|
||||
print(f"📦 Processing batch of {batch_size} operations with error monitoring...")
|
||||
|
||||
results = []
|
||||
errors = []
|
||||
|
||||
for i in range(batch_size):
|
||||
try:
|
||||
result = retry_operation(i + 1)
|
||||
results.append(result)
|
||||
except Exception as e:
|
||||
errors.append(f"Operation {i + 1}: {str(e)}")
|
||||
|
||||
return results, errors
|
||||
|
||||
def main():
|
||||
"""Main function demonstrating error handling with performance monitoring."""
|
||||
print("=" * 70)
|
||||
print("EXAMPLE 5: Error Handling Performance Monitoring")
|
||||
print("=" * 70)
|
||||
|
||||
# Phase 1: Batch processing with errors
|
||||
print("\n📊 Phase 1: Batch Processing with Error Handling")
|
||||
successes, failures = process_batch_with_error_handling(5)
|
||||
|
||||
print(f"\n📈 Batch Results:")
|
||||
print(f" Successful operations: {len(successes)}")
|
||||
print(f" Failed operations: {len(failures)}")
|
||||
|
||||
# Phase 2: Agent execution with error scenarios
|
||||
print("\n🤖 Phase 2: Agent Execution with Error Scenarios")
|
||||
|
||||
test_queries = [
|
||||
"What is artificial intelligence?",
|
||||
"How do computers work?",
|
||||
"Explain quantum physics",
|
||||
]
|
||||
|
||||
agent_results = []
|
||||
for query in test_queries:
|
||||
try:
|
||||
result = run_robust_agent(query, with_error_simulation=True)
|
||||
agent_results.append(result)
|
||||
print(f"✅ Query processed: '{query[:30]}...'")
|
||||
except Exception as e:
|
||||
print(f"❌ Query failed: '{query[:30]}...' - {e}")
|
||||
|
||||
# Performance Analysis with Error Metrics
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 ERROR HANDLING PERFORMANCE ANALYSIS")
|
||||
print("=" * 70)
|
||||
|
||||
# Function performance with error rates
|
||||
print("\n📈 Function Performance (including errors):")
|
||||
stats = get_function_stats()
|
||||
for func_name, data in stats.items():
|
||||
avg_time = data['total_time'] / data['call_count'] if data['call_count'] > 0 else 0
|
||||
error_rate = data['error_count'] / data['call_count'] * 100 if data['call_count'] > 0 else 0
|
||||
success_rate = 100 - error_rate
|
||||
|
||||
print(f" {func_name}:")
|
||||
print(f" Calls: {data['call_count']}")
|
||||
print(f" Errors: {data['error_count']}")
|
||||
print(f" Success Rate: {success_rate:.1f}%")
|
||||
print(f" Avg Time: {avg_time:.3f}s")
|
||||
|
||||
# API call reliability
|
||||
print("\n🌐 API Call Reliability:")
|
||||
api_stats = get_api_stats()
|
||||
for api_name, stats in api_stats.items():
|
||||
success_rate = stats['success_count'] / stats['call_count'] * 100 if stats['call_count'] > 0 else 0
|
||||
avg_time = stats['total_time'] / stats['call_count'] if stats['call_count'] > 0 else 0
|
||||
|
||||
print(f" {api_name}:")
|
||||
print(f" Success Rate: {success_rate:.1f}%")
|
||||
print(f" Average Response Time: {avg_time:.3f}s")
|
||||
print(f" Total Calls: {stats['call_count']}")
|
||||
|
||||
# Error patterns analysis
|
||||
print("\n⚠️ Error Patterns:")
|
||||
total_operations = sum(s['call_count'] for s in api_stats.values())
|
||||
total_errors = sum(s['error_count'] for s in api_stats.values())
|
||||
overall_success_rate = ((total_operations - total_errors) / total_operations * 100) if total_operations > 0 else 0
|
||||
|
||||
print(f" Total Operations: {total_operations}")
|
||||
print(f" Total Errors: {total_errors}")
|
||||
print(f" Overall Success Rate: {overall_success_rate:.1f}%")
|
||||
|
||||
# Recommendations
|
||||
print(f"\n💡 Performance Recommendations:")
|
||||
if overall_success_rate < 90:
|
||||
print(" - Consider implementing more robust error handling")
|
||||
print(" - Review retry strategies and backoff algorithms")
|
||||
if total_errors > 0:
|
||||
print(f" - Investigate {total_errors} errors for root cause analysis")
|
||||
print(" - Monitor error patterns for system reliability improvements")
|
||||
|
||||
return {
|
||||
'successful_operations': len(successes),
|
||||
'failed_operations': len(failures),
|
||||
'agent_results': len(agent_results),
|
||||
'overall_success_rate': overall_success_rate
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = main()
|
||||
print(f"\n✅ Error handling monitoring completed!")
|
||||
print(f"Success rate: {result['overall_success_rate']:.1f}%")
|
||||
@@ -0,0 +1,218 @@
|
||||
"""
|
||||
Memory Agent with Performance Monitoring - Example 6
|
||||
|
||||
Demonstrates performance monitoring for agents with memory capabilities:
|
||||
- Memory storage and retrieval timing
|
||||
- Knowledge base operations
|
||||
- Session persistence performance
|
||||
- Memory search optimization
|
||||
|
||||
Shows how to monitor performance in stateful agents.
|
||||
"""
|
||||
|
||||
import time
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.telemetry import (
|
||||
monitor_function, track_api_call, get_performance_report,
|
||||
get_slowest_functions, analyze_performance_trends
|
||||
)
|
||||
|
||||
# Simulate memory operations since actual memory might not be available
|
||||
@monitor_function("memory_storage")
|
||||
def store_memory(key: str, value: str, memory_type: str = "short_term"):
|
||||
"""Simulate memory storage operation with monitoring."""
|
||||
print(f"💾 Storing {memory_type} memory: {key}")
|
||||
# Simulate storage time - short term is faster
|
||||
storage_time = 0.05 if memory_type == "short_term" else 0.15
|
||||
time.sleep(storage_time)
|
||||
return f"Stored {key} in {memory_type} memory"
|
||||
|
||||
@monitor_function("memory_retrieval")
|
||||
def retrieve_memory(key: str, memory_type: str = "short_term"):
|
||||
"""Simulate memory retrieval operation with monitoring."""
|
||||
print(f"🔍 Retrieving {memory_type} memory: {key}")
|
||||
# Simulate retrieval time
|
||||
retrieval_time = 0.03 if memory_type == "short_term" else 0.12
|
||||
time.sleep(retrieval_time)
|
||||
return f"Retrieved {key} from {memory_type} memory"
|
||||
|
||||
@monitor_function("knowledge_search")
|
||||
def search_knowledge_base(query: str):
|
||||
"""Simulate knowledge base search with monitoring."""
|
||||
print(f"📚 Searching knowledge base for: {query}")
|
||||
# Simulate vector search time
|
||||
time.sleep(0.08)
|
||||
return f"Knowledge search results for: {query}"
|
||||
|
||||
@monitor_function("memory_consolidation")
|
||||
def consolidate_memories(session_id: str):
|
||||
"""Simulate memory consolidation process."""
|
||||
print(f"🔄 Consolidating memories for session: {session_id}")
|
||||
|
||||
# Simulate consolidation steps
|
||||
store_memory("consolidated_session", session_id, "long_term")
|
||||
retrieve_memory("recent_interactions", "short_term")
|
||||
|
||||
time.sleep(0.1)
|
||||
return f"Memories consolidated for session: {session_id}"
|
||||
|
||||
@monitor_function("stateful_agent_interaction")
|
||||
def run_stateful_agent(query: str, session_id: str, conversation_turn: int):
|
||||
"""Run agent with memory operations and monitoring."""
|
||||
|
||||
# Simulate memory-enabled agent
|
||||
agent = Agent(
|
||||
instructions="""You are a memory-enabled AI assistant. You can remember
|
||||
previous conversations and build on past interactions. Use your memory
|
||||
to provide personalized and contextual responses.""",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Pre-processing: retrieve relevant memories
|
||||
with track_api_call("memory_preprocessing"):
|
||||
print(f"🧠 Pre-processing memories for turn {conversation_turn}")
|
||||
|
||||
# Retrieve session context
|
||||
if conversation_turn > 1:
|
||||
retrieve_memory(f"session_{session_id}", "short_term")
|
||||
|
||||
# Search for relevant knowledge
|
||||
search_knowledge_base(query)
|
||||
|
||||
time.sleep(0.05) # Simulate context preparation
|
||||
|
||||
# Main agent execution
|
||||
with track_api_call("memory_agent_execution"):
|
||||
print(f"🚀 Processing query with memory context...")
|
||||
result = agent.start(query)
|
||||
|
||||
# Post-processing: store new memories
|
||||
with track_api_call("memory_postprocessing"):
|
||||
print(f"💭 Post-processing: storing interaction memories")
|
||||
|
||||
# Store the interaction
|
||||
store_memory(f"interaction_{conversation_turn}", query, "short_term")
|
||||
store_memory(f"response_{conversation_turn}", result, "short_term")
|
||||
|
||||
# Update session memory
|
||||
store_memory(f"session_{session_id}", f"Turn {conversation_turn} completed", "short_term")
|
||||
|
||||
time.sleep(0.03) # Simulate memory update
|
||||
|
||||
return result
|
||||
|
||||
@monitor_function("multi_turn_conversation")
|
||||
def simulate_multi_turn_conversation():
|
||||
"""Simulate a multi-turn conversation with memory."""
|
||||
session_id = "demo_session_001"
|
||||
|
||||
conversation = [
|
||||
"Hello, I'm interested in learning about machine learning",
|
||||
"What are the main types of machine learning algorithms?",
|
||||
"Can you give me examples of supervised learning?",
|
||||
"How does this relate to what we discussed about machine learning earlier?"
|
||||
]
|
||||
|
||||
results = []
|
||||
|
||||
for turn, query in enumerate(conversation, 1):
|
||||
print(f"\n💬 Conversation Turn {turn}")
|
||||
print(f"User: {query}")
|
||||
|
||||
result = run_stateful_agent(query, session_id, turn)
|
||||
results.append(result)
|
||||
|
||||
print(f"Agent: {result[:100]}..." if len(result) > 100 else f"Agent: {result}")
|
||||
|
||||
# Simulate user thinking time
|
||||
time.sleep(0.1)
|
||||
|
||||
# End of conversation: consolidate memories
|
||||
consolidate_memories(session_id)
|
||||
|
||||
return results
|
||||
|
||||
def main():
|
||||
"""Main function demonstrating memory agent performance monitoring."""
|
||||
print("=" * 70)
|
||||
print("EXAMPLE 6: Memory Agent Performance Monitoring")
|
||||
print("=" * 70)
|
||||
|
||||
# Warm up the memory system
|
||||
print("\n🚀 Phase 1: Memory System Initialization")
|
||||
store_memory("system_initialized", "true", "long_term")
|
||||
search_knowledge_base("initialization")
|
||||
|
||||
# Run multi-turn conversation
|
||||
print("\n💬 Phase 2: Multi-Turn Conversation with Memory")
|
||||
conversation_results = simulate_multi_turn_conversation()
|
||||
|
||||
# Memory performance analysis
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 MEMORY PERFORMANCE ANALYSIS")
|
||||
print("=" * 70)
|
||||
|
||||
# Performance trends analysis
|
||||
print("\n📈 Performance Trends:")
|
||||
try:
|
||||
trends = analyze_performance_trends()
|
||||
print(f" Performance trend analysis completed")
|
||||
if trends:
|
||||
print(f" Functions analyzed: {len(trends.get('functions', []))}")
|
||||
except Exception as e:
|
||||
print(f" Trends analysis: {str(e)}")
|
||||
|
||||
# Slowest operations
|
||||
print("\n🐌 Slowest Memory Operations:")
|
||||
slowest = get_slowest_functions()
|
||||
for func_name, avg_time in slowest[:5]: # Top 5 slowest
|
||||
print(f" {func_name}: {avg_time:.3f}s average")
|
||||
|
||||
# Memory operation breakdown
|
||||
print("\n💾 Memory Operation Breakdown:")
|
||||
from praisonaiagents.telemetry import get_function_stats
|
||||
stats = get_function_stats()
|
||||
|
||||
memory_operations = {k: v for k, v in stats.items() if 'memory' in k.lower()}
|
||||
|
||||
for op_name, data in memory_operations.items():
|
||||
avg_time = data['total_time'] / data['call_count'] if data['call_count'] > 0 else 0
|
||||
print(f" {op_name}:")
|
||||
print(f" Operations: {data['call_count']}")
|
||||
print(f" Avg Time: {avg_time:.3f}s")
|
||||
print(f" Total Time: {data['total_time']:.3f}s")
|
||||
|
||||
# Session efficiency metrics
|
||||
total_memory_ops = sum(data['call_count'] for data in memory_operations.values())
|
||||
total_memory_time = sum(data['total_time'] for data in memory_operations.values())
|
||||
|
||||
print(f"\n🔍 Session Efficiency:")
|
||||
print(f" Total Memory Operations: {total_memory_ops}")
|
||||
print(f" Total Memory Time: {total_memory_time:.3f}s")
|
||||
print(f" Avg Time per Memory Op: {total_memory_time/total_memory_ops:.3f}s" if total_memory_ops > 0 else " No memory operations")
|
||||
|
||||
# Performance report
|
||||
print("\n📋 Comprehensive Performance Report:")
|
||||
report = get_performance_report()
|
||||
print(report[:400] + "..." if len(report) > 400 else report)
|
||||
|
||||
# Memory optimization suggestions
|
||||
print(f"\n💡 Memory Optimization Suggestions:")
|
||||
if total_memory_time > 1.0:
|
||||
print(" - Consider implementing memory caching strategies")
|
||||
print(" - Optimize vector search indexes for faster retrieval")
|
||||
if total_memory_ops > 10:
|
||||
print(" - Consider batching memory operations for efficiency")
|
||||
print(" - Monitor memory consolidation frequency vs performance")
|
||||
|
||||
return {
|
||||
'conversation_turns': len(conversation_results),
|
||||
'memory_operations': total_memory_ops,
|
||||
'total_memory_time': total_memory_time
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = main()
|
||||
print(f"\n✅ Memory agent monitoring completed!")
|
||||
print(f"Processed {result['conversation_turns']} conversation turns")
|
||||
print(f"Executed {result['memory_operations']} memory operations in {result['total_memory_time']:.3f}s")
|
||||
Loaded 100 of 233 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user