Skip to content

An open-source framework for building distributed, scalable, and collaborative multi-agent applications.

License

Notifications You must be signed in to change notification settings

OpenCSGs/coagent

Repository files navigation

Coagent

CI

An open-source framework for building distributed, scalable, and collaborative multi-agent applications.

Features

  • Event-driven
  • Distributed & Fault-tolerant
  • Single-agent
    • Function-calling
    • ReAct
  • Multi-agent orchestration
    • Agent Discovery
    • Static orchestration
      • Sequential
    • Dynamic orchestration
      • Dynamic Triage
      • Handoffs (based on async Swarm)
      • Group Chat
  • Runtime
    • NATSRuntime (NATS-based Distributed Runtime)
    • HTTPRuntime (HTTP-based Distributed Runtime)
    • LocalRuntime (In-process Runtime)
  • CoS (Multi-language support)
  • Cross-language support
    • Protocol Buffers

Three-tier Architecture

Prerequisites

Start a NATS server (docs):

docker run -p 4222:4222 --name nats-server -ti nats:latest

Installation

pip install git+https://github.com/OpenCSGs/coagent.git

Quick Start

Simple Agent

Create a Ping-pong agent:

# server.py

import asyncio

from coagent.core import (
    BaseAgent,
    Context,
    handler,
    idle_loop,
    Message,
    new,
)
from coagent.runtimes import NATSRuntime


class Ping(Message):
    pass


class Pong(Message):
    pass


class Server(BaseAgent):
    """The Ping-pong agent."""

    @handler
    async def handle(self, msg: Ping, ctx: Context) -> Pong:
        """Handle the Ping message and return a Pong message."""
        return Pong()


async def main():
    async with NATSRuntime.from_servers("nats://localhost:4222") as runtime:
        await runtime.register("server", new(Server))
        await idle_loop()


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

Run the agent:

python server.py

Communicate with the agent:

coagent server -H type:Ping

Chat Agent (LLM-based)

Create a Translator agent:

# translator.py

import asyncio
import os

from coagent.agents import ChatAgent, ModelClient
from coagent.core import idle_loop, new, set_stderr_logger
from coagent.runtimes import NATSRuntime


class Translator(ChatAgent):
    """The translator agent."""

    system = "You are a professional translator that can translate Chinese to English."

    client = ModelClient(
        model=os.getenv("MODEL_NAME"),
        api_base=os.getenv("MODEL_API_BASE"),
        api_version=os.getenv("MODEL_API_VERSION"),
        api_key=os.getenv("MODEL_API_KEY"),
    )


async def main():
    async with NATSRuntime.from_servers("nats://localhost:4222") as runtime:
        await runtime.register("translator", new(Translator))
        await idle_loop()


if __name__ == "__main__":
    set_stderr_logger("TRACE")
    asyncio.run(main())

Run the agent:

export MODEL_NAME=<YOUR_MODEL_NAME>
export MODEL_API_BASE=<YOUR_MODEL_API_BASE>
export MODEL_API_VERSION=<YOUR_MODEL_API_VERSION>
export MODEL_API_KEY=<YOUR_MODEL_API_KEY>
python translator.py

Communicate with the agent:

coagent translator -H type:ChatHistory -d '{"messages":[{"role":"user","content":"你好"}]}' --chat

Examples

About

An open-source framework for building distributed, scalable, and collaborative multi-agent applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published