FastAgency

repository·main·Indexed 20 days ago

https://github.com/ag2ai/fastagency

A deployment and orchestration framework for AG2 (formerly AutoGen) multi-agent workflows. FastAgency bridges the gap between Jupyter notebook prototypes and production-ready applications by providing unified interfaces for UIs (Console, Mesop) and scalable network adapters (FastAPI, NATS).

Tokens
42.4K
Snippets
151
Records
234
Agent score
67%

What's inside fastagency

  1. What is FastAgency?

    main

    FastAgency is an open-source framework designed to accelerate the transition of multi-agent AI workflows from prototype to production. It provides a unified programming interface for deploying workflows written in the AG2 (formerly AutoGen) framework.

    Key capabilities include:

    • Unified UI Support: Develop core workflows once and reuse them across different interfaces like ConsoleUI (CLI) and MesopUI (Web).
    • External API Integration: Easily connect agents to external services by importing OpenAPI specifications.
    • Production Scaling: Transition from local development to distributed systems using network adapters like FastAPIAdapter (REST) or NatsAdapter (NATS.io message broker).
    • Testing & CI: Use a dedicated Tester Class to write and execute tests for multi-agent interactions within CI pipelines.
  2. Available User Interfaces in FastAgency

    main

    FastAgency supports multiple user interface (UI) options to interact with and manage multi-agent workflows, catering to different stages of the development lifecycle:

    1. ConsoleUI: A command-line interface (CLI) designed for quick testing and prototyping directly in the terminal.
    2. MesopUI: A web-based interface designed for graphical and interactive browser-based experiences, suitable for more user-friendly applications.

    Choose ConsoleUI for early-stage development and terminal-based debugging, or MesopUI when building applications that require a web interface for end-users.

  3. What is Dependency Injection in FastAgency?

    main

    Dependency Injection in FastAgency is a security pattern used to connect external functions to agents without exposing sensitive data (such as passwords, tokens, or personal information) to the Large Language Model (LLM).

    By using this pattern, sensitive parameters are kept in a local context dictionary and are only injected into the target function at runtime. This prevents sensitive data from being included in prompts, protecting against prompt injection attacks and ensuring compliance with data privacy regulations like the EU AI Act.

  4. Configure WebSurferAgent and Giphy Agent

    main

    In a multi-agent system:

    • WebSurferAgent: Responsible for scraping web content. It can be configured with a summarizer to condense retrieved data before passing it to other agents.
    • Giphy Agent: Typically implemented as a ConversableAgent from AG2. It uses system messages and registered functions (like search_gifs or trending_gifs) to interact with the Giphy API based on the conversation context.
  5. Configure authentication for FastAgency

    main

    FastAgency supports several authentication mechanisms. The default is Basic Authentication.

    • Basic Authentication: Configure usernames and their bcrypt hashed passwords in the BasicAuth class, then apply the authentication object to the MesopUI object.
    • Firebase Authentication: Supported via specific integration.
    • No Authentication: Available for local/unsecured testing.

    You can select the authentication type during the initial project setup with Cookiecutter.

  6. Supported Network Adapters for Scalable Deployment

    main

    To move from local prototypes to production-ready, scalable architectures, FastAgency provides chainable network adapters:

    • REST API (FastAPI): Use the FastAPIAdapter to serve your workflow via a FastAPI server. This allows workflows to run in multiple workers and leverages the ASGI server for high extensibility and stability.
    • NATS.io (FastStream): Use the NatsAdapter to utilize the NATS.io message broker. This is suitable for highly-scalable, production-ready setups, such as workflows running within a VPN or combined with FastAPIAdapter to serve public workflows in a secure, authenticated manner.
  7. Use MesopUI for interactive multi-agent applications

    main
    The MesopUI interface allows developers to build interactive, web-based multi-agent applications. It provides a visual, browser-accessible environment for users to interact with agents, making it suitable for complex scenarios like tutoring systems, customer support, or real-time information retrieval.
  8. Use ConsoleUI for text-based workflow interaction

    main

    ConsoleUI provides a command-line interface for interacting with multi-agent workflows. This is useful for rapid prototyping and debugging without a web UI.

    To use it, instantiate ConsoleUI and link it to your workflow. You can then run your application using the fastagency run CLI command.

    from fastagency.ui.console import ConsoleUI
    
    # Assuming 'app' is your FastAgency application instance
    ui = ConsoleUI(app)
    ui.run()
  9. Define a workflow in FastAgency

    main

    Workflows are defined by registering functions to a Workflow instance. You use the UI object to handle user interactions (like text inputs) and the register decorator to name and describe the workflow step. Inside the workflow, you can define agents (e.g., using autogen.ConversableAgent) and execute their interactions.

    Key components:

    • Workflow(): The main container for registering workflow steps.
    • @wf.register(...): Decorator to define a workflow function.
    • ui.text_input(...): Captures input from the user.
    • ui.process(response): Processes the agent interaction result for the UI.
    import os
    from typing import Any
    from autogen import ConversableAgent, LLMConfig
    from fastagency import UI
    from fastagency.runtimes.ag2 import Workflow
    
    llm_config = LLMConfig(
        model="gpt-4o-mini",
        api_key=os.getenv("OPENAI_API_KEY"),
        temperature=0.8,
    )
    
    wf = Workflow()
    
    @wf.register(name="simple_learning", description="Student and teacher learning chat")
    def simple_workflow(ui: UI, params: dict[str, Any]) -> str:
        initial_message = ui.text_input(
            sender="Workflow",
            recipient="User",
            prompt="I can help you learn about mathematics. What subject you would like to explore?",
        )
    
        with llm_config:
          student_agent = ConversableAgent(
              name="Student_Agent",
              system_message="You are a student willing to learn.",
          )
          teacher_agent = ConversableAgent(
              name="Teacher_Agent",
              system_message="You are a math teacher.",
          )
    
        response = student_agent.run(
            teacher_agent,
            message=initial_message,
            summary_method="reflection_with_llm",
            max_turns=3,
        )
    
        return ui.process(response)
  10. Use NatsAdapter for scalable distributed workflows

    main

    The NatsAdapter integrates FastAgency workflows with the Nats.io message broker. It is designed for scenarios requiring high scalability and observability.

    When to use NatsAdapter:

    • High User Demand: When you need to scale beyond the capacity of multiple workers on a single FastAPIAdapter instance. It allows scaling across multiple machines and clusters using a distributed message-queue architecture.
    • Observability: When you need to audit workflow executions both in real-time and retrospectively using the NATS infrastructure.
  11. Use the NatsAdapter for scalable messaging and observability

    main

    The NatsAdapter integrates workflows with the Nats.io message broker (MQ). It is designed for:

    • High User Demand: Scaling beyond the capabilities of multiple FastAPI workers by using a distributed message-queue architecture that spans multiple machines and clusters.
    • Observability: Providing the infrastructure necessary to audit workflow executions both in real-time and retrospectively.