Compare commits

...
205 Commits
Author SHA1 Message Date
Mervin Praison eca66f9213 feat: bump praisonai to v2.2.76 and praisonaiagents to v0.0.149 with callback refactor 2025-07-25 09:41:04 +01:00
Mervin PraisonandClaude f83f3eae0b fix: ensure consistent display output between OpenAI and Gemini models
- Added execute_sync_callback call in custom LLM path to match OpenAI behavior
- Fixed issue where Gemini/custom LLM responses were not triggering callbacks
- Prevents duplicate display by relying on LLM class to handle verbose display
- Ensures both paths execute callbacks for consistent behavior

The issue was that custom LLM providers (like Gemini) were not calling
_execute_callback_and_display after guardrail validation, while OpenAI
path was. This caused missing Task/Response display boxes for Gemini.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-25 08:31:38 +01:00
Mervin Praison 68dd9fb5f1 feat: enable httpx debug logging when LOGLEVEL=debug and remove Ollama handling docs 2025-07-24 10:00:41 +01:00
Mervin Praison e0531cedf7 feat: optimize LLM tool handling and response generation for Ollama and Gemini models 2025-07-24 01:53:54 +01:00
Mervin Praison 197e98d3ff refactor: consolidate warning and logging configuration with minimal overhead 2025-07-24 01:06:10 +01:00
Mervin Praison 3b7fd3688c update 2025-07-24 00:59:36 +01:00
Mervin Praison dd7bfa39f4 refactor: centralize logging configuration in new _logging.py module 2025-07-24 00:27:12 +01:00
Mervin Praison fca801c3f7 chore: remove test files for display generating and streaming fixes 2025-07-23 23:57:08 +01:00
Mervin Praison 33c81861c0 feat: implement comprehensive agent monitoring system with streaming enhancements 2025-07-23 23:56:19 +01:00
Mervin Praison b49b6d567f Merge pull request #1050 from MervinPraison/claude/pr-1049-20250723-0902
fix: Remove display_generating when stream=false to prevent streaming-like behavior
2025-07-23 10:45:49 +01:00
github-actions[bot]andMervin Praison 8c1677556c fix: Complete display_generating fix for consistency across all code paths
- Fix inconsistency in custom LLM path (line 1116) to match OpenAI path behavior
- Update test script to verify all three fix locations (2 OpenAI + 1 custom LLM)
- Ensure no streaming-like behavior when stream=False regardless of LLM provider
- Maintain backward compatibility and existing functionality

Co-authored-by: Mervin Praison <[email protected]>
2025-07-23 09:34:50 +00:00
github-actions[bot]andMervin Praison a656ea2bca fix: Remove display_generating when stream=false to prevent streaming-like behavior
When stream=false but verbose=true, the system was incorrectly showing
streaming-like visual behavior (Generating... X.Xs) due to display_generating
being used even when users explicitly set stream=false.

Fixed by changing display_fn from display_generating to None when stream=false
in both locations (lines 1073 and 1172) in agent.py.

This ensures true non-streaming behavior when stream=false is set, regardless
of verbose setting.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-23 09:07:13 +00:00
Mervin Praison 02846b0efd Merge pull request #1049 from MervinPraison/claude/pr-1048-20250723-0615
Fix: Add display_generating support for OpenAI non-streaming mode
2025-07-23 07:30:21 +01:00
github-actions[bot]andMervin Praison 8d463e6897 Improve display_generating fix for OpenAI non-streaming mode
- Add missing display_fn support to async version (achat_completion_with_tools)
- Add proper error handling with try-catch around Live context manager
- Add safer content access with length checks for choices array
- Add console cleanup (console.print()) consistent with streaming mode
- Maintain backward compatibility with fallback to regular completion

Co-authored-by: Mervin Praison <[email protected]>
2025-07-23 06:23:50 +00:00
github-actions[bot]andClaude b47021d98f Fix: Add display_generating support for OpenAI non-streaming mode
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-23 06:19:44 +00:00
Mervin Praison ab4561d7a4 Merge pull request #1048 from MervinPraison/claude/pr-1046-20250722-2152
PR #1046: Changes from Claude
2025-07-22 23:19:32 +01:00
Mervin Praison 81af51f3e7 Merge pull request #1047 from MervinPraison/claude/pr-1045-20250722-2138
fix: resolve display_generating issue by ensuring stream parameter is correctly passed
2025-07-22 23:02:46 +01:00
github-actions[bot]andMervin Praison 550d9c30d4 fix: restore display_generating visibility in verbose streaming mode
- Modified streaming logic to show display_generating during verbose streaming
- Preserves non-verbose streaming behavior (no display_generating)
- Fixes user-reported issue where display_generating disappeared in basic-agents.py
- Maintains full backward compatibility with existing code
- Addresses display functionality regression from streaming fallback changes

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:59:47 +00:00
Mervin Praison 39a8942054 Merge pull request #1046 from MervinPraison/claude/fix-streaming-fallback-logic-20250722
fix: implement proper streaming fallback logic for JSON parsing errors
2025-07-22 22:46:55 +01:00
github-actions[bot]andMervin Praison 6ecb6e4887 fix: prevent duplicate API calls in streaming fallback logic
- Added fallback_completed flag to track successful fallback execution
- Modified non-streaming condition to only execute when fallback hasn''t completed
- Prevents duplicate litellm.completion calls that waste API quota and increase latency
- Addresses critical bug identified by Cursor bot review

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:43:35 +00:00
github-actions[bot]andMervin Praison d64c1381ed fix: resolve display_generating issue by ensuring stream parameter is correctly passed from start() to chat() method
- Fix stream parameter not being explicitly passed in start() method
- Ensures display_generating shows when agent.start() is called with default settings
- Agent defaults: verbose=True, stream=False should show ''Generating...'' message
- Resolves issue where chat() method default stream=True overrode agent''s stream=False setting
- Tested logic flow: (not stream and verbose) now correctly evaluates to True

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:43:19 +00:00
Mervin Praison 8e6bc3845b Merge pull request #1045 from MervinPraison/claude/pr-1042-20250722-2118
fix: correct display_generating logic to only show when stream=False AND verbose=True
2025-07-22 22:36:55 +01:00
github-actions[bot]andMervin Praison d2b2ad525e fix: implement proper streaming fallback logic for JSON parsing errors
- Fixed critical control flow bug where streaming errors set use_streaming=False
  but continued executing in streaming block instead of triggering fallback
- When recoverable streaming error occurs, immediately perform actual non-streaming
  API call instead of creating mock response with partial/empty data
- Added proper callback handling for both verbose/non-verbose modes
- Enhanced graceful degradation if both streaming and non-streaming fail
- Updated test to use agent.llm_model instead of agent.llm property

