|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: AI chat API |
| 4 | +--- |
| 5 | + |
| 6 | +The AI chat API converts natural language questions into SQL queries for retrieving CFEngine data. |
| 7 | + |
| 8 | +## Send message to AI chat |
| 9 | + |
| 10 | +Converts a question into a SQL query and explanation. The AI chat uses LLM configured via [AI settings][AI settings API]. |
| 11 | + |
| 12 | +**URI:** https://hub.cfengine.com/api/ai/text-to-sql |
| 13 | + |
| 14 | +**Method:** POST |
| 15 | + |
| 16 | +**Prerequisites:** |
| 17 | + |
| 18 | +At least one LLM configuration must be created using the [AI settings API][AI settings API#Create AI setting]. |
| 19 | + |
| 20 | +**Parameters:** |
| 21 | + |
| 22 | +- **question** _(string)_ |
| 23 | + The question to ask. Required. |
| 24 | +- **history** _(array)_ |
| 25 | + Optional conversation history for context. Array of previous question/answer pairs to maintain conversation context. |
| 26 | + |
| 27 | +**Example request (curl):** |
| 28 | + |
| 29 | +```console |
| 30 | +curl --user <username>:<password> \ |
| 31 | + -X POST \ |
| 32 | + https://hub.cfengine.com/api/ai/text-to-sql \ |
| 33 | + -H 'Content-Type: application/json' \ |
| 34 | + -d '{ |
| 35 | + "question": "Hosts that have SSH port open" |
| 36 | + }' |
| 37 | +``` |
| 38 | + |
| 39 | +**Example request with conversation history:** |
| 40 | + |
| 41 | +```console |
| 42 | +curl --user <username>:<password> \ |
| 43 | + -X POST \ |
| 44 | + https://hub.cfengine.com/api/ai/text-to-sql \ |
| 45 | + -H 'Content-Type: application/json' \ |
| 46 | + -d '{ |
| 47 | + "question": "What about their uptime?", |
| 48 | + "history": [ |
| 49 | + { |
| 50 | + "question": "Hosts that have SSH port open", |
| 51 | + "sql": "SELECT h.hostname, i.values ->> '\''OS'\'' AS os, h.ipaddress, i.values ->> '\''Ports listening'\'' AS open_ports FROM hosts h JOIN inventory_new i ON h.hostkey = i.hostkey WHERE COALESCE(i.values ->> '\''Ports listening'\'', '\'' '\'') ~ '\''(,\\s|^)22(,|$)'\''" |
| 52 | + } |
| 53 | + ] |
| 54 | + }' |
| 55 | +``` |
| 56 | + |
| 57 | +**Example response:** |
| 58 | + |
| 59 | +``` |
| 60 | +HTTP 200 OK |
| 61 | +{ |
| 62 | + "explanation": "This query lists hosts with SSH port open, including their hostname, OS, IP address, open ports, and uptime in minutes.", |
| 63 | + "sql": "SELECT h.hostname, i.values ->> 'OS' AS os, h.ipaddress, i.values ->> 'Ports listening' AS open_ports, i.values ->> 'Uptime minutes' AS uptime_minutes FROM hosts h JOIN inventory_new i ON h.hostkey = i.hostkey WHERE COALESCE(i.values ->> 'Ports listening', '\u3000') ~ '(,\\s|^)22(,|$)'", |
| 64 | + "meta": { |
| 65 | + "provider": "openai", |
| 66 | + "model": "gpt-4.1" |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +**Output:** |
| 72 | + |
| 73 | +- **explanation** |
| 74 | + Explanation of what the SQL query does and what data it retrieves. |
| 75 | +- **sql** |
| 76 | + The generated SQL query that can be executed against the CFEngine reporting database. |
| 77 | +- **meta** |
| 78 | + Metadata about the LLM used to generate the response. |
| 79 | + - **provider** - The LLM provider used (e.g., `openai`, `anthropic`, `ollama`). |
| 80 | + - **model** - The specific model used (e.g., `gpt-4`, `claude-3-opus`). |
| 81 | + |
| 82 | +**Responses:** |
| 83 | + |
| 84 | +| HTTP response code | Description | |
| 85 | +| ---------------------------- | ------------------------------------------------ | |
| 86 | +| 200 OK | Successful response with SQL query | |
| 87 | +| 400 Bad Request | Invalid request data or LLM processing error | |
| 88 | +| 422 Unprocessable Entity | No LLM configuration found | |
| 89 | + |
| 90 | +**Error response examples:** |
| 91 | + |
| 92 | +Missing LLM configuration: |
| 93 | + |
| 94 | +``` |
| 95 | +HTTP 422 Unprocessable Entity |
| 96 | +Add an LLM configuration before using the chat. |
| 97 | +``` |
| 98 | + |
| 99 | +Invalid request: |
| 100 | + |
| 101 | +``` |
| 102 | +HTTP 400 Bad Request |
| 103 | +{ |
| 104 | + "success": false, |
| 105 | + "errors": [ |
| 106 | + { |
| 107 | + "field": "[question]", |
| 108 | + "message": "This value should be of type string." |
| 109 | + } |
| 110 | + ] |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Security considerations |
| 115 | + |
| 116 | +The AI chat does not execute SQL queries automatically - it only generates them. You must execute the queries separately through the [Query API][Query REST API#Execute SQL query]. AI does not have access to any |
| 117 | +of your data, unless this data will be sent to the chat by a user. |
0 commit comments