# AI Secondary Development

## 1. Scope

AI secondary development usually includes adding model providers, extending model parameters, changing knowledge parsing, integrating protocol adaptation, or connecting business pages to AI assistants.

## 2. Development Map

```mermaid
%%{init: {"flowchart": {"htmlLabels": false}}}%%
flowchart LR
  A["Provider"] --> B["Model"]
  B --> C["Chat service"]
  B --> D["Knowledge service"]
  D --> E["Embedding / retrieval"]
  D --> F["Protocol adaptation"]
  B --> G["NL2SQL"]
  G --> H["Business query"]
```

## 3. Extension Points

| Area | Focus |
| --- | --- |
| Provider | Endpoint, API key, authentication, timeout, and error handling. |
| Model | Model type, parameter validation, context length, and availability. |
| Knowledge | File parsing, chunking, embedding, versioning, and publishing. |
| Protocol adaptation | Prompt, protocol field extraction, thing-model mapping, and test cases. |
| NL2SQL | Semantic fields, data permission, SQL safety, and query result formatting. |
| Frontend | Chat UI, provider management, knowledge operations, and result display. |

## 4. Safety Rules

* Never expose model keys in frontend code.
* Validate and log model errors without leaking secrets.
* Restrict NL2SQL by user permission and table whitelist.
* Review AI-generated protocol or SQL output before execution.
* Keep knowledge-base version rollback available.

## 5. Verification

| Item | Expected Result |
| --- | --- |
| Provider test | Correct key succeeds and wrong key fails clearly. |
| Chat | Simple prompts return stable answers. |
| Knowledge | Search hits expected document chunks. |
| Protocol adaptation | Output can be verified by real device messages. |
| NL2SQL | Unauthorized fields or tables cannot be queried. |

## Related Docs

* [AI Overview](https://www.fastbee.cn/doc/en/business/v2/manual/ai-overview.html)
* [Model Provider](https://www.fastbee.cn/doc/en/business/v2/manual/ai-provider.html)
* [Knowledge Base And RAG](https://www.fastbee.cn/doc/en/business/v2/manual/ai-knowledge.html)