Fixes: Issue #1043 streaming JSON parsing errors causing "Response: None"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-22 21:35:49 +00:00
github-actions[bot]andMervin Praison 60508ba410 fix: correct hardcoded True to stream variable in display_generating logic
Fixed critical bug where (not True and self.verbose) was always False,
preventing display_generating from ever being called in one code path.
Changed to (not stream and self.verbose) to match intended logic.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:30:16 +00:00
Mervin Praison a2951184d7 Merge pull request #1044 from MervinPraison/claude/issue-1043-20250722-2015
fix: enhance LiteLLM streaming error handling for JSON parsing errors (Issue #1043)
2025-07-22 22:25:13 +01:00
github-actions[bot]andMervin Praison 4e26bbecfb fix: correct display_generating logic to only show when stream=False AND verbose=True
- Change logic from (stream or verbose) to (not stream and verbose)
- Ensure display_generating only appears for non-streaming verbose mode
- Remove streaming display blocks since no display needed when streaming
- Update test files to reflect correct expected behavior

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:24:01 +00:00
github-actions[bot]andMervin Praison f77583c64f fix: resolve critical fallback logic and display_generating behavior
- Fix broken streaming fallback: now properly calls non-streaming LLM instead of creating mock responses
- Update display_generating behavior per requirements:
  * stream=true (any verbose): no display_generating
  * stream=false + verbose=true: show display_generating
  * stream=false + verbose=false: no display_generating
- Clean up unnecessary f-strings without placeholders
- Maintain backward compatibility and existing functionality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 21:23:42 +00:00
github-actions[bot]andMervin Praison 6071850401 fix: enhance LiteLLM streaming error handling for JSON parsing errors
- Add iterator-level error handling in get_response_stream method
- Wrap for chunk in stream_iterator loops with JSON parsing error recovery
- Add error handling to both verbose/non-verbose streaming in get_response
- Leverage existing _is_streaming_error_recoverable() logic for consistency
- Maintain automatic fallback to non-streaming mode on repeated errors
- Preserve backward compatibility with no breaking API changes
- Add comprehensive test script for Issue #1043 scenario

Fixes #1043: Error parsing chunk - JSON parsing error in Gemini streaming

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 20:23:51 +00:00
Mervin Praison 6ce4363e92 Merge pull request #1042 from MervinPraison/claude/issue-981-20250722-1946
fix: ensure display_generating is called when verbose=True regardless of streaming mode
2025-07-22 21:13:02 +01:00
github-actions[bot]andMervin Praison 3622838f9f fix: complete display_generating fix for both OpenAI and custom LLM paths
- Extend fix to custom LLM providers (Anthropic, Groq, local models)
- Add Live display wrapper for non-streaming custom LLM path
- Ensure consistent behavior: display when (stream OR verbose)
- Maintain backward compatibility for all existing functionality
- Add comprehensive test coverage for both agent paths

Fixes #981 completely for all LLM provider types

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 20:07:35 +00:00
github-actions[bot]andMervin Praison 60f0cb308a fix: ensure display_generating is called when verbose=True regardless of streaming mode
- Fix issue where display_generating was only called during streaming
- Now display_generating is called when either stream=True OR verbose=True
- Resolves user issue where verbose=True with default stream=False wasn''t showing ''Generating...'' display
- Add logic tests to verify the fix

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 19:49:49 +00:00
Mervin Praison a3afd4f7fd Merge pull request #1041 from MervinPraison/claude/pr-1039-20250722-1308
PR #1039: Logging
2025-07-22 20:42:51 +01:00
github-actions[bot]andMervin Praison 7118b9a47b perf: optimize litellm logger suppression using hierarchical logging
Remove redundant loops that iterate over all loggers to suppress litellm
submodules. Python''s hierarchical logging automatically propagates the
CRITICAL level from parent ''litellm'' logger to all child loggers
(litellm.utils, litellm.proxy, etc.).

Changes:
- Remove 3 redundant loops in __init__.py and llm/__init__.py
- Remove explicit configuration of child loggers (litellm.utils, etc.)
- Maintain litellm_logging as separate hierarchy (not a child of litellm)
- Add explanatory comments about hierarchical logging inheritance
- Preserve backward compatibility and DEBUG mode behavior

Performance: Eliminates unnecessary iteration over all existing loggers
while maintaining identical logging suppression behavior.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 19:35:02 +00:00
Mervin Praison 3fa0173bf8 refactor: reorganize test files and add MongoDB memory/knowledge integrations 2025-07-22 14:44:51 +01:00
Mervin Praison b9ea6accf9 Merge pull request #1040 from MervinPraison/claude/pr-1038-20250722-1255
PR #1038: Monitoring examples
2025-07-22 14:43:58 +01:00
github-actions[bot]andClaude 80d2491a59 fix: resolve critical issues in monitoring examples
- Fix ZeroDivisionError in 02_multi_agent_workflow_monitoring.py by adding zero-check for call counts
- Improve async handling in 04_async_agent_monitoring.py with better simulation and timing
- Fix UnboundLocalError in 05_error_handling_monitoring.py by correcting variable references
- Add zero-check protection in 07_hierarchical_agents_monitoring.py to prevent division errors
- Remove unused json import from 08_custom_tools_monitoring.py
- Remove unused timedelta import from 09_streaming_monitoring.py
- Fix UnboundLocalError in 10_comprehensive_dashboard.py by initializing avg_function_time variable

🤖 Generated with Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-22 13:33:04 +00:00
github-actions[bot]andMervin Praison 52f23b87c9 fix: Remove logger disabling to allow DEBUG mode re-enabling
- Remove `disabled = True` from logger configuration to prevent permanent disabling
- Keep CRITICAL level suppression but allow dynamic re-enabling in DEBUG mode
- Maintain backward compatibility while fixing DEBUG mode functionality
- Add comprehensive test to verify dynamic mode switching works correctly
- Addresses reviewer concerns about permanent logger disabling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 13:24:56 +00:00
github-actions[bot]andMervin Praison 70a3d2df34 feat: Add 10 comprehensive performance monitoring examples
- Create examples/python/monitoring/ directory with 10 diverse examples
- Cover basic monitoring, multi-agent workflows, tools, async, errors, memory, hierarchical, custom tools, streaming, and comprehensive dashboard
- Each example demonstrates different aspects of the performance monitoring system
- Include detailed README with usage patterns and best practices
- Show practical integration of @monitor_function and track_api_call
- Demonstrate performance analytics, reporting, and optimization insights

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 13:04:17 +00:00
Mervin Praison d3c08660f5 Merge pull request #1039 from MervinPraison/claude/fix-litellm-warnings-20250722-1151
Fix: Comprehensive LiteLLM deprecation warning suppression
2025-07-22 13:58:24 +01:00
github-actions[bot]andMervin Praison 4e09b7c878 fix: clean up warning suppression implementation
- Fix LLM module _should_suppress_warnings() consistency by adding missing test detection
- Remove redundant warning filters in main module (lines 74-76 duplicated lines 63-65)
- Remove duplicate sys import in main module
- All tests now pass: DEBUG mode properly respects LOGLEVEL=DEBUG
- Maintains backward compatibility and existing functionality

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 12:54:07 +00:00
Mervin Praison 6bc6ff79dd Merge pull request #1038 from MervinPraison/claude/issue-1035-20250722-1143
feat: Add comprehensive performance monitoring system
2025-07-22 13:44:08 +01:00
github-actions[bot]andMervin Praison ab78189601 fix: implement DEBUG mode support for warning suppression (issue #1033)
- Implement missing DEBUG mode functionality as specified in PR description
- Add _should_suppress_warnings() function to respect LOGLEVEL=DEBUG
- Fix overly broad global warning suppression (now module-specific)
- Remove unsafe pydantic.warnings.warn monkey-patching
- Improve test detection with pytest module checks
- Add comprehensive test scripts for validation

Key improvements:
• Warnings are suppressed in normal mode (LOGLEVEL=INFO)
• Warnings are shown when LOGLEVEL=DEBUG (as per PR requirement)
• More targeted warning suppression (module-specific vs global)
• Safer pydantic warning handling with specific filters
• Better backward compatibility and security

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-22 12:35:18 +00:00
github-actions[bot]andMervin Praison 54c29a6ffe fix: address code quality issues in performance monitoring
- Remove unused imports (inspect, timedelta, time, deque, etc.)
- Fix division by zero error in performance trend analysis
- Improve call chain building algorithm for better efficiency and accuracy
- Replace bare except clauses with specific Exception handling
- Use collections.deque with maxlen instead of manual truncation
- Remove unused variables and loop variables
- Fix f-strings without placeholders
- Replace magic numbers with named constants
- Improve error handling specificity

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 12:33:33 +00:00
Mervin Praison afb32705dc Merge pull request #1037 from MervinPraison/claude/pr-1030-20250722-1138
fix: correct tool call argument parsing in streaming mode
2025-07-22 13:23:45 +01:00
github-actions[bot]andMervin Praison 9f9a373c31 feat: Add comprehensive performance monitoring system for function and API call analysis
This implementation addresses all requirements from Issue #1035:

Features Added:
- PerformanceMonitor class for function performance evaluation with detailed statistics
- FunctionFlowAnalyzer for execution flow visualization and bottleneck identification
- API call tracking and performance comparison (fast vs slow calls)
- Real-time performance reporting and comprehensive analytics
- CLI interface for easy command-line access to performance data
- Full backward compatibility with existing telemetry system

Key Capabilities:
1. Function Performance Evaluation: Decorators and context managers for automatic timing
2. Function Flow Analysis: Visual execution flow with bottleneck detection
3. Execution Speed Analysis: Min/max/average timing with trend analysis
4. API Call Performance: Success rates, response times, reliability metrics
5. API Call Visibility: Complete tracking and history of all API interactions

Files Modified:
- Enhanced telemetry/__init__.py to expose new performance tools
- Added performance_monitor.py with core monitoring functionality
- Added performance_utils.py with advanced analysis and visualization
- Added performance_cli.py for command-line interface
- Added comprehensive demo and examples

Integration:
- Seamless integration with existing PraisonAI telemetry infrastructure
- Zero breaking changes to existing functionality
- Optional import structure for graceful degradation
- Thread-safe operations with memory management

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:57:58 +00:00
github-actions[bot]andMervin Praison 2c16131770 fix: comprehensive suppression of litellm deprecation warnings
- Add environment variables to suppress warnings at source (LITELLM_TELEMETRY, HTTPX_DISABLE_WARNINGS)
- Implement global deprecation warning filters before imports
- Add specific message filters for exact warning text patterns
- Set aggressive logging levels (CRITICAL) for all litellm loggers
- Configure runtime litellm settings to disable debug output
- Apply comprehensive suppression across main and LLM modules
- Addresses remaining warnings from httpx content parameter and pydantic dict method usage

Resolves issue with persistent litellm warnings after initial fix

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:53:45 +00:00
github-actions[bot]andMervin Praison bc0d58104c enhance: improve JSON parsing error handling for tool arguments
- Add explicit JSON parsing error handling in both streaming and non-streaming modes
- Remove redundant import json statement in streaming mode (already imported at module level)
- Ensure graceful fallback to empty dict when JSON parsing fails
- Maintain backward compatibility while improving robustness

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:50:29 +00:00
Mervin Praison a11990fa40 Merge pull request #1034 from MervinPraison/claude/issue-1033-20250722-1128
Fix litellm deprecation warnings for issue #1033
2025-07-22 12:44:09 +01:00
github-actions[bot]andMervin Praison e1f6601137 refactor: consolidate and document warning filters for issue #1033
- Centralized warning suppression in main __init__.py with comprehensive comments
- Removed duplicated warning filters from llm/__init__.py
- Improved pydantic regex specificity and added module targeting
- Added clear documentation explaining why each warning is suppressed

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:43:27 +00:00
github-actions[bot]andMervin Praison 05dc3b5223 fix: correct tool call argument parsing in streaming mode
- Fix critical bug where tool_call[''function''][''arguments''] was passed as JSON string instead of parsed dict to execute_tool
- Add proper JSON parsing with fallback to empty dict for missing arguments
- Resolves tool execution failures during streaming when tools are called
- Maintains backward compatibility and error handling

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:42:01 +00:00
Mervin Praison c8fdab5885 Merge pull request #1032 from MervinPraison/claude/issue-1031-20250722-1110
fix: eliminate streaming pause caused by telemetry tracking
2025-07-22 12:40:04 +01:00
8f1c18da70 Fix litellm deprecation warnings for issue #1033
Added comprehensive deprecation warning filters to suppress warnings from:
- litellm module and event loop related warnings
- httpx module deprecation warnings
- Pydantic model_dump deprecation warnings

This resolves the noisy deprecation warnings that were cluttering the output
when using PraisonAI agents with newer versions of dependencies.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:36:17 +00:00
Mervin Praison 739c25cbea Merge pull request #1030 from MervinPraison/claude/fix-streaming-display-bypass-20250722
fix: bypass display_generation for OpenAI streaming to enable raw chunk output
2025-07-22 12:25:03 +01:00
github-actions[bot]andMervin Praison bbbb77ba5c feat: enhance streaming implementation with proper tool call support
- Fix critical OpenAI client access bug: use sync_client instead of client
- Reduce code duplication by consolidating completion creation logic
- Improve test script: remove unused imports and properly test generator consumption
- Verify comprehensive tool call handling in streaming mode is working correctly
- All automated review feedback addressed while maintaining backward compatibility

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:17:54 +00:00
github-actions[bot] 00b81080a3 merge: accept comprehensive async telemetry fix from remote
The remote branch already implements a more comprehensive solution using
threading at the integration level, which addresses the streaming pause issue
more thoroughly than the async_mode parameter approach.

Remote changes include:
- All telemetry tracking made asynchronous using background threads
- Special handling for streaming generators in agent.start()
- Better error handling with try/catch blocks
- Maintains the async_mode parameter in telemetry.py for future use

Resolves streaming pause issue #1031
2025-07-22 11:17:41 +00:00
github-actions[bot]andMervin Praison 2c7cbdf0c9 fix: eliminate streaming pause caused by synchronous telemetry tracking
- Add async_mode parameter to track_agent_execution() to enable background PostHog capture
- Update telemetry integration to auto-detect streaming agents and use async mode
- Maintains backward compatibility for non-streaming agents
- Resolves pause issue after "execution tracked: success=True" message in streaming workflows

Fixes #1031

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:16:58 +00:00
github-actions[bot]andMervin Praison 112a7248ca fix: Eliminate telemetry pause in streaming responses
- Make telemetry tracking asynchronous in all agent methods (chat, start, run)
- For streaming responses, defer telemetry tracking until after generator consumption
- Use separate daemon threads to prevent blocking on PostHog capture calls
- Add comprehensive test script to verify fix works
- Fixes issue where ~20 second pause occurred after ''execution tracked: success=True''

This resolves the streaming performance issue where telemetry calls were
blocking the start of streaming responses, causing users to wait before
seeing any output when using agent.start() with stream=True.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:15:50 +00:00
github-actions[bot]andMervin Praison ce214edd99 feat: enhance streaming implementation with proper tool call support
- Add comprehensive tool call handling in OpenAI streaming mode
- Properly accumulate and execute tool calls during streaming
- Maintain chat history integrity with tool call results
- Preserve backward compatibility and existing functionality
- Add error handling for tool execution failures in streaming

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:12:51 +00:00
Mervin Praison ab8932be28 Merge pull request #1029 from MervinPraison/claude/fix-gemini-streaming-json-parsing-20250722
fix: enhance Gemini streaming robustness with graceful JSON parsing error handling
2025-07-22 12:08:02 +01:00
github-actions[bot]andMervin Praison 47c91ae624 fix: correct critical bugs in Gemini streaming error handling
- Fix error counter reset bug: only reset after successful chunk processing
- Fix error masking bug: non-recoverable errors are re-raised immediately
- Add proper exception chaining with ''from chunk_error''
- Remove unnecessary f-string prefix
- Move test to proper location in tests/ directory
- Remove hardcoded sys.path in test script

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:04:14 +00:00
github-actions[bot]andMervin Praison 626260f787 fix: bypass display_generation for OpenAI streaming to enable raw chunk output
This fix modifies Agent._start_stream() to handle OpenAI-style models with direct
streaming that bypasses the display_generation UI, allowing users to get raw
streaming chunks when stream=True.

Key changes:
- Replace self.chat() fallback with direct OpenAI client streaming
- Add proper knowledge search and tool handling for OpenAI path
- Use _build_messages helper for consistent message formatting
- Yield raw chunks without display function interference
- Add robust error handling with chat history rollback
- Preserve fallback to simulated streaming on errors

Fixes streaming behavior where chunks were wrapped in display_generation
instead of being yielded directly to user code.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 11:03:23 +00:00
github-actions[bot]andMervin Praison c0b8aba75b fix: enhance Gemini streaming robustness with graceful JSON parsing error handling
- Add chunk-level error handling in get_response_stream() to catch malformed JSON chunks
- Implement smart error recovery that skips recoverable errors (JSON parsing) vs non-recoverable (connection)
- Add consecutive error limit (3) with automatic fallback to non-streaming mode
- Enhance error logging with specific categorization for better debugging
- Add _is_streaming_error_recoverable() helper method for error classification
- Create test script to validate streaming robustness improvements

Fixes issue where Gemini streaming fails with ''Expecting property name enclosed in double quotes''
when receiving incomplete JSON chunks like ''{'' during streaming responses.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 08:56:50 +00:00
Mervin Praison e3b0b3830e Merge pull request #1028 from MervinPraison/claude/fix-real-time-streaming-20250722
fix: implement real-time streaming for Agent.start() method
2025-07-22 09:47:33 +01:00
github-actions[bot]andMervin Praison 8c3e1f8569 fix: add comprehensive tool call support to streaming implementation
- Remove erroneous temperature parameter from _build_messages() call in get_response_stream()
- Fix critical bug that would cause TypeError during streaming
- Tool call handling already implemented with _process_stream_delta helper
- Real-time streaming now works with content and tool execution
- Follow-up responses after tool completion properly handled
- Backward compatibility maintained with stream=False default

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 08:10:27 +00:00
github-actions[bot]andMervin Praison ab01fe2965 fix: add comprehensive tool call support to streaming implementation
Fixes critical gap in get_response_stream method where tool calls were
completely ignored during streaming. The method now properly:

- Accumulates tool calls during streaming using _process_stream_delta
- Executes tool calls after streaming completes if execute_tool_fn provided
- Continues conversation with follow-up response after tool execution
- Handles both Ollama and other providers correctly
- Maintains real-time content streaming while adding tool support

This makes streaming fully functional for agents that use tools, which
is essential for PraisonAI''s core functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-22 07:58:31 +00:00
github-actions[bot]andMervin Praison e6472b5052 fix: restore backward compatibility by defaulting stream=False
- Change Agent constructor default: stream=True -> stream=False
- Update documentation to clarify backward compatibility priority
- Ensure existing code agent.start(''prompt'') returns string not generator
- Maintain streaming capability when explicitly enabled with stream=True

Fixes backward compatibility issue where basic usage would return generator
instead of expected string response, breaking existing codebases.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 06:40:27 +00:00
github-actions[bot]andMervin Praison a178abe759 fix: implement real-time streaming for Agent.start() method
- Add get_response_stream() generator method to LLM class for true real-time streaming
- Modify Agent._start_stream() to use new streaming generator for custom LLMs
- Replace simulated streaming with actual LiteLLM streaming chunks
- Maintain backward compatibility for OpenAI-style models (temporary fallback)
- Add comprehensive error handling and chat history management
- Include test files for validation

Fixes the issue where streaming was simulated by chunking complete responses
instead of yielding real-time chunks from the underlying LLM.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-22 06:32:17 +00:00
Mervin Praison b139ea98ba Merge pull request #1027 from MervinPraison/claude/issue-981-20250721-1557
fix: implement real-time streaming for Agent.start() method
2025-07-22 07:17:42 +01:00
github-actions[bot]andMervin Praison 589650029b refactor: simplify streaming implementation to eliminate code duplication
- Replace duplicated logic in _custom_llm_stream() and _openai_stream() with single _start_stream() method
- Reuse existing chat() method logic to maintain all features (tools, knowledge, MCP, etc.)
- Improve error handling and consistency across LLM types
- Maintain backward compatibility and all existing functionality
- Reduce maintenance burden by eliminating 150+ lines of duplicated code

Co-authored-by: Mervin Praison <[email protected]>
2025-07-21 16:42:18 +00:00
github-actions[bot]andMervin Praison 97b25f5003 fix: implement real-time streaming for Agent.start() method
- Fix streaming issue where Agent.start() was not yielding real-time chunks
- Add streaming generator support with proper type hints
- Implement _start_stream() method that routes to appropriate handlers
- Add _custom_llm_stream() for custom LLM providers with simulated chunking
- Add _openai_stream() for OpenAI client with true streaming chunks
- Maintain backward compatibility for existing non-streaming usage
- Support both OpenAI and custom LLM streaming with graceful error handling
- Preserve chat history management and proper conversation state

The user''s code pattern now works as expected:
for chunk in agent.start(prompt, stream=True):
    print(chunk, end='''', flush=True)

Fixes #981

Co-authored-by: Mervin Praison <[email protected]>
2025-07-21 16:02:10 +00:00
Mervin Praison 5d30ce9a1b Merge pull request #1026 from MervinPraison/claude/issue-1025-20250721-0849
feat: add context CLI command with --url and --goal parameters
2025-07-21 12:17:40 +01:00
github-actions[bot]andMervin Praison 5ad39b5f1b refactor: improve context CLI command with minor code enhancements
- Fix f-string formatting issues by removing unnecessary f prefix
- Add return type hint to handle_context_command method
- Enhance docstring with return type documentation
- Maintain backward compatibility with existing usage

Co-authored-by: Mervin Praison <[email protected]>
2025-07-21 10:12:16 +00:00
github-actions[bot]andMervin Praison 28c60d52f5 feat: enhance context CLI command with configurable options
- Add --auto-analyze flag for user control over analysis mode
- Use environment MODEL_NAME/OPENAI_MODEL_NAME instead of hardcoded model
- Update usage messages to include new optional flag
- Follow existing CLI patterns for model configuration
- Maintain backward compatibility with defaults

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-21 10:08:48 +00:00
github-actions[bot]andMervin Praison 3a9e26670f feat: add context CLI command with --url and --goal parameters
- Add ''context'' to special_commands list
- Add --url and --goal CLI arguments
- Add handle_context_command method to create ContextAgent
- Maintains backward compatibility
- Implements exact functionality requested in issue #1025

Usage: praisonai context --url <url> --goal <goal>

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-21 08:54:53 +00:00
Mervin Praison 209793bdb1 feat: add MongoDB integration examples and context engineering improvements 2025-07-20 03:47:29 +01:00
Mervin Praison aa965cf719 Merge pull request #1024 from MervinPraison/fix-knowledge-verbose-headers
fix: remove verbose knowledge headers from AI agent prompts
2025-07-19 13:06:45 +01:00
Mervin Praison 4927f24d4a Merge pull request #1021 from MervinPraison/claude/issue-1018-20250719-0632
Implement Context Engineering - Add ContextAgent for AI Context Generation
2025-07-19 09:45:51 +01:00
github-actions[bot]andMervin Praison 2a3240156d fix: add backward compatibility for embedding_db_config key in knowledge context processing
- Support both ''vector_store'' and ''embedding_db_config'' keys for knowledge context
- Maintains backward compatibility with existing test files
- No functional changes to the verbose header removal functionality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 08:43:17 +00:00
github-actions[bot]andClaude 77e8ccb3c9 Transform hardcoded analysis to Agent-based dynamic analysis
This commit transforms all hardcoded analysis methods in ContextAgent to use specialized AI agents for dynamic analysis. The changes enable more flexible, intelligent, and adaptable context processing by leveraging agent-based approaches instead of static analysis patterns.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 08:42:49 +00:00
github-actions[bot]andMervin Praison 031c73c7d3 fix: remove verbose knowledge headers from AI agent prompts
- Remove ''[DB Context]:'' and ''[Vector DB Error]:'' prefixes from agent outputs
- Add debug logging to preserve troubleshooting information
- Return clean content without verbose headers in terminal display
- Hide error details from AI prompts while preserving debug logs
- Consistent with memory.py verbose header removal approach

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 08:33:29 +00:00
Mervin Praison 82b6f9c5ff Merge pull request #1023 from MervinPraison/claude/pr-1020-20250719-0708
fix: remove verbose memory context headers from AI agent prompts
2025-07-19 09:06:01 +01:00
github-actions[bot]andMervin Praison 22449a35b7 feat: hide memory content from terminal display unless debug logging enabled
- Add include_in_output parameter to build_context_for_task() method
- Memory content now only included in output when logging level is DEBUG
- Memory is always logged for debugging regardless of output inclusion
- Preserves AI agent functionality while hiding memory from user display
- Addresses user requirement to hide memory items from terminal by default

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 07:55:41 +00:00
Mervin Praison 75bf3ac73b Merge pull request #1022 from MervinPraison/claude/issue-1019-20250719-0623
fix: move embedding debug logs to trace level
2025-07-19 08:52:32 +01:00
4d3e19462b Reorganize TRACE logging test files and improve test structure
- Move test_embedding_logging.py from root to tests/ directory for better organization
- Improve test structure with comprehensive TRACE logging validation
- Ensure proper test isolation and cleanup
- Maintain backward compatibility while improving code organization

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
Co-Authored-By: Claude <[email protected]>
2025-07-19 07:42:08 +00:00
github-actions[bot]andClaude efaff8845d Fix Context Engineering implementation: Complete functionality and async support
- Fixed core ContextAgent functionality with proper imports and async support
- Added comprehensive error handling and robust implementation
- Cleaned up imports and fixed async method patterns
- Moved test file from root to proper tests/ directory location
- Made implementation fully functional with all required methods
- Enhanced examples with working context engineering workflows

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 07:15:22 +00:00
github-actions[bot]andMervin Praison a4e7a1bdbc fix: remove verbose memory context headers from AI agent prompts
- Remove ''Short-term Memory (X items)'' and ''Long-term Memory (X items)'' headers from AI prompts
- Remove ''Relevant Memory Context:'' and ''Previous task'' prefixes from agent context
- Keep actual memory content and task results for AI functionality
- Move verbose information to debug logging only
- Prevents AI agents from echoing memory context headers in terminal output

This fixes the issue where memory context headers were still appearing in terminal
displays even after the original PR moved verbose information to debug logging.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 07:14:37 +00:00
Mervin Praison fd346b410d Merge pull request #1020 from MervinPraison/claude/fix-context-display-issues
fix: move verbose context information from live display to debug logging
2025-07-19 07:53:34 +01:00
github-actions[bot]andMervin Praison 9acff8f977 Implement Context Engineering - Add ContextAgent for AI Context Generation
- Add ContextAgent class with comprehensive Context Engineering capabilities
- Implements automated context generation for AI coding assistants
- Features: codebase analysis, pattern extraction, validation loops, PRP generation
- 10x better than prompt engineering, 100x better than vibe coding
- Includes factory function create_context_agent() for easy instantiation
- Add comprehensive examples in agents/ and concepts/ directories
- Full backward compatibility maintained with existing PraisonAI functionality
- Complete test suite with 7/7 tests passing

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 06:46:51 +00:00
github-actions[bot]andMervin Praison 4bc3ad97a1 fix: restore AI agent access to actual memory and task context content
- agents.py: Restore actual task result content and memory context in AI prompts
- task.py: Include actual task context results for proper task chaining
- memory.py: Return actual memory content with clean formatting instead of placeholders
- Fix f-string formatting issue in agents.py

This fixes critical functionality bugs where AI agents were receiving placeholder
messages instead of actual content, breaking memory access and task dependencies.
AI agents now get full context while debug logging is preserved.

Resolves issues identified by automated reviews while maintaining backward compatibility.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 06:45:55 +00:00
Claude Code a91228b46e Add custom TRACE logging level to reduce embedding data in debug output
- Add TRACE_LEVEL (5) below DEBUG (10) for fine-grained logging control
- Implement trace() method on logging.Logger class for backward compatibility
- Convert 4 embedding-related debug statements to use trace level
- Embedding data now only appears when logging level set to TRACE or lower
- Maintains full backward compatibility with existing debug logging
- Addresses issue where sensitive embedding data cluttered terminal output

Test coverage:
- Add test_embedding_logging.py to validate implementation functionality
- Verify TRACE level behavior and message filtering

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 06:45:31 +00:00
Mervin Praison 74403501ed Merge pull request #1017 from MervinPraison/claude/issue-1016-20250719-0556
fix(knowledge): resolve 'list' object has no attribute 'get' error
2025-07-19 07:17:56 +01:00
github-actions[bot]andClaude c1703dd234 Fix backward compatibility issue in knowledge.py memory result handling
- Add type checking for memory_result in _process_single_input method
- Handle both dict and list formats for backward compatibility
- Gracefully handle unexpected types with warning instead of crashing
- Resolves issue #1016 where MongoDB memory provider returns dict while Chroma returns list

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 06:08:08 +00:00
github-actions[bot]andClaude 256ad050e4 fix: move verbose context information from live display to debug logging
This commit addresses live display context issues by moving detailed content
from user-facing prompts to debug logging while maintaining functionality:

- agents.py line 653: Memory context now logged via logger.debug() with
  brief user-facing note in prompt instead of full content
- agents.py line 84: Previous task results logged via logger.debug() with
  brief completion status returned instead of full raw content
- task.py line 406: Previous task results logged via logger.debug() with
  brief completion message appended instead of full raw content
- memory.py: Memory section headers now use brief titles with item counts
  and log detailed content via logger.debug() instead of showing full content

These changes maintain backward compatibility and functionality while
improving user experience by reducing verbose output in live displays.
Detailed information remains available through debug logging for debugging
and troubleshooting purposes.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 06:01:11 +00:00
github-actions[bot]andMervin Praison 4ca844e667 fix(knowledge): resolve ''list'' object has no attribute ''get'' error
Fixed line 717 in knowledge.py where memory_result.get(''results'', [])
was called on a list instead of a dict. Both MongoDBMemory.add() and
CustomMemory._add_to_vector_store() return lists directly, so changed
to all_results.extend(memory_result) for proper list concatenation.

Resolves #1016

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 06:00:09 +00:00
Mervin Praison 7e473dcb14 Merge pull request #1015 from MervinPraison/claude/issue-1014-20250719-0501
fix: show context only when loglevel=debug
2025-07-19 06:29:23 +01:00
Mervin Praison 0188c6bf77 docs: add llms.md documentation and update MongoDB example to use local knowledge file 2025-07-19 06:27:15 +01:00
github-actions[bot]andClaude df304df3cf Fix logger configuration to support DEBUG level for verbose >= 3
- Add DEBUG logging level when verbose >= 3
- Maintain INFO level for verbose >= 5
- Keep WARNING level for verbose < 3
- This makes the verbose flag functional for debugging purposes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-19 05:22:03 +00:00
Claude CodeandMervin Praison 82cdda36d7 fix: show context only when loglevel=debug
Changed logger.info to logger.debug for context content display
in agents.py to reduce log noise while maintaining the ability
to see context information when debug logging is enabled.

Changes:
- agents.py:315 - Context content logging moved to debug level
- agents.py:638 - Context content logging moved to debug level

Maintains backward compatibility as context count logging remains
at INFO level, only the actual context content is moved to DEBUG.

Fixes #1014

Co-authored-by: Mervin Praison <[email protected]>
2025-07-19 05:08:27 +00:00
Mervin Praison 9d6c6d7ab2 refactor: improve result handling and display in MongoDB example files 2025-07-19 06:01:20 +01:00
Mervin Praison e47c6b4278 feat: upgrade praisonaiagents to 0.0.144 and add MongoDB support 2025-07-18 22:28:29 +01:00
Mervin Praison aab077ce92 refactor: optimise agent initialisation and streamline function calls
- Removed unnecessary force_shutdown_telemetry() call from basic-agents.py to enhance clarity.
- Updated llm parameter in gemini-sequential.py from "gemini-2.5-pro" to "gemini-2.5-flash" for improved performance.
- Simplified the for loop in realtime-streaming.py by removing the return_generator=True argument, maintaining functionality while improving readability.

These changes aim to enhance code efficiency and maintainability without altering existing functionality.
2025-07-18 22:17:02 +01:00
Mervin Praison 6d717d59b1 Merge pull request #1013 from MervinPraison/claude/issue-1011-20250718-1758
fix: prevent PostHog shutdown errors during interpreter shutdown
2025-07-18 22:12:19 +01:00
github-actions[bot]andMervin Praison 07cc581c04 refactor: remove duplicate threading import in _is_interpreter_shutting_down method
- Remove redundant ''import threading'' since threading is already imported at module level
- Addresses reviewer feedback on code duplication
- Maintains all functionality while improving code cleanliness

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 20:59:52 +00:00
github-actions[bot]andMervin Praison 283282d98b refactor: improve telemetry shutdown error handling and reduce code duplication
- Add centralized _SHUTDOWN_ERROR_PHRASES constant for consistent error detection
- Create _is_shutdown_related_error() helper method to eliminate code duplication
- Remove duplicate import threading in _is_interpreter_shutting_down()
- Remove redundant _posthog = None assignment handled by finally block
- Ensure all shutdown error detection uses consistent phrase list

Addresses reviewer feedback on duplicate imports, inconsistent error handling,
and code duplication while maintaining full backward compatibility.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 20:54:58 +00:00
github-actions[bot]andMervin Praison c231d625c3 fix: prevent PostHog shutdown errors during interpreter shutdown
- Add interpreter shutdown detection using sys.is_finalizing() and thread tests
- Skip PostHog operations when interpreter is shutting down to prevent:
  - cannot schedule new futures after interpreter shutdown errors
  - can''t register atexit after shutdown errors
  - Event loop and threading errors during shutdown
- Enhanced error handling with specific shutdown error message filtering
- Apply shutdown prevention to flush, thread cleanup, and shutdown operations
- Maintain full backward compatibility with existing API
- Add comprehensive debug logging for shutdown scenarios

Fixes issue #1011 where PostHog telemetry was causing shutdown errors
when force_shutdown_telemetry() was called during interpreter shutdown.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 18:03:08 +00:00
Mervin Praison d1b4ab0d95 Merge pull request #1010 from MervinPraison/claude/issue-1009-20250718-1740
fix: prevent telemetry shutdown from hanging indefinitely
2025-07-18 18:56:25 +01:00
github-actions[bot]andMervin Praison c0d5eec2a7 fix: enhance telemetry shutdown robustness and prevent critical issues
- Add thread safety with shutdown locks to prevent concurrent shutdown calls
- Fix double thread pool shutdown issue that could cause undefined behavior
- Implement memory leak prevention with bounded metrics storage (max 1000 entries)
- Replace unsafe daemon threads with ThreadPoolExecutor for better control
- Increase flush timeout from 2s to 5s for improved reliability
- Add comprehensive error handling with proper logging levels
- Split shutdown logic into maintainable helper methods
- Maintain full backward compatibility with existing API

These changes address critical thread safety issues while preserving all
existing functionality and ensuring reliable telemetry shutdown behavior.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 17:53:46 +00:00
github-actions[bot]andMervin Praison 94f7edaae4 fix: prevent telemetry shutdown from hanging indefinitely
- Added timeout-based flush operation in MinimalTelemetry.shutdown()
- Replaced blocking PostHog flush with threaded timeout approach
- Prevents indefinite blocking during program termination
- Maintains backward compatibility and data integrity
- Fixes issue where PostHog queue.join() would hang on exit

Resolves #1009

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 17:43:38 +00:00
Mervin Praison 6607b6279e refactor: streamline chat method by consolidating logging and error handling
- Simplified logging of parameters in debug mode for better clarity.
- Enhanced error handling during LLM calls and reflections to ensure robust rollback mechanisms.
- Improved structure for handling knowledge search and prompt normalization.
- Reduced code duplication by integrating common functionalities into streamlined processes.
2025-07-18 18:32:34 +01:00
Mervin Praison d1cf53c543 Merge pull request #998 from MervinPraison/claude/pr-996-20250718-1439
Fix: Correct indentation in chat method try-finally block
2025-07-18 18:22:55 +01:00
Mervin Praison 4dcc72a02b Merge branch 'main' into claude/pr-996-20250718-1439 2025-07-18 18:22:19 +01:00
Mervin Praison d2a70c45ab Merge branch 'main' of https://github.com/MervinPraison/PraisonAI 2025-07-18 18:11:56 +01:00
Mervin Praison a3cfaaabec refactor: reorganize test files into src directory and add telemetry cleanup 2025-07-18 18:11:06 +01:00
Mervin Praison 4743ba20be Merge pull request #1006 from MervinPraison/revert-1005-claude/pr-1002-20250718-1556
Revert "PR #1002: Changes from Claude"
2025-07-18 17:36:26 +01:00
Mervin Praison 81d4042578 Revert "PR #1002: Changes from Claude" 2025-07-18 17:36:18 +01:00
Mervin Praison 9a6b156b6e Merge pull request #1005 from MervinPraison/claude/pr-1002-20250718-1556
PR #1002: Changes from Claude
2025-07-18 17:35:55 +01:00
github-actions[bot]andMervin Praison fbbee806d1 Refactor telemetry cleanup to use try...finally pattern
- Replace 13 scattered _cleanup_telemetry() calls with single try...finally block
- Ensure cleanup happens on ALL exit paths regardless of success/failure
- Standardize test tolerances to 2 threads across all test files
- Improve test error messages to show specific thread details
- Maintain backward compatibility and all existing functionality
- Reduce code duplication by 95% in chat method

🤖 Generated with Claude Code

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 16:32:22 +00:00
Mervin Praison 2241ecbdde Merge pull request #1004 from MervinPraison/claude/issue-1003-20250718-1604
Fix: Agent.start() now auto-consumes generator for better UX
2025-07-18 17:14:00 +01:00
github-actions[bot]andMervin Praison 0501a85b9e Fix: Agent.start() now auto-consumes generator for better UX
- Modified Agent.start() method to automatically consume the generator by default
- Added return_generator=True parameter for backwards compatibility
- Made MCP import optional to prevent import errors
- Updated examples to demonstrate new behavior
- Fixes issue where basic-agents.py would stop without producing output

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 16:13:19 +00:00
github-actions[bot]andMervin Praison c5c728e148 Fix: Enhance telemetry cleanup to prevent agent termination hang
- Added comprehensive telemetry cleanup calls in agent.py at all return points
- Ensures proper cleanup after guardrail validation failures
- Prevents hanging during agent termination by forcing telemetry shutdown
- Added test files to validate telemetry cleanup functionality
- Addresses hanging issues in async agent execution flows

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-18 16:04:37 +00:00
Mervin Praison 6d4c1d71fb Merge pull request #1002 from MervinPraison/claude/pr-999-20250718-1529
Fix: Add comprehensive telemetry cleanup to prevent agent termination hang
2025-07-18 16:47:38 +01:00
Mervin Praison 31435c454f Merge pull request #1001 from MervinPraison/claude/pr-997-20250718-1454
Fix: Add telemetry cleanup to execute() and aexecute() methods to prevent hanging
2025-07-18 16:46:47 +01:00
github-actions[bot]andMervin Praison 00e722aae1 Fix: Add comprehensive telemetry cleanup to prevent agent termination hang
- Fixed 7 missing telemetry cleanup calls in agent.py
- Added cleanup to all return paths in chat() and achat() methods
- Added cleanup to custom LLM guardrail validation paths
- Added cleanup to reasoning content validation paths
- Added cleanup to exception handlers in achat() method
- Ensures proper termination of telemetry threads
- Prevents hanging after agent task completion
- Maintains full backward compatibility
- Comprehensive testing shows all agents now terminate properly

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 15:40:17 +00:00
Mervin Praison 76a65b3667 Merge pull request #1000 from MervinPraison/claude/fix-telemetry-termination-20250718
Fix: Comprehensive telemetry cleanup to prevent agent termination hang
2025-07-18 16:30:42 +01:00
github-actions[bot]andMervin Praison cd5459d4dc Fix: Comprehensive telemetry cleanup to prevent agent termination hang
This commit addresses the critical issue where PraisonAI agents would not
terminate properly after completing their tasks, requiring manual interruption.

Root Cause: PostHog telemetry client running in async mode creates
background threads that persist after standard shutdown() calls.

Solution:
- Enhanced telemetry shutdown mechanism with comprehensive thread cleanup
- Added force_shutdown_telemetry() function for robust cleanup
- Implemented timeout-based thread termination with proper error handling
- Updated agent._cleanup_telemetry() to use the new robust cleanup method

Impact:
- Agents now terminate cleanly without hanging
- Maintains full backward compatibility
- Comprehensive thread cleanup prevents resource leaks
- Timeout mechanism prevents infinite waiting

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 15:27:44 +00:00
Mervin Praison 326390863f Merge pull request #999 from MervinPraison/claude/issue-987-20250718-1504
Fix: Resolve agent termination issue by adding comprehensive telemetry cleanup
2025-07-18 16:18:33 +01:00
github-actions[bot]andMervin Praison d1e6706427 Fix: Resolve agent termination issue by adding comprehensive telemetry cleanup
- Added _cleanup_telemetry() calls to all return paths in chat() and achat() methods
- Ensures proper program termination without requiring Ctrl+C
- Maintains full backward compatibility with existing APIs
- Addresses syntax error concerns by implementing clean, error-free code
- Added test script to verify the fix works correctly

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 15:16:38 +00:00
github-actions[bot]andMervin Praison 7f0f8d5ed9 Fix: Add telemetry cleanup to execute() and aexecute() methods to prevent hanging
- Add try-finally blocks to execute() and aexecute() methods
- Ensures telemetry cleanup happens for all public entry points
- Prevents programs from hanging due to PostHog background threads
- Maintains backward compatibility
- Addresses critical gaps identified in PR reviews

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 14:57:09 +00:00
github-actions[bot]andMervin Praison 556d1298f1 Fix: Correct indentation in chat method try-finally block
- Fixed syntax error at line 1197 where code was outside try block
- Properly indented all main logic to be inside try block
- Ensures telemetry cleanup finally block executes correctly
- Maintains backward compatibility
- No breaking changes

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 14:49:41 +00:00
Mervin Praison 48acfce433 Merge pull request #994 from MervinPraison/claude/issue-992-20250718-1345
feat: integrate MongoDB as memory and knowledge provider
2025-07-18 15:44:44 +01:00
Mervin Praison 1ddcafbaa4 Merge branch 'main' of https://github.com/MervinPraison/PraisonAI 2025-07-18 15:43:11 +01:00
Mervin Praison eb2073fe77 Enhance workflow configurations by adding a timeout of 15 minutes to all job definitions in the CI/CD pipeline. This change aims to improve resource management and prevent long-running jobs from hanging indefinitely. 2025-07-18 15:43:10 +01:00
Mervin Praison 732e4e6c75 Merge pull request #997 from MervinPraison/revert-996-claude/pr-990-20250718-1429
Revert "Fix: Comprehensive telemetry cleanup to prevent agent termination issues"
2025-07-18 15:39:49 +01:00
Mervin Praison 7f01facb36 Revert "Fix: Comprehensive telemetry cleanup to prevent agent termination issues" 2025-07-18 15:39:39 +01:00
Mervin Praison b2cc7ebfd9 Merge pull request #996 from MervinPraison/claude/pr-990-20250718-1429
Fix: Comprehensive telemetry cleanup to prevent agent termination issues
2025-07-18 15:38:31 +01:00
github-actions[bot]andMervin Praison 3c51b6f644 Fix: Comprehensive telemetry cleanup to prevent agent termination issues
- Added telemetry cleanup to all agent entry points (chat, achat, execute, aexecute)
- Fixed streaming issue by adding cleanup to _start_stream method
- Removed duplicate cleanup from convenience methods
- Ensures proper program termination regardless of which method is used

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 14:36:46 +00:00
Mervin Praison 44f02cdf43 Merge pull request #990 from MervinPraison/claude/issue-987-20250718-1031
Fix: Resolve agent termination issue by properly cleaning up telemetry system
2025-07-18 15:24:30 +01:00
Mervin Praison 55668c9510 Merge branch 'main' into claude/issue-987-20250718-1031 2025-07-18 15:24:03 +01:00
github-actions[bot]andMervin Praison 28e95388f9 fix: resolve MongoDB integration issues
- Fix ID generation consistency between MongoDB and SQLite storage
- Fix inconsistent error handling in memory operations
- Fix security issue with hardcoded API key placeholders in examples
- Fix unused imports in mongodb_tools.py
- Fix pymongo version to >= 4.6.3 to address security vulnerabilities
- Improve datetime import organization
- Ensure consistent storage order for better reliability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 14:18:53 +00:00
github-actions[bot]andMervin Praison 663150a0ee fix: resolve MongoDB integration issues
- Fix memory record citation duplication (lines 861, 880, 933 in memory.py)
- Make vector dimensions configurable based on embedding model
- Add _get_embedding_dimensions method for proper dimension detection
- Support multiple embedding models (text-embedding-3-small, 3-large, ada-002)
- Improve citation format to use record IDs instead of duplicating text

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 14:13:14 +00:00
github-actions[bot]andMervin Praison 225ce7b9f2 feat: integrate MongoDB as memory and knowledge provider
Add comprehensive MongoDB integration for PraisonAI agents including:

**Memory Provider**:
- MongoDB as persistent memory storage option
- Vector search capabilities with Atlas Vector Search
- Support for both key-value and vector operations
- Quality scoring and filtering
- User-specific memory isolation
- Graceful fallback to existing SQLite/ChromaDB

**Knowledge Store**:
- MongoDB as vector store for knowledge base
- Document processing with embeddings
- Vector similarity search for knowledge retrieval
- Chunking and metadata management
- Compatible with existing Mem0/ChromaDB workflow

**Tools Integration**:
- Complete MongoDB tools suite (CRUD operations)
- Vector search and text search capabilities
- Collection management and indexing
- Production-ready connection handling
- Error handling and logging

**Examples**:
- Memory provider example with persistent storage
- Knowledge store example with vector search
- Tools integration example with database operations
- Comprehensive business scenario simulation
- Complete documentation and best practices

**Technical Details**:
- Optional dependency: pip install ''praisonaiagents[mongodb]''
- Backward compatibility maintained
- Supports both local MongoDB and Atlas
- Configurable connection pooling and timeouts
- Comprehensive error handling

Resolves #992

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-18 14:02:34 +00:00
Mervin Praison b2cacb243f Merge pull request #991 from MervinPraison/claude/issue-981-20250718-1032
fix: implement real-time streaming for Agent.start() method
2025-07-18 15:01:30 +01:00
github-actions[bot]andMervin Praison 9e25572c84 fix: resolve critical issues in streaming implementation
- Fix chat_history_length initialization bug in both streaming methods
- Add safe array access checks for chunk.choices in OpenAI client
- Add JSON parsing error handling for malformed tool arguments
- Remove unused imports and variables
- Update test files to use mock LLM instead of real APIs
- Ensure robust error handling throughout streaming pipeline

These fixes address all critical issues identified by code review bots
while maintaining backward compatibility and streaming functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 13:53:45 +00:00
Mervin Praison 801aedc00a Merge pull request #989 from MervinPraison/claude/issue-988-20250718-1318
fix: Add tool usage time tracking to MCP execution
2025-07-18 14:50:48 +01:00
github-actions[bot]andMervin Praison f18103f66b fix: Add thread safety and fix initialization timeout bug in tool timing
- Add thread locks to MCPToolRunner._tool_timings and MinimalTelemetry._metrics
- Fix initialization timeout bug where telemetry tracking was bypassed
- Move timing measurement after initialization check for accuracy
- Ensure telemetry is captured for all tool execution outcomes

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 13:48:22 +00:00
Mervin Praison a75745729b Merge pull request #986 from MervinPraison/claude/issue-985-20250717-1851
fix: resolve vertex_ai parameter compatibility issue for image generation
2025-07-18 14:29:00 +01:00
github-actions[bot]andMervin Praison 2bc61a1d55 fix: Add tool usage time tracking to MCP execution
- Enhanced telemetry.track_tool_usage() to accept execution_time parameter
- Added timing instrumentation to MCPToolRunner.call_tool() method
- Tool usage timing is now captured and stored in telemetry metrics
- Maintains backward compatibility with no breaking changes
- Addresses issue #988 where tool usage time was always 0

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 13:24:36 +00:00
github-actions[bot]andMervin Praison a3877c34e5 improve: enhance image agent parameter filtering robustness
- Replace overly broad exception handling with specific exceptions
- Consolidate duplicated gemini parameter filtering logic
- Add debug logging for provider detection failures
- Improve code readability with better variable separation
- Maintain backward compatibility and safety mechanisms

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 10:42:15 +00:00
github-actions[bot]andMervin Praison ff1022bdb6 fix: implement real-time streaming for Agent.start() method
- Add streaming generator support to Agent.start() method
- Implement _start_stream() method for streaming logic
- Add _chat_stream() method to route streaming to appropriate handlers
- Add _custom_llm_stream() for custom LLM streaming support
- Add _openai_stream() for OpenAI client streaming support
- Add chat_completion_with_tools_stream() method to OpenAI client
- Maintain backward compatibility for existing code
- Add proper error handling and chat history management

Fixes #981

Co-authored-by: Mervin Praison <[email protected]>
2025-07-18 10:40:49 +00:00
github-actions[bot]andClaude 22e9904aef fix: resolve agent termination issue by properly cleaning up telemetry system
- Add proper cleanup in Agent.start() and Agent.astart() methods using try-finally blocks
- Implement _cleanup_telemetry() method to ensure telemetry system shutdown
- Prevent hanging after agent execution by properly shutting down background threads
- Add test files to verify the termination fix works correctly

Fixes #987

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
2025-07-18 10:36:19 +00:00
Mervin Praison e5b7f7b740 Merge pull request #983 from Dhivya-Bharathy/models-providers-reorganization
feat: Add comprehensive provider examples for all major LiteLLM providers with clean API structure
2025-07-18 11:30:39 +01:00
github-actions[bot]andMervin Praison 3a137fb71e fix: resolve vertex_ai parameter compatibility issue for image generation
- Add provider-aware parameter filtering for image generation
- Filter out unsupported ''style'' parameter for vertex_ai models
- Maintain backward compatibility for other providers (OpenAI, Gemini)
- Add drop_params=True safety mechanism
- Resolves litellm.UnsupportedParamsError for gemini-2.0-flash-preview-image-generation

Co-authored-by: Mervin Praison <[email protected]>
2025-07-17 19:01:41 +00:00
Dhivya-Bharathy 8e09f5e4b4 Add comprehensive model examples with 5 agents per model: Llama, Claude, GPT 2025-07-17 13:46:49 +05:30
Dhivya-Bharathy 82e6673488 Add 3 more provider examples: Moonshot AI, Databricks, AI21 2025-07-17 13:17:30 +05:30
Dhivya-Bharathy dd9b24597a Add comprehensive provider examples for all major LiteLLM providers 2025-07-17 13:01:08 +05:30
Mervin Praison dbd6062edf chore: bump praisonai to v2.2.70 and praisonaiagents to v0.0.142 across all files 2025-07-17 01:31:56 +01:00
Mervin Praison 7fc615ff70 Merge pull request #974 from MervinPraison/claude/pr-965-20250716-2356
fix: make TelemetryCollector.stop() consistent with shutdown behavior
2025-07-17 01:24:26 +01:00
Mervin Praison fabac466f9 Merge pull request #966 from MervinPraison/claude/issue-964-20250716-2340
fix: improve Ollama sequential tool execution to prevent redundant calls
2025-07-17 01:22:11 +01:00
github-actions[bot]andMervin Praison 8d98f27f9c fix: correct Ollama early stopping logic placement for sequential tool execution
Fixes critical issues identified in PR review:
- Move early stopping logic from inside ''if tool_calls:'' block to ''else:'' block
- Fix logic placement in both sync and async execution paths
- Remove unreachable code in _generate_ollama_tool_summary()
- Ensure early stopping triggers correctly when Ollama returns empty responses after tool execution

This ensures the fix properly handles the Ollama edge case where it returns
empty responses after successful tool execution, preventing infinite loops
while allowing proper sequential tool calls.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-17 00:02:48 +00:00
github-actions[bot]andMervin Praison cf0af4999d fix: make TelemetryCollector.stop() consistent with shutdown behavior
Ensures TelemetryCollector.stop() calls shutdown() instead of just flush()
for consistency with the main atexit handler and proper PostHog session cleanup.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-17 00:01:42 +00:00
github-actions[bot]andMervin Praison f6ecd3b4db fix: improve Ollama early stopping logic to allow proper sequential tool execution
- Only trigger early stopping when no new tool calls are present in current response
- Prevents premature termination during sequential tool calls like get_stock_price -> multiply
- Maintains backward compatibility while fixing issue #964
- Applied to both sync and async execution paths

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:56:48 +00:00
Mervin Praison a348d186c3 Merge pull request #965 from MervinPraison/claude/issue-963-20250716-2334
fix: properly shutdown telemetry aiohttp sessions to prevent ImportError during Python shutdown
2025-07-17 00:54:16 +01:00
Mervin Praison 30c0f2458f Merge pull request #961 from MervinPraison/claude/issue-958-20250716-2304
fix: ensure consistent Task/Response formatting across all LLM providers
2025-07-17 00:53:53 +01:00
Mervin Praison 668d61344f Merge branch 'main' into claude/issue-958-20250716-2304 2025-07-17 00:47:55 +01:00
github-actions[bot]andMervin Praison e8e9625f22 fix: improve Ollama sequential tool execution to prevent redundant calls
- Add early stopping logic for Ollama after first successful tool execution
- Enhance tool summary generation to create more natural, OpenAI-like responses
- Apply fix to both sync and async execution paths
- Prevent multiple tool calls when execution is complete
- Maintains full backward compatibility

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:47:43 +00:00
Mervin Praison cc721ba0db feat: upgrade praisonai to v2.2.69 and add realtime streaming support 2025-07-17 00:45:38 +01:00
Mervin Praison 07a30ab33d Merge pull request #962 from MervinPraison/claude/issue-956-20250716-2316
fix: enable real-time streaming regardless of verbose setting
2025-07-17 00:41:34 +01:00
github-actions[bot]andMervin Praison 92978fdb1c fix: properly shutdown telemetry aiohttp sessions to prevent ImportError during Python shutdown
- Change atexit handler from flush() to shutdown() in telemetry/__init__.py
- This ensures PostHog aiohttp sessions are properly closed before Python shutdown
- Prevents ''ImportError: sys.meta_path is None, Python is likely shutting down'' error
- Maintains backward compatibility with existing functionality

Fixes #963

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:40:40 +00:00
github-actions[bot]andMervin Praison 15a307dd4e fix: complete streaming fix for all LLM providers and improve console handling
- Fix custom LLM streaming (Gemini/Vertex AI) in llm.py by removing verbose condition
- Improve console parameter consistency when streaming is enabled
- Ensures streaming works for stream=True regardless of verbose setting
- Maintains full backward compatibility

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:38:27 +00:00
Mervin Praison 053ad043b8 Merge pull request #960 from MervinPraison/claude/issue-940-20250716-2252
fix: accumulate tool results across iterations in Ollama sequential execution
2025-07-17 00:35:49 +01:00
github-actions[bot]andMervin Praison 6e664995e8 fix: add missing flag reset in async achat method for consistent display behavior
- Added _final_display_shown = False reset in achat() method to match chat() method
- Ensures proper display flag management for both sync and async agent interactions
- Maintains consistency across all agent conversation entry points

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:32:12 +00:00
github-actions[bot]andMervin Praison ccb1bccf0f chore: remove unused custom_display_fn from agent.py
- Removed unused custom_display_fn function that was defined but never called
- Cleanup after streaming fix to avoid confusion
- No functional changes, streaming fix remains intact

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:31:38 +00:00
Mervin Praison 94c8fc21e6 Merge pull request #959 from MervinPraison/claude/issue-947-20250716-2244
fix: resolve task_name undefined error in async execution
2025-07-17 00:29:30 +01:00
github-actions[bot]andMervin Praison 48575b4ee0 fix: ensure callback execution in all achat() paths
- Enhanced _chat_completion method signature to accept task metadata parameters
- Updated all calls to _chat_completion to pass task context
- Fixed NameError where task_name, task_description, task_id were undefined
- Updated _apply_guardrail_with_retry to propagate task metadata
- Ensures complete task metadata propagation in sync chat execution path

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:28:05 +00:00
github-actions[bot]andMervin Praison 2ce7136d0c refactor: improve Ollama sequential fix with better test and deduplication
- Enhanced test file with comprehensive behavioral validation
- Added proper module import handling and mock-based testing
- Extracted duplicated logic into _handle_ollama_sequential_logic helper
- Eliminated 18-line code duplication between sync and async methods
- Improved maintainability while preserving backward compatibility

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:27:36 +00:00
github-actions[bot]andMervin Praison 3bea2ffb10 fix: enable real-time streaming regardless of verbose setting
- Remove dependency on verbose=True for streaming display function
- Allow stream=True to work with verbose=False for real-time output
- Maintain backward compatibility with existing verbose=True usage
- Fixes issue where Gemini/Google Vertex AI streaming was buffered

Resolves #956

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:22:35 +00:00
Mervin Praison fcbd915a0e docs: move star history section above feature roadmap for better visibility 2025-07-17 00:21:52 +01:00
github-actions[bot]andMervin Praison ae7eb29684 fix: ensure callback execution in all achat() paths
Adds missing callback execution to tool-based responses and JSON/Pydantic
output paths in the achat() method to ensure consistent task metadata
propagation across all execution flows.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:19:57 +00:00
Mervin Praison c5ec1ffea0 refactor: move test files to src/praisonai-agents directory for better organization 2025-07-17 00:19:56 +01:00
Mervin Praison aad5f286e2 chore: bump PraisonAI to v2.2.68 and PraisonAI Agents to v0.0.140 2025-07-17 00:19:32 +01:00
github-actions[bot]andMervin Praison 1aa20de337 fix: ensure consistent Task/Response formatting across all LLM providers
- Modified display condition to show formatted output for both OpenAI and custom LLM providers (like Gemini)
- Added _final_display_shown flag to prevent duplicate displays within same execution
- Reset flag at start of each chat() call for new conversations
- Maintains backward compatibility and existing verbose control behavior

Fixes #958

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:15:08 +00:00
Mervin Praison 3bc185d15d Merge pull request #957 from MervinPraison/claude/comprehensive-task-name-fix-20250716
fix: comprehensive task_name undefined error resolution
2025-07-17 00:14:20 +01:00
github-actions[bot]andMervin Praison 6b8a7110f2 refactor: simplify redundant hasattr checks and fix test formatting
- Remove redundant hasattr checks in agent.py as getattr handles missing attributes
- Fix unnecessary f-string prefixes in test file
- Remove unused variable assignment in test

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:08:04 +00:00
Mervin Praison 5ab0f245c5 Merge branch 'main' of https://github.com/MervinPraison/PraisonAI 2025-07-17 00:03:23 +01:00
Mervin Praison 5baa3f9b96 fix: update LLM model in Ollama sequential agent from qwen3 to llama3.2 for improved performance 2025-07-17 00:03:04 +01:00
github-actions[bot]andMervin Praison 28c6d026f7 fix: resolve task_name undefined error in async execution
Addresses issue #947 where task_name undefined errors occurred during async parallel task execution.

Key Changes:
- Enhanced Agent.achat() method to accept task context parameters (task_name, task_description, task_id)
- Updated aexecute_task() to pass task metadata when calling agent.achat()
- Added callback execution to both async execution paths in achat() method
- Updated callback system to properly propagate task context

Root Cause: Async execution path was missing task metadata propagation to LLM callbacks.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 23:00:34 +00:00
github-actions[bot]andMervin Praison 802716e5ec fix: comprehensive task_name undefined error resolution
- Add task_name, task_description, task_id parameters to achat method signature
- Update all achat call sites in agents.py to pass task parameters where available
- Provide fallback None values for API handler contexts without task context
- Fix Agent.aexecute to extract task info when available
- Resolves NameError: name ''task_name'' is not defined in parallel agent execution

Fixes persistent errors in agentic parallelization workflows where callbacks
expect task context parameters but they were not being passed correctly through
the async execution chain.

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 22:58:16 +00:00
github-actions[bot]andMervin Praison 9eaa2731da fix: accumulate tool results across iterations in Ollama sequential execution
- After successful tool execution, give Ollama LLM explicit prompt for final answer
- Only generate tool summary if LLM fails to respond to explicit prompt
- Prevents premature tool summary generation after sequential execution
- Applied to both sync and async tool execution loops
- Preserves infinite loop prevention while enabling natural responses

Fixes #940

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 22:56:02 +00:00
Mervin Praison 0d3e1632a3 Merge pull request #949 from Dhivya-Bharathy/models-providers-reorganization
Reorganize models and providers structure with comprehensive examples
2025-07-16 23:45:08 +01:00
Mervin Praison da14b1d8e9 Merge pull request #955 from MervinPraison/claude/pr-953-20250716-1258
fix: resolve Ollama infinite loop issue with minimal changes
2025-07-16 16:02:48 +01:00
Mervin Praison c0679f4528 Merge pull request #954 from MervinPraison/claude/issue-940-20250716-1247
fix: prevent premature termination in Ollama sequential tool execution
2025-07-16 15:18:42 +01:00
github-actions[bot]andMervin Praison 930555d921 fix: accumulate tool results across iterations in Ollama sequential execution
- Replace hardcoded iteration threshold (3) with class constant OLLAMA_SUMMARY_ITERATION_THRESHOLD
- Add proper constant definition for better maintainability and readability
- Applied consistently to both sync and async methods
- Addresses reviewer feedback about magic numbers
- Ensures proper tool result accumulation across iterations

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 13:24:35 +00:00
Mervin Praison 859fb6a263 chore: bump praisonai to v2.2.67 and praisonaiagents to v0.0.138 across all files 2025-07-16 14:16:30 +01:00
github-actions[bot]andMervin Praison f154dba804 fix: accumulate tool results across iterations in Ollama sequential execution
- Add accumulated_tool_results list to store tool results across all iterations
- Prevents loss of tool execution history when generating summaries
- Applied to both sync (get_response) and async (get_response_async) methods
- Maintains backward compatibility with existing functionality
- Fixes issue where only current iteration''s tool results were available to summary generation

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 13:15:14 +00:00
Mervin Praison fb2a16126f Merge pull request #952 from MervinPraison/claude/issue-947-20250716-1205
fix: resolve task_name undefined error in LLM callback execution
2025-07-16 14:14:52 +01:00
github-actions[bot] ddb547f6a5 cleanup: remove debug test files 2025-07-16 13:08:03 +00:00
github-actions[bot]andMervin Praison 24437b40d9 fix: resolve Ollama infinite loop issue with minimal changes
- Remove redundant length check in _generate_ollama_tool_summary
- Simplify verbose conditional checks in both sync and async methods
- Add safety checks to prevent infinite loops after 5 iterations
- Maintain backward compatibility with other LLM providers
- Add comprehensive test suite to validate fixes

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 13:07:39 +00:00
Dhivya-Bharathy 582049f759 Update model examples to use new clean format with praisonaiagents.Agent 2025-07-16 18:32:35 +05:30
github-actions[bot]andMervin Praison e1b93000be fix: prevent premature termination in Ollama sequential tool execution
- Modified tool execution logic to allow at least 3 iterations before triggering summary generation
- This prevents Ollama from prematurely terminating when sequential tool calls are needed
- Applied fix to both conversation() and reasoning() methods in LLM class
- Resolves issue where Ollama would generate tool summaries too early, blocking proper tool chaining

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Mervin Praison <[email protected]>
2025-07-16 12:51:13 +00:00
github-actions[bot]andMervin Praison 318ececfb4 fix: correct function name in test_fix_verification.py
- Fixed unused variable 'results' in test_fix_verification.py
- Maintained test functionality while removing static analysis warning

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 12:35:26 +00:00
github-actions[bot]andMervin Praison 83afc0891f fix: correct function name in test_fix_verification.py
- Changed register_callback to register_display_callback
- Ensures test runs correctly and validates the fix

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 12:29:42 +00:00
Dhivya-Bharathy 8dd6494a59 Update all provider examples to use new clean format with praisonaiagents.Agent 2025-07-16 17:54:50 +05:30
github-actions[bot]andMervin Praison 857238a82a fix: resolve task_name undefined error in LLM callback execution
- Fixed 6 direct execute_sync_callback calls missing task-related parameters
- Added agent_name, agent_role, agent_tools, task_name, task_description, task_id
- Ensures consistent callback parameter passing between verbose and non-verbose modes
- Prevents infinite loop errors when callbacks expect task_name parameter
- Maintains backward compatibility with existing functionality

Fixes #947

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2025-07-16 12:21:27 +00:00
Dhivya-Bharathy 1ab09fa259 Reorganize models and providers structure - Move Kimi2 agents to models/kimi with kimi_ prefix - Move Grok4 agents to models/grok with grok_ prefix - Create 10 new Groq agents in models/groq with groq_ prefix - Add provider examples for OpenAI, Anthropic, Google, DeepSeek, Mistral, Cohere, xAI - Remove README files as requested 2025-07-16 16:35:00 +05:30
Dhivya-Bharathy f7f4a6c0bf Add 10 new agents to Kimi2_Intelligent_Agents (aerospace, bioinformatics, blockchain, energy, IoT, metaverse, nanotechnology, quantum AI, quantum computing, robotics) 2025-07-16 15:02:46 +05:30
232 changed files with 20862 additions and 944 deletions

No files matched your search

+1
View File
@@ -11,6 +11,7 @@ on:
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
packages: write
+1
View File
@@ -10,6 +10,7 @@ jobs:
test:
name: Run tests and collect coverage
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
+1
View File
@@ -26,6 +26,7 @@ on:
jobs:
comprehensive-test:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
python-version: ["3.11"]
+1
View File
@@ -9,6 +9,7 @@ on:
jobs:
test-core:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
python-version: [3.11]
+12 -1
View File
@@ -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
+1
View File
@@ -24,6 +24,7 @@ on:
jobs:
framework-tests:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
python-version: [3.11]
+1
View File
@@ -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:
+1
View File
@@ -5,6 +5,7 @@ on: [push, pull_request]
jobs:
quick-test:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
+4 -4
View File
@@ -120,6 +120,10 @@ agent.start('Write a movie script about a robot in Mars');
![PraisonAI CLI Demo](docs/demo/praisonai-cli-demo.gif)
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=MervinPraison/PraisonAI&type=Date)](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
[![Star History Chart](https://api.star-history.com/svg?repos=MervinPraison/PraisonAI&type=Date)](https://docs.praison.ai)
## Video Tutorials
| Topic | Video |
+1 -1
View File
@@ -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.76" \
"praisonai[api]" \
gunicorn \
markdown
+1 -1
View File
@@ -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.76" \
"praisonai[chat]" \
"embedchain[github,youtube]"
+1 -1
View File
@@ -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.76" \
"praisonai[ui]" \
"praisonai[chat]" \
"praisonai[realtime]" \
+1 -1
View File
@@ -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.76" \
"praisonai[ui]" \
"praisonai[crewai]"
+2 -2
View File
@@ -121,7 +121,7 @@ healthcheck:
## 📦 Package Versions
All Docker images use consistent, up-to-date versions:
- PraisonAI: `>=2.2.66`
- PraisonAI: `>=2.2.76`
- 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.76" "praisonaiagents==0.0.92"
```
## 🌐 Production Deployment
+231
View File
@@ -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?")
@@ -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)
@@ -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?")
@@ -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?")
@@ -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?")
@@ -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?")
@@ -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?")
+315
View File
@@ -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.
+20
View File
@@ -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 232 files, more files were not shown because too many files have changed in this diff. Show more