Skip to main content

Extension for SPADE to integrate Large Language Models in agents

Project description

SPADE-LLM Logo

SPADE-LLM

Extension for SPADE that integrates Large Language Models into multi-agent systems. Build intelligent, collaborative agents that can communicate, reason, and take actions in complex distributed environments.

Table of Contents

Key Features

  • Multi-Provider Support - OpenAI, Ollama, LM Studio, vLLM integration
  • Tool System - Function calling with async execution
  • Context Management - Multi-conversation support with automatic cleanup
  • Memory Extensions - Agent-based and agent-thread memory for persistent state
  • Message Routing - Conditional routing based on LLM responses
  • Guardrails System - Content filtering and safety controls for input/output
  • MCP Integration - Model Context Protocol server support
  • Human-in-the-Loop - Web interface for human expert consultation

Quick Start

import spade
from spade_llm import LLMAgent, LLMProvider

async def main():
    provider = LLMProvider.create_openai(
        api_key="your-api-key",
        model="gpt-4o-mini"
    )
    
    agent = LLMAgent(
        jid="assistant@example.com",
        password="password",
        provider=provider,
        system_prompt="You are a helpful assistant"
    )
    
    await agent.start()

if __name__ == "__main__":
    spade.run(main())

Installation

⚠️ Warning: spade_llm is not yet available on PyPI. It will be published in the coming days. Stay tuned!

pip install spade_llm

Examples

Multi-Provider Support

# OpenAI
provider = LLMProvider.create_openai(api_key="key", model="gpt-4o-mini")

# Ollama (local)
provider = LLMProvider.create_ollama(model="llama3.1:8b")

# LM Studio (local)
provider = LLMProvider.create_lm_studio(model="local-model")

Tools and Function Calling

from spade_llm import LLMTool

async def get_weather(city: str) -> str:
    return f"Weather in {city}: 22°C, sunny"

weather_tool = LLMTool(
    name="get_weather",
    description="Get weather for a city",
    parameters={
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"]
    },
    func=get_weather
)

agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    tools=[weather_tool]
)

Content Safety with Guardrails

from spade_llm.guardrails import KeywordGuardrail, GuardrailAction

# Block harmful content
safety_filter = KeywordGuardrail(
    name="safety_filter",
    blocked_keywords=["hack", "exploit", "malware"],
    action=GuardrailAction.BLOCK,
    blocked_message="I cannot help with potentially harmful activities."
)

agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    input_guardrails=[safety_filter]  # Filter incoming messages
)

Message Routing

def router(msg, response, context):
    if "technical" in response.lower():
        return "tech-support@example.com"
    return str(msg.sender)

agent = LLMAgent(
    jid="router@example.com",
    password="password",
    provider=provider,
    routing_function=router
)

Interactive Chat

from spade_llm import ChatAgent

# Create chat interface
chat_agent = ChatAgent(
    jid="human@example.com",
    password="password",
    target_agent_jid="assistant@example.com"
)

await chat_agent.start()
await chat_agent.run_interactive()  # Start interactive chat

Memory Extensions

# Agent-based memory: Single shared memory per agent
agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    agent_base_memory=(True, "./memory.db")  # Enabled with custom path
)

# Agent-thread memory: Separate memory per conversation
agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    agent_thread_memory=(True, "./thread_memory.db")  # Enabled with custom path
)

# Default memory paths (if path not specified)
agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    agent_base_memory=(True, None)  # Uses default path
)

Context Management

from spade_llm.context import SmartWindowSizeContext, FixedWindowSizeContext

# Smart context: Dynamic window sizing based on content
smart_context = SmartWindowSizeContext(
    max_tokens=4000,
    include_system_prompt=True,
    preserve_last_k_messages=5
)

# Fixed context: Traditional sliding window
fixed_context = FixedWindowSizeContext(
    max_messages=20,
    include_system_prompt=True
)

agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    context_manager=smart_context
)

Human-in-the-Loop

from spade_llm import HumanInTheLoopTool

# Create tool for human consultation
human_tool = HumanInTheLoopTool(
    human_expert_jid="expert@example.com",
    timeout=300.0  # 5 minutes
)

agent = LLMAgent(
    jid="assistant@example.com",
    password="password",
    provider=provider,
    tools=[human_tool]  # Pass tools in constructor
)

# Start web interface for human expert
# python -m spade_llm.human_interface.web_server
# Open http://localhost:8080 and connect as expert

Architecture

graph LR
    A[LLMAgent] --> C[ContextManager]
    A --> D[LLMProvider]
    A --> E[LLMTool]
    A --> G[Guardrails]
    A --> M[Memory]
    D --> F[OpenAI/Ollama/etc]
    G --> H[Input/Output Filtering]
    E --> I[Human-in-the-Loop]
    E --> J[MCP]
    E --> P[CustomTool/LangchainTool]
    J --> K[STDIO]
    J --> L[HTTP Streaming]
    M --> N[Agent-based]
    M --> O[Agent-thread]

Documentation

Examples Directory

The /examples directory contains complete working examples:

  • multi_provider_chat_example.py - Chat with different LLM providers
  • ollama_with_tools_example.py - Local models with tool calling
  • langchain_tools_example.py - LangChain tool integration
  • guardrails_example.py - Content filtering and safety controls
  • human_in_the_loop_example.py - Human expert consultation via web interface
  • valencia_multiagent_trip_planner.py - Multi-agent workflow

Requirements

  • Python 3.10+
  • SPADE 3.3.0+

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

See Contributing Guide for details.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spade_llm-0.1.0.tar.gz (5.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spade_llm-0.1.0-py3-none-any.whl (73.1 kB view details)

Uploaded Python 3

File details

Details for the file spade_llm-0.1.0.tar.gz.

File metadata

  • Download URL: spade_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for spade_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 510f70c767484c3af33d465302faec0c934330e98f5307b822993b514be66441
MD5 8d39e68dd14a50e453ccf2f60eef6c71
BLAKE2b-256 1771ad427089321d870657c161a7b485d21942e0365b5badf31457c755180ad5

See more details on using hashes here.

File details

Details for the file spade_llm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: spade_llm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 73.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for spade_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de24a935fbf68770b5c5d4d0addef3498a7cc954c144fdf687c9ae9010248ff4
MD5 3d6c4ea0af4ef12441a7cba8976f705f
BLAKE2b-256 f1d8ef88ab6935e7d93ddb115e6469ff1e167efd24b569297ed059df2c091fa4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page