mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-09 11:51:37 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3be4c4c70e | ||
|
|
709640240f | ||
|
|
93d526eb7b | ||
|
|
624a2cfa17 | ||
|
|
4d2a6a2e2e | ||
|
|
a1bdb056a0 | ||
|
|
b4a6401bae | ||
|
|
772ce7d2e2 | ||
|
|
20c2d28f35 | ||
|
|
f8589e4363 | ||
|
|
49d9d40199 | ||
|
|
445cc037ce | ||
|
|
9e7c172f55 | ||
|
|
d89570b085 | ||
|
|
ee5dfddccd | ||
|
|
a3bfc6f795 | ||
|
|
ebc130854d | ||
|
|
f40a5c1359 | ||
|
|
3284f7bbc6 | ||
|
|
87928611f7 | ||
|
|
2727bf5992 | ||
|
|
45d04163c3 | ||
|
|
477d0d2e26 | ||
|
|
77394f5e5f | ||
|
|
29e533c18c | ||
|
|
48dbea0566 | ||
|
|
969d6697e1 | ||
|
|
787d2508b1 | ||
|
|
ec42dc92e4 | ||
|
|
5b7bae2f43 | ||
|
|
69a1efac47 |
No files matched your search
+1
-1
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
"praisonai>=2.2.22" \
|
||||
"praisonai>=2.2.24" \
|
||||
"praisonai[api]" \
|
||||
gunicorn \
|
||||
markdown
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.22" \
|
||||
"praisonai>=2.2.24" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.22" \
|
||||
"praisonai>=2.2.24" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.22" \
|
||||
"praisonai>=2.2.24" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
+2
-2
@@ -121,7 +121,7 @@ healthcheck:
|
||||
## 📦 Package Versions
|
||||
|
||||
All Docker images use consistent, up-to-date versions:
|
||||
- PraisonAI: `>=2.2.22`
|
||||
- PraisonAI: `>=2.2.24`
|
||||
- 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.22" "praisonaiagents==0.0.92"
|
||||
RUN pip install "praisonai==2.2.24" "praisonaiagents==0.0.92"
|
||||
```
|
||||
|
||||
## 🌐 Production Deployment
|
||||
|
||||
@@ -110,7 +110,7 @@ Loads environment variables from .env file or system and sets them.</p>
|
||||
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.2.22 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.24 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN pip install flask praisonai==2.2.22 watchdog
|
||||
RUN pip install flask praisonai==2.2.24 watchdog
|
||||
|
||||
EXPOSE 5555
|
||||
|
||||
|
||||
+1
-1
@@ -155,7 +155,7 @@ To facilitate local development with live reload, you can use Docker. Follow the
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN pip install flask praisonai==2.2.22 watchdog
|
||||
RUN pip install flask praisonai==2.2.24 watchdog
|
||||
|
||||
EXPOSE 5555
|
||||
|
||||
|
||||
+1
-1
@@ -208,7 +208,7 @@ To facilitate local development with live reload, you can use Docker. Follow the
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN pip install flask praisonai==2.2.22 watchdog
|
||||
RUN pip install flask praisonai==2.2.24 watchdog
|
||||
|
||||
EXPOSE 5555
|
||||
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
---
|
||||
title: "MCP with Streamlit"
|
||||
description: "How to properly integrate MCP (Model Context Protocol) tools with PraisonAI Agents in Streamlit applications"
|
||||
---
|
||||
|
||||
# MCP with Streamlit
|
||||
|
||||
This guide explains how to properly integrate MCP (Model Context Protocol) tools with PraisonAI Agents in Streamlit applications, addressing common issues and providing working solutions.
|
||||
|
||||
## Common Issues
|
||||
|
||||
When integrating MCP tools with Streamlit, users often encounter these problems:
|
||||
|
||||
### Issue #1: Agent Re-initialization
|
||||
**Problem**: Agent gets re-initialized on every Streamlit interaction, causing MCP tools to fail.
|
||||
|
||||
**Solution**: Use Streamlit's session state to initialize the agent only once.
|
||||
|
||||
### Issue #2: LLM Provider Format
|
||||
**Problem**: Using provider/model format like `"ollama/llama3.2"` can cause tool calling issues in Streamlit environments.
|
||||
|
||||
**Solution**: Use standard model names like `"gpt-4o-mini"` instead of provider/model format.
|
||||
|
||||
### Issue #3: Missing Error Handling
|
||||
**Problem**: No user feedback when MCP initialization fails.
|
||||
|
||||
**Solution**: Implement comprehensive error handling with user-friendly messages.
|
||||
|
||||
## Working Example
|
||||
|
||||
Here's a complete working example that demonstrates the correct approach:
|
||||
|
||||
```python
|
||||
import streamlit as st
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.mcp import MCP
|
||||
import traceback
|
||||
|
||||
st.title("🏠 AI Airbnb Assistant")
|
||||
|
||||
# Configuration in sidebar
|
||||
with st.sidebar:
|
||||
st.header("⚙️ Configuration")
|
||||
|
||||
# Use standard model names, not provider/model format
|
||||
llm_model = st.selectbox(
|
||||
"Choose LLM Model",
|
||||
options=[
|
||||
"gpt-4o-mini", # ✅ Correct format
|
||||
"gpt-4o",
|
||||
"claude-3-5-sonnet-20241022"
|
||||
],
|
||||
index=0
|
||||
)
|
||||
|
||||
debug_mode = st.checkbox("Enable Debug Mode", value=False)
|
||||
|
||||
# Initialize session state (CRITICAL for Streamlit)
|
||||
if "agent_initialized" not in st.session_state:
|
||||
st.session_state.agent_initialized = False
|
||||
st.session_state.agent = None
|
||||
st.session_state.init_error = None
|
||||
|
||||
# Function to initialize agent with proper error handling
|
||||
def initialize_agent():
|
||||
try:
|
||||
with st.spinner("🔄 Initializing AI agent..."):
|
||||
# Create MCP tools
|
||||
mcp_tools = MCP(
|
||||
"npx -y @openbnb/mcp-server-airbnb --ignore-robots-txt",
|
||||
timeout=60,
|
||||
debug=debug_mode
|
||||
)
|
||||
|
||||
# Create agent - this should only happen ONCE
|
||||
agent = Agent(
|
||||
instructions="You are a helpful Airbnb assistant...",
|
||||
llm=llm_model, # Use standard format
|
||||
tools=mcp_tools,
|
||||
verbose=debug_mode
|
||||
)
|
||||
|
||||
return agent, None
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Failed to initialize agent: {str(e)}"
|
||||
if debug_mode:
|
||||
error_msg += f"\n\nFull traceback:\n{traceback.format_exc()}"
|
||||
return None, error_msg
|
||||
|
||||
# Agent initialization (only runs once)
|
||||
if not st.session_state.agent_initialized:
|
||||
if st.button("🚀 Initialize AI Assistant", type="primary"):
|
||||
agent, error = initialize_agent()
|
||||
|
||||
if agent:
|
||||
st.session_state.agent = agent
|
||||
st.session_state.agent_initialized = True
|
||||
st.session_state.init_error = None
|
||||
st.success("✅ AI Assistant initialized successfully!")
|
||||
st.rerun()
|
||||
else:
|
||||
st.session_state.init_error = error
|
||||
st.error(f"❌ Initialization failed: {error}")
|
||||
|
||||
# Main interface (only show if agent is ready)
|
||||
if st.session_state.agent_initialized and st.session_state.agent:
|
||||
query = st.text_input("🔍 What are you looking for?")
|
||||
|
||||
if st.button("Search") and query:
|
||||
try:
|
||||
with st.spinner("🔍 Searching..."):
|
||||
# Use the SAME agent instance from session state
|
||||
result = st.session_state.agent.start(query)
|
||||
st.write(result)
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"❌ Search failed: {str(e)}")
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Session State Management
|
||||
Always use Streamlit's session state to manage agent lifecycle:
|
||||
|
||||
```python
|
||||
# ✅ Correct - Initialize once in session state
|
||||
if "agent" not in st.session_state:
|
||||
st.session_state.agent = Agent(...)
|
||||
|
||||
# ❌ Wrong - Re-initializes on every interaction
|
||||
agent = Agent(...)
|
||||
```
|
||||
|
||||
### 2. LLM Model Selection
|
||||
Use standard model names, avoid provider/model format in Streamlit:
|
||||
|
||||
```python
|
||||
# ✅ Correct - Standard model names
|
||||
llm="gpt-4o-mini"
|
||||
llm="claude-3-5-sonnet-20241022"
|
||||
|
||||
# ❌ Problematic in Streamlit - Provider/model format
|
||||
llm="ollama/llama3.2"
|
||||
llm="anthropic/claude-3-5-sonnet"
|
||||
```
|
||||
|
||||
### 3. Error Handling
|
||||
Implement comprehensive error handling:
|
||||
|
||||
```python
|
||||
try:
|
||||
# MCP initialization
|
||||
mcp_tools = MCP("your-mcp-command")
|
||||
agent = Agent(tools=mcp_tools, ...)
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"Initialization failed: {str(e)}")
|
||||
# Provide troubleshooting tips
|
||||
```
|
||||
|
||||
### 4. User Feedback
|
||||
Provide clear feedback during initialization:
|
||||
|
||||
```python
|
||||
with st.spinner("🔄 Initializing AI agent and MCP tools..."):
|
||||
# Initialization code here
|
||||
pass
|
||||
|
||||
if initialization_successful:
|
||||
st.success("✅ AI Assistant initialized successfully!")
|
||||
else:
|
||||
st.error("❌ Initialization failed")
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### MCP Tools Not Found
|
||||
If you get "MCP tool cannot be found" errors:
|
||||
|
||||
1. **Check MCP Server Command**: Ensure the MCP server command is correct and the server starts successfully
|
||||
2. **Verify Dependencies**: Make sure all required dependencies (Node.js, npm, etc.) are installed
|
||||
3. **Test Manually**: Try running the MCP server command manually first
|
||||
4. **Enable Debug Mode**: Set `debug=True` in MCP constructor for detailed logs
|
||||
|
||||
### Tool Calling Issues
|
||||
If tools aren't being called properly:
|
||||
|
||||
1. **Use Standard LLM Format**: Avoid provider/model format like `"ollama/llama3.2"`
|
||||
2. **Check API Keys**: Ensure proper environment variables are set for your chosen LLM
|
||||
3. **Session State**: Verify agent is properly stored in session state
|
||||
4. **Timeout Settings**: Increase timeout if needed: `MCP(command, timeout=120)`
|
||||
|
||||
### Threading Conflicts
|
||||
If you encounter threading-related errors:
|
||||
|
||||
1. **Single Agent Instance**: Use only one agent instance per session
|
||||
2. **Proper Cleanup**: Let Streamlit handle cleanup naturally
|
||||
3. **Avoid Manual Threading**: Don't create additional threads in Streamlit
|
||||
|
||||
## Complete Working Example
|
||||
|
||||
For a complete, production-ready example, see:
|
||||
- [`examples/python/ui/mcp-streamlit-airbnb.py`](https://github.com/MervinPraison/PraisonAI/blob/main/examples/python/ui/mcp-streamlit-airbnb.py)
|
||||
|
||||
This example includes:
|
||||
- ✅ Proper session state management
|
||||
- ✅ Comprehensive error handling
|
||||
- ✅ User-friendly interface
|
||||
- ✅ Debug mode support
|
||||
- ✅ Troubleshooting guidance
|
||||
- ✅ Configuration options
|
||||
|
||||
## Environment Setup
|
||||
|
||||
Make sure your environment has the required dependencies:
|
||||
|
||||
```bash
|
||||
# Install PraisonAI Agents
|
||||
pip install praisonaiagents
|
||||
|
||||
# Install Streamlit
|
||||
pip install streamlit
|
||||
|
||||
# For Airbnb MCP server example
|
||||
npm install -g @openbnb/mcp-server-airbnb
|
||||
|
||||
# Set environment variables for your chosen LLM
|
||||
export OPENAI_API_KEY="your-key"
|
||||
# or
|
||||
export ANTHROPIC_API_KEY="your-key"
|
||||
```
|
||||
|
||||
## Running the Example
|
||||
|
||||
```bash
|
||||
# Run the working example
|
||||
streamlit run examples/python/ui/mcp-streamlit-airbnb.py
|
||||
|
||||
# Or create your own based on the patterns above
|
||||
streamlit run your-mcp-app.py
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
||||
The key to successfully using MCP with Streamlit is:
|
||||
|
||||
1. **Proper session state management** - Initialize agent only once
|
||||
2. **Standard LLM format** - Avoid provider/model format in Streamlit
|
||||
3. **Comprehensive error handling** - Provide user feedback
|
||||
4. **Correct usage patterns** - Follow Streamlit best practices
|
||||
|
||||
By following these guidelines, MCP tools will work reliably in your Streamlit applications without requiring any modifications to the core MCP implementation.
|
||||
@@ -0,0 +1,159 @@
|
||||
"""
|
||||
Knowledge Agent with Reranker Example
|
||||
===================================
|
||||
|
||||
This example demonstrates how to use Mem0's built-in reranking feature
|
||||
with the Knowledge Agent for improved search result relevance.
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.knowledge import Knowledge
|
||||
|
||||
def main():
|
||||
"""
|
||||
Demonstrates knowledge search with and without reranking.
|
||||
"""
|
||||
|
||||
# Create sample content for knowledge base
|
||||
sample_documents = [
|
||||
"Artificial Intelligence is a field of computer science that aims to create machines that can perform tasks that typically require human intelligence.",
|
||||
"Machine Learning is a subset of AI that enables computers to learn and improve from experience without being explicitly programmed.",
|
||||
"Deep Learning is a type of machine learning that uses neural networks with multiple layers to model and understand complex patterns.",
|
||||
"Natural Language Processing (NLP) is a branch of AI that helps computers understand, interpret and manipulate human language.",
|
||||
"Computer Vision is an AI field that trains computers to interpret and understand the visual world from digital images and videos.",
|
||||
"Reinforcement Learning is a type of machine learning where an agent learns to make decisions by taking actions in an environment.",
|
||||
"Neural Networks are computing systems inspired by biological neural networks that constitute animal brains.",
|
||||
"Data Science combines domain expertise, programming skills, and knowledge of mathematics and statistics to extract insights from data."
|
||||
]
|
||||
|
||||
print("=== Knowledge Agent Reranker Example ===\n")
|
||||
|
||||
# Example 1: Basic Knowledge without reranking
|
||||
print("1. Creating Knowledge base without reranking...")
|
||||
basic_config = {
|
||||
"vector_store": {
|
||||
"provider": "chroma",
|
||||
"config": {
|
||||
"collection_name": "knowledge_basic",
|
||||
"path": ".praison_basic"
|
||||
}
|
||||
},
|
||||
"reranker": {
|
||||
"enabled": True,
|
||||
"default_rerank": False # Disabled by default
|
||||
}
|
||||
}
|
||||
|
||||
basic_knowledge = Knowledge(config=basic_config, verbose=1)
|
||||
|
||||
# Add documents to knowledge base
|
||||
for i, doc in enumerate(sample_documents):
|
||||
basic_knowledge.store(doc, metadata={"doc_id": f"doc_{i}", "category": "AI"})
|
||||
|
||||
# Example 2: Knowledge with reranking enabled by default
|
||||
print("\n2. Creating Knowledge base with reranking enabled...")
|
||||
rerank_config = {
|
||||
"vector_store": {
|
||||
"provider": "chroma",
|
||||
"config": {
|
||||
"collection_name": "knowledge_rerank",
|
||||
"path": ".praison_rerank"
|
||||
}
|
||||
},
|
||||
"reranker": {
|
||||
"enabled": True,
|
||||
"default_rerank": True # Enabled by default
|
||||
}
|
||||
}
|
||||
|
||||
rerank_knowledge = Knowledge(config=rerank_config, verbose=1)
|
||||
|
||||
# Add same documents to reranking knowledge base
|
||||
for i, doc in enumerate(sample_documents):
|
||||
rerank_knowledge.store(doc, metadata={"doc_id": f"doc_{i}", "category": "AI"})
|
||||
|
||||
print("\n=== Search Results Comparison ===\n")
|
||||
|
||||
query = "What is machine learning and how does it work?"
|
||||
|
||||
# Search without reranking
|
||||
print("3. Search WITHOUT reranking:")
|
||||
basic_results = basic_knowledge.search(query, rerank=False)
|
||||
print(f"Query: {query}")
|
||||
print(f"Results (limit=3):")
|
||||
for i, result in enumerate(basic_results[:3]):
|
||||
text = result.get('memory', result.get('text', str(result)))
|
||||
score = result.get('score', 'N/A')
|
||||
print(f" {i+1}. Score: {score}")
|
||||
print(f" Text: {text[:100]}...")
|
||||
print()
|
||||
|
||||
# Search with reranking explicitly enabled
|
||||
print("4. Search WITH reranking:")
|
||||
rerank_results = basic_knowledge.search(query, rerank=True)
|
||||
print(f"Query: {query}")
|
||||
print(f"Results (limit=3):")
|
||||
for i, result in enumerate(rerank_results[:3]):
|
||||
text = result.get('memory', result.get('text', str(result)))
|
||||
score = result.get('score', 'N/A')
|
||||
print(f" {i+1}. Score: {score}")
|
||||
print(f" Text: {text[:100]}...")
|
||||
print()
|
||||
|
||||
# Search using config default (reranking enabled)
|
||||
print("5. Search using config default (reranking enabled):")
|
||||
default_results = rerank_knowledge.search(query) # Will use default_rerank=True
|
||||
print(f"Query: {query}")
|
||||
print(f"Results (limit=3):")
|
||||
for i, result in enumerate(default_results[:3]):
|
||||
text = result.get('memory', result.get('text', str(result)))
|
||||
score = result.get('score', 'N/A')
|
||||
print(f" {i+1}. Score: {score}")
|
||||
print(f" Text: {text[:100]}...")
|
||||
print()
|
||||
|
||||
print("=== Advanced Search Options ===\n")
|
||||
|
||||
# Example with additional Mem0 advanced retrieval options
|
||||
print("6. Search with keyword search + reranking:")
|
||||
advanced_results = basic_knowledge.search(
|
||||
query,
|
||||
rerank=True,
|
||||
keyword_search=True, # Enable keyword search for better recall
|
||||
filter_memories=True # Enable filtering for better precision
|
||||
)
|
||||
print(f"Query: {query}")
|
||||
print(f"Advanced results with keyword_search=True, filter_memories=True:")
|
||||
for i, result in enumerate(advanced_results[:3]):
|
||||
text = result.get('memory', result.get('text', str(result)))
|
||||
score = result.get('score', 'N/A')
|
||||
print(f" {i+1}. Score: {score}")
|
||||
print(f" Text: {text[:100]}...")
|
||||
print()
|
||||
|
||||
# Example with Agent using reranking knowledge
|
||||
print("\n=== Agent with Reranking Knowledge ===\n")
|
||||
|
||||
agent = Agent(
|
||||
name="AI Research Assistant",
|
||||
instructions="You are an AI research assistant. Use your knowledge base to answer questions about artificial intelligence topics.",
|
||||
knowledge=sample_documents, # Will use default knowledge config
|
||||
knowledge_config=rerank_config, # Use reranking config
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
print("7. Agent with reranking knowledge:")
|
||||
response = agent.run("Explain the difference between machine learning and deep learning, and how they relate to AI.")
|
||||
print(f"Agent response: {response}")
|
||||
|
||||
print("\n=== Performance Notes ===")
|
||||
print("- Basic search: ~10ms latency")
|
||||
print("- Reranking: 150-200ms additional latency")
|
||||
print("- Reranking improves relevance ordering significantly")
|
||||
print("- Use rerank=True for better results when query relevance is critical")
|
||||
print("- Use keyword_search=True for better recall")
|
||||
print("- Use filter_memories=True for better precision")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,252 @@
|
||||
import streamlit as st
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.mcp import MCP
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
# Configure page
|
||||
st.set_page_config(
|
||||
page_title="AI Airbnb Assistant",
|
||||
page_icon="🏠",
|
||||
layout="wide"
|
||||
)
|
||||
|
||||
st.title("🏠 AI Airbnb Assistant")
|
||||
st.write("Enter your accommodation search query below to get started!")
|
||||
|
||||
# Add configuration options in sidebar
|
||||
with st.sidebar:
|
||||
st.header("⚙️ Configuration")
|
||||
|
||||
# LLM Model Selection
|
||||
llm_model = st.selectbox(
|
||||
"Choose LLM Model",
|
||||
options=[
|
||||
"gpt-4o-mini",
|
||||
"gpt-4o",
|
||||
"gpt-3.5-turbo",
|
||||
"claude-3-5-sonnet-20241022",
|
||||
"claude-3-5-haiku-20241022"
|
||||
],
|
||||
index=0,
|
||||
help="Select the language model to use. Avoid provider/model format like 'ollama/llama3.2' in Streamlit."
|
||||
)
|
||||
|
||||
# Debug Mode
|
||||
debug_mode = st.checkbox(
|
||||
"Enable Debug Mode",
|
||||
value=False,
|
||||
help="Enable detailed logging for troubleshooting"
|
||||
)
|
||||
|
||||
# MCP Server Command
|
||||
mcp_command = st.text_input(
|
||||
"MCP Server Command",
|
||||
value="npx -y @openbnb/mcp-server-airbnb --ignore-robots-txt",
|
||||
help="The command to start the MCP server"
|
||||
)
|
||||
|
||||
# Initialize session state
|
||||
if "agent_initialized" not in st.session_state:
|
||||
st.session_state.agent_initialized = False
|
||||
st.session_state.agent = None
|
||||
st.session_state.init_error = None
|
||||
|
||||
# Function to initialize agent
|
||||
def initialize_agent():
|
||||
"""Initialize the agent with proper error handling."""
|
||||
try:
|
||||
with st.spinner("🔄 Initializing AI agent and MCP tools..."):
|
||||
if debug_mode:
|
||||
st.info("🐛 Debug mode enabled - check console for detailed logs")
|
||||
|
||||
# Create MCP tools
|
||||
mcp_tools = MCP(
|
||||
mcp_command,
|
||||
timeout=60,
|
||||
debug=debug_mode
|
||||
)
|
||||
|
||||
# Create agent with session state management
|
||||
agent = Agent(
|
||||
instructions="""You are a helpful Airbnb assistant. Help users find and book apartments.
|
||||
Use the available tools to search for accommodations, get property details, and provide helpful recommendations.
|
||||
Always be polite and provide comprehensive information about properties.""",
|
||||
llm=llm_model,
|
||||
tools=mcp_tools,
|
||||
verbose=debug_mode
|
||||
)
|
||||
|
||||
return agent, None
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Failed to initialize agent: {str(e)}"
|
||||
if debug_mode:
|
||||
error_msg += f"\n\nFull traceback:\n{traceback.format_exc()}"
|
||||
return None, error_msg
|
||||
|
||||
# Agent initialization section
|
||||
if not st.session_state.agent_initialized:
|
||||
if st.button("🚀 Initialize AI Assistant", type="primary"):
|
||||
agent, error = initialize_agent()
|
||||
|
||||
if agent:
|
||||
st.session_state.agent = agent
|
||||
st.session_state.agent_initialized = True
|
||||
st.session_state.init_error = None
|
||||
st.success("✅ AI Assistant initialized successfully!")
|
||||
st.rerun()
|
||||
else:
|
||||
st.session_state.init_error = error
|
||||
st.error(f"❌ Initialization failed: {error}")
|
||||
|
||||
# Show initialization status
|
||||
if st.session_state.init_error:
|
||||
st.error(f"❌ Initialization Error: {st.session_state.init_error}")
|
||||
|
||||
with st.expander("🔧 Troubleshooting Tips"):
|
||||
st.markdown("""
|
||||
**Common issues and solutions:**
|
||||
|
||||
1. **MCP Server Issues:**
|
||||
- Ensure Node.js and npm are installed
|
||||
- Check if the MCP server command is correct
|
||||
- Try running the MCP command manually first
|
||||
|
||||
2. **LLM Model Issues:**
|
||||
- Make sure you have API keys set as environment variables
|
||||
- Avoid provider/model formats (e.g., "ollama/llama3.2") in Streamlit
|
||||
- Use standard model names (e.g., "gpt-4o-mini")
|
||||
|
||||
3. **Network/Timeout Issues:**
|
||||
- Check your internet connection
|
||||
- Increase timeout if needed
|
||||
- Enable debug mode for more details
|
||||
|
||||
4. **Environment Issues:**
|
||||
- Make sure all required packages are installed
|
||||
- Check Python version compatibility
|
||||
""")
|
||||
|
||||
# Main chat interface (only show if agent is initialized)
|
||||
if st.session_state.agent_initialized and st.session_state.agent:
|
||||
st.success("✅ AI Assistant is ready!")
|
||||
|
||||
# Create two columns for better layout
|
||||
col1, col2 = st.columns([2, 1])
|
||||
|
||||
with col1:
|
||||
# Query input
|
||||
query = st.text_area(
|
||||
"🔍 What are you looking for?",
|
||||
placeholder="e.g., 'Find a 2-bedroom apartment in Paris for next weekend' or 'Show me luxury stays in Tokyo under $200/night'",
|
||||
height=100
|
||||
)
|
||||
|
||||
# Search button
|
||||
search_col1, search_col2 = st.columns([1, 4])
|
||||
with search_col1:
|
||||
search_button = st.button("🔍 Search", type="primary", use_container_width=True)
|
||||
|
||||
with col2:
|
||||
# Tips and examples
|
||||
with st.container():
|
||||
st.markdown("### 💡 Tips")
|
||||
st.markdown("""
|
||||
- Be specific about location and dates
|
||||
- Mention budget preferences
|
||||
- Include number of guests
|
||||
- Ask about amenities
|
||||
""")
|
||||
|
||||
with st.expander("📝 Example Queries"):
|
||||
st.markdown("""
|
||||
- "Find a beachfront apartment in Barcelona for 4 people"
|
||||
- "Show me pet-friendly stays in London under £150/night"
|
||||
- "I need a place with a kitchen near downtown Tokyo"
|
||||
- "Find luxury accommodations in NYC for a business trip"
|
||||
""")
|
||||
|
||||
# Handle search
|
||||
if search_button and query:
|
||||
if not query.strip():
|
||||
st.warning("⚠️ Please enter a search query")
|
||||
else:
|
||||
try:
|
||||
with st.spinner("🔍 Searching for accommodations..."):
|
||||
# Add query to chat history
|
||||
if "chat_history" not in st.session_state:
|
||||
st.session_state.chat_history = []
|
||||
|
||||
st.session_state.chat_history.append({"role": "user", "content": query})
|
||||
|
||||
# Get response from agent
|
||||
result = st.session_state.agent.start(query)
|
||||
|
||||
st.session_state.chat_history.append({"role": "assistant", "content": result})
|
||||
|
||||
# Display result
|
||||
st.markdown("### 🏠 Search Results")
|
||||
st.markdown(result)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Search failed: {str(e)}"
|
||||
st.error(f"❌ {error_msg}")
|
||||
|
||||
if debug_mode:
|
||||
st.error(f"Full traceback:\n{traceback.format_exc()}")
|
||||
|
||||
# Chat history section
|
||||
if "chat_history" in st.session_state and st.session_state.chat_history:
|
||||
st.markdown("---")
|
||||
st.markdown("### 💬 Conversation History")
|
||||
|
||||
for i, message in enumerate(st.session_state.chat_history):
|
||||
if message["role"] == "user":
|
||||
st.markdown(f"**🙋 You:** {message['content']}")
|
||||
else:
|
||||
st.markdown(f"**🤖 Assistant:** {message['content']}")
|
||||
|
||||
if i < len(st.session_state.chat_history) - 1:
|
||||
st.markdown("---")
|
||||
|
||||
# Clear history button
|
||||
if st.button("🗑️ Clear History"):
|
||||
st.session_state.chat_history = []
|
||||
st.rerun()
|
||||
|
||||
else:
|
||||
# Show instructions for first-time users
|
||||
st.info("👆 Please initialize the AI Assistant first using the button above.")
|
||||
|
||||
with st.expander("ℹ️ About this Application"):
|
||||
st.markdown("""
|
||||
This Streamlit application demonstrates how to properly integrate MCP (Model Context Protocol)
|
||||
tools with PraisonAI Agents in a Streamlit environment.
|
||||
|
||||
**Key Features:**
|
||||
- ✅ Proper session state management for Streamlit
|
||||
- ✅ Comprehensive error handling and user feedback
|
||||
- ✅ Debug mode for troubleshooting
|
||||
- ✅ Flexible LLM model selection
|
||||
- ✅ Clean and intuitive user interface
|
||||
|
||||
**Technical Implementation:**
|
||||
- Uses session state to prevent agent re-initialization
|
||||
- Implements proper error handling for MCP setup
|
||||
- Provides detailed troubleshooting guidance
|
||||
- Demonstrates best practices for Streamlit + MCP integration
|
||||
""")
|
||||
|
||||
# Footer
|
||||
st.markdown("---")
|
||||
st.markdown(
|
||||
"""
|
||||
<div style='text-align: center; color: gray;'>
|
||||
Built with PraisonAI Agents + MCP •
|
||||
<a href='https://github.com/MervinPraison/PraisonAI' target='_blank'>GitHub</a> •
|
||||
<a href='https://docs.praisonai.com' target='_blank'>Documentation</a>
|
||||
</div>
|
||||
""",
|
||||
unsafe_allow_html=True
|
||||
)
|
||||
@@ -0,0 +1,54 @@
|
||||
import streamlit as st
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.mcp import MCP
|
||||
|
||||
st.title("AI Airbnb")
|
||||
st.write("Enter your research query below to get started!")
|
||||
|
||||
# CRITICAL: Initialize agent in session state to prevent re-initialization
|
||||
if "agent" not in st.session_state:
|
||||
try:
|
||||
with st.spinner("Initializing AI agent..."):
|
||||
# Initialize agent only once and store in session state
|
||||
st.session_state.agent = Agent(
|
||||
instructions="You help book apartments on Airbnb.",
|
||||
llm="gpt-4o-mini", # ✅ Use standard format instead of "ollama/llama3.2"
|
||||
tools=MCP("npx -y @openbnb/mcp-server-airbnb --ignore-robots-txt"),
|
||||
verbose=True # Enable debugging
|
||||
)
|
||||
st.success("✅ Agent initialized successfully!")
|
||||
except Exception as e:
|
||||
st.error(f"❌ Failed to initialize agent: {str(e)}")
|
||||
st.stop()
|
||||
|
||||
# Create the input field
|
||||
query = st.text_input("Query")
|
||||
|
||||
# Add a search button
|
||||
if st.button("Search"):
|
||||
if query:
|
||||
try:
|
||||
with st.spinner("Researching..."):
|
||||
# Use the agent from session state (already initialized)
|
||||
result = st.session_state.agent.start(query)
|
||||
st.write(result)
|
||||
except Exception as e:
|
||||
st.error(f"❌ Search failed: {str(e)}")
|
||||
else:
|
||||
st.warning("Please enter a query")
|
||||
|
||||
# Add troubleshooting information
|
||||
with st.expander("🔧 Troubleshooting"):
|
||||
st.markdown("""
|
||||
**Key differences from problematic code:**
|
||||
|
||||
1. **Session State**: Agent is initialized once in `st.session_state.agent`
|
||||
2. **LLM Format**: Using `"gpt-4o-mini"` instead of `"ollama/llama3.2"`
|
||||
3. **Error Handling**: Proper try-catch blocks with user feedback
|
||||
4. **Initialization Check**: Agent only created once, not on every interaction
|
||||
|
||||
**If you still have issues:**
|
||||
- Make sure you have Node.js installed for the MCP server
|
||||
- Set your OpenAI API key: `export OPENAI_API_KEY="your-key"`
|
||||
- Try the debug mode by setting `verbose=True`
|
||||
""")
|
||||
@@ -101,7 +101,11 @@ class Knowledge:
|
||||
}
|
||||
},
|
||||
"version": "v1.1",
|
||||
"custom_prompt": "Return {{\"facts\": [text]}} where text is the exact input provided and json response"
|
||||
"custom_prompt": "Return {{\"facts\": [text]}} where text is the exact input provided and json response",
|
||||
"reranker": {
|
||||
"enabled": False,
|
||||
"default_rerank": False
|
||||
}
|
||||
}
|
||||
|
||||
# If config is provided, merge it with base config
|
||||
@@ -129,6 +133,10 @@ class Knowledge:
|
||||
# Merge llm config if provided
|
||||
if "llm" in self._config:
|
||||
base_config["llm"] = self._config["llm"]
|
||||
|
||||
# Merge reranker config if provided
|
||||
if "reranker" in self._config:
|
||||
base_config["reranker"].update(self._config["reranker"])
|
||||
return base_config
|
||||
|
||||
@cached_property
|
||||
@@ -191,9 +199,25 @@ class Knowledge:
|
||||
"""Retrieve a specific memory by ID."""
|
||||
return self.memory.get(memory_id)
|
||||
|
||||
def search(self, query, user_id=None, agent_id=None, run_id=None):
|
||||
"""Search for memories related to a query."""
|
||||
return self.memory.search(query, user_id=user_id, agent_id=agent_id, run_id=run_id)
|
||||
def search(self, query, user_id=None, agent_id=None, run_id=None, rerank=None, **kwargs):
|
||||
"""Search for memories related to a query.
|
||||
|
||||
Args:
|
||||
query: The search query string
|
||||
user_id: Optional user ID for user-specific search
|
||||
agent_id: Optional agent ID for agent-specific search
|
||||
run_id: Optional run ID for run-specific search
|
||||
rerank: Whether to use Mem0's advanced reranking. If None, uses config default
|
||||
**kwargs: Additional search parameters to pass to Mem0 (keyword_search, filter_memories, etc.)
|
||||
|
||||
Returns:
|
||||
List of search results, reranked if rerank=True
|
||||
"""
|
||||
# Use config default if rerank not explicitly specified
|
||||
if rerank is None:
|
||||
rerank = self.config.get("reranker", {}).get("default_rerank", False)
|
||||
|
||||
return self.memory.search(query, user_id=user_id, agent_id=agent_id, run_id=run_id, rerank=rerank, **kwargs)
|
||||
|
||||
def update(self, memory_id, data):
|
||||
"""Update a memory."""
|
||||
|
||||
@@ -299,7 +299,11 @@ class MCP:
|
||||
required_params.append(name)
|
||||
|
||||
# Create the function signature
|
||||
params = []
|
||||
# Separate required and optional parameters to ensure proper ordering
|
||||
# (required parameters must come before optional parameters)
|
||||
required_param_objects = []
|
||||
optional_param_objects = []
|
||||
|
||||
for name in param_names:
|
||||
is_required = name in required_params
|
||||
param = inspect.Parameter(
|
||||
@@ -308,7 +312,14 @@ class MCP:
|
||||
default=inspect.Parameter.empty if is_required else None,
|
||||
annotation=param_annotations.get(name, Any)
|
||||
)
|
||||
params.append(param)
|
||||
|
||||
if is_required:
|
||||
required_param_objects.append(param)
|
||||
else:
|
||||
optional_param_objects.append(param)
|
||||
|
||||
# Combine parameters with required first, then optional
|
||||
params = required_param_objects + optional_param_objects
|
||||
|
||||
# Create function template to be properly decorated
|
||||
def template_function(*args, **kwargs):
|
||||
|
||||
@@ -262,13 +262,18 @@ class Memory:
|
||||
query: str,
|
||||
limit: int = 5,
|
||||
min_quality: float = 0.0,
|
||||
relevance_cutoff: float = 0.0
|
||||
relevance_cutoff: float = 0.0,
|
||||
rerank: bool = False,
|
||||
**kwargs
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Search short-term memory with optional quality filter"""
|
||||
self._log_verbose(f"Searching short memory for: {query}")
|
||||
|
||||
if self.use_mem0 and hasattr(self, "mem0_client"):
|
||||
results = self.mem0_client.search(query=query, limit=limit)
|
||||
# Pass rerank and other kwargs to Mem0 search
|
||||
search_params = {"query": query, "limit": limit, "rerank": rerank}
|
||||
search_params.update(kwargs)
|
||||
results = self.mem0_client.search(**search_params)
|
||||
filtered = [r for r in results if r.get("score", 1.0) >= relevance_cutoff]
|
||||
return filtered
|
||||
|
||||
@@ -439,7 +444,9 @@ class Memory:
|
||||
query: str,
|
||||
limit: int = 5,
|
||||
relevance_cutoff: float = 0.0,
|
||||
min_quality: float = 0.0
|
||||
min_quality: float = 0.0,
|
||||
rerank: bool = False,
|
||||
**kwargs
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Search long-term memory with optional quality filter"""
|
||||
self._log_verbose(f"Searching long memory for: {query}")
|
||||
@@ -448,7 +455,10 @@ class Memory:
|
||||
found = []
|
||||
|
||||
if self.use_mem0 and hasattr(self, "mem0_client"):
|
||||
results = self.mem0_client.search(query=query, limit=limit)
|
||||
# Pass rerank and other kwargs to Mem0 search
|
||||
search_params = {"query": query, "limit": limit, "rerank": rerank}
|
||||
search_params.update(kwargs)
|
||||
results = self.mem0_client.search(**search_params)
|
||||
# Filter by quality
|
||||
filtered = [r for r in results if r.get("metadata", {}).get("quality", 0.0) >= min_quality]
|
||||
logger.info(f"Found {len(filtered)} results in Mem0")
|
||||
@@ -595,12 +605,15 @@ class Memory:
|
||||
else:
|
||||
self.store_long_term(text, metadata=meta)
|
||||
|
||||
def search_user_memory(self, user_id: str, query: str, limit: int = 5) -> List[Dict[str, Any]]:
|
||||
def search_user_memory(self, user_id: str, query: str, limit: int = 5, rerank: bool = False, **kwargs) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
If mem0 is used, pass user_id in. Otherwise fallback to local filter on user in metadata.
|
||||
"""
|
||||
if self.use_mem0 and hasattr(self, "mem0_client"):
|
||||
return self.mem0_client.search(query=query, limit=limit, user_id=user_id)
|
||||
# Pass rerank and other kwargs to Mem0 search
|
||||
search_params = {"query": query, "limit": limit, "user_id": user_id, "rerank": rerank}
|
||||
search_params.update(kwargs)
|
||||
return self.mem0_client.search(**search_params)
|
||||
else:
|
||||
hits = self.search_long_term(query, limit=20)
|
||||
filtered = []
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.93"
|
||||
version = "0.0.95"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
requires-python = ">=3.10"
|
||||
authors = [
|
||||
|
||||
Generated
+1
-1
@@ -2233,7 +2233,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.93"
|
||||
version = "0.0.95"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "mcp" },
|
||||
|
||||
@@ -3,8 +3,8 @@ class Praisonai < Formula
|
||||
|
||||
desc "AI tools for various AI applications"
|
||||
homepage "https://github.com/MervinPraison/PraisonAI"
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.22.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.22.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.24.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.24.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -121,12 +121,12 @@ Tools are not available for {framework}. To use tools, install:
|
||||
mode=instructor.Mode.JSON,
|
||||
)
|
||||
|
||||
def generate(self):
|
||||
def generate(self, merge=False):
|
||||
"""
|
||||
Generates a team structure for the specified topic.
|
||||
|
||||
Args:
|
||||
None
|
||||
merge (bool): Whether to merge with existing agents.yaml file instead of overwriting.
|
||||
|
||||
Returns:
|
||||
str: The full path of the YAML file containing the generated team structure.
|
||||
@@ -149,46 +149,119 @@ Tools are not available for {framework}. To use tools, install:
|
||||
]
|
||||
)
|
||||
json_data = json.loads(response.model_dump_json())
|
||||
self.convert_and_save(json_data)
|
||||
self.convert_and_save(json_data, merge=merge)
|
||||
full_path = os.path.abspath(self.agent_file)
|
||||
return full_path
|
||||
|
||||
def convert_and_save(self, json_data):
|
||||
def convert_and_save(self, json_data, merge=False):
|
||||
"""Converts the provided JSON data into the desired YAML format and saves it to a file.
|
||||
|
||||
Args:
|
||||
json_data (dict): The JSON data representing the team structure.
|
||||
topic (str, optional): The topic to be inserted into the YAML. Defaults to "Artificial Intelligence".
|
||||
agent_file (str, optional): The name of the YAML file to save. Defaults to "test.yaml".
|
||||
merge (bool): Whether to merge with existing agents.yaml file instead of overwriting.
|
||||
"""
|
||||
|
||||
yaml_data = {
|
||||
"framework": self.framework,
|
||||
"topic": self.topic,
|
||||
"roles": {},
|
||||
"dependencies": []
|
||||
}
|
||||
|
||||
for role_id, role_details in json_data['roles'].items():
|
||||
yaml_data['roles'][role_id] = {
|
||||
"backstory": "" + role_details['backstory'],
|
||||
"goal": role_details['goal'],
|
||||
"role": role_details['role'],
|
||||
"tasks": {},
|
||||
# "tools": role_details.get('tools', []),
|
||||
"tools": ['']
|
||||
# Handle merge functionality
|
||||
if merge and os.path.exists(self.agent_file):
|
||||
yaml_data = self.merge_with_existing_agents(json_data)
|
||||
else:
|
||||
# Original behavior: create new yaml_data structure
|
||||
yaml_data = {
|
||||
"framework": self.framework,
|
||||
"topic": self.topic,
|
||||
"roles": {},
|
||||
"dependencies": []
|
||||
}
|
||||
|
||||
for task_id, task_details in role_details['tasks'].items():
|
||||
yaml_data['roles'][role_id]['tasks'][task_id] = {
|
||||
"description": "" + task_details['description'],
|
||||
"expected_output": "" + task_details['expected_output']
|
||||
for role_id, role_details in json_data['roles'].items():
|
||||
yaml_data['roles'][role_id] = {
|
||||
"backstory": "" + role_details['backstory'],
|
||||
"goal": role_details['goal'],
|
||||
"role": role_details['role'],
|
||||
"tasks": {},
|
||||
# "tools": role_details.get('tools', []),
|
||||
"tools": ['']
|
||||
}
|
||||
|
||||
for task_id, task_details in role_details['tasks'].items():
|
||||
yaml_data['roles'][role_id]['tasks'][task_id] = {
|
||||
"description": "" + task_details['description'],
|
||||
"expected_output": "" + task_details['expected_output']
|
||||
}
|
||||
|
||||
# Save to YAML file, maintaining the order
|
||||
with open(self.agent_file, 'w') as f:
|
||||
yaml.dump(yaml_data, f, allow_unicode=True, sort_keys=False)
|
||||
|
||||
def merge_with_existing_agents(self, new_json_data):
|
||||
"""
|
||||
Merge existing agents.yaml with new auto-generated agents.
|
||||
|
||||
Args:
|
||||
new_json_data (dict): The JSON data representing the new team structure.
|
||||
|
||||
Returns:
|
||||
dict: The merged YAML data structure.
|
||||
"""
|
||||
try:
|
||||
# Load existing agents.yaml
|
||||
with open(self.agent_file, 'r') as f:
|
||||
existing_data = yaml.safe_load(f)
|
||||
|
||||
if not existing_data:
|
||||
# If existing file is empty, treat as new file
|
||||
existing_data = {"roles": {}, "dependencies": []}
|
||||
except (yaml.YAMLError, FileNotFoundError) as e:
|
||||
logging.warning(f"Could not load existing agents file {self.agent_file}: {e}")
|
||||
logging.warning("Creating new file instead of merging")
|
||||
existing_data = {"roles": {}, "dependencies": []}
|
||||
|
||||
# Start with existing data structure
|
||||
merged_data = existing_data.copy()
|
||||
|
||||
# Ensure required fields exist
|
||||
if 'roles' not in merged_data:
|
||||
merged_data['roles'] = {}
|
||||
if 'dependencies' not in merged_data:
|
||||
merged_data['dependencies'] = []
|
||||
if 'framework' not in merged_data:
|
||||
merged_data['framework'] = self.framework
|
||||
|
||||
# Handle topic merging
|
||||
existing_topic = merged_data.get('topic', '')
|
||||
new_topic = self.topic
|
||||
if existing_topic and existing_topic != new_topic:
|
||||
merged_data['topic'] = f"{existing_topic} + {new_topic}"
|
||||
else:
|
||||
merged_data['topic'] = new_topic
|
||||
|
||||
# Merge new roles with existing ones
|
||||
for role_id, role_details in new_json_data['roles'].items():
|
||||
# Check for conflicts and rename if necessary
|
||||
final_role_id = role_id
|
||||
counter = 1
|
||||
while final_role_id in merged_data['roles']:
|
||||
final_role_id = f"{role_id}_auto_{counter}"
|
||||
counter += 1
|
||||
|
||||
# Add the new role
|
||||
merged_data['roles'][final_role_id] = {
|
||||
"backstory": "" + role_details['backstory'],
|
||||
"goal": role_details['goal'],
|
||||
"role": role_details['role'],
|
||||
"tasks": {},
|
||||
"tools": ['']
|
||||
}
|
||||
|
||||
# Add tasks for this role
|
||||
for task_id, task_details in role_details['tasks'].items():
|
||||
merged_data['roles'][final_role_id]['tasks'][task_id] = {
|
||||
"description": "" + task_details['description'],
|
||||
"expected_output": "" + task_details['expected_output']
|
||||
}
|
||||
|
||||
return merged_data
|
||||
|
||||
def get_user_content(self):
|
||||
"""
|
||||
Generates a prompt for the OpenAI API to generate a team structure.
|
||||
|
||||
@@ -5,6 +5,7 @@ import argparse
|
||||
from .version import __version__
|
||||
import yaml
|
||||
import os
|
||||
import time
|
||||
from rich import print
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
@@ -25,6 +26,7 @@ CALL_MODULE_AVAILABLE = False
|
||||
CREWAI_AVAILABLE = False
|
||||
AUTOGEN_AVAILABLE = False
|
||||
PRAISONAI_AVAILABLE = False
|
||||
TRAIN_AVAILABLE = False
|
||||
try:
|
||||
# Create necessary directories and set CHAINLIT_APP_ROOT
|
||||
if "CHAINLIT_APP_ROOT" not in os.environ:
|
||||
@@ -71,6 +73,12 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from unsloth import FastLanguageModel
|
||||
TRAIN_AVAILABLE = True
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
logging.basicConfig(level=os.environ.get('LOGLEVEL', 'INFO'), format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logging.getLogger('alembic').setLevel(logging.ERROR)
|
||||
logging.getLogger('gradio').setLevel(logging.ERROR)
|
||||
@@ -217,9 +225,66 @@ class PraisonAI:
|
||||
# If no command or direct_prompt, preserve agent_file from constructor (don't overwrite)
|
||||
|
||||
if args.deploy:
|
||||
from .deploy import CloudDeployer
|
||||
deployer = CloudDeployer()
|
||||
deployer.run_commands()
|
||||
if args.schedule or args.schedule_config:
|
||||
# Scheduled deployment
|
||||
from .scheduler import create_scheduler
|
||||
|
||||
# Load configuration from file if provided
|
||||
config = {"max_retries": args.max_retries}
|
||||
schedule_expr = args.schedule
|
||||
provider = args.provider
|
||||
|
||||
if args.schedule_config:
|
||||
try:
|
||||
with open(args.schedule_config, 'r') as f:
|
||||
file_config = yaml.safe_load(f)
|
||||
|
||||
# Extract deployment config
|
||||
deploy_config = file_config.get('deployment', {})
|
||||
schedule_expr = schedule_expr or deploy_config.get('schedule')
|
||||
provider = deploy_config.get('provider', provider)
|
||||
config['max_retries'] = deploy_config.get('max_retries', config['max_retries'])
|
||||
|
||||
# Apply environment variables if specified
|
||||
env_vars = file_config.get('environment', {})
|
||||
for key, value in env_vars.items():
|
||||
os.environ[key] = str(value)
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"Configuration file not found: {args.schedule_config}")
|
||||
sys.exit(1)
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error parsing configuration file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
if not schedule_expr:
|
||||
print("Error: Schedule expression required. Use --schedule or specify in config file.")
|
||||
sys.exit(1)
|
||||
|
||||
scheduler = create_scheduler(provider=provider, config=config)
|
||||
|
||||
print(f"Starting scheduled deployment with schedule: {schedule_expr}")
|
||||
print(f"Provider: {provider}")
|
||||
print(f"Max retries: {config['max_retries']}")
|
||||
print("Press Ctrl+C to stop the scheduler")
|
||||
|
||||
if scheduler.start(schedule_expr, config['max_retries']):
|
||||
try:
|
||||
# Keep the main thread alive
|
||||
while scheduler.is_running:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopping scheduler...")
|
||||
scheduler.stop()
|
||||
print("Scheduler stopped successfully")
|
||||
else:
|
||||
print("Failed to start scheduler")
|
||||
sys.exit(1)
|
||||
else:
|
||||
# One-time deployment (backward compatible)
|
||||
from .deploy import CloudDeployer
|
||||
deployer = CloudDeployer()
|
||||
deployer.run_commands()
|
||||
return
|
||||
|
||||
if getattr(args, 'chat', False):
|
||||
@@ -242,6 +307,11 @@ class PraisonAI:
|
||||
return
|
||||
|
||||
if args.command == 'train':
|
||||
if not TRAIN_AVAILABLE:
|
||||
print("[red]ERROR: Training dependencies not installed. Install with:[/red]")
|
||||
print("\npip install \"praisonai[train]\"")
|
||||
print("Or run: praisonai train init\n")
|
||||
sys.exit(1)
|
||||
package_root = os.path.dirname(os.path.abspath(__file__))
|
||||
config_yaml_destination = os.path.join(os.getcwd(), 'config.yaml')
|
||||
|
||||
@@ -347,7 +417,7 @@ class PraisonAI:
|
||||
|
||||
self.agent_file = "test.yaml"
|
||||
generator = AutoGenerator(topic=self.topic, framework=self.framework, agent_file=self.agent_file)
|
||||
self.agent_file = generator.generate()
|
||||
self.agent_file = generator.generate(merge=getattr(args, 'merge', False))
|
||||
agents_generator = AgentsGenerator(self.agent_file, self.framework, self.config_list)
|
||||
result = agents_generator.generate_crew_and_kickoff()
|
||||
print(result)
|
||||
@@ -360,7 +430,7 @@ class PraisonAI:
|
||||
|
||||
self.agent_file = "agents.yaml"
|
||||
generator = AutoGenerator(topic=self.topic, framework=self.framework, agent_file=self.agent_file)
|
||||
self.agent_file = generator.generate()
|
||||
self.agent_file = generator.generate(merge=getattr(args, 'merge', False))
|
||||
print(f"File {self.agent_file} created successfully")
|
||||
return f"File {self.agent_file} created successfully"
|
||||
|
||||
@@ -406,6 +476,36 @@ class PraisonAI:
|
||||
'unittest' in sys.modules
|
||||
)
|
||||
|
||||
# Check if we're being used as a library (not from praisonai CLI)
|
||||
# Skip CLI parsing to avoid conflicts with applications like Fabric
|
||||
is_library_usage = (
|
||||
'praisonai' not in sys.argv[0] and
|
||||
not in_test_env
|
||||
)
|
||||
|
||||
if is_library_usage:
|
||||
# Return default args when used as library to prevent CLI conflicts
|
||||
default_args = argparse.Namespace()
|
||||
default_args.framework = None
|
||||
default_args.ui = None
|
||||
default_args.auto = None
|
||||
default_args.init = None
|
||||
default_args.command = None
|
||||
default_args.deploy = False
|
||||
default_args.schedule = None
|
||||
default_args.schedule_config = None
|
||||
default_args.provider = "gcp"
|
||||
default_args.max_retries = 3
|
||||
default_args.model = None
|
||||
default_args.llm = None
|
||||
default_args.hf = None
|
||||
default_args.ollama = None
|
||||
default_args.dataset = "yahma/alpaca-cleaned"
|
||||
default_args.realtime = False
|
||||
default_args.call = False
|
||||
default_args.public = False
|
||||
return default_args
|
||||
|
||||
# Define special commands
|
||||
special_commands = ['chat', 'code', 'call', 'realtime', 'train', 'ui']
|
||||
|
||||
@@ -416,6 +516,10 @@ class PraisonAI:
|
||||
parser.add_argument("--init", nargs=argparse.REMAINDER, help="Initialize agents with optional topic")
|
||||
parser.add_argument("command", nargs="?", help="Command to run or direct prompt")
|
||||
parser.add_argument("--deploy", action="store_true", help="Deploy the application")
|
||||
parser.add_argument("--schedule", type=str, help="Schedule deployment (e.g., 'daily', 'hourly', '*/6h', '3600')")
|
||||
parser.add_argument("--schedule-config", type=str, help="Path to scheduling configuration file")
|
||||
parser.add_argument("--provider", type=str, default="gcp", help="Deployment provider (gcp, aws, azure)")
|
||||
parser.add_argument("--max-retries", type=int, default=3, help="Maximum retry attempts for scheduled deployments")
|
||||
parser.add_argument("--model", type=str, help="Model name")
|
||||
parser.add_argument("--llm", type=str, help="LLM model to use for direct prompts")
|
||||
parser.add_argument("--hf", type=str, help="Hugging Face model name")
|
||||
@@ -424,6 +528,7 @@ class PraisonAI:
|
||||
parser.add_argument("--realtime", action="store_true", help="Start the realtime voice interaction interface")
|
||||
parser.add_argument("--call", action="store_true", help="Start the PraisonAI Call server")
|
||||
parser.add_argument("--public", action="store_true", help="Use ngrok to expose the server publicly (only with --call)")
|
||||
parser.add_argument("--merge", action="store_true", help="Merge existing agents.yaml with auto-generated agents instead of overwriting")
|
||||
|
||||
# If we're in a test environment, parse with empty args to avoid pytest interference
|
||||
if in_test_env:
|
||||
@@ -509,6 +614,11 @@ class PraisonAI:
|
||||
sys.exit(0)
|
||||
|
||||
elif args.command == 'train':
|
||||
if not TRAIN_AVAILABLE:
|
||||
print("[red]ERROR: Training dependencies not installed. Install with:[/red]")
|
||||
print("\npip install \"praisonai[train]\"")
|
||||
print("Or run: praisonai train init\n")
|
||||
sys.exit(1)
|
||||
package_root = os.path.dirname(os.path.abspath(__file__))
|
||||
config_yaml_destination = os.path.join(os.getcwd(), 'config.yaml')
|
||||
|
||||
|
||||
@@ -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.2.22 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.24 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Union, Optional, Callable, Dict, Any
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DeployerInterface(ABC):
|
||||
"""Abstract interface for deployers to ensure provider compatibility."""
|
||||
|
||||
@abstractmethod
|
||||
def deploy(self) -> bool:
|
||||
"""Execute deployment. Returns True on success, False on failure."""
|
||||
pass
|
||||
|
||||
class CloudDeployerAdapter(DeployerInterface):
|
||||
"""Adapter for existing CloudDeployer to match interface."""
|
||||
|
||||
def __init__(self):
|
||||
from .deploy import CloudDeployer
|
||||
self._deployer = CloudDeployer()
|
||||
|
||||
def deploy(self) -> bool:
|
||||
"""Execute deployment using CloudDeployer."""
|
||||
try:
|
||||
self._deployer.run_commands()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Deployment failed: {e}")
|
||||
return False
|
||||
|
||||
class ScheduleParser:
|
||||
"""Parse schedule expressions into intervals."""
|
||||
|
||||
@staticmethod
|
||||
def parse(schedule_expr: str) -> int:
|
||||
"""
|
||||
Parse schedule expression and return interval in seconds.
|
||||
|
||||
Supported formats:
|
||||
- "daily" -> 86400 seconds
|
||||
- "hourly" -> 3600 seconds
|
||||
- "*/30m" -> 1800 seconds (every 30 minutes)
|
||||
- "*/6h" -> 21600 seconds (every 6 hours)
|
||||
- "60" -> 60 seconds (plain number)
|
||||
"""
|
||||
schedule_expr = schedule_expr.strip().lower()
|
||||
|
||||
if schedule_expr == "daily":
|
||||
return 86400
|
||||
elif schedule_expr == "hourly":
|
||||
return 3600
|
||||
elif schedule_expr.isdigit():
|
||||
return int(schedule_expr)
|
||||
elif schedule_expr.startswith("*/"):
|
||||
# Handle */30m, */6h patterns
|
||||
interval_part = schedule_expr[2:]
|
||||
if interval_part.endswith("m"):
|
||||
minutes = int(interval_part[:-1])
|
||||
return minutes * 60
|
||||
elif interval_part.endswith("h"):
|
||||
hours = int(interval_part[:-1])
|
||||
return hours * 3600
|
||||
elif interval_part.endswith("s"):
|
||||
return int(interval_part[:-1])
|
||||
else:
|
||||
return int(interval_part)
|
||||
else:
|
||||
raise ValueError(f"Unsupported schedule format: {schedule_expr}")
|
||||
|
||||
class DeploymentScheduler:
|
||||
"""
|
||||
Minimal deployment scheduler with provider-agnostic design.
|
||||
|
||||
Features:
|
||||
- Simple interval-based scheduling
|
||||
- Thread-safe operation
|
||||
- Extensible deployer factory pattern
|
||||
- Minimal dependencies
|
||||
"""
|
||||
|
||||
def __init__(self, schedule_config: Optional[Dict[str, Any]] = None):
|
||||
self.config = schedule_config or {}
|
||||
self.is_running = False
|
||||
self._stop_event = threading.Event()
|
||||
self._thread = None
|
||||
self._deployer = None
|
||||
|
||||
def set_deployer(self, deployer: DeployerInterface):
|
||||
"""Set custom deployer implementation."""
|
||||
self._deployer = deployer
|
||||
|
||||
def _get_deployer(self) -> DeployerInterface:
|
||||
"""Get deployer instance using factory pattern."""
|
||||
if self._deployer:
|
||||
return self._deployer
|
||||
|
||||
# Default to CloudDeployer for backward compatibility
|
||||
return CloudDeployerAdapter()
|
||||
|
||||
def start(self, schedule_expr: str, max_retries: int = 3) -> bool:
|
||||
"""
|
||||
Start scheduled deployment.
|
||||
|
||||
Args:
|
||||
schedule_expr: Schedule expression (e.g., "daily", "*/6h", "3600")
|
||||
max_retries: Maximum retry attempts on failure
|
||||
|
||||
Returns:
|
||||
True if scheduler started successfully
|
||||
"""
|
||||
if self.is_running:
|
||||
logger.warning("Scheduler is already running")
|
||||
return False
|
||||
|
||||
try:
|
||||
interval = ScheduleParser.parse(schedule_expr)
|
||||
self.is_running = True
|
||||
self._stop_event.clear()
|
||||
|
||||
self._thread = threading.Thread(
|
||||
target=self._run_schedule,
|
||||
args=(interval, max_retries),
|
||||
daemon=True
|
||||
)
|
||||
self._thread.start()
|
||||
|
||||
logger.info(f"Deployment scheduler started with {interval}s interval")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start scheduler: {e}")
|
||||
self.is_running = False
|
||||
return False
|
||||
|
||||
def stop(self) -> bool:
|
||||
"""Stop the scheduler."""
|
||||
if not self.is_running:
|
||||
return True
|
||||
|
||||
self._stop_event.set()
|
||||
if self._thread and self._thread.is_alive():
|
||||
self._thread.join(timeout=5)
|
||||
|
||||
self.is_running = False
|
||||
logger.info("Deployment scheduler stopped")
|
||||
return True
|
||||
|
||||
def _run_schedule(self, interval: int, max_retries: int):
|
||||
"""Internal method to run scheduled deployments."""
|
||||
deployer = self._get_deployer()
|
||||
|
||||
while not self._stop_event.is_set():
|
||||
logger.info("Starting scheduled deployment")
|
||||
|
||||
success = False
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
if deployer.deploy():
|
||||
logger.info(f"Deployment successful on attempt {attempt + 1}")
|
||||
success = True
|
||||
break
|
||||
else:
|
||||
logger.warning(f"Deployment failed on attempt {attempt + 1}")
|
||||
except Exception as e:
|
||||
logger.error(f"Deployment error on attempt {attempt + 1}: {e}")
|
||||
|
||||
if attempt < max_retries - 1:
|
||||
time.sleep(30) # Wait before retry
|
||||
|
||||
if not success:
|
||||
logger.error(f"Deployment failed after {max_retries} attempts")
|
||||
|
||||
# Wait for next scheduled time
|
||||
self._stop_event.wait(interval)
|
||||
|
||||
def deploy_once(self) -> bool:
|
||||
"""Execute a single deployment immediately."""
|
||||
deployer = self._get_deployer()
|
||||
try:
|
||||
return deployer.deploy()
|
||||
except Exception as e:
|
||||
logger.error(f"One-time deployment failed: {e}")
|
||||
return False
|
||||
|
||||
def create_scheduler(provider: str = "gcp", config: Optional[Dict[str, Any]] = None) -> DeploymentScheduler:
|
||||
"""
|
||||
Factory function to create scheduler for different providers.
|
||||
|
||||
Args:
|
||||
provider: Deployment provider ("gcp", "aws", "azure", etc.)
|
||||
config: Optional configuration dict
|
||||
|
||||
Returns:
|
||||
Configured DeploymentScheduler instance
|
||||
"""
|
||||
scheduler = DeploymentScheduler(config)
|
||||
|
||||
# Provider-specific deployer setup can be added here
|
||||
if provider == "gcp":
|
||||
# Default CloudDeployer for GCP
|
||||
pass
|
||||
elif provider == "aws":
|
||||
# Future: AWS deployer implementation
|
||||
logger.warning("AWS provider not yet implemented, using default")
|
||||
elif provider == "azure":
|
||||
# Future: Azure deployer implementation
|
||||
logger.warning("Azure provider not yet implemented, using default")
|
||||
else:
|
||||
logger.warning(f"Unknown provider {provider}, using default")
|
||||
|
||||
return scheduler
|
||||
@@ -20,6 +20,7 @@ from chainlit.input_widget import TextInput
|
||||
from chainlit.types import ThreadDict
|
||||
import chainlit.data as cl_data
|
||||
from litellm import acompletion
|
||||
import litellm
|
||||
from db import DatabaseManager
|
||||
|
||||
# Load environment variables
|
||||
@@ -40,6 +41,15 @@ logger.addHandler(console_handler)
|
||||
# Set the logging level for the logger
|
||||
logger.setLevel(log_level)
|
||||
|
||||
# Configure litellm same as in llm.py
|
||||
litellm.set_verbose = False
|
||||
litellm.success_callback = []
|
||||
litellm._async_success_callback = []
|
||||
litellm.callbacks = []
|
||||
litellm.drop_params = True
|
||||
litellm.modify_params = True
|
||||
litellm.suppress_debug_messages = True
|
||||
|
||||
CHAINLIT_AUTH_SECRET = os.getenv("CHAINLIT_AUTH_SECRET")
|
||||
|
||||
if not CHAINLIT_AUTH_SECRET:
|
||||
@@ -55,6 +65,17 @@ db_manager.initialize()
|
||||
|
||||
deleted_thread_ids = [] # type: List[str]
|
||||
|
||||
def _build_completion_params(model_name, **override_params):
|
||||
"""Build parameters for litellm completion calls with proper model handling"""
|
||||
params = {
|
||||
"model": model_name,
|
||||
}
|
||||
|
||||
# Override with any provided parameters
|
||||
params.update(override_params)
|
||||
|
||||
return params
|
||||
|
||||
def save_setting(key: str, value: str):
|
||||
"""Saves a setting to the database.
|
||||
|
||||
@@ -237,12 +258,12 @@ Context:
|
||||
msg = cl.Message(content="")
|
||||
await msg.send()
|
||||
|
||||
# Prepare the completion parameters
|
||||
completion_params = {
|
||||
"model": model_name,
|
||||
"messages": message_history,
|
||||
"stream": True,
|
||||
}
|
||||
# Prepare the completion parameters using the helper function
|
||||
completion_params = _build_completion_params(
|
||||
model_name,
|
||||
messages=message_history,
|
||||
stream=True,
|
||||
)
|
||||
|
||||
# If an image is uploaded, include it in the message
|
||||
if image:
|
||||
@@ -344,9 +365,11 @@ Context:
|
||||
logger.error(f"Failed to parse function arguments: {function_args}")
|
||||
|
||||
second_response = await acompletion(
|
||||
model=model_name,
|
||||
stream=True,
|
||||
messages=messages,
|
||||
**_build_completion_params(
|
||||
model_name,
|
||||
stream=True,
|
||||
messages=messages,
|
||||
)
|
||||
)
|
||||
logger.debug(f"Second LLM response: {second_response}")
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ async def tavily_web_search_handler(query):
|
||||
try:
|
||||
response = tavily_client.search(query_with_date)
|
||||
logger.debug(f"Tavily search response: {response}")
|
||||
results = process_tavily_results(response)
|
||||
results = await process_tavily_results(response)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in Tavily search: {str(e)}")
|
||||
results = await fallback_to_duckduckgo(query_with_date)
|
||||
@@ -121,27 +121,27 @@ async def tavily_web_search_handler(query):
|
||||
"results": results
|
||||
})
|
||||
|
||||
def process_tavily_results(response):
|
||||
crawler = AsyncWebCrawler()
|
||||
crawler.warmup()
|
||||
results = []
|
||||
for result in response.get('results', []):
|
||||
url = result.get('url')
|
||||
if url:
|
||||
try:
|
||||
crawl_result = crawler.run(url=url)
|
||||
results.append({
|
||||
"content": result.get('content'),
|
||||
"url": url,
|
||||
"full_content": crawl_result.markdown
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error crawling {url}: {str(e)}")
|
||||
results.append({
|
||||
"content": result.get('content'),
|
||||
"url": url,
|
||||
"full_content": "Error: Unable to crawl this URL"
|
||||
})
|
||||
async def process_tavily_results(response):
|
||||
async with AsyncWebCrawler() as crawler:
|
||||
results = []
|
||||
for result in response.get('results', []):
|
||||
url = result.get('url')
|
||||
if url:
|
||||
try:
|
||||
crawl_result = await crawler.arun(url=url)
|
||||
full_content = crawl_result.markdown if crawl_result and hasattr(crawl_result, 'markdown') and crawl_result.markdown else "No content available"
|
||||
results.append({
|
||||
"content": result.get('content'),
|
||||
"url": url,
|
||||
"full_content": full_content
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error crawling {url}: {str(e)}")
|
||||
results.append({
|
||||
"content": result.get('content'),
|
||||
"url": url,
|
||||
"full_content": "Error: Unable to crawl this URL"
|
||||
})
|
||||
return results
|
||||
|
||||
async def fallback_to_duckduckgo(query):
|
||||
@@ -151,33 +151,33 @@ async def fallback_to_duckduckgo(query):
|
||||
|
||||
logger.debug(f"DuckDuckGo search results: {ddg_results}")
|
||||
|
||||
crawler = AsyncWebCrawler()
|
||||
crawler.warmup()
|
||||
results = []
|
||||
|
||||
for result in ddg_results:
|
||||
url = result.get('href')
|
||||
if url:
|
||||
try:
|
||||
crawl_result = crawler.run(url=url)
|
||||
async with AsyncWebCrawler() as crawler:
|
||||
results = []
|
||||
|
||||
for result in ddg_results:
|
||||
url = result.get('href')
|
||||
if url:
|
||||
try:
|
||||
crawl_result = await crawler.arun(url=url)
|
||||
full_content = crawl_result.markdown if crawl_result and hasattr(crawl_result, 'markdown') and crawl_result.markdown else "No content available"
|
||||
results.append({
|
||||
"content": result.get('body'),
|
||||
"url": url,
|
||||
"full_content": full_content
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error crawling {url}: {str(e)}")
|
||||
results.append({
|
||||
"content": result.get('body'),
|
||||
"url": url,
|
||||
"full_content": "Error: Unable to crawl this URL"
|
||||
})
|
||||
else:
|
||||
results.append({
|
||||
"content": result.get('body'),
|
||||
"url": url,
|
||||
"full_content": crawl_result.markdown
|
||||
"url": "N/A",
|
||||
"full_content": "No URL provided for crawling"
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error crawling {url}: {str(e)}")
|
||||
results.append({
|
||||
"content": result.get('body'),
|
||||
"url": url,
|
||||
"full_content": "Error: Unable to crawl this URL"
|
||||
})
|
||||
else:
|
||||
results.append({
|
||||
"content": result.get('body'),
|
||||
"url": "N/A",
|
||||
"full_content": "No URL provided for crawling"
|
||||
})
|
||||
|
||||
return results
|
||||
except Exception as e:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.22"
|
||||
version = "2.2.24"
|
||||
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.91",
|
||||
"praisonaiagents>=0.0.95",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
@@ -89,12 +89,13 @@ call = [
|
||||
"rich",
|
||||
"openai>=1.54.0",
|
||||
]
|
||||
train = []
|
||||
crewai = ["crewai>=0.32.0", "praisonai-tools>=0.0.15"]
|
||||
autogen = ["pyautogen>=0.2.19", "praisonai-tools>=0.0.15", "crewai"]
|
||||
|
||||
[tool.poetry]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.22"
|
||||
version = "2.2.24"
|
||||
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 = ""
|
||||
@@ -112,7 +113,7 @@ python = ">=3.10,<3.13"
|
||||
rich = ">=13.7"
|
||||
markdown = ">=3.5"
|
||||
pyparsing = ">=3.0.0"
|
||||
praisonaiagents = ">=0.0.91"
|
||||
praisonaiagents = ">=0.0.95"
|
||||
python-dotenv = ">=0.19.0"
|
||||
instructor = ">=1.3.3"
|
||||
PyYAML = ">=6.0"
|
||||
@@ -146,6 +147,7 @@ sqlalchemy = {version = ">=2.0.36", optional = true}
|
||||
playwright = {version = ">=1.47.0", optional = true}
|
||||
openai = {version = ">=1.54.0", optional = true}
|
||||
pydantic = {version = "<=2.10.1", optional = true}
|
||||
# unsloth = {version = ">=2024.11.7", extras = ["colab-new"], optional = true}
|
||||
|
||||
[tool.poetry.group.docs.dependencies]
|
||||
mkdocs = "*"
|
||||
@@ -249,7 +251,7 @@ code = [
|
||||
"playwright",
|
||||
"pydantic"
|
||||
]
|
||||
train = ["setup-conda-env"]
|
||||
train = []
|
||||
realtime = [
|
||||
"chainlit",
|
||||
"litellm",
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# PraisonAI Deployment Scheduler Configuration
|
||||
# This is an example configuration file for scheduled deployments
|
||||
|
||||
deployment:
|
||||
# Schedule expression (e.g., "daily", "hourly", "*/6h", "3600")
|
||||
schedule: "daily"
|
||||
|
||||
# Deployment provider (gcp, aws, azure)
|
||||
provider: "gcp"
|
||||
|
||||
# Maximum retry attempts on deployment failure
|
||||
max_retries: 3
|
||||
|
||||
# Optional: timezone for scheduling (future feature)
|
||||
# timezone: "UTC"
|
||||
|
||||
# Optional: retry delay in seconds
|
||||
# retry_delay: 30
|
||||
|
||||
# Optional: Environment variables for deployment
|
||||
# environment:
|
||||
# OPENAI_MODEL_NAME: "gpt-4o"
|
||||
# OPENAI_API_KEY: "your-api-key"
|
||||
# OPENAI_API_BASE: "https://api.openai.com/v1"
|
||||
|
||||
# Optional: Notification settings (future feature)
|
||||
# notifications:
|
||||
# on_success: true
|
||||
# on_failure: true
|
||||
# webhook_url: "https://your-webhook-url.com"
|
||||
@@ -6,7 +6,7 @@ import sys
|
||||
import os
|
||||
|
||||
# Add the source directory to Python path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src', 'praisonai-agents'))
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..', 'praisonai-agents'))
|
||||
|
||||
from praisonaiagents.llm.llm import LLM
|
||||
from praisonaiagents.agent.agent import Agent
|
||||
@@ -69,7 +69,7 @@ def test_tool_call_parsing():
|
||||
}
|
||||
}
|
||||
name, args, id = llm._parse_tool_call_arguments(tool_call_bad, is_ollama=False)
|
||||
assert name == "hello_world", f"Expected 'hello_world', got '{name}'"
|
||||
assert name == "unknown_function", f"Expected 'unknown_function' for malformed JSON, got '{name}'"
|
||||
assert args == {}, f"Expected empty dict, got {args}"
|
||||
print("✓ Error handling works")
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for the deployment scheduler functionality.
|
||||
Tests the core components without actually deploying.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
import yaml
|
||||
|
||||
# Add the src directory to the path so we can import modules
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src/praisonai'))
|
||||
|
||||
def test_schedule_parser():
|
||||
"""Test the schedule parser functionality."""
|
||||
try:
|
||||
from praisonai.scheduler import ScheduleParser
|
||||
|
||||
# Test various schedule expressions
|
||||
test_cases = [
|
||||
("daily", 86400),
|
||||
("hourly", 3600),
|
||||
("*/30m", 1800),
|
||||
("*/6h", 21600),
|
||||
("60", 60),
|
||||
("3600", 3600)
|
||||
]
|
||||
|
||||
print("Testing ScheduleParser...")
|
||||
for expr, expected in test_cases:
|
||||
result = ScheduleParser.parse(expr)
|
||||
assert result == expected, f"Expected {expected}, got {result} for '{expr}'"
|
||||
print(f" ✓ '{expr}' -> {result} seconds")
|
||||
|
||||
print("ScheduleParser tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"ScheduleParser test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_scheduler_creation():
|
||||
"""Test scheduler creation and basic functionality."""
|
||||
try:
|
||||
from praisonai.scheduler import create_scheduler, DeploymentScheduler
|
||||
|
||||
print("Testing scheduler creation...")
|
||||
|
||||
# Test default scheduler
|
||||
scheduler = create_scheduler()
|
||||
assert isinstance(scheduler, DeploymentScheduler)
|
||||
print(" ✓ Default scheduler created")
|
||||
|
||||
# Test with different providers
|
||||
for provider in ["gcp", "aws", "azure"]:
|
||||
scheduler = create_scheduler(provider=provider)
|
||||
assert isinstance(scheduler, DeploymentScheduler)
|
||||
print(f" ✓ {provider} scheduler created")
|
||||
|
||||
print("Scheduler creation tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Scheduler creation test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_config_file_parsing():
|
||||
"""Test configuration file parsing."""
|
||||
try:
|
||||
# Create a temporary config file
|
||||
config_data = {
|
||||
'deployment': {
|
||||
'schedule': 'daily',
|
||||
'provider': 'gcp',
|
||||
'max_retries': 5
|
||||
},
|
||||
'environment': {
|
||||
'TEST_VAR': 'test_value'
|
||||
}
|
||||
}
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
|
||||
yaml.dump(config_data, f)
|
||||
config_file = f.name
|
||||
|
||||
print("Testing config file parsing...")
|
||||
|
||||
# Test loading the config
|
||||
with open(config_file, 'r') as f:
|
||||
loaded_config = yaml.safe_load(f)
|
||||
|
||||
assert loaded_config['deployment']['schedule'] == 'daily'
|
||||
assert loaded_config['deployment']['provider'] == 'gcp'
|
||||
assert loaded_config['deployment']['max_retries'] == 5
|
||||
print(" ✓ Config file parsed correctly")
|
||||
|
||||
# Clean up
|
||||
os.unlink(config_file)
|
||||
|
||||
print("Config file parsing tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Config file parsing test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_cli_argument_parsing():
|
||||
"""Test CLI argument parsing for scheduling options."""
|
||||
try:
|
||||
from praisonai.cli import PraisonAI
|
||||
|
||||
print("Testing CLI argument parsing...")
|
||||
|
||||
# Test basic CLI instantiation
|
||||
praison = PraisonAI()
|
||||
assert praison is not None
|
||||
print(" ✓ PraisonAI CLI instance created")
|
||||
|
||||
print("CLI argument parsing tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"CLI argument parsing test failed: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all tests."""
|
||||
print("Running PraisonAI Scheduler Tests")
|
||||
print("=" * 40)
|
||||
|
||||
tests = [
|
||||
test_schedule_parser,
|
||||
test_scheduler_creation,
|
||||
test_config_file_parsing,
|
||||
test_cli_argument_parsing
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
print()
|
||||
if test():
|
||||
passed += 1
|
||||
else:
|
||||
print("❌ Test failed")
|
||||
|
||||
print()
|
||||
print("=" * 40)
|
||||
print(f"Tests passed: {passed}/{total}")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All tests passed!")
|
||||
return 0
|
||||
else:
|
||||
print("❌ Some tests failed")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Generated
+5
-5
@@ -3931,7 +3931,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonai"
|
||||
version = "2.2.22"
|
||||
version = "2.2.24"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "instructor" },
|
||||
@@ -4073,7 +4073,7 @@ requires-dist = [
|
||||
{ name = "plotly", marker = "extra == 'realtime'", specifier = ">=5.24.0" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.15" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.15" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.0.91" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.0.95" },
|
||||
{ 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" },
|
||||
@@ -4130,7 +4130,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.91"
|
||||
version = "0.0.95"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "mcp" },
|
||||
@@ -4138,9 +4138,9 @@ dependencies = [
|
||||
{ name = "pydantic" },
|
||||
{ name = "rich" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/9f/63/a26fe7ae31191ed5998fabe6cb13f779728caf3123ac8c8e8f4707a99e58/praisonaiagents-0.0.91.tar.gz", hash = "sha256:ec0409591f9278b766c4c6b3876ed142cfc217b1545c1b9809494f8a826b55fa", size = 125710 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/6a/7c/c6ae58b98d652f3c986683e72883eda5a3a4c2de054c01f6674ebdb0ae26/praisonaiagents-0.0.95.tar.gz", hash = "sha256:3b71869c3c73241e7dee109232d492545ea2a397fbfd9c922d4c9cfd41d93094", size = 127328 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/8c/2b/2e2d29ceb21a8e3af15beaf60955b85a9373ee6164d2d76f649a3e533786/praisonaiagents-0.0.91-py3-none-any.whl", hash = "sha256:aaaa51370a81aae29500168678d4b7208845890485f0a13da5367f1210dfaa3c", size = 143898 },
|
||||
{ url = "https://files.pythonhosted.org/packages/00/d2/63c8725b03d4f4bb1387594ed2cf93d1d544a6216ec7c374536ebfe6bf2a/praisonaiagents-0.0.95-py3-none-any.whl", hash = "sha256:04528314fd23240b4cb388149d8f181714753e6a3b68d429de46f66fc2c94857", size = 145811 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in new issue
Block a user