BitNet-LLM-Virtual-Coworker-Builder

BitNet_LLM_Virtual_Coworker_Builder

bitnet_llm_vc_builder_icon

A powerful and efficient AI virtual co-worker framework built on top of BitNet’s 1-bit quantized language models.

License: MIT Python 3.8+ Version

Overview

BitNet_LLM_Virtual_Coworker_Builder is a Python framework for creating AI virtual co-workers powered by BitNet’s highly efficient 1-bit quantized language models. This framework enables the development of sophisticated AI virtual co-workers that can run on edge devices with limited resources, offering significant advantages in terms of speed, energy efficiency, and deployment flexibility.

The framework is designed to be modular, extensible, and easy to use, allowing developers to create custom virtual co-workers for a wide range of applications, from simple task automation to complex multi-agent systems that collaborate to solve complex problems.

Key Features

Installation

pip install bitnet-vc-builder

From Source

# Clone the repository
git clone https://github.com/ai-in-pm/BitNet-LLM-Virtual-Coworker-Builder.git
cd BitNet-LLM-Virtual-Coworker-Builder

# Install in development mode
pip install -e .

With Optional Dependencies

# Install with UI dependencies
pip install bitnet-vc-builder[ui]

# Install with development dependencies
pip install bitnet-vc-builder[dev]

# Install with all dependencies
pip install bitnet-vc-builder[ui,dev]

Prerequisites

BitNet Model Installation

BitNet models can be downloaded from the Hugging Face model hub:

# Create a directory for models
mkdir -p models

# Download a BitNet model
python -m bitnet_vc_builder.utils.download_model --model "bitnet-b1.58-2b" --output "models/"

Quick Start

Single Virtual Co-worker

Create a simple virtual co-worker that can search the web and answer questions:

from bitnet_vc_builder import BitNetVirtualCoworker, BitNetModel, Tool

# Initialize BitNet model
model = BitNetModel(
    model_path="models/bitnet-b1.58-2b",
    kernel_type="i2_s",  # Options: i2_s (default), tl1, tl2
    num_threads=4,       # Number of CPU threads to use
    context_size=2048,   # Maximum context size
    temperature=0.7      # Generation temperature (0.0-1.0)
)

# Create a web search tool
def web_search(query):
    """Simple web search function (replace with actual implementation)"""
    # This is a placeholder - implement actual web search functionality
    return f"Search results for: {query}"

search_tool = Tool(
    name="search",
    description="Search the web for information",
    function=web_search,
    args_schema={
        "query": {
            "type": "string",
            "description": "Search query"
        }
    }
)

# Create a calculator tool
def calculator(expression):
    """Simple calculator tool"""
    try:
        return eval(expression)
    except Exception as e:
        return f"Error: {str(e)}"

calculator_tool = Tool(
    name="calculator",
    description="Calculate a mathematical expression",
    function=calculator,
    args_schema={
        "expression": {
            "type": "string",
            "description": "Mathematical expression to calculate"
        }
    }
)

# Create virtual co-worker with tools
coworker = BitNetVirtualCoworker(
    model=model,
    tools=[search_tool, calculator_tool],
    name="AssistantCoworker",
    description="A helpful virtual co-worker that can search the web and perform calculations"
)

# Run virtual co-worker on a task
result = coworker.run("Find information about climate change and calculate the average temperature increase per decade")
print(result)

Multi-Co-worker Collaboration

Create a team of specialized virtual co-workers that work together to solve complex tasks:

from bitnet_vc_builder import BitNetVirtualCoworker, BitNetModel, Tool, BitNetTeam
from bitnet_vc_builder.core.team import CollaborationMode
from bitnet_vc_builder.memory.memory import Memory

# Initialize BitNet model
model = BitNetModel(model_path="models/bitnet-b1.58-2b")

# Create shared memory for the team
shared_memory = Memory(max_items=100)

# Create specialized tools for each virtual co-worker
def web_search(query):
    """Web search function"""
    return f"Search results for: {query}"

def analyze_data(data):
    """Data analysis function"""
    return f"Analysis of data: {data}"

def generate_report(title, content):
    """Report generation function"""
    return f"# {title}\n\n{content}"

# Create virtual co-workers with specialized tools
researcher = BitNetVirtualCoworker(
    model=model,
    tools=[Tool(name="search", description="Search the web", function=web_search)],
    name="Researcher",
    description="A virtual co-worker that specializes in research and information gathering",
    memory=shared_memory
)

analyst = BitNetVirtualCoworker(
    model=model,
    tools=[Tool(name="analyze", description="Analyze data", function=analyze_data)],
    name="Analyst",
    description="A virtual co-worker that specializes in data analysis and interpretation",
    memory=shared_memory
)

writer = BitNetVirtualCoworker(
    model=model,
    tools=[Tool(name="generate_report", description="Generate a report", function=generate_report)],
    name="Writer",
    description="A virtual co-worker that specializes in writing clear and concise reports",
    memory=shared_memory
)

# Create a team with different collaboration modes
team = BitNetTeam(
    agents=[researcher, analyst, writer],
    name="ResearchTeam",
    description="A team that researches topics, analyzes data, and writes reports",
    collaboration_mode=CollaborationMode.HIERARCHICAL,  # Options: SEQUENTIAL, HIERARCHICAL, PARALLEL
    enable_conflict_resolution=True,
    enable_task_prioritization=True,
    enable_performance_tracking=True
)

# Run the team on a task
result = team.run(
    "Research climate change, analyze the data on temperature changes over the last century, and write a comprehensive report"
)
print(result)

Using the Configuration File

You can also configure and run virtual co-workers using a YAML configuration file:

from bitnet_vc_builder.main import load_config, create_agent, create_team

# Load configuration from file
config = load_config("config/config.yaml")

# Create model, virtual co-workers, and teams from configuration
model = load_model(config)
agents = {agent_config["name"]: create_agent(agent_config, model) for agent_config in config.get("agents", [])}
teams = {team_config["name"]: create_team(team_config, agents) for team_config in config.get("teams", [])}

# Run a virtual co-worker
result = agents["AssistantCoworker"].run("What is the capital of France?")
print(result)

# Run a team
result = teams["ResearchTeam"].run("Research quantum computing advancements in 2023")
print(result)

Architecture

BitNet_LLM_Virtual_Coworker_Builder is built with a modular architecture that allows for easy extension and customization:

bitnet_vc_builder/
├── core/                  # Core components
│   ├── virtual_coworker.py  # Base virtual co-worker class
│   ├── team.py            # Team management
│   └── config.py          # Configuration utilities
├── models/                # Model integration
│   ├── bitnet_wrapper.py  # BitNet model wrapper
│   ├── optimizers.py      # Performance optimizations
│   └── kernels/           # Specialized kernels for different architectures
├── tools/                 # Tool integration
│   ├── base_tools.py      # Tool base classes
│   ├── common_tools.py    # Common utility tools
│   ├── data_tools.py      # Data analysis tools
│   ├── code_tools.py      # Code generation tools
│   └── document_tools.py  # Document processing tools
├── memory/                # Memory management
│   ├── memory.py          # Memory system
│   └── context.py         # Context management
├── api/                   # API server
│   ├── server.py          # FastAPI server
│   └── routes/            # API endpoints
├── ui/                    # User interfaces
│   ├── web/               # Web UI
│   └── cli/               # Command-line interface
└── utils/                 # Utility functions
    ├── logging.py         # Logging utilities
    └── download_model.py  # Model download utilities

Core Components

Extension Points

The framework is designed to be easily extended:

User Interfaces

BitNet_LLM_Virtual_Coworker_Builder provides multiple interfaces for interacting with virtual co-workers and teams:

Web UI

The web-based user interface provides a user-friendly way to create, configure, and manage virtual co-workers and teams:

# Run the web UI
python -m bitnet_vc_builder.ui.web.app

The web UI is accessible at http://localhost:8501 and provides the following features:

Desktop Application

The cross-platform desktop application built with Tauri provides native performance and integration with the operating system:

# Navigate to the Tauri UI directory
cd tauri-ui

# Install dependencies
npm install

# Run the development version
npm run tauri dev

# Build the production version for your platform
npm run tauri build

The desktop application includes all the features of the web UI, plus:

Command-Line Interface

For automation and scripting, the framework provides a command-line interface:

# Get help
bitnet-vc --help

# Run a virtual co-worker
bitnet-vc run-virtual-coworker --name "AssistantCoworker" --task "What is the capital of France?"

# Run a team
bitnet-vc run-team --name "ResearchTeam" --task "Research quantum computing advancements"

# Start the API server
bitnet-vc server --host 0.0.0.0 --port 8000

# Start the web UI
bitnet-vc ui --port 8501

API Server

For integration with other applications, the framework provides a RESTful API server:

# Start the API server
python -m bitnet_vc_builder.api.server

The API documentation is available at http://localhost:8000/docs and includes endpoints for:

See the API Documentation for more information on the API endpoints.

Python API

For direct integration with Python applications, the framework provides a Python API:

from bitnet_vc_builder import BitNetVirtualCoworker, BitNetModel, Tool, BitNetTeam

# Create and use virtual co-workers and teams programmatically
# (See Quick Start examples above)

See the Python API Documentation for more information on the Python API.

Advanced Usage

The framework provides many advanced features for building sophisticated virtual co-worker systems:

Custom Virtual Co-workers

Create specialized virtual co-workers by subclassing the BitNetVirtualCoworker class:

from bitnet_vc_builder import BitNetVirtualCoworker, BitNetModel, Tool

class CodeVirtualCoworker(BitNetVirtualCoworker):
    """A virtual co-worker specialized for code generation and review."""

    def __init__(self, model, tools=None, name=None, description=None, memory=None, supported_languages=None):
        super().__init__(model, tools, name, description, memory)
        self.supported_languages = supported_languages or ["python", "javascript", "java"]

    def generate_code(self, language, requirements):
        """Generate code in the specified language based on requirements."""
        if language not in self.supported_languages:
            return f"Sorry, I don't support {language}. Supported languages: {', '.join(self.supported_languages)}"

        prompt = f"Generate {language} code for: {requirements}"
        return self.run(prompt)

    def review_code(self, code, language):
        """Review code and provide feedback."""
        prompt = f"Review this {language} code and provide feedback:\n\n```{language}\n{code}\n```"
        return self.run(prompt)

Custom Tools

Create specialized tools for virtual co-workers:

from bitnet_vc_builder import Tool
import pandas as pd
import matplotlib.pyplot as plt
import io
import base64

def analyze_data(data_str, analysis_type="summary"):
    """
    Analyze data and return results.

    Args:
        data_str: CSV data as a string
        analysis_type: Type of analysis to perform (summary, correlation, visualization)

    Returns:
        Analysis results
    """
    # Convert string data to DataFrame
    data = pd.read_csv(io.StringIO(data_str))

    if analysis_type == "summary":
        return data.describe().to_string()

    elif analysis_type == "correlation":
        return data.corr().to_string()

    elif analysis_type == "visualization":
        # Create a simple visualization
        plt.figure(figsize=(10, 6))
        data.plot()
        plt.title("Data Visualization")

        # Convert plot to base64 string
        buffer = io.BytesIO()
        plt.savefig(buffer, format="png")
        buffer.seek(0)
        image_base64 = base64.b64encode(buffer.read()).decode("utf-8")

        return f"data:image/png;base64,{image_base64}"

    else:
        return f"Unknown analysis type: {analysis_type}"

# Create the tool
data_analysis_tool = Tool(
    name="analyze_data",
    description="Analyze data and return results",
    function=analyze_data,
    args_schema={
        "data_str": {
            "type": "string",
            "description": "CSV data as a string"
        },
        "analysis_type": {
            "type": "string",
            "description": "Type of analysis to perform (summary, correlation, visualization)",
            "enum": ["summary", "correlation", "visualization"],
            "default": "summary"
        }
    }
)

Advanced Team Configurations

Configure teams with different collaboration modes and strategies:

from bitnet_vc_builder import BitNetTeam, CollaborationMode

# Create a team with hierarchical collaboration
team = BitNetTeam(
    agents=[researcher, analyst, writer],
    name="ResearchTeam",
    description="A team that researches topics, analyzes data, and writes reports",
    collaboration_mode=CollaborationMode.HIERARCHICAL,
    coordinator_agent="researcher",  # Specify which virtual co-worker coordinates the team
    enable_conflict_resolution=True,
    conflict_resolution_strategy="voting",  # Options: voting, consensus, authority
    enable_task_prioritization=True,
    enable_performance_tracking=True,
    max_parallel_tasks=4
)

For more advanced usage examples and detailed API reference, see the documentation.

Performance Optimization

BitNet_LLM_Virtual_Coworker_Builder includes several features for optimizing performance:

Model Optimization

from bitnet_vc_builder.models.optimizers import optimize_model

# Optimize a BitNet model for the current hardware
optimized_model_path = optimize_model(
    model_path="models/bitnet-b1.58-2b",
    target_device="cpu",
    num_threads=4,
    quantization_level="int8"  # Options: none, int8, int4
)

# Use the optimized model
model = BitNetModel(model_path=optimized_model_path)

Batch Processing

# Process multiple tasks in batch mode
results = coworker.run_batch([
    "What is the capital of France?",
    "What is the population of Tokyo?",
    "What is the tallest mountain in the world?"
])

Caching

from bitnet_vc_builder.utils.caching import enable_caching

# Enable caching for the virtual co-worker
enable_caching(coworker, cache_dir="cache", max_cache_size=1000)

# Subsequent identical queries will be served from cache
result1 = coworker.run("What is the capital of France?")  # Computed
result2 = coworker.run("What is the capital of France?")  # Served from cache

Production Setup

For production deployment, BitNet_LLM_Virtual_Coworker_Builder includes comprehensive tools and scripts to set up a robust production environment.

Automated Production Setup

The easiest way to set up the production environment is to use the automated setup script:

# Run as Administrator
.\setup_production.ps1

This script will:

Production Monitoring

The production environment includes a comprehensive monitoring system:

# Set up monitoring
cd monitoring
.\setup_monitoring.ps1

This will:

Backup and Recovery

The production environment includes a robust backup and recovery system:

# Set up backups
cd backup
.\setup_backup.ps1

This will:

For more detailed information on production setup, see the Production Setup Guide.

Contributing

Contributions are welcome! Here’s how you can contribute to the project:

Setting Up Development Environment

# Clone the repository
git clone https://github.com/ai-in-pm/BitNet-LLM-Virtual-Coworker-Builder.git
cd BitNet-LLM-Virtual-Coworker-Builder

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode with all dependencies
pip install -e ".[dev,ui]"

# Run tests
pytest

Development Guidelines

For more detailed guidelines, please see CONTRIBUTING.md.

Community and Support

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements