Chainlit Integration

AgenticFleet provides seamless integration with Chainlit for building chat-based applications with rich UI components.

Setup

First, install the required dependencies:

pip install agentic-fleet[chainlit]

Basic Integration

Create a Chainlit app with AgenticFleet:

import chainlit as cl
from agentic_fleet import Agent
from agentic_fleet.integrations.chainlit import ChainlitUI

@cl.on_chat_start
async def start():
    # Initialize agent
    agent = Agent(
        name="assistant",
        model="gpt-4"
    )
    
    # Create UI wrapper
    ui = ChainlitUI(agent)
    
    # Store in user session
    cl.user_session.set("ui", ui)

@cl.on_message
async def main(message: str):
    # Get UI wrapper
    ui = cl.user_session.get("ui")
    
    # Process message
    response = await ui.process_message(message)
    
    # Send response
    await cl.Message(
        content=response.content,
        elements=response.elements
    ).send()

UI Components

Message Elements

from agentic_fleet.integrations.chainlit import (
    TextElement,
    ImageElement,
    FileElement
)

# Send text with formatting
await ui.send_text("**Bold** and *italic* text")

# Send images
await ui.send_image(
    url="https://example.com/image.png",
    caption="Image caption"
)

# Send files
await ui.send_file(
    file_path="document.pdf",
    name="Document"
)

Interactive Elements

# Add action buttons
await ui.add_button(
    name="help",
    label="Get Help",
    action=lambda: print("Help clicked")
)

# Add select inputs
options = ["Option 1", "Option 2"]
await ui.add_select(
    name="choice",
    options=options,
    callback=lambda x: print(f"Selected: {x}")
)

Customization

Styling

# Configure UI theme
ui.set_theme({
    "primary_color": "#0A84FF",
    "font_family": "Inter"
})

# Custom CSS
ui.add_css("""
.message {
    padding: 1rem;
    border-radius: 8px;
}
""")

Templates

# Define message templates
ui.set_template(
    "greeting",
    "Hello {name}! How can I help you today?"
)

# Use template
await ui.send_template("greeting", name="User")

Advanced Features

Multi-Agent Chat

# Create multiple agents
agents = {
    "researcher": Agent(name="researcher"),
    "writer": Agent(name="writer")
}

# Initialize UI with multiple agents
ui = ChainlitUI(agents)

# Send message to specific agent
await ui.send_to_agent(
    "research quantum computing",
    agent="researcher"
)

Progress Tracking

# Show progress
async with ui.show_progress("Processing") as progress:
    # Update progress
    for i in range(5):
        await progress.update(f"Step {i+1}/5")
        await some_task()

Error Handling

from agentic_fleet.exceptions import UIError

try:
    await ui.process_message("Hello")
except UIError as e:
    await ui.show_error(str(e))

Best Practices

  1. Handle errors gracefully
  2. Use appropriate UI components
  3. Implement proper state management
  4. Monitor performance
  5. Follow Chainlit’s guidelines
  6. Test UI interactions thoroughly
  7. Provide clear user feedback