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
- Create a
.envfile following the.env.exampletemplate - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tfy_assistant_framework-0.1.6rc2.tar.gz.
File metadata
- Download URL: tfy_assistant_framework-0.1.6rc2.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61d646ae1a47f6df09152433bc4c115600059c7c3434a236d5b360c5a65b5c0e
|
|
| MD5 |
184122c4f1a39d705c645445fa3a1d07
|
|
| BLAKE2b-256 |
9b2823bb75acfd8b6d77619f07ba791c2ac1549f53097aa7a36bdd76cb89ea4e
|
File details
Details for the file tfy_assistant_framework-0.1.6rc2-py3-none-any.whl.
File metadata
- Download URL: tfy_assistant_framework-0.1.6rc2-py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
441136412cdcfdadf0f78e6b1e3b511410b0485483d2e9daa2b3ceef3762ba06
|
|
| MD5 |
e90195e441030188534c84db8879cf75
|
|
| BLAKE2b-256 |
2ab947be37b5816077eb67b0aafc21ed553eff6c2919cb2c96f4b37decf9ccdc
|