Skip to main content

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.

Release files for tfy-assistant-framework 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tfy-assistant-framework 0.1.10
File Size Uploaded
tfy_assistant_framework-0.1.10.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tfy-assistant-framework 0.1.10
File Interpreter ABI Platform
tfy_assistant_framework-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 52.0 kB

Release files / tfy_assistant_framework-0.1.10.tar.gz

Download URL tfy_assistant_framework-0.1.10.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
22f378207eb2a5a5615566592527912931d346b14566e44307d9419d89a5b147
BLAKE2b-256 checksum
How to use checksums
918c477f9b26959a59fffa9df8bf54e6525c12d23a9500c2fba870d052f37ea0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / tfy_assistant_framework-0.1.10-py3-none-any.whl

Download URL tfy_assistant_framework-0.1.10-py3-none-any.whl
Size 29.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2108e2a8de558a6b91fb55008005cfd30ebf008951647cf96ae44cac74ca3ef3
BLAKE2b-256 checksum
How to use checksums
0dc0a116057aaeff2ea6083041f274675ad3e3b0904f2d501b9085068b1db5b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page