Complete Examples

Use these end-to-end snippets as starting points for real applications and automation workflows.

RAG Pipeline

from touai import TouAI
 
with TouAI() as client:
    conn = client.connectors.connections.create(
        "Analytics DB",
        "postgresql",
        {"host": "db.example.com", "database": "analytics", "...": "..."},
        auto_sync=True,
    )
 
    client.connectors.sync.wait_until_ready(conn.connection_id, timeout=600)
 
    results = client.connectors.search(
        "What were our top-performing products in Q1?",
        connection_ids=[conn.connection_id],
        top_k=10,
        rerank=True,
    )
 
    for hit in results.results:
        print(f"[{hit.score:.2f}] {hit.citation.get('entity_name')}")

Document Processing Pipeline

from touai import TouAI
 
with TouAI() as client:
    job = client.unstructured.jobs.create(
        source={"source_type": "url", "url": "https://example.com/report.pdf"},
        options={"chunking": {"enabled": True}},
    )
 
    result = client.unstructured.jobs.wait_until_complete(job.job_id)
    print(f"Processed: {result.status}")
 
    research = client.deep_research.research(
        "Summarize the key findings from the processed document",
        mode="pro",
    )
    print(research.content)

Web Intelligence Gathering

from touai import TouAI
 
with TouAI() as client:
    crawl = client.web_access.deep_crawl_and_wait(
        "https://docs.example.com",
        max_depth=3,
        max_pages=100,
        timeout=300,
    )
    print(f"Crawled {len(crawl.pages)} pages")
 
    for event in client.deep_research.research_stream(
        "What are the main features documented on this site?",
        mode="pro",
    ):
        if event.type == "complete":
            print(event.data.get("content"))

Store, Index, and Search a Knowledge Base

from touai import TouAI
 
with TouAI() as client:
    kb = client.knowledge_base.bases.create("Product Docs")
 
    # Indexing starts automatically; poll until the document is searchable
    doc = client.knowledge_base.documents.upload(kb.id, "handbook.pdf")
    while doc.index_state == "pending":
        doc = client.knowledge_base.documents.get(doc.id)
 
    hits = client.knowledge_base.search(kb.id, "How do refunds work?", top_k=5)
    for hit in hits.hits:
        print(f"[{hit.score:.2f}] {hit.title}")
 
    # Archive the source file in project object storage
    stored = client.object_storage.store(open("handbook.pdf", "rb"))
    print(stored.key)

These examples are intended to be copied and adapted. Start with the simplest one that matches your workflow, then add auth, retries, and persistence for your production environment.

Type Reference