Aller au contenu principal

AI Chat with API Tools

The AI chatbot now has access to your Baldr API through a comprehensive tool system, allowing it to perform CRUD operations and query your data.

Available Tools

Health & System

  • get_api_health: Check API health status

Module Management

  • list_modules: Get all available modules
  • get_module_by_slug: Get detailed module information

Database Queries

  • query_collection: Query any collection with filters and pagination
    • Supported collections: news, users, contacts, galleries

CRUD Operations

Create

  • crud_create: Create a new item in any collection
    {
    "collection": "news",
    "data": {
    "title": "My News Article",
    "subtitle": "A great subtitle",
    "content": "Article content here",
    "moduleSlug": "blog",
    "active": true
    }
    }

Read

  • crud_get_by_id: Get a single item by ID
  • crud_get_all: Get all items with pagination
    {
    "collection": "news",
    "page": 1,
    "limit": 10,
    "search": "keyword",
    "filter": { "active": true }
    }

Update

  • crud_update: Update an existing item
    {
    "collection": "news",
    "id": "507f1f77bcf86cd799439011",
    "data": {
    "title": "Updated Title"
    }
    }

Delete

  • crud_delete: Delete a single item
    {
    "collection": "news",
    "id": "507f1f77bcf86cd799439011"
    }

How It Works

1. Tool Discovery

When tools are enabled, the AI receives a system message describing all available tools and their capabilities.

2. Natural Language Commands

Users can ask the AI to perform operations in natural language:

  • "Create a new news article about AI"
  • "Show me all active news articles"
  • "Update the article with ID xyz"
  • "Delete the contact with ID abc"
  • "List all modules"

3. Tool Execution

The AI determines which tool to use and constructs the appropriate arguments based on the user's request.

API Endpoints

Get Available Tools

GET /api/ai-chat/tools
Authorization: Bearer <token>

Response:

{
"success": true,
"tools": [
{
"name": "crud_create",
"description": "Create a new item in a collection",
"parameters": { ... }
}
],
"count": 12
}

Execute a Tool

POST /api/ai-chat/execute-tool
Authorization: Bearer <token>

{
"tool": "crud_create",
"args": {
"collection": "news",
"data": { ... }
}
}

Response:

{
"success": true,
"data": {
"message": "news created successfully",
"data": { ... }
}
}

Chat with Tools Enabled

POST /api/ai-chat/chat
Authorization: Bearer <token>

{
"provider": "openai",
"model": "gpt-4o-mini",
"message": "Create a news article about AI",
"history": [],
"enableTools": true
}

Supported Providers

OpenAI (Best Support)

  • Full function calling support
  • Automatic tool detection and execution
  • Models: GPT-4o, GPT-4o-mini, GPT-4-turbo

Mistral AI

  • System message with tool descriptions
  • Manual tool selection via natural language
  • Models: Mistral Large, Medium, Small

Anthropic (Claude)

  • System message with tool descriptions
  • Excellent at following tool usage instructions
  • Models: Claude 3.5 Sonnet, Opus, Haiku

Google (Gemini)

  • System message with tool descriptions
  • Good at understanding tool capabilities
  • Models: Gemini 1.5 Pro, Flash

Example Conversations

Creating Content

User: "Create a news article titled 'AI Revolution' with content about modern AI"

AI: Uses crud_create tool

Response: "I've created a news article titled 'AI Revolution' with the content you requested. The article has been saved to the database."

Querying Data

User: "Show me the latest 5 news articles"

AI: Uses crud_get_all tool

Response: "Here are the 5 most recent news articles:

  1. AI Revolution (2025-12-01)
  2. Tech Update (2025-11-30) ..."

Updating Content

User: "Update the article with ID 123abc to make it active"

AI: Uses crud_update tool

Response: "I've updated the article. It's now marked as active and visible on your website."

Frontend Integration

The frontend chat interface automatically:

  • Enables tools for all AI conversations
  • Displays available tools to users
  • Handles tool execution responses
  • Shows tool results in a user-friendly format

Security

  • All tool executions are authenticated via JWT
  • Users can only access data they have permissions for
  • Tool execution is logged for audit purposes
  • API keys are never exposed to the AI