Skip to main content

Framework for building AI assistants with streaming updates and user interactions

Project description

Tfy Assistant Framework

Overview

The Tfy Assistant Framework is a framework for building AI assistants with streaming updates and user interactions.

Installation

pip install tfy-assistant-framework

Usage

Prerequisites

  1. Create a .env file following the .env.example template
  2. Configure TFY_* environment variables if using Truefoundry's NATS service
    • Skip this if using your own NATS connection and JetStream context

Basic Chat Assistant

Here's a minimal example of a chat assistant:

from tfy_assistant_framework.swarm import Agent, client
from typing import Any

# Custom chat agent that inherits from the base Agent class
class ChatAssistant(Agent[Any]): ...

# Initialize the chat agent
chat_agent = ChatAssistant(instructions="You are a helpful AI assistant")

async def run_chat_assistant() -> None:
    # Start interactive chat session
    async for agent_run in client.run(
        agent=chat_agent,
        messages=[],  # Initial messages (if any)
        interactive=True
    ):
        agent_run.debug_log()

if __name__ == "__main__":
    import asyncio
    asyncio.run(run_chat_assistant())

FastAPI Integration

To serve the assistant via a REST API:

from fastapi import FastAPI, Response
from nats.js import JetStreamContext
from contextlib import asynccontextmanager
from typing import AsyncIterator
import uuid

from tfy_assistant_framework.assistant import (
    AssistantServe,
    CreateAssistantTaskRunResult,
    NATSAssistantUpdateSink,
    PostAssistantTaskMessage,
)
from tfy_assistant_framework.nats_client import nats_connection

# Initialize global variables
js: JetStreamContext
assistant_serve = AssistantServe()

@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
    global js
    async with nats_connection() as (nc, js_):
        js = js_
        async with assistant_serve.init(nc=nc, app=app):
            yield

# Create FastAPI app
app = FastAPI(lifespan=lifespan)

@app.post("/assistants/chat/task")
async def create_chat_task() -> CreateAssistantTaskRunResult:
    """Create a new chat assistant task"""
    task_id = uuid.uuid4().hex
    return await assistant_serve.create_assistant_task(
        task_id=task_id,
        assistant_awaitable=run_chat_assistant(),
        assistant_update_sink=NATSAssistantUpdateSink(task_id=task_id, js=js),
    )

@app.post("/tasks/{task_id}/message")
async def send_message(
    task_id: str,
    message: PostAssistantTaskMessage,
) -> Response:
    """Send a message to an existing assistant task"""
    return await assistant_serve.send_message_to_assistant_task(task_id, message)

For an end-to-end example, see examples.

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

tfy_assistant_framework-0.1.5rc3.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

tfy_assistant_framework-0.1.5rc3-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file tfy_assistant_framework-0.1.5rc3.tar.gz.

File metadata

File hashes

Hashes for tfy_assistant_framework-0.1.5rc3.tar.gz
Algorithm Hash digest
SHA256 5fd8d617f35be25f40f5cdf9b1bf9db22f940fc566a4a7e6155344565754e6e6
MD5 2551b9fcaa80c8ce5162f6cda7e786f9
BLAKE2b-256 140c70128e928c8b587a63c4dee075b66d1115237bfb33c6481214ab8d7d2b5e

See more details on using hashes here.

File details

Details for the file tfy_assistant_framework-0.1.5rc3-py3-none-any.whl.

File metadata

File hashes

Hashes for tfy_assistant_framework-0.1.5rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 b4a30b800d89343c0d666722a53b7c6cce5331c954da165cf9509db93a3f232e
MD5 d8c5a90351d9d7a9e066b7cd2f261df3
BLAKE2b-256 8b01351b5eca301a06ca79a3611db60e5d133a028361cb2360ae9b2a18f0c3eb

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