Skip to main content

Official Python SDK for ToothFairyAI API

Project description

ToothFairyAI Python SDK

Official Python SDK for the ToothFairyAI API.

Installation

pip install toothfairyai

Quick Start

from toothfairyai import ToothFairyClient

# Initialize the client
client = ToothFairyClient(
    api_key="your-api-key",
    workspace_id="your-workspace-id"
)

# Send a message to an agent (non-streaming)
response = client.chat.send_to_agent(
    message="Hello, how can you help me?",
    agent_id="your-agent-id"
)
print(response.agent_response)

Streaming Responses

Stream responses with an iterator pattern, similar to OpenAI's Python SDK:

# Simple streaming - iterate over events
stream = client.streaming.send_to_agent(
    message="Tell me about dental care",
    agent_id="your-agent-id"
)

for event in stream:
    print(event.text, end="", flush=True)

print()
print(f"Chat ID: {stream.chat_id}")

Streaming with Event Types

stream = client.streaming.send_to_agent("Hello", "agent-id")

for event in stream:
    if event.is_token:
        # Token events contain streaming text
        print(event.text, end="", flush=True)
    elif event.is_complete:
        # Stream is complete
        print("\nDone!")
    elif event.is_error:
        # Handle errors
        print(f"Error: {event.data}")

Collect Full Response

stream = client.streaming.send_to_agent("Hello", "agent-id")
full_response = stream.collect()  # Blocks until complete
print(full_response)

Continue a Conversation

# First message creates a new chat
stream1 = client.streaming.send_to_agent("Hello", "agent-id")
for event in stream1:
    print(event.text, end="")

# Continue in the same chat
stream2 = client.streaming.send_to_agent(
    message="Tell me more",
    agent_id="agent-id",
    chat_id=stream1.chat_id  # Use the chat ID from first stream
)
for event in stream2:
    print(event.text, end="")

Features

  • Chat Management: Create, update, and manage chat conversations
  • Streaming: Real-time streaming responses with iterator pattern (like OpenAI)
  • Document Management: Upload, search, and manage documents
  • Entity Management: Manage topics, intents, and NER entities
  • Folder Management: Organize content with folder hierarchies
  • Prompt Management: Create and manage prompt templates

API Reference

Client Configuration

from toothfairyai import ToothFairyClient

client = ToothFairyClient(
    api_key="your-api-key",           # Required
    workspace_id="your-workspace-id", # Required
    base_url="https://api.toothfairyai.com",  # Optional
    ai_url="https://ai.toothfairyai.com",     # Optional
    ai_stream_url="https://ais.toothfairyai.com",  # Optional
    timeout=120  # Optional, in seconds
)

Chat Operations

# Create a chat
chat = client.chat.create(
    name="Customer Support",
    customer_id="customer-123"
)

# Get a chat
chat = client.chat.get(chat_id="chat-id")

# List chats
chats = client.chat.list(limit=10, offset=0)

# Send message to agent (non-streaming)
response = client.chat.send_to_agent(
    message="Hello",
    agent_id="agent-id",
    chat_id="existing-chat-id",  # Optional
    attachments={
        "images": ["https://example.com/image.jpg"],
        "files": ["https://example.com/document.pdf"]
    }
)

Streaming Operations

# Stream response from agent
stream = client.streaming.send_to_agent(
    message="Hello",
    agent_id="agent-id",
    chat_id="existing-chat-id",  # Optional - continue conversation
    attachments={
        "images": ["https://example.com/image.jpg"]
    }
)

# Iterate over events
for event in stream:
    print(event.text, end="")

# Access metadata after streaming
print(f"Chat: {stream.chat_id}")
print(f"Message: {stream.message_id}")
print(f"Full text: {stream.text}")

Document Operations

# Upload a document
result = client.documents.upload(
    file_path="./document.pdf",
    on_progress=lambda p, l, t: print(f"Progress: {p}%")
)

# Upload from base64
result = client.documents.upload_from_base64(
    base64_data="...",
    filename="document.pdf",
    content_type="application/pdf"
)

# Search documents
results = client.documents.search(
    text="dental procedures",
    top_k=10
)

# Create document from URL
doc = client.documents.create_from_path(
    file_path="https://example.com/document.pdf",
    user_id="user-123",
    title="My Document",
    folder_id="folder-id"
)

Entity Operations

# Create an entity
entity = client.entities.create(
    user_id="user-123",
    label="Dental Cleaning",
    entity_type="topic",
    description="Professional teeth cleaning procedures"
)

# List entities by type
topics = client.entities.get_by_type("topic")
intents = client.entities.get_by_type("intent")

# Search entities
results = client.entities.search("cleaning", entity_type="topic")

Folder Operations

# Create a folder
folder = client.folders.create(
    user_id="user-123",
    name="Procedures",
    description="Medical procedures documentation"
)

# Get folder tree
tree = client.folders.get_tree()

# Get subfolders
subfolders = client.folders.get_subfolders(parent_id="folder-id")

Prompt Operations

# Create a prompt
prompt = client.prompts.create(
    user_id="user-123",
    label="Greeting",
    prompt_type="greeting",
    interpolation_string="Hello {{name}}, how can I help you today?"
)

# Clone a prompt with modifications
cloned = client.prompts.clone(
    prompt_id="prompt-id",
    user_id="user-123",
    label="New Greeting"
)

Error Handling

from toothfairyai import ToothFairyClient, ToothFairyError

client = ToothFairyClient(api_key="...", workspace_id="...")

try:
    response = client.chat.send_to_agent("Hello", "agent-id")
except ToothFairyError as e:
    print(f"Error: {e.message}")
    print(f"Code: {e.code}")
    print(f"Status: {e.status_code}")

Connection Testing

# Test connection
is_connected = client.test_connection()
print(f"Connected: {is_connected}")

# Get health status
health = client.get_health()
print(f"Status: {health['status']}")

License

MIT License - see LICENSE for details.

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

toothfairyai-0.4.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

toothfairyai-0.4.0-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file toothfairyai-0.4.0.tar.gz.

File metadata

  • Download URL: toothfairyai-0.4.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for toothfairyai-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c21b3f57f1ad4a8afdf92aaef9ea4d9b20b92bb91a197961c48d09c7be5386c9
MD5 eb6087f2bad5da2b60948199f8f8321e
BLAKE2b-256 8716e7aad6049acf8eeaeb0a31bc5e74db69e96c98ad1d82a6d57f13fbf70ee1

See more details on using hashes here.

File details

Details for the file toothfairyai-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: toothfairyai-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 29.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for toothfairyai-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c05c68ffb881d3198b9218ea11983d56e33f15a00074215f9c2bcf9732f53314
MD5 d838f7d374820f199416a1c27856b0ce
BLAKE2b-256 07b4815068d06823901a53100b11f1437775086f454845a47be20d1631895a5e

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