Adding PraisonAI Code

This commit is contained in:
MervinPraison committed 2024-07-13 00:51:17 +01:00
1 parent bb49efe019
commit 5491f1619b
7 files changed
+498 -5

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==0.0.47 gunicorn markdown
RUN pip install flask praisonai==0.0.48 gunicorn markdown
EXPOSE 8080
CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]
+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==0.0.47 gunicorn markdown\n&#34;)
file.write(&#34;RUN pip install flask praisonai==0.0.48 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;)
+35
View File
@@ -95,6 +95,10 @@ class PraisonAI:
self.create_chainlit_chat_interface()
return
if getattr(args, 'code', False):
self.create_code_interface()
return
invocation_cmd = "praisonai"
version_string = f"PraisonAI version {__version__}"
@@ -177,6 +181,9 @@ class PraisonAI:
if args.agent_file == 'chat':
args.ui = 'chainlit'
args.chat = True
if args.agent_file == 'code':
args.ui = 'chainlit'
args.code = True
return args
@@ -207,6 +214,34 @@ class PraisonAI:
chainlit_run([chat_ui_path])
else:
print("ERROR: Chat UI is not installed. Please install it with 'pip install \"praisonai\[chat]\"' to use the chat UI.")
def create_code_interface(self):
"""
Create a Chainlit interface for the code application.
This function sets up a Chainlit application that listens for messages.
When a message is received, it runs PraisonAI with the provided message as the topic.
The generated agents are then used to perform tasks.
Returns:
None: This function does not return any value. It starts the Chainlit application.
"""
if CHAINLIT_AVAILABLE:
import praisonai
os.environ["CHAINLIT_PORT"] = "8086"
public_folder = os.path.join(os.path.dirname(praisonai.__file__), 'public')
if not os.path.exists("public"): # Check if the folder exists in the current directory
if os.path.exists(public_folder):
shutil.copytree(public_folder, 'public', dirs_exist_ok=True)
logging.info("Public folder copied successfully!")
else:
logging.info("Public folder not found in the package.")
else:
logging.info("Public folder already exists.")
code_ui_path = os.path.join(os.path.dirname(praisonai.__file__), 'ui', 'code.py')
chainlit_run([code_ui_path])
else:
print("ERROR: Code UI is not installed. Please install it with 'pip install \"praisonai\[code]\"' to use the code UI.")
def create_gradio_interface(self):
"""
+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==0.0.47 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==0.0.48 gunicorn markdown\n")
file.write("EXPOSE 8080\n")
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
+317
View File
@@ -0,0 +1,317 @@
import chainlit as cl
from chainlit.input_widget import TextInput
from chainlit.types import ThreadDict
from litellm import acompletion
import os
import sqlite3
from datetime import datetime
from typing import Dict, List, Optional
from dotenv import load_dotenv
load_dotenv()
import chainlit.data as cl_data
from chainlit.step import StepDict
from literalai.helper import utc_now
import logging
import json
from sql_alchemy import SQLAlchemyDataLayer
from context import ContextGatherer
# Set up logging
logger = logging.getLogger(__name__)
log_level = os.getenv("LOGLEVEL", "INFO").upper()
logger.handlers = []
# Set up logging to console
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)
console_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
# Set the logging level for the logger
logger.setLevel(log_level)
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"
CHAINLIT_AUTH_SECRET = os.getenv("CHAINLIT_AUTH_SECRET")
now = utc_now()
create_step_counter = 0
DB_PATH = "threads.db"
def initialize_db():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
identifier TEXT NOT NULL UNIQUE,
metadata JSONB NOT NULL,
createdAt TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS threads (
id UUID PRIMARY KEY,
createdAt TEXT,
name TEXT,
userId UUID,
userIdentifier TEXT,
tags TEXT[],
metadata JSONB NOT NULL DEFAULT '{}',
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS steps (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
threadId UUID NOT NULL,
parentId UUID,
disableFeedback BOOLEAN NOT NULL,
streaming BOOLEAN NOT NULL,
waitForAnswer BOOLEAN,
isError BOOLEAN,
metadata JSONB,
tags TEXT[],
input TEXT,
output TEXT,
createdAt TEXT,
start TEXT,
end TEXT,
generation JSONB,
showInput TEXT,
language TEXT,
indent INT,
FOREIGN KEY (threadId) REFERENCES threads (id) ON DELETE CASCADE
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS elements (
id UUID PRIMARY KEY,
threadId UUID,
type TEXT,
url TEXT,
chainlitKey TEXT,
name TEXT NOT NULL,
display TEXT,
objectKey TEXT,
size TEXT,
page INT,
language TEXT,
forId UUID,
mime TEXT,
FOREIGN KEY (threadId) REFERENCES threads (id) ON DELETE CASCADE
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS feedbacks (
id UUID PRIMARY KEY,
forId UUID NOT NULL,
value INT NOT NULL,
threadId UUID,
comment TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT UNIQUE,
value TEXT
)
''')
conn.commit()
conn.close()
def save_setting(key: str, value: str):
"""Saves a setting to the database.
Args:
key: The setting key.
value: The setting value.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute(
"""
INSERT OR REPLACE INTO settings (id, key, value)
VALUES ((SELECT id FROM settings WHERE key = ?), ?, ?)
""",
(key, key, value),
)
conn.commit()
conn.close()
def load_setting(key: str) -> str:
"""Loads a setting from the database.
Args:
key: The setting key.
Returns:
The setting value, or None if the key is not found.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('SELECT value FROM settings WHERE key = ?', (key,))
result = cursor.fetchone()
conn.close()
return result[0] if result else None
# Initialize the database
initialize_db()
deleted_thread_ids = [] # type: List[str]
cl_data._data_layer = SQLAlchemyDataLayer(conninfo=f"sqlite+aiosqlite:///{DB_PATH}")
@cl.on_chat_start
async def start():
initialize_db()
model_name = load_setting("model_name")
if model_name:
cl.user_session.set("model_name", model_name)
else:
# If no setting found, use default or environment variable
model_name = os.getenv("MODEL_NAME", "gpt-3.5-turbo")
cl.user_session.set("model_name", model_name)
logger.debug(f"Model name: {model_name}")
settings = cl.ChatSettings(
[
TextInput(
id="model_name",
label="Enter the Model Name",
placeholder="e.g., gpt-3.5-turbo",
initial=model_name
)
]
)
cl.user_session.set("settings", settings)
await settings.send()
gatherer = ContextGatherer()
context, token_count, context_tree = gatherer.run()
msg = cl.Message(content="""Token Count: {token_count},
Files include: \n```bash\n{context_tree}\n"""
.format(token_count=token_count, context_tree=context_tree))
await msg.send()
@cl.on_settings_update
async def setup_agent(settings):
logger.debug(settings)
cl.user_session.set("settings", settings)
model_name = settings["model_name"]
cl.user_session.set("model_name", model_name)
# Save in settings table
save_setting("model_name", model_name)
# Save in thread metadata
thread_id = cl.user_session.get("thread_id")
if thread_id:
thread = await cl_data.get_thread(thread_id)
if thread:
metadata = thread.get("metadata", {})
metadata["model_name"] = model_name
# Always store metadata as a JSON string
await cl_data.update_thread(thread_id, metadata=json.dumps(metadata))
# Update the user session with the new metadata
cl.user_session.set("metadata", metadata)
@cl.on_message
async def main(message: cl.Message):
model_name = load_setting("model_name") or os.getenv("MODEL_NAME") or "gpt-3.5-turbo"
message_history = cl.user_session.get("message_history", [])
message_history.append({"role": "user", "content": message.content})
gatherer = ContextGatherer()
context, token_count, context_tree = gatherer.run()
prompt_history = message_history
prompt_history.append({"role": "user", "content": """
Answer the question:\n{question}.\n\n
Below is the Context:\n{context}\n\n"""
.format(context=context, question=message.content)})
msg = cl.Message(content="")
await msg.send()
response = await acompletion(
model=model_name,
messages=prompt_history,
stream=True,
# temperature=0.7,
# max_tokens=500,
# top_p=1
)
full_response = ""
async for part in response:
if token := part['choices'][0]['delta']['content']:
await msg.stream_token(token)
full_response += token
logger.debug(f"Full response: {full_response}")
message_history.append({"role": "assistant", "content": full_response})
logger.debug(f"Message history: {message_history}")
cl.user_session.set("message_history", message_history)
await msg.update()
username = os.getenv("CHAINLIT_USERNAME", "admin") # Default to "admin" if not found
password = os.getenv("CHAINLIT_PASSWORD", "admin") # Default to "admin" if not found
@cl.password_auth_callback
def auth_callback(username: str, password: str):
if (username, password) == (username, password):
return cl.User(
identifier=username, metadata={"role": "ADMIN", "provider": "credentials"}
)
else:
return None
async def send_count():
await cl.Message(
f"Create step counter: {create_step_counter}", disable_feedback=True
).send()
@cl.on_chat_resume
async def on_chat_resume(thread: cl_data.ThreadDict):
logger.info(f"Resuming chat: {thread['id']}")
model_name = load_setting("model_name") or os.getenv("MODEL_NAME") or "gpt-3.5-turbo"
logger.debug(f"Model name: {model_name}")
settings = cl.ChatSettings(
[
TextInput(
id="model_name",
label="Enter the Model Name",
placeholder="e.g., gpt-3.5-turbo",
initial=model_name
)
]
)
await settings.send()
thread_id = thread["id"]
cl.user_session.set("thread_id", thread["id"])
# The metadata should now already be a dictionary
metadata = thread.get("metadata", {})
cl.user_session.set("metadata", metadata)
message_history = cl.user_session.get("message_history", [])
steps = thread["steps"]
for message in steps:
msg_type = message.get("type")
if msg_type == "user_message":
message_history.append({"role": "user", "content": message.get("output", "")})
elif msg_type == "assistant_message":
message_history.append({"role": "assistant", "content": message.get("output", "")})
else:
logger.warning(f"Message without type: {message}")
cl.user_session.set("message_history", message_history)
+140
View File
@@ -0,0 +1,140 @@
import os
import fnmatch
import re
class ContextGatherer:
def __init__(self, directory='.', output_file='context.txt',
relevant_extensions=None, max_file_size=1_000_000, max_tokens=60000):
self.directory = directory
self.output_file = output_file
self.relevant_extensions = relevant_extensions or ['.py']
self.max_file_size = max_file_size
self.max_tokens = max_tokens
self.ignore_patterns = self.get_ignore_patterns()
def get_ignore_patterns(self):
"""Read .gitignore file and return ignore patterns."""
default_patterns = [".*", "*.pyc", "__pycache__", ".git", ".gitignore", ".vscode",
".idea", ".DS_Store", "*.lock", "*.pyc", ".env",
"docs", "tests", "test", "tmp", "temp",
"*.txt", "*.md", "*.json", "*.csv", "*.tsv", "*.yaml", "*.yml","public",
"*.sql", "*.sqlite", "*.db", "*.db3", "*.sqlite3", "*.log", "*.zip", "*.gz",
"*.tar", "*.rar", "*.7z", "*.pdf", "*.jpg", "*.jpeg", "*.png", "*.gif", "*.svg",
"cookbooks", "assets", "__pycache__", "dist", "build", "node_modules", "venv",]
gitignore_path = os.path.join(self.directory, '.gitignore')
if os.path.exists(gitignore_path):
with open(gitignore_path, 'r') as f:
gitignore_patterns = [line.strip() for line in f if line.strip() and not line.startswith('#')]
return list(set(default_patterns + gitignore_patterns))
return default_patterns
def should_ignore(self, file_path):
"""Check if a file should be ignored based on patterns."""
relative_path = os.path.relpath(file_path, self.directory)
if relative_path.startswith('.'):
return True
for pattern in self.ignore_patterns:
if fnmatch.fnmatch(relative_path, pattern):
return True
return False
def is_relevant_file(self, file_path):
"""Determine if a file is relevant for the context."""
if os.path.getsize(file_path) > self.max_file_size:
return False
return any(file_path.endswith(ext) for ext in self.relevant_extensions)
def gather_context(self):
"""Gather context from relevant files in the directory."""
context = []
total_files = sum(len(files) for _, _, files in os.walk(self.directory))
processed_files = 0
for root, dirs, files in os.walk(self.directory):
dirs[:] = [d for d in dirs if not self.should_ignore(os.path.join(root, d))]
for file in files:
file_path = os.path.join(root, file)
if not self.should_ignore(file_path) and self.is_relevant_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
context.append(f"File: {file_path}\n\n{content}\n\n{'='*50}\n")
except Exception as e:
print(f"Error reading {file_path}: {e}")
processed_files += 1
print(f"\rProcessed {processed_files}/{total_files} files", end="", flush=True)
print() # New line after progress indicator
return '\n'.join(context)
def count_tokens(self, text):
"""Count the number of tokens in the given text using a simple tokenizer."""
# Split on whitespace and punctuation
tokens = re.findall(r'\b\w+\b|[^\w\s]', text)
return len(tokens)
def truncate_context(self, context):
"""Truncate context to fit within the specified token limit."""
tokens = re.findall(r'\b\w+\b|[^\w\s]', context)
if len(tokens) > self.max_tokens:
truncated_tokens = tokens[:self.max_tokens]
return ' '.join(truncated_tokens)
return context
def save_context(self, context):
"""Save the gathered context to a file."""
with open(self.output_file, 'w', encoding='utf-8') as f:
f.write(context)
def get_context_tree(self):
"""Generate a formatted tree structure of the folder, including only relevant files."""
tree = []
start_dir = os.path.abspath(self.directory)
def add_to_tree(path, prefix=''):
contents = sorted(os.listdir(path))
pointers = [('└── ' if i == len(contents) - 1 else '├── ') for i in range(len(contents))]
for pointer, name in zip(pointers, contents):
full_path = os.path.join(path, name)
if self.should_ignore(full_path):
continue
rel_path = os.path.relpath(full_path, start_dir)
tree.append(f"{prefix}{pointer}{name}")
if os.path.isdir(full_path):
add_to_tree(full_path, prefix + (' ' if pointer == '└── ' else '│ '))
elif self.is_relevant_file(full_path):
continue # We've already added the file to the tree
add_to_tree(start_dir)
return '\n'.join(tree)
def run(self):
"""Run the context gathering process and return the context and token count."""
context = self.gather_context()
context = self.truncate_context(context)
token_count = self.count_tokens(context)
print(f"Context gathered successfully.")
print(f"Total number of tokens (estimated): {token_count}")
# self.save_context(context)
context_tree = self.get_context_tree()
print("\nContext Tree Structure:")
print(context_tree)
return context, token_count, context_tree
def main():
gatherer = ContextGatherer(
directory='.',
output_file='context.txt',
relevant_extensions=['.py'],
max_file_size=500_000, # 500KB
max_tokens=60000
)
context, token_count, context_tree = gatherer.run()
print(f"\nThe context contains approximately {token_count} tokens.")
print("First 500 characters of context:")
print(context[:500] + "...")
if __name__ == "__main__":
main()
+3 -2
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "PraisonAI"
version = "0.0.47"
version = "0.0.48"
description = "PraisonAI application combines AutoGen and CrewAI or similar frameworks into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customization, and efficient human-agent collaboration."
authors = ["Mervin Praison"]
license = ""
@@ -95,4 +95,5 @@ google = ["langchain-google-genai"]
openai = ["langchain-openai"]
anthropic = ["langchain-anthropic"]
cohere = ["langchain-cohere"]
chat = ["chainlit", "litellm"]
chat = ["chainlit", "litellm"]
code = ["chainlit", "litellm"]