---
title: LLM assistance
summary: Turn a sentence into a BQL query or a BVL visualisation with TextToAction, run it on the server with Query or ResolveBvl, and show the result in the 3D viewer, all through the OpenAPI.
order: 60
languages: [javascript]
terms: [action, bql, bvl, qid, scope]
requires: [AI]
since: 3.0
see-also: [reference/bvl, reference/bql, how-to/query/run-a-bql-query, how-to/viewer3d/select-and-colour-objects, how-to/viewer3d/embed-the-viewer, api/authentication]
---

# LLM assistance

A user types *colour the external walls red* and the model in your viewer turns red. That takes three calls: a language model turns the sentence into an [[term:action|action]], the server runs that action against the model, and the viewer shows the answer. This guide makes the three calls through the [[term:openapi|OpenAPI]], so they work from any language; only the last step runs in the browser.

| Step | Call | Answer |
| --- | --- | --- |
| 1. Text → action | [[openapi:TextToAction]] | A [[term:bql|BQL]] query or a [[term:bvl|BVL]] visualisation, as code |
| 2. Run it on the server | [[openapi:Query]] for BQL, [[openapi:ResolveBvl]] for BVL | Records, or the objects to show, hide, select and colour |
| 3. Show it in the viewer | none: the 3D viewer's `ObjectStateManager` and `BvlHelper` | The objects coloured and framed |

## Try it

The demo below makes the three calls against the demo model with your account's demo API token. Type a sentence or pick an example, then *Run all three steps*, or go one step at a time: every card shows its request and the answer, and its `POST /openapi/…` line links to the operation.

```demo
name: text-to-action
title: Text to action: translate, run, show
models: schependomlaan
height: 680
needs: Loads the demo model in this page with the demo API token of your account, and asks the language model of this instance: open BIM.works and sign in once in this browser.
notes: |
  The language model writes the action, so the same sentence can come back worded differently. Edit the code in step 2 and run it again to see what changes. Without the AI feature on your account, or AI on the instance, step 1 is dimmed; type BQL or BVL in step 2 yourself and run it. A query only shows objects when its records carry a `qid`; the demo offers to add it.
```

## Before you start

- AI on the instance (`ai.enabled`, with a model under `ai.text_to_actions`) and the [[feature:AI]] feature on the account behind your token, switched on per account or organisation in the account settings. Without them TextToAction answers `403`, for example *Scope `ai:use` requires the AI user feature*; steps 2 and 3 do not need them.
- An auth token with the [[term:scope|scopes]] `ai:use` (step 1), `query:run` (BQL) and `actions:read` (BVL). [[page:how-to/auth/log-in-with-an-api-token]] gets one from an [[term:api-token|API token]] with [[openapi:LoginWithApiToken]]; every call below sends it as `Authorization: Bearer <token>` ([[page:api/authentication]]).
- The [[term:version|version]] UUID of the model; the examples use the first demo model.

## 1. Turn the text into an action {#step-1}

Send the sentence to [[openapi:TextToAction]]. `models` is optional context: the language model may run aggregate queries on those models first, to use the property names they really carry. The server chooses the connection with `ai.text_to_action_tools`: `mcp` (the default, requiring `ai.mcp_server_enabled` and a reachable server) or `inline` (queries run internally, with no inbound connection required). `language` (`BQL`, `BVL` or `BCL`) forces the kind of action; without it the model picks one from the wording.

```try-it {#try-text-to-action}
operation: TextToAction
method: POST
path: /openapi/ai/TextToAction
body: |
  {
    "input": "Colour the external walls red",
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}]
  }
```

The answer is the action, parsed and formatted by the server:

```json {#text-to-action-answer}
{
  "language": "BVL",
  "query": "if (type = IFCWALL AND property.*.IsExternal = true) {\n    COLOR \"red\";\n}\n",
  "main_type": "BVL",
  "description": "Colour the external walls red"
}
```

- `language` says which call runs `query`: `BQL` goes to [[openapi:Query]], `BVL` to [[openapi:ResolveBvl]], and `BCL`, a [[term:check|check]] in [[term:bcl|BCL]], to [[openapi:Check]].
- Code that does not parse, or an answer in the wrong format (for example an invented JSON `action` block), goes back to the model once with correction feedback. If the second answer still has invalid code or format, or the model answers in another language than the `language` you asked for, the call fails with `502` and the `message` says what happened, the model's own words included. Show that message to the user and let them rephrase.
- Nothing runs and nothing is saved as an action; the instance logs the sentence and the answer of every successful call. `main_type` is the action type to use when you do save it in the [[term:actions-tree|actions tree]].

## 2. Run the action on the server {#step-2}

Send `query` as it came. For BQL that is [[openapi:Query]], the call of [[page:how-to/query/run-a-bql-query]]:

```try-it {#try-query}
operation: Query
method: POST
path: /openapi/ifc/Query
body: |
  {
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
    "query": "GET qid, type, attribute.Name WHERE type = IFCDOOR"
  }
```

For BVL it is [[openapi:ResolveBvl]]. The server evaluates every condition of the script and answers with what the script does: sets of objects to show, hide, select and frame, a set per colour, labels and annotations.

```try-it {#try-resolve-bvl}
operation: ResolveBvl
method: POST
path: /openapi/ifc/ResolveBvl
body: |
  {
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
    "bvl": "if (type = IFCWALL AND property.*.IsExternal = true) { COLOR \"red\" VIEWFIT; }"
  }
```

```json {#resolve-bvl-answer}
{
  "color": {"#ff0000ff": {"797e993c-a87f-4aa7-9587-c59d30ec5c4e": [1316, 1402]}},
  "viewFit": {"797e993c-a87f-4aa7-9587-c59d30ec5c4e": [1316, 1402]},
  "force": true
}
```

Every set is keyed by version UUID and lists object ids, so `797e993c-…-1316` is the [[term:qid|qid]] of the first wall; a colour is `#rrggbbaa`, the last byte its opacity. The script language itself is on [[page:reference/bvl]].

## 3. Show the result in the viewer {#step-3}

This step runs in the browser, with a viewer set up as in [[page:how-to/viewer3d/embed-the-viewer]]. A BVL answer goes to the viewer as it is; `BvlHelper` of `@bimworks/viewercommon` applies every key of it to the [[term:object-state-manager|ObjectStateManager]]:

```javascript {#show-bvl}
import {BvlHelper} from "@bimworks/viewercommon";

await objectStateManager.resetAll();   // undo what the previous answer did
await new BvlHelper(objectStateManager, viewer).processResults([resolved]);
```

A BQL answer is records, so you decide what showing it means. Colouring the objects and framing them works for any query whose records carry a `qid`:

```javascript {#show-bql}
import {QidSet} from "@bimworks/jssdk";
import {ColorChangeMap} from "@bimworks/viewercommon";

const qids = new QidSet();
const colours = new ColorChangeMap();
for (const record of records) {
	if (record.qid) {
		qids.add(record.qid);
		colours.changeQid(record.qid, "#E68523");
	}
}
await objectStateManager.resetAll();
await objectStateManager.changeColorMulti(colours);
await objectStateManager.viewFit(qids, {animate: true});
```

A query without `qid` in its `GET` list (a count per type, say) has no object to point at: ask for `qid`, or `COLLECT(qid)` next to a `GROUP BY` to get the objects of every group. More on colours and selection in [[page:how-to/viewer3d/select-and-colour-objects]].

## Check

With *Colour the external walls red* the demo answers with BVL in step 1, step 2 lists one colour with the external walls under it, and step 3 turns those walls red in the viewer. *List every door with its name* answers with BQL; when the records carry a `qid`, step 3 colours and frames the doors.
