> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/ai/chat-style-messages/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

Send structured **role-based messages** and optionally enable **knowledge-base retrieval**.

## Support agent with system prompt

```python
import asyncio
from dnotifier import DNotifier

USER_ID = "user-123"

async def main():
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="http",
        user_id=USER_ID,
    )

    await notifier.connect()

    response = await notifier.send_ai(
        sender_id=USER_ID,
        message={
            "useKnowledgeBase": False,
            "messages": [
                {
                    "role": "system",
                    "content": "You are a helpful support agent. Answer in under 100 words.",
                },
                {"role": "user", "content": "How do I reset my password?"},
            ],
        },
        save_history=True,
    )

    print(response)

asyncio.run(main())
```

## RAG-enabled Q\&A

```python
response = await notifier.send_ai(
    sender_id=USER_ID,
    message={
        "useKnowledgeBase": True,
        "messages": [
            {
                "role": "system",
                "content": "Answer using only the provided knowledge. If unsure, say you do not know.",
            },
            {"role": "user", "content": "What is the refund timeline?"},
        ],
    },
)
```

## Few-shot classification

```python
await notifier.send_ai(
    sender_id=USER_ID,
    message={
        "messages": [
            {
                "role": "system",
                "content": 'Classify intent. Reply JSON only: {"intent":"billing"|"technical"}',
            },
            {"role": "user", "content": "My invoice is wrong"},
            {"role": "assistant", "content": '{"intent":"billing"}'},
            {"role": "user", "content": "The app crashes on launch"},
        ],
    },
    save_history=False,
)
```

## Helper function

```python
async def ask_support(notifier: DNotifier, question: str) -> str:
    response = await notifier.send_ai(
        sender_id=USER_ID,
        message={
            "useKnowledgeBase": True,
            "messages": [
                {"role": "system", "content": "You are a support agent."},
                {"role": "user", "content": question},
            ],
        },
    )
    if isinstance(response, dict) and isinstance(response.get("data"), dict):
        return str(response["data"].get("content", ""))
    return str(response)
```

## Learn more

* [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/python.md) — `session_id` for multi-turn
* [**Manage documents**](https://dnotifier.gitbook.io/product-docs/ai/manage-documents/python.md) — Index content
