Compare commits

...
7 Commits
Author SHA1 Message Date
Mervin Praison e717b625d2 Merge pull request #262 from MervinPraison/develop
Develop
2025-01-05 07:39:54 +00:00
MervinPraison 84638ad759 praisonai ui working 2025-01-05 07:39:36 +00:00
MervinPraison 626bc963f7 ui working together 2025-01-05 07:11:06 +00:00
Mervin Praison efa7110b38 Merge pull request #260 from MervinPraison/develop
adding async callbacks
2025-01-05 00:20:03 +00:00
MervinPraison 539c10796c adding ui with instant messages working 2025-01-05 00:19:26 +00:00
MervinPraison 815becb6f2 async callback fixes for main display 2025-01-04 22:48:21 +00:00
MervinPraison 87790695db adding async callbacks 2025-01-04 17:19:19 +00:00
20 changed files with 2434 additions and 537 deletions

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install flask praisonai==2.0.24 gunicorn markdown
RUN pip install flask praisonai==2.0.26 gunicorn markdown
EXPOSE 8080
CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]
+2 -10
View File
@@ -7,12 +7,8 @@ roles:
information.
goal: Gather information about Mars and cats
role: Researcher
llm:
model: "gpt-4o"
reflect_llm:
model: "gpt-4o"
min_reflect: 2
max_reflect: 4
min_reflect: 1
max_reflect: 2
tasks:
research_task:
description: Research about Mars, its environment, and the feasibility of
@@ -25,8 +21,6 @@ roles:
stories.
goal: Create a story concept for a movie about a cat in Mars
role: Narrative Designer
llm:
model: "gpt-4o"
tasks:
story_concept_development:
description: Use the research findings to craft a unique story concept for
@@ -39,8 +33,6 @@ roles:
scripts.
goal: Write a movie script about a cat in Mars
role: Scriptwriter
llm:
model: "gpt-4o"
tasks:
scriptwriting_task:
description: Turn the story concept into a production-ready movie script,
+9 -1
View File
@@ -1,4 +1,12 @@
from praisonaiagents import Agent, Task, PraisonAIAgents, error_logs, register_display_callback
from praisonaiagents import (
Agent,
Task,
PraisonAIAgents,
error_logs,
register_display_callback,
sync_display_callbacks,
async_display_callbacks
)
from duckduckgo_search import DDGS
from rich.console import Console
import json
+4 -2
View File
@@ -17,7 +17,8 @@ from .main import (
clean_triple_backticks,
error_logs,
register_display_callback,
display_callbacks,
sync_display_callbacks,
async_display_callbacks,
)
__all__ = [
@@ -35,5 +36,6 @@ __all__ = [
'clean_triple_backticks',
'error_logs',
'register_display_callback',
'display_callbacks',
'sync_display_callbacks',
'async_display_callbacks',
]
+164 -21
View File
@@ -12,6 +12,7 @@ from rich.text import Text
from rich.markdown import Markdown
from rich.logging import RichHandler
from rich.live import Live
import asyncio
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
@@ -25,12 +26,49 @@ logging.basicConfig(
# Global list to store error logs
error_logs = []
# Global callback registry
display_callbacks = {}
# Separate registries for sync and async callbacks
sync_display_callbacks = {}
async_display_callbacks = {}
def register_display_callback(display_type: str, callback_fn):
"""Register a callback function for a specific display type."""
display_callbacks[display_type] = callback_fn
# At the top of the file, add display_callbacks to __all__
__all__ = [
'error_logs',
'register_display_callback',
'sync_display_callbacks',
'async_display_callbacks',
# ... other exports
]
def register_display_callback(display_type: str, callback_fn, is_async: bool = False):
"""Register a synchronous or asynchronous callback function for a specific display type.
Args:
display_type (str): Type of display event ('interaction', 'self_reflection', etc.)
callback_fn: The callback function to register
is_async (bool): Whether the callback is asynchronous
"""
if is_async:
async_display_callbacks[display_type] = callback_fn
else:
sync_display_callbacks[display_type] = callback_fn
async def execute_callback(display_type: str, **kwargs):
"""Execute both sync and async callbacks for a given display type.
Args:
display_type (str): Type of display event
**kwargs: Arguments to pass to the callback functions
"""
# Execute synchronous callback if registered
if display_type in sync_display_callbacks:
callback = sync_display_callbacks[display_type]
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, lambda: callback(**kwargs))
# Execute asynchronous callback if registered
if display_type in async_display_callbacks:
callback = async_display_callbacks[display_type]
await callback(**kwargs)
def _clean_display_content(content: str, max_length: int = 20000) -> str:
"""Helper function to clean and truncate content for display."""
@@ -53,11 +91,10 @@ def _clean_display_content(content: str, max_length: int = 20000) -> str:
return content.strip()
def display_interaction(message, response, markdown=True, generation_time=None, console=None):
"""Display the interaction between user and assistant."""
"""Synchronous version of display_interaction."""
if console is None:
console = Console()
# Handle multimodal content (list)
if isinstance(message, list):
text_content = next((item["text"] for item in message if item["type"] == "text"), "")
message = text_content
@@ -65,16 +102,16 @@ def display_interaction(message, response, markdown=True, generation_time=None,
message = _clean_display_content(str(message))
response = _clean_display_content(str(response))
# Execute callback if registered
if 'interaction' in display_callbacks:
display_callbacks['interaction'](
# Execute synchronous callback if registered
if 'interaction' in sync_display_callbacks:
sync_display_callbacks['interaction'](
message=message,
response=response,
markdown=markdown,
generation_time=generation_time
)
# Existing display logic...
# Rest of the display logic...
if generation_time:
console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
@@ -93,8 +130,8 @@ def display_self_reflection(message: str, console=None):
message = _clean_display_content(str(message))
# Execute callback if registered
if 'self_reflection' in display_callbacks:
display_callbacks['self_reflection'](message=message)
if 'self_reflection' in sync_display_callbacks:
sync_display_callbacks['self_reflection'](message=message)
console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
@@ -106,8 +143,8 @@ def display_instruction(message: str, console=None):
message = _clean_display_content(str(message))
# Execute callback if registered
if 'instruction' in display_callbacks:
display_callbacks['instruction'](message=message)
if 'instruction' in sync_display_callbacks:
sync_display_callbacks['instruction'](message=message)
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
@@ -119,8 +156,8 @@ def display_tool_call(message: str, console=None):
message = _clean_display_content(str(message))
# Execute callback if registered
if 'tool_call' in display_callbacks:
display_callbacks['tool_call'](message=message)
if 'tool_call' in sync_display_callbacks:
sync_display_callbacks['tool_call'](message=message)
console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
@@ -132,8 +169,8 @@ def display_error(message: str, console=None):
message = _clean_display_content(str(message))
# Execute callback if registered
if 'error' in display_callbacks:
display_callbacks['error'](message=message)
if 'error' in sync_display_callbacks:
sync_display_callbacks['error'](message=message)
console.print(Panel.fit(Text(message, style="bold red"), title="Error", border_style="red"))
error_logs.append(message)
@@ -150,8 +187,114 @@ def display_generating(content: str = "", start_time: Optional[float] = None):
content = _clean_display_content(str(content))
# Execute callback if registered
if 'generating' in display_callbacks:
display_callbacks['generating'](
if 'generating' in sync_display_callbacks:
sync_display_callbacks['generating'](
content=content,
elapsed_time=elapsed_str.strip() if elapsed_str else None
)
return Panel(Markdown(content), title=f"Generating...{elapsed_str}", border_style="green")
# Async versions with 'a' prefix
async def adisplay_interaction(message, response, markdown=True, generation_time=None, console=None):
"""Async version of display_interaction."""
if console is None:
console = Console()
if isinstance(message, list):
text_content = next((item["text"] for item in message if item["type"] == "text"), "")
message = text_content
message = _clean_display_content(str(message))
response = _clean_display_content(str(response))
# Execute callbacks
await execute_callback(
'interaction',
message=message,
response=response,
markdown=markdown,
generation_time=generation_time
)
# Rest of the display logic...
if generation_time:
console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
if markdown:
console.print(Panel.fit(Markdown(message), title="Message", border_style="cyan"))
console.print(Panel.fit(Markdown(response), title="Response", border_style="cyan"))
else:
console.print(Panel.fit(Text(message, style="bold green"), title="Message", border_style="cyan"))
console.print(Panel.fit(Text(response, style="bold blue"), title="Response", border_style="cyan"))
async def adisplay_self_reflection(message: str, console=None):
"""Async version of display_self_reflection."""
if not message or not message.strip():
return
if console is None:
console = Console()
message = _clean_display_content(str(message))
if 'self_reflection' in async_display_callbacks:
await async_display_callbacks['self_reflection'](message=message)
console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
async def adisplay_instruction(message: str, console=None):
"""Async version of display_instruction."""
if not message or not message.strip():
return
if console is None:
console = Console()
message = _clean_display_content(str(message))
if 'instruction' in async_display_callbacks:
await async_display_callbacks['instruction'](message=message)
console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
async def adisplay_tool_call(message: str, console=None):
"""Async version of display_tool_call."""
if not message or not message.strip():
return
if console is None:
console = Console()
message = _clean_display_content(str(message))
if 'tool_call' in async_display_callbacks:
await async_display_callbacks['tool_call'](message=message)
console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
async def adisplay_error(message: str, console=None):
"""Async version of display_error."""
if not message or not message.strip():
return
if console is None:
console = Console()
message = _clean_display_content(str(message))
if 'error' in async_display_callbacks:
await async_display_callbacks['error'](message=message)
console.print(Panel.fit(Text(message, style="bold red"), title="Error", border_style="red"))
error_logs.append(message)
async def adisplay_generating(content: str = "", start_time: Optional[float] = None):
"""Async version of display_generating."""
if not content or not str(content).strip():
return Panel("", title="", border_style="green")
elapsed_str = ""
if start_time is not None:
elapsed = time.time() - start_time
elapsed_str = f" {elapsed:.1f}s"
content = _clean_display_content(str(content))
if 'generating' in async_display_callbacks:
await async_display_callbacks['generating'](
content=content,
elapsed_time=elapsed_str.strip() if elapsed_str else None
)
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "praisonaiagents"
version = "0.0.18"
version = "0.0.20"
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
authors = [
{ name="Mervin Praison" }
+1 -1
View File
@@ -186,7 +186,7 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.18"
version = "0.0.20"
source = { editable = "." }
dependencies = [
{ name = "openai" },
-1
View File
@@ -1 +0,0 @@
# Welcome to Praison AI! 🚀🤖
+1 -1
View File
@@ -110,7 +110,7 @@ Loads environment variables from .env file or system and sets them.</p>
file.write(&#34;FROM python:3.11-slim\n&#34;)
file.write(&#34;WORKDIR /app\n&#34;)
file.write(&#34;COPY . .\n&#34;)
file.write(&#34;RUN pip install flask praisonai==2.0.24 gunicorn markdown\n&#34;)
file.write(&#34;RUN pip install flask praisonai==2.0.26 gunicorn markdown\n&#34;)
file.write(&#34;EXPOSE 8080\n&#34;)
file.write(&#39;CMD [&#34;gunicorn&#34;, &#34;-b&#34;, &#34;0.0.0.0:8080&#34;, &#34;api:app&#34;]\n&#39;)
+277 -4
View File
@@ -40,8 +40,17 @@ icon: "bell"
print(f"Received message: {message}")
print(f"Got response: {response}")
# Register the callback
register_display_callback('interaction', simple_callback)
# Register as synchronous callback
register_display_callback('interaction', simple_callback, is_async=False)
# For async callbacks
async def async_simple_callback(message=None, response=None, **kwargs):
await asyncio.sleep(0) # Non-blocking pause
print(f"Received message: {message}")
print(f"Got response: {response}")
# Register as async callback
register_display_callback('interaction', async_simple_callback, is_async=True)
```
</Step>
@@ -75,6 +84,10 @@ icon: "bell"
</Step>
</Steps>
<Note>
PraisonAI Agents supports both synchronous and asynchronous callbacks. When registering a callback, specify `is_async=True` for async callbacks. Both sync and async callbacks for the same display type can be executed simultaneously.
</Note>
## Understanding Callbacks
<Card title="What are Callbacks?" icon="question">
@@ -124,8 +137,16 @@ logging.basicConfig(level=logging.INFO)
def log_callback(message=None, **kwargs):
logging.info(f"Agent message: {message}")
# Register callback
register_display_callback('interaction', log_callback)
# Register synchronous callback
register_display_callback('interaction', log_callback, is_async=False)
# Register asynchronous callback
async def async_log_callback(message=None, **kwargs):
await asyncio.sleep(0)
logging.info(f"Agent message: {message}")
# Register as async callback
register_display_callback('interaction', async_log_callback, is_async=True)
```
```python Advanced
@@ -475,8 +496,260 @@ def main():
if __name__ == "__main__":
main()
## Async Callbacks
Async callbacks allow you to handle events asynchronously, which is particularly useful for long-running operations or when dealing with multiple agents simultaneously.
### Basic Async Callback Implementation
```python
import asyncio
from praisonaiagents import register_display_callback
async def async_interaction_callback(message=None, response=None, **kwargs):
"""Async callback for handling interactions"""
await asyncio.sleep(0) # Non-blocking pause
print(f"Async processing - Message: {message}")
print(f"Response: {response}")
# Register the async callback
register_display_callback('interaction', async_interaction_callback)
```
### Complete Async Example
```python
import asyncio
from praisonaiagents import Agent, Task, PraisonAIAgents, register_display_callback
import logging
from datetime import datetime
# Setup async logging
async def setup_async_logging():
logging.basicConfig(
filename='async_ai_interactions.log',
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Async callbacks
async def async_interaction_callback(message=None, response=None, markdown=None, generation_time=None):
await asyncio.sleep(0)
logging.info(f"""
=== ASYNC INTERACTION ===
Time: {datetime.now()}
Generation Time: {generation_time}s
Message: {message}
Response: {response}
""")
async def async_error_callback(message=None):
await asyncio.sleep(0)
logging.error(f"""
=== ASYNC ERROR ===
Time: {datetime.now()}
Message: {message}
""")
# Register async callbacks
register_display_callback('interaction', async_interaction_callback, is_async=True)
register_display_callback('error', async_error_callback, is_async=True)
# Create async task callback
async def async_task_callback(output):
await asyncio.sleep(0)
logging.info(f"""
=== ASYNC TASK COMPLETED ===
Time: {datetime.now()}
Description: {output.description}
Agent: {output.agent}
Output: {output.raw[:200]}...
""")
async def main():
await setup_async_logging()
# Create agent with async capabilities
agent = Agent(
name="AsyncAgent",
role="Assistant",
goal="Demonstrate async callbacks",
backstory="I am an async-capable assistant",
verbose=True
)
# Create task with async callback
task = Task(
name="async_task",
description="Demonstrate async callbacks",
agent=agent,
expected_output="Async demonstration complete",
callback=async_task_callback
)
# Create and run agents with async support
agents = PraisonAIAgents(
agents=[agent],
tasks=[task],
verbose=True
)
await agents.start_async()
if __name__ == "__main__":
asyncio.run(main())
```
### Available Async Functions
PraisonAI Agents provides several async versions of display functions, prefixed with 'a'. Here's the complete list:
<CardGroup cols={2}>
<Card title="adisplay_interaction" icon="comments">
```python
async def adisplay_interaction(
message,
response,
markdown=True,
generation_time=None,
console=None
)
```
Async version of display_interaction for showing conversations.
</Card>
<Card title="adisplay_self_reflection" icon="brain">
```python
async def adisplay_self_reflection(
message: str,
console=None
)
```
Async version for displaying agent's self-reflection moments.
</Card>
<Card title="adisplay_instruction" icon="list-check">
```python
async def adisplay_instruction(
message: str,
console=None
)
```
Async version for showing instructions.
</Card>
<Card title="adisplay_tool_call" icon="wrench">
```python
async def adisplay_tool_call(
message: str,
console=None
)
```
Async version for displaying tool calls.
</Card>
<Card title="adisplay_error" icon="triangle-exclamation">
```python
async def adisplay_error(
message: str,
console=None
)
```
Async version for error messages.
</Card>
<Card title="adisplay_generating" icon="spinner">
```python
async def adisplay_generating(
content: str = "",
start_time: Optional[float] = None
)
```
Async version for showing generation progress.
</Card>
</CardGroup>
#### Example Usage
```python
import asyncio
from praisonaiagents import (
adisplay_interaction,
adisplay_error,
adisplay_tool_call
)
async def main():
# Display an interaction
await adisplay_interaction(
message="What's the weather?",
response="Let me check that for you.",
generation_time=0.5
)
# Display a tool call
await adisplay_tool_call(
"Calling weather API for location data..."
)
# Handle an error
try:
raise Exception("API connection failed")
except Exception as e:
await adisplay_error(str(e))
if __name__ == "__main__":
asyncio.run(main())
```
### Best Practices for Async Callbacks
<AccordionGroup>
<Accordion title="Error Handling">
Implement proper error handling in async callbacks:
```python
async def safe_async_callback(message=None, **kwargs):
try:
await asyncio.sleep(0)
# Process the callback
await process_message(message)
except Exception as e:
logging.error(f"Async callback error: {e}")
```
</Accordion>
<Accordion title="Performance">
Optimize async callbacks for better performance:
```python
async def optimized_async_callback(message=None, **kwargs):
# Use asyncio.gather for concurrent operations
results = await asyncio.gather(
process_message(message),
update_status(),
log_interaction(message)
)
return results
```
</Accordion>
</AccordionGroup>
### Key Considerations
<CardGroup cols={2}>
<Card title="Concurrency" icon="arrows-split-up-and-left">
- Use asyncio.gather for parallel operations
- Avoid blocking calls in async callbacks
- Handle concurrent access to shared resources
</Card>
<Card title="Resource Management" icon="server">
- Implement proper cleanup in async contexts
- Use async context managers when appropriate
- Monitor memory usage in long-running callbacks
</Card>
</CardGroup>
<Note>
When using async callbacks, ensure all dependent operations are also async-compatible to prevent blocking the event loop.
</Note>
## Troubleshooting
+1 -1
View File
@@ -3,7 +3,7 @@ class Praisonai < Formula
desc "AI tools for various AI applications"
homepage "https://github.com/MervinPraison/PraisonAI"
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/2.0.24.tar.gz"
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/2.0.26.tar.gz"
sha256 "1828fb9227d10f991522c3f24f061943a254b667196b40b1a3e4a54a8d30ce32" # Replace with actual SHA256 checksum
license "MIT"
+1 -1
View File
@@ -56,7 +56,7 @@ class CloudDeployer:
file.write("FROM python:3.11-slim\n")
file.write("WORKDIR /app\n")
file.write("COPY . .\n")
file.write("RUN pip install flask praisonai==2.0.24 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==2.0.26 gunicorn markdown\n")
file.write("EXPOSE 8080\n")
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
+575 -482
View File
File diff suppressed because it is too large. Load diff
+57
View File
@@ -0,0 +1,57 @@
import logging
from typing import Dict, Any, Callable, Optional, Union
import asyncio
logger = logging.getLogger(__name__)
class CallbackManager:
"""Manages callbacks for the PraisonAI UI"""
def __init__(self):
self._callbacks: Dict[str, Dict[str, Union[Callable, bool]]] = {}
def register(self, name: str, callback: Callable, is_async: bool = False) -> None:
"""Register a callback function"""
self._callbacks[name] = {
'func': callback,
'is_async': is_async
}
async def call(self, name: str, **kwargs) -> None:
"""Call a registered callback"""
if name not in self._callbacks:
logger.warning(f"No callback registered for {name}")
return
callback_info = self._callbacks[name]
func = callback_info['func']
is_async = callback_info['is_async']
try:
if is_async:
await func(**kwargs)
else:
if asyncio.iscoroutinefunction(func):
await func(**kwargs)
else:
await asyncio.get_event_loop().run_in_executor(None, lambda: func(**kwargs))
except Exception as e:
logger.error(f"Error in callback {name}: {str(e)}")
# Global callback manager instance
callback_manager = CallbackManager()
def register_callback(name: str, callback: Callable, is_async: bool = False) -> None:
"""Register a callback with the global callback manager"""
callback_manager.register(name, callback, is_async)
async def trigger_callback(name: str, **kwargs) -> None:
"""Trigger a callback from the global callback manager"""
await callback_manager.call(name, **kwargs)
# Decorator for registering callbacks
def callback(name: str, is_async: bool = False):
def decorator(func):
register_callback(name, func, is_async)
return func
return decorator
+474
View File
@@ -0,0 +1,474 @@
from praisonaiagents import Agent, Task, PraisonAIAgents
import os
import importlib
import inspect
import yaml
import logging
from .callbacks import trigger_callback
import asyncio
import chainlit as cl
from queue import Queue
logger = logging.getLogger(__name__)
agent_file = "agents.yaml"
with open(agent_file, 'r') as f:
config = yaml.safe_load(f)
topic = "get from the message content from the chainlit user message"
# Create a message queue
message_queue = Queue()
async def process_message_queue():
"""Process messages in the queue and send them to Chainlit"""
while True:
try:
if not message_queue.empty():
msg_data = message_queue.get()
await cl.Message(**msg_data).send()
await asyncio.sleep(0.1) # Small delay to prevent busy waiting
except Exception as e:
logger.error(f"Error processing message queue: {e}")
def load_tools_from_tools_py():
"""
Imports and returns all contents from tools.py file.
Also adds the tools to the global namespace.
Returns:
list: A list of callable functions with proper formatting
"""
tools_list = []
try:
# Try to import tools.py from current directory
spec = importlib.util.spec_from_file_location("tools", "tools.py")
logger.info(f"Spec: {spec}")
if spec is None:
logger.info("tools.py not found in current directory")
return tools_list
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Get all module attributes except private ones and classes
for name, obj in inspect.getmembers(module):
if (not name.startswith('_') and
callable(obj) and
not inspect.isclass(obj)):
# Add the function to global namespace
globals()[name] = obj
# Format the tool as an OpenAI function
tool = {
"type": "function",
"function": {
"name": name,
"description": obj.__doc__ or f"Function to {name.replace('_', ' ')}",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query to look up information about"
}
},
"required": ["query"]
}
}
}
# Add formatted tool to tools list
tools_list.append(tool)
logger.info(f"Loaded and globalized tool function: {name}")
logger.info(f"Loaded {len(tools_list)} tool functions from tools.py")
logger.info(f"Tools list: {tools_list}")
except Exception as e:
logger.warning(f"Error loading tools from tools.py: {e}")
return tools_list
async def step_callback(step_details):
"""Callback for agent steps"""
logger.info(f"[CALLBACK DEBUG] Step callback triggered with details: {step_details}")
try:
# Queue message for agent response
if step_details.get("response"):
message_queue.put({
"content": f"Agent Response: {step_details.get('response')}",
"author": step_details.get("agent_name", "Agent")
})
logger.info("[CALLBACK DEBUG] Queued agent response message")
# Queue message for tool usage
if step_details.get("tool_name"):
message_queue.put({
"content": f"🛠️ Using tool: {step_details.get('tool_name')}",
"author": "System"
})
logger.info("[CALLBACK DEBUG] Queued tool usage message")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in step callback: {str(e)}", exc_info=True)
async def task_callback(task_output):
"""Callback for task completion"""
logger.info(f"[CALLBACK DEBUG] Task callback triggered with output: {task_output}")
try:
# Create message content
if hasattr(task_output, 'raw'):
content = task_output.raw
elif hasattr(task_output, 'content'):
content = task_output.content
else:
content = str(task_output)
# Queue the message
message_queue.put({
"content": f"Task Output: {content}",
"author": "Task"
})
logger.info("[CALLBACK DEBUG] Queued task completion message")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in task callback: {str(e)}", exc_info=True)
async def step_callback_wrapper(step_details):
logger.info(f"[CALLBACK DEBUG] Step callback wrapper triggered with details: {step_details}")
try:
# Check if we have a Chainlit context
if not cl.context.context_var.get():
logger.warning("[CALLBACK DEBUG] No Chainlit context available in wrapper")
return
logger.info("[CALLBACK DEBUG] Chainlit context found in wrapper")
# Create a message for the agent's response
if step_details.get("response"):
logger.info(f"[CALLBACK DEBUG] Sending agent response from wrapper: {step_details.get('response')}")
try:
await cl.Message(
content=f"{role_name}: {step_details.get('response')}",
author=role_name,
).send()
logger.info("[CALLBACK DEBUG] Successfully sent agent response message from wrapper")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error sending agent response message from wrapper: {str(e)}")
# Create a message for any tool usage
if step_details.get("tool_name"):
logger.info(f"[CALLBACK DEBUG] Sending tool usage from wrapper: {step_details.get('tool_name')}")
try:
await cl.Message(
content=f"🛠️ {role_name} is using tool: {step_details.get('tool_name')}",
author="System",
).send()
logger.info("[CALLBACK DEBUG] Successfully sent tool usage message from wrapper")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error sending tool usage message from wrapper: {str(e)}")
# Create a message for any thoughts or reasoning
if step_details.get("thought"):
logger.info(f"[CALLBACK DEBUG] Sending thought from wrapper: {step_details.get('thought')}")
try:
await cl.Message(
content=f"💭 {role_name}'s thought: {step_details.get('thought')}",
author=role_name,
).send()
logger.info("[CALLBACK DEBUG] Successfully sent thought message from wrapper")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error sending thought message from wrapper: {str(e)}")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in step callback wrapper: {str(e)}", exc_info=True)
try:
await cl.Message(
content=f"Error in step callback: {str(e)}",
author="System",
).send()
except Exception as send_error:
logger.error(f"[CALLBACK DEBUG] Error sending error message: {str(send_error)}")
async def task_callback_wrapper(task_output):
logger.info(f"[CALLBACK DEBUG] Task callback wrapper triggered with output type: {type(task_output)}")
try:
# Check if we have a Chainlit context
if not cl.context.context_var.get():
logger.warning("[CALLBACK DEBUG] No Chainlit context available in task wrapper")
return
logger.info("[CALLBACK DEBUG] Chainlit context found in task wrapper")
# Create a message for task completion
if hasattr(task_output, 'raw'):
content = task_output.raw
logger.info("[CALLBACK DEBUG] Using raw output")
elif hasattr(task_output, 'content'):
content = task_output.content
logger.info("[CALLBACK DEBUG] Using content output")
else:
content = str(task_output)
logger.info("[CALLBACK DEBUG] Using string representation of output")
logger.info(f"[CALLBACK DEBUG] Sending task completion message from wrapper: {content[:100]}...")
try:
await cl.Message(
content=f"✅ {role_name} completed task:\n{content}",
author=role_name,
).send()
logger.info("[CALLBACK DEBUG] Successfully sent task completion message from wrapper")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error sending task completion message from wrapper: {str(e)}")
# If there are any additional task details
if hasattr(task_output, 'details'):
logger.info("[CALLBACK DEBUG] Task has additional details")
try:
await cl.Message(
content=f"📝 Additional details:\n{task_output.details}",
author=role_name,
).send()
logger.info("[CALLBACK DEBUG] Successfully sent additional details message")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error sending additional details message: {str(e)}")
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in task callback wrapper: {str(e)}", exc_info=True)
try:
await cl.Message(
content=f"Error in task callback: {str(e)}",
author="System",
).send()
except Exception as send_error:
logger.error(f"[CALLBACK DEBUG] Error sending error message: {str(send_error)}")
def sync_task_callback_wrapper(task_output):
logger.info("[CALLBACK DEBUG] Sync task callback wrapper triggered")
try:
# Create a new event loop for this thread if there isn't one
try:
loop = asyncio.get_event_loop()
logger.info("[CALLBACK DEBUG] Got existing event loop")
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.info("[CALLBACK DEBUG] Created new event loop")
if loop.is_running():
# If loop is running, schedule the callback
logger.info("[CALLBACK DEBUG] Loop is running, scheduling callback")
asyncio.run_coroutine_threadsafe(
task_callback_wrapper(task_output),
loop
)
else:
# If loop is not running, run it directly
logger.info("[CALLBACK DEBUG] Loop is not running, running callback directly")
loop.run_until_complete(task_callback_wrapper(task_output))
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in sync task callback: {str(e)}", exc_info=True)
def sync_step_callback_wrapper(step_details):
logger.info("[CALLBACK DEBUG] Sync step callback wrapper triggered")
try:
# Create a new event loop for this thread if there isn't one
try:
loop = asyncio.get_event_loop()
logger.info("[CALLBACK DEBUG] Got existing event loop")
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.info("[CALLBACK DEBUG] Created new event loop")
if loop.is_running():
# If loop is running, schedule the callback
logger.info("[CALLBACK DEBUG] Loop is running, scheduling callback")
asyncio.run_coroutine_threadsafe(
step_callback_wrapper(step_details),
loop
)
else:
# If loop is not running, run it directly
logger.info("[CALLBACK DEBUG] Loop is not running, running callback directly")
loop.run_until_complete(step_callback_wrapper(step_details))
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in sync step callback: {str(e)}", exc_info=True)
async def ui_run_praisonai(config, topic, tools_dict):
"""Run PraisonAI with the given configuration and topic."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
agents = {}
tasks = []
tasks_dict = {}
try:
# Start message queue processor
queue_processor = asyncio.create_task(process_message_queue())
# Create agents for each role
for role, details in config['roles'].items():
# Format the role name and other details
role_name = details.get('name', role).format(topic=topic)
role_filled = details.get('role', role).format(topic=topic)
goal_filled = details['goal'].format(topic=topic)
backstory_filled = details['backstory'].format(topic=topic)
# Test message to verify Chainlit is working
await cl.Message(
content=f"[DEBUG] Creating agent: {role_name}",
author="System"
).send()
# Create a sync wrapper for the step callback
def step_callback_sync(step_details):
try:
# Create a new event loop for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Add agent name to step details
step_details["agent_name"] = role_name
# Run the callback
loop.run_until_complete(step_callback(step_details))
loop.close()
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in step callback: {str(e)}", exc_info=True)
agent = Agent(
name=role_name,
role=role_filled,
goal=goal_filled,
backstory=backstory_filled,
llm=details.get('llm', 'gpt-4o'),
verbose=True,
allow_delegation=details.get('allow_delegation', False),
max_iter=details.get('max_iter', 15),
max_rpm=details.get('max_rpm'),
max_execution_time=details.get('max_execution_time'),
cache=details.get('cache', True),
step_callback=step_callback_sync
)
agents[role] = agent
# Create tasks for each role
for role, details in config['roles'].items():
agent = agents[role]
tools_list = []
# Get tools for this role
for tool_name in details.get('tools', []):
if tool_name in tools_dict:
tool_func = tools_dict[tool_name]
tools_list.append(tool_func)
# Create tasks for the agent
for task_name, task_details in details.get('tasks', {}).items():
description_filled = task_details['description'].format(topic=topic)
expected_output_filled = task_details['expected_output'].format(topic=topic)
# Test message to verify task creation
await cl.Message(
content=f"[DEBUG] Created task: {task_name} for agent {role_name}",
author="System"
).send()
# Create a sync wrapper for the task callback
def task_callback_sync(task_output):
try:
# Create a new event loop for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Run the callback
loop.run_until_complete(task_callback(task_output))
loop.close()
except Exception as e:
logger.error(f"[CALLBACK DEBUG] Error in task callback: {str(e)}", exc_info=True)
task = Task(
description=description_filled,
expected_output=expected_output_filled,
agent=agent,
tools=tools_list,
async_execution=True,
context=[],
config=task_details.get('config', {}),
output_json=task_details.get('output_json'),
output_pydantic=task_details.get('output_pydantic'),
output_file=task_details.get('output_file', ""),
callback=task_callback_sync,
create_directory=task_details.get('create_directory', False)
)
tasks.append(task)
tasks_dict[task_name] = task
# Set up task contexts
for role, details in config['roles'].items():
for task_name, task_details in details.get('tasks', {}).items():
task = tasks_dict[task_name]
context_tasks = [tasks_dict[ctx] for ctx in task_details.get('context', [])
if ctx in tasks_dict]
task.context = context_tasks
# Send the start message
await cl.Message(
content="Starting PraisonAI agents execution...",
author="System"
).send()
# Create and run the PraisonAI agents
if config.get('process') == 'hierarchical':
crew = PraisonAIAgents(
agents=list(agents.values()),
tasks=tasks,
verbose=True,
process="hierarchical",
manager_llm=config.get('manager_llm', 'gpt-4o')
)
else:
crew = PraisonAIAgents(
agents=list(agents.values()),
tasks=tasks,
verbose=2
)
# Store the crew in the user session
cl.user_session.set("crew", crew)
# Run the agents in a separate thread
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(None, crew.start)
logger.debug(f"[CALLBACK DEBUG] Result: {response}")
# Convert response to string if it's not already
if hasattr(response, 'raw'):
result = response.raw
elif hasattr(response, 'content'):
result = response.content
else:
result = str(response)
# Send the completion message
await cl.Message(
content="PraisonAI agents execution completed.",
author="System"
).send()
# After getting the response, wait a bit for remaining messages
await asyncio.sleep(1) # Give time for final messages to be processed
queue_processor.cancel() # Stop the queue processor
return result
except Exception as e:
error_msg = f"Error in ui_run_praisonai: {str(e)}"
logger.error(error_msg, exc_info=True)
await cl.Message(
content=error_msg,
author="System"
).send()
raise
+81
View File
@@ -0,0 +1,81 @@
import os
import logging
from dotenv import load_dotenv
import chainlit as cl
from chainlit.types import ThreadDict
import yaml
import sys
import os
from datetime import datetime
# Add the parent directory to sys.path to allow imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
from praisonai.ui.colab import ui_run_praisonai, load_tools_from_tools_py
from praisonai.ui.callbacks import callback, trigger_callback
# Load environment variables
load_dotenv()
# Set up logging
logger = logging.getLogger(__name__)
log_level = os.getenv("LOGLEVEL", "INFO").upper()
logger.setLevel(log_level)
# Load agent configuration
agent_file = "agents.yaml"
with open(agent_file, 'r') as f:
config = yaml.safe_load(f)
# Load tools
tools_dict = load_tools_from_tools_py()
@cl.on_message
async def main(message: cl.Message):
"""Main message handler for Chainlit"""
try:
logger.info(f"Processing message: {message.content}")
await cl.Message(
content=f"🔄 Processing your request about: {message.content}...",
author="System"
).send()
await cl.Message(
content="Using Running PraisonAI Agents...",
author="System"
).send()
# Run PraisonAI with the message content as the topic
result = await ui_run_praisonai(config, message.content, tools_dict)
# Send the final result
await cl.Message(
content=result,
author="System"
).send()
except Exception as e:
error_msg = f"Error running PraisonAI agents: {str(e)}"
logger.error(error_msg, exc_info=True)
await cl.Message(
content=error_msg,
author="System"
).send()
@cl.on_chat_start
async def start():
"""Handler for chat start"""
await cl.Message(
content="👋 Welcome! I'm your AI assistant. What would you like to work on?",
author="System"
).send()
# Authentication setup (optional)
if os.getenv("CHAINLIT_AUTH_SECRET"):
@cl.password_auth_callback
def auth_callback(username: str, password: str) -> cl.User:
# Replace with your authentication logic
if username == os.getenv("CHAINLIT_USERNAME", "admin") and \
password == os.getenv("CHAINLIT_PASSWORD", "admin"):
return cl.User(identifier=username, metadata={"role": "user"})
return None
+1 -1
View File
@@ -1 +1 @@
# Welcome to Praison AI! 🚀🤖
# Welcome to PraisonAI! 🚀
+775
View File
@@ -0,0 +1,775 @@
from chainlit.input_widget import Select, TextInput
import os
import sys
import yaml
import logging
import inspect
import chainlit as cl
framework = "praisonai"
config_list = [
{
'model': os.environ.get("OPENAI_MODEL_NAME", "gpt-4o"),
'base_url': os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1"),
'api_key': os.environ.get("OPENAI_API_KEY", "")
}
]
agent_file = "test.yaml"
actions=[
cl.Action(name="run", value="run", label="✅ Run"),
cl.Action(name="modify", value="modify", label="🔧 Modify"),
]
import os
import sys
import yaml
import logging
import inspect
import asyncio
import importlib.util
import sqlite3
from queue import Queue
from datetime import datetime
from dotenv import load_dotenv
# Chainlit imports
import chainlit as cl
from chainlit.types import ThreadDict
import chainlit.data as cl_data
# External imports from your local packages (adjust if needed)
from praisonaiagents import Agent, Task, PraisonAIAgents
# -----------------------------------------------------------------------------
# Global Setup
# -----------------------------------------------------------------------------
load_dotenv()
log_level = os.getenv("LOGLEVEL", "INFO").upper()
logger = logging.getLogger(__name__)
logger.setLevel(log_level)
message_queue = Queue() # Queue to handle messages sent to Chainlit UI
agent_file = "agents.yaml"
# -----------------------------------------------------------------------------
# Database and Settings Logic (untouched logic with minimal additions for Chainlit)
# -----------------------------------------------------------------------------
MAX_RETRIES = 3
RETRY_DELAY = 1 # seconds
# Original DatabaseManager from chainlit_ui.py, unmodified logic, plus minimal
# placeholder methods for Chainlit's data layer calls (create_user, get_user, etc.)
from db import DatabaseManager
async def init_database_with_retry():
db = DatabaseManager()
for attempt in range(MAX_RETRIES):
try:
db.initialize()
return db
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
db_manager = asyncio.run(init_database_with_retry())
cl_data._data_layer = db_manager
async def save_setting_with_retry(key: str, value: str):
for attempt in range(MAX_RETRIES):
try:
await db_manager.save_setting(key, value)
return
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
async def load_setting_with_retry(key: str) -> str:
for attempt in range(MAX_RETRIES):
try:
return await db_manager.load_setting(key)
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
return ""
def save_setting(key: str, value: str):
asyncio.run(save_setting_with_retry(key, value))
def load_setting(key: str) -> str:
return asyncio.run(load_setting_with_retry(key))
async def update_thread_metadata(thread_id: str, metadata: dict):
for attempt in range(MAX_RETRIES):
try:
await cl_data.update_thread(thread_id, metadata=metadata)
return
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
# -----------------------------------------------------------------------------
# Callback Manager (untouched from colab_combined.py)
# -----------------------------------------------------------------------------
class CallbackManager:
def __init__(self):
self._callbacks = {}
def register(self, name: str, callback, is_async: bool = False) -> None:
self._callbacks[name] = {'func': callback, 'is_async': is_async}
async def call(self, name: str, **kwargs) -> None:
if name not in self._callbacks:
logger.warning(f"No callback registered for {name}")
return
callback_info = self._callbacks[name]
func = callback_info['func']
is_async = callback_info['is_async']
try:
if is_async:
await func(**kwargs)
else:
if asyncio.iscoroutinefunction(func):
await func(**kwargs)
else:
await asyncio.get_event_loop().run_in_executor(None, lambda: func(**kwargs))
except Exception as e:
logger.error(f"Error in callback {name}: {str(e)}")
callback_manager = CallbackManager()
def register_callback(name: str, callback, is_async: bool = False) -> None:
callback_manager.register(name, callback, is_async)
async def trigger_callback(name: str, **kwargs) -> None:
await callback_manager.call(name, **kwargs)
def callback(name: str, is_async: bool = False):
def decorator(func):
register_callback(name, func, is_async)
return func
return decorator
# -----------------------------------------------------------------------------
# Tools Loader (untouched logic but returning dict for tools)
# -----------------------------------------------------------------------------
def load_tools_from_tools_py():
"""
Imports and returns all contents from tools.py file.
Also adds the tools to the global namespace.
"""
tools_dict = {}
try:
spec = importlib.util.spec_from_file_location("tools", "tools.py")
logger.info(f"Spec: {spec}")
if spec is None:
logger.info("tools.py not found in current directory")
return tools_dict
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, obj in inspect.getmembers(module):
if not name.startswith('_') and callable(obj) and not inspect.isclass(obj):
globals()[name] = obj
tool_def = {
"type": "function",
"function": {
"name": name,
"description": obj.__doc__ or f"Function to {name.replace('_', ' ')}",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query to look up information about"
}
},
"required": ["query"]
}
}
}
tools_dict[name] = tool_def
logger.info(f"Loaded and globalized tool function: {name}")
logger.info(f"Loaded {len(tools_dict)} tool functions from tools.py")
except Exception as e:
logger.warning(f"Error loading tools from tools.py: {e}")
return tools_dict
# -----------------------------------------------------------------------------
# Async Queue Processor (untouched logic)
# -----------------------------------------------------------------------------
async def process_message_queue():
while True:
try:
if not message_queue.empty():
msg_data = message_queue.get()
await cl.Message(**msg_data).send()
await asyncio.sleep(0.1)
except Exception as e:
logger.error(f"Error processing message queue: {e}")
# -----------------------------------------------------------------------------
# Step & Task Callbacks (Async & Sync) (untouched logic)
# -----------------------------------------------------------------------------
async def step_callback(step_details):
logger.info(f"[CALLBACK DEBUG] step_callback: {step_details}")
agent_name = step_details.get("agent_name", "Agent")
try:
if step_details.get("response"):
message_queue.put({
"content": f"Agent Response: {step_details['response']}",
"author": agent_name
})
if step_details.get("tool_name"):
message_queue.put({
"content": f"🛠️ Using tool: {step_details['tool_name']}",
"author": "System"
})
except Exception as e:
logger.error(f"Error in step_callback: {e}", exc_info=True)
async def task_callback(task_output):
logger.info(f"[CALLBACK DEBUG] task_callback: type={type(task_output)}")
try:
if hasattr(task_output, 'raw'):
content = task_output.raw
elif hasattr(task_output, 'content'):
content = task_output.content
else:
content = str(task_output)
message_queue.put({
"content": f"Task Output: {content}",
"author": "Task"
})
except Exception as e:
logger.error(f"Error in task_callback: {e}", exc_info=True)
async def step_callback_wrapper(step_details):
logger.info(f"[CALLBACK DEBUG] step_callback_wrapper: {step_details}")
agent_name = step_details.get("agent_name", "Agent")
try:
if not cl.context.context_var.get():
logger.warning("[CALLBACK DEBUG] No Chainlit context in wrapper.")
return
if step_details.get("response"):
await cl.Message(
content=f"{agent_name}: {step_details['response']}",
author=agent_name,
).send()
if step_details.get("tool_name"):
await cl.Message(
content=f"🛠️ {agent_name} is using tool: {step_details['tool_name']}",
author="System",
).send()
if step_details.get("thought"):
await cl.Message(
content=f"💭 {agent_name}'s thought: {step_details['thought']}",
author=agent_name,
).send()
except Exception as e:
logger.error(f"Error in step_callback_wrapper: {e}", exc_info=True)
try:
await cl.Message(content=f"Error in step callback: {e}", author="System").send()
except Exception as send_error:
logger.error(f"Error sending error message: {send_error}")
async def task_callback_wrapper(task_output):
logger.info("[CALLBACK DEBUG] task_callback_wrapper triggered")
try:
if not cl.context.context_var.get():
logger.warning("[CALLBACK DEBUG] No Chainlit context in task wrapper.")
return
if hasattr(task_output, 'raw'):
content = task_output.raw
elif hasattr(task_output, 'content'):
content = task_output.content
else:
content = str(task_output)
await cl.Message(
content=f"✅ Agent completed task:\n{content}",
author="Agent",
).send()
if hasattr(task_output, 'details'):
await cl.Message(
content=f"📝 Additional details:\n{task_output.details}",
author="Agent",
).send()
except Exception as e:
logger.error(f"Error in task_callback_wrapper: {e}", exc_info=True)
try:
await cl.Message(content=f"Error in task callback: {e}", author="System").send()
except Exception as send_error:
logger.error(f"Error sending error message: {send_error}")
def sync_task_callback_wrapper(task_output):
logger.info("[CALLBACK DEBUG] sync_task_callback_wrapper")
try:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if loop.is_running():
asyncio.run_coroutine_threadsafe(task_callback_wrapper(task_output), loop)
else:
loop.run_until_complete(task_callback_wrapper(task_output))
except Exception as e:
logger.error(f"Error in sync_task_callback_wrapper: {e}", exc_info=True)
def sync_step_callback_wrapper(step_details):
logger.info("[CALLBACK DEBUG] sync_step_callback_wrapper")
try:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if loop.is_running():
asyncio.run_coroutine_threadsafe(step_callback_wrapper(step_details), loop)
else:
loop.run_until_complete(step_callback_wrapper(step_details))
except Exception as e:
logger.error(f"Error in sync_step_callback_wrapper: {e}", exc_info=True)
# -----------------------------------------------------------------------------
# Main PraisonAI Runner (untouched logic)
# -----------------------------------------------------------------------------
async def ui_run_praisonai(config, topic, tools_dict):
logger.info("Starting ui_run_praisonai")
agents_map = {}
tasks = []
tasks_dict = {}
try:
queue_processor = asyncio.create_task(process_message_queue())
# Create agents
for role, details in config['roles'].items():
role_name = details.get('name', role).format(topic=topic)
role_filled = details.get('role', role).format(topic=topic)
goal_filled = details['goal'].format(topic=topic)
backstory_filled = details['backstory'].format(topic=topic)
await cl.Message(content=f"[DEBUG] Creating agent: {role_name}", author="System").send()
def step_callback_sync(step_details):
step_details["agent_name"] = role_name
try:
loop_ = asyncio.new_event_loop()
asyncio.set_event_loop(loop_)
loop_.run_until_complete(step_callback(step_details))
loop_.close()
except Exception as e:
logger.error(f"Error in step_callback_sync: {e}", exc_info=True)
agent = Agent(
name=role_name,
role=role_filled,
goal=goal_filled,
backstory=backstory_filled,
llm=details.get('llm', 'gpt-4o'),
verbose=True,
allow_delegation=details.get('allow_delegation', False),
max_iter=details.get('max_iter', 15),
max_rpm=details.get('max_rpm'),
max_execution_time=details.get('max_execution_time'),
cache=details.get('cache', True),
step_callback=step_callback_sync
)
agents_map[role] = agent
# Create tasks
for role, details in config['roles'].items():
agent = agents_map[role]
role_name = agent.name
role_tools = []
for tool_name in details.get('tools', []):
if tool_name in tools_dict:
role_tools.append(tools_dict[tool_name])
for tname, tdetails in details.get('tasks', {}).items():
description_filled = tdetails['description'].format(topic=topic)
expected_output_filled = tdetails['expected_output'].format(topic=topic)
await cl.Message(content=f"[DEBUG] Created task: {tname} for {role_name}", author="System").send()
def task_callback_sync(task_output):
try:
loop_ = asyncio.new_event_loop()
asyncio.set_event_loop(loop_)
loop_.run_until_complete(task_callback(task_output))
loop_.close()
except Exception as e:
logger.error(f"Error in task_callback_sync: {e}", exc_info=True)
task = Task(
description=description_filled,
expected_output=expected_output_filled,
agent=agent,
tools=role_tools,
async_execution=True,
context=[],
config=tdetails.get('config', {}),
output_json=tdetails.get('output_json'),
output_pydantic=tdetails.get('output_pydantic'),
output_file=tdetails.get('output_file', ""),
callback=task_callback_sync,
create_directory=tdetails.get('create_directory', False)
)
tasks.append(task)
tasks_dict[tname] = task
# Build context links
for role, details in config['roles'].items():
for tname, tdetails in details.get('tasks', {}).items():
if tname not in tasks_dict:
continue
task = tasks_dict[tname]
context_tasks = [
tasks_dict[ctx]
for ctx in tdetails.get('context', [])
if ctx in tasks_dict
]
task.context = context_tasks
await cl.Message(content="Starting PraisonAI agents execution...", author="System").send()
if config.get('process') == 'hierarchical':
prai_agents = PraisonAIAgents(
agents=list(agents_map.values()),
tasks=tasks,
verbose=True,
process="hierarchical",
manager_llm=config.get('manager_llm', 'gpt-4o')
)
else:
prai_agents = PraisonAIAgents(
agents=list(agents_map.values()),
tasks=tasks,
verbose=2
)
cl.user_session.set("agents", prai_agents)
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(None, prai_agents.start)
if hasattr(response, 'raw'):
result = response.raw
elif hasattr(response, 'content'):
result = response.content
else:
result = str(response)
await cl.Message(content="PraisonAI agents execution completed.", author="System").send()
await asyncio.sleep(1)
queue_processor.cancel()
return result
except Exception as e:
error_msg = f"Error in ui_run_praisonai: {str(e)}"
logger.error(error_msg, exc_info=True)
await cl.Message(content=error_msg, author="System").send()
raise
# -----------------------------------------------------------------------------
# Chainlit Handlers + logic from chainlit_ui.py (no changes in logic)
# -----------------------------------------------------------------------------
tools_dict = load_tools_from_tools_py()
# Load agent config (default) from 'agents.yaml'
with open(agent_file, 'r') as f:
config = yaml.safe_load(f)
AUTH_PASSWORD_ENABLED = os.getenv("AUTH_PASSWORD_ENABLED", "true").lower() == "true"
CHAINLIT_AUTH_SECRET = os.getenv("CHAINLIT_AUTH_SECRET")
if not CHAINLIT_AUTH_SECRET:
os.environ["CHAINLIT_AUTH_SECRET"] = "p8BPhQChpg@J>jBz$wGxqLX2V>yTVgP*7Ky9H$aV:axW~ANNX-7_T:o@lnyCBu^U"
username_env = os.getenv("CHAINLIT_USERNAME", "admin")
password_env = os.getenv("CHAINLIT_PASSWORD", "admin")
def simple_auth_callback(u: str, p: str):
if (u, p) == (username_env, password_env):
return cl.User(identifier=u, metadata={"role": "ADMIN", "provider": "credentials"})
return None
if AUTH_PASSWORD_ENABLED:
auth_callback = cl.password_auth_callback(simple_auth_callback)
@cl.set_chat_profiles
async def set_profiles(current_user: cl.User):
"""
Keep all the same starter logic from chainlit_ui.py.
"""
return [
cl.ChatProfile(
name="Auto",
markdown_description=(
"Automatically generate agents and tasks based on your input."
),
starters=[
cl.Starter(
label="Create a movie script",
message=(
"Create a movie script about a futuristic society where AI "
"and humans coexist, focusing on the conflict and resolution "
"between them. Start with an intriguing opening scene."
),
icon="/public/movie.svg",
),
cl.Starter(
label="Design a fantasy world",
message=(
"Design a detailed fantasy world with unique geography, "
"cultures, and magical systems. Start by describing the main "
"continent and its inhabitants."
),
icon="/public/fantasy.svg",
),
cl.Starter(
label="Write a futuristic political thriller",
message=(
"Write a futuristic political thriller involving a conspiracy "
"within a global government. Start with a high-stakes meeting "
"that sets the plot in motion."
),
icon="/public/thriller.svg",
),
cl.Starter(
label="Develop a new board game",
message=(
"Develop a new, innovative board game. Describe the game's "
"objective, rules, and unique mechanics. Create a scenario to "
"illustrate gameplay."
),
icon="/public/game.svg",
),
],
),
cl.ChatProfile(
name="Manual",
markdown_description="Manually define your agents and tasks using a YAML file.",
),
]
@cl.on_chat_start
async def start_chat():
try:
# Load model name from database
model_name = load_setting("model_name") or os.getenv("MODEL_NAME", "gpt-4o-mini")
cl.user_session.set("model_name", model_name)
logger.debug(f"Model name: {model_name}")
cl.user_session.set(
"message_history",
[{"role": "system", "content": "You are a helpful assistant."}],
)
# Create tools.py if it doesn't exist
if not os.path.exists("tools.py"):
with open("tools.py", "w") as f:
f.write("# Add your custom tools here\n")
settings = await cl.ChatSettings(
[
TextInput(id="Model", label="OpenAI - Model", initial=model_name),
TextInput(id="BaseUrl", label="OpenAI - Base URL", initial=config_list[0]['base_url']),
TextInput(id="ApiKey", label="OpenAI - API Key", initial=config_list[0]['api_key']),
Select(
id="Framework",
label="Framework",
values=["praisonai", "crewai", "autogen"],
initial_index=0,
),
]
).send()
cl.user_session.set("settings", settings)
chat_profile = cl.user_session.get("chat_profile")
if chat_profile=="Manual":
agent_file = "agents.yaml"
full_agent_file_path = os.path.abspath(agent_file)
if os.path.exists(full_agent_file_path):
with open(full_agent_file_path, 'r') as f:
yaml_content = f.read()
msg = cl.Message(content=yaml_content, language="yaml")
await msg.send()
full_tools_file_path = os.path.abspath("tools.py")
if os.path.exists(full_tools_file_path):
with open(full_tools_file_path, 'r') as f:
tools_content = f.read()
msg = cl.Message(content=tools_content, language="python")
await msg.send()
settings = await cl.ChatSettings(
[
TextInput(id="Model", label="OpenAI - Model", initial=model_name),
TextInput(id="BaseUrl", label="OpenAI - Base URL", initial=config_list[0]['base_url']),
TextInput(id="ApiKey", label="OpenAI - API Key", initial=config_list[0]['api_key']),
Select(
id="Framework",
label="Framework",
values=["praisonai", "crewai", "autogen"],
initial_index=0,
),
TextInput(id="agents", label="agents.yaml", initial=yaml_content, multiline=True),
TextInput(id="tools", label="tools.py", initial=tools_content, multiline=True),
]
).send()
cl.user_session.set("settings", settings)
res = await cl.AskActionMessage(
content="Pick an action!",
actions=actions,
).send()
if res and res.get("value") == "modify":
await cl.Message(content="Modify the agents and tools from below settings", actions=actions).send()
elif res and res.get("value") == "run":
await main(cl.Message(content="", actions=actions))
await on_settings_update(settings)
except Exception as e:
logger.error(f"Error in start_chat: {str(e)}")
await cl.Message(content=f"An error occurred while starting the chat: {str(e)}").send()
@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict):
"""
Logic from chainlit_ui.py for chat resume:
- Restore message history from thread steps
"""
try:
message_history = cl.user_session.get("message_history", [])
root_messages = [m for m in thread["steps"] if m["parentId"] is None]
for message in root_messages:
if message["type"] == "user_message":
message_history.append({"role": "user", "content": message["output"]})
elif message["type"] == "ai_message":
message_history.append({"role": "assistant", "content": message["content"]})
cl.user_session.set("message_history", message_history)
except Exception as e:
logger.error(f"Error in on_chat_resume: {str(e)}")
@cl.on_message
async def main(message: cl.Message):
"""
Merged logic from colab_combined.py and chainlit_ui.py for main message:
- Use existing ui_run_praisonai to process user message with PraisonAI
- Keep message history, no changes to logic
"""
try:
logger.info(f"User message: {message.content}")
await cl.Message(
content=f"🔄 Processing your request: {message.content}...",
author="System"
).send()
# Run PraisonAI
result = await ui_run_praisonai(config, message.content, tools_dict)
# Update message history
message_history = cl.user_session.get("message_history", [])
message_history.append({"role": "user", "content": message.content})
message_history.append({"role": "assistant", "content": str(result)})
cl.user_session.set("message_history", message_history)
except Exception as e:
error_msg = f"Error running PraisonAI agents: {str(e)}"
logger.error(error_msg, exc_info=True)
await cl.Message(content=error_msg, author="System").send()
@cl.on_settings_update
async def on_settings_update(settings):
"""Handle updates to the ChatSettings form."""
try:
global config_list, framework
config_list[0]['model'] = settings["Model"]
config_list[0]['base_url'] = settings["BaseUrl"]
config_list[0]['api_key'] = settings["ApiKey"]
# Save settings to database with retry
for attempt in range(MAX_RETRIES):
try:
await save_setting_with_retry("model_name", config_list[0]['model'])
await save_setting_with_retry("base_url", config_list[0]['base_url'])
await save_setting_with_retry("api_key", config_list[0]['api_key'])
break
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
# Save to environment variables for compatibility
os.environ["OPENAI_API_KEY"] = config_list[0]['api_key']
os.environ["OPENAI_MODEL_NAME"] = config_list[0]['model']
os.environ["OPENAI_API_BASE"] = config_list[0]['base_url']
os.environ["MODEL_NAME"] = config_list[0]['model']
framework = settings["Framework"]
os.environ["FRAMEWORK"] = framework
if "agents" in settings:
with open("agents.yaml", "w") as f:
f.write(settings["agents"])
if "tools" in settings:
with open("tools.py", "w") as f:
f.write(settings["tools"])
# Update thread metadata if exists with retry
thread_id = cl.user_session.get("thread_id")
if thread_id:
for attempt in range(MAX_RETRIES):
try:
thread = await cl_data.get_thread(thread_id)
if thread:
metadata = thread.get("metadata", {})
if isinstance(metadata, str):
try:
metadata = json.loads(metadata)
except json.JSONDecodeError:
metadata = {}
metadata["model_name"] = config_list[0]['model']
await cl_data.update_thread(thread_id, metadata=metadata)
cl.user_session.set("metadata", metadata)
break
except sqlite3.OperationalError as e:
if "database is locked" in str(e) and attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
continue
raise
logger.info("Settings updated successfully")
except Exception as e:
logger.error(f"Error updating settings: {str(e)}")
await cl.Message(content=f"An error occurred while updating settings: {str(e)}. Retrying...").send()
# One final retry after a longer delay
try:
await asyncio.sleep(RETRY_DELAY * 2)
await on_settings_update(settings)
except Exception as e:
logger.error(f"Final retry failed: {str(e)}")
await cl.Message(content=f"Failed to update settings after retries: {str(e)}").send()
+4 -4
View File
@@ -1,6 +1,6 @@
[project]
name = "PraisonAI"
version = "2.0.24"
version = "2.0.26"
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human–agent collaboration."
readme = "README.md"
license = ""
@@ -12,7 +12,7 @@ dependencies = [
"rich>=13.7",
"markdown>=3.5",
"pyparsing>=3.0.0",
"praisonaiagents>=0.0.18",
"praisonaiagents>=0.0.20",
"python-dotenv>=0.19.0",
"instructor>=1.3.3",
"PyYAML>=6.0",
@@ -84,7 +84,7 @@ autogen = ["pyautogen>=0.2.19", "praisonai-tools>=0.0.7", "crewai"]
[tool.poetry]
name = "PraisonAI"
version = "2.0.24"
version = "2.0.26"
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human–agent collaboration."
authors = ["Mervin Praison"]
license = ""
@@ -102,7 +102,7 @@ python = ">=3.10,<3.13"
rich = ">=13.7"
markdown = ">=3.5"
pyparsing = ">=3.0.0"
praisonaiagents = ">=0.0.18"
praisonaiagents = ">=0.0.20"
python-dotenv = ">=0.19.0"
instructor = ">=1.3.3"
PyYAML = ">=6.0"
Generated
+5 -5
View File
@@ -3061,7 +3061,7 @@ wheels = [
[[package]]
name = "praisonai"
version = "2.0.24"
version = "2.0.26"
source = { editable = "." }
dependencies = [
{ name = "instructor" },
@@ -3198,7 +3198,7 @@ requires-dist = [
{ name = "plotly", marker = "extra == 'realtime'", specifier = ">=5.24.0" },
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.7" },
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.7" },
{ name = "praisonaiagents", specifier = ">=0.0.18" },
{ name = "praisonaiagents", specifier = ">=0.0.20" },
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = ">=0.2.19" },
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
@@ -3251,16 +3251,16 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.18"
version = "0.0.20"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "openai" },
{ name = "pydantic" },
{ name = "rich" },
]
sdist = { url = "https://files.pythonhosted.org/packages/09/e4/9bf056c4a9ecb5cf47766b9442b3b608881f530c4c28e233cbc6adb1f2d1/praisonaiagents-0.0.18.tar.gz", hash = "sha256:56744f82f806999fca328f9f9f8ae8b5c40cf312cf4fb6c0c34c30661f206d23", size = 23337 }
sdist = { url = "https://files.pythonhosted.org/packages/8e/89/0e9d17b7113772ab469f3d113af51fb3ff5c2f16eb0a1b70667a12478358/praisonaiagents-0.0.20.tar.gz", hash = "sha256:15e08f2c8c06643e091870ba0f15c1614f6052dea45986c877dad52e8fe7f06e", size = 23885 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/78/b8/abfccfc0e82832721987b9c58d703f163a5573c5615ae5b5f3609f8877a0/praisonaiagents-0.0.18-py3-none-any.whl", hash = "sha256:fa9374f065ade18535ce05e27d2bb1ca326810ed6bbec5421597526f31add792", size = 29334 },
{ url = "https://files.pythonhosted.org/packages/f8/94/5f62ded3aad4ee5d879e8b9712be4a3268abc2d89f71b10999109cef147c/praisonaiagents-0.0.20-py3-none-any.whl", hash = "sha256:3e9c62f3c4d8978bea8e0c95359a49e20db833511e17e122ccc06eb77996a618", size = 29883 },
]
[[package]]