@@ -15,7 +15,7 @@ As well as providing a straightforward way to integrate with various AI provider
1515
1616## What is AI in this context?
1717
18- When we talk about AI in the context of this subsystem, AI is: anything that's plugged in via a Provider
18+ When we talk about AI in the context of this subsystem, AI is anything that's plugged in via a Provider
1919Plugin and provides one or more Actions.
2020
2121Artificial Intelligence (AI), Machine Learning (ML), Large Language Models (LLMs), Generative AI, etc.
@@ -24,26 +24,45 @@ the subsystem everything is just called AI.
2424
2525## Design Overview
2626
27+ ``` mermaid
28+ flowchart TD
29+ Placement["Placement"]:::placementStyle
30+ Action["Action"]:::actionStyle
31+ SubsystemManager["Subsystem Manager"]:::managerStyle
32+ Provider["Provider"]:::providerStyle
33+
34+ Placement --> |"Instantiates Action"| Action
35+ Action --> |"Passes to Subsystem Manager"| SubsystemManager
36+ SubsystemManager --> |"Selects & Invokes Provider"| Provider
37+ Provider --> |"Returns Response to Action"| Action
38+
39+ %% Define styles for each block
40+ classDef placementStyle fill:#FFDDC1,stroke:#FF6F61,stroke-width:2px;
41+ classDef actionStyle fill:#C1E1C1,stroke:#4CAF50,stroke-width:2px;
42+ classDef managerStyle fill:#C1DFF0,stroke:#2196F3,stroke-width:2px;
43+ classDef providerStyle fill:#FFD700,stroke:#FFC107,stroke-width:2px;
44+ ```
45+
2746The AI subsystem consists of the following (main) components:
2847
2948- ** Placements**
3049 - Are the UI components and associated workflows that allow users to interact with AI.
3150 - They implement one or more AI actions.
32- - They provide a consistent experience for users regardless of which AI is providing the action
33- - They are LMS plugins
51+ - They provide a consistent experience for users regardless of which AI is providing the action.
52+ - They are LMS plugins.
3453- ** Actions**
3554 - These are the "things" users can do with AI.
3655 - Examples of an action include: generating text, generating an image, providing a chat interface.
3756 - Actions don't have a UI, they are classes that provide a predictable interface.
3857- ** Providers**
3958 - Providers are the interface between AI systems and the Moodle LMS.
4059 - They implement support for one or more actions.
41- - Providers should not have an UI, apart from those required for settings,
60+ - Providers should not have a UI, apart from those required for settings,
4261 to configure and manage the provider. For example an Administration UI to allow setting of API
4362 keys; and/or to enable or disable the enabled actions.
4463 - The aim of Providers is to make it "easy" to integrate AI systems with LMS,
4564 without the need to implement a UI to use them.
46- - They are LMS plugins
65+ - They are LMS plugins.
4766- ** Subsystem Manager**
4867 - This is implemented at code level and is a core part of the subsystem design.
4968 - It is the "controller" that sits between Placements and Providers.
@@ -119,19 +138,32 @@ Users only need to accept the policy once.
119138
120139To assist Placements with policy display and acceptance the Manager provides the following functionality:
121140
122- - The Manager makes the following methods available for Placements to call directly:
123- - ` \core_ai\manger::get_user_policy(int $userid): bool ` -
124- Given a user ID (record id in user table), returns true if the user has accepted the policy,
125- false otherwise. It handles looking up the status and caching the response.
126- - ` \core_ai\manager::set_user_policy(int $userid, int $contextid): bool ` -
127- Given a user ID and a Context ID (of the context the policy was displayed in) set the policy
128- acceptance.
129- - The manager class also makes available webservices to be used for policy setting and getting.
130- This helps Placements set policy acceptance via ajax as part of a UI workflow:
131- - ` core_ai_get_policy_status ` -
132- Gets the policy status for a user. Calls: ` core_ai\external\get_policy_status `
133- - ` core_ai_set_policy_status ` -
134- Sets the policy status for a user. Calls: ` core_ai\external\set_policy_status `
141+ #### Direct call
142+
143+ The Manager makes the following methods available for Placements to call directly:
144+
145+ ##### ` \core_ai\manager::get_user_policy_status(int $userid): bool `
146+
147+ Given a user ID (record id in user table), returns true if the user has accepted the policy,
148+ false otherwise. It handles looking up the status and caching the response.
149+
150+ ##### ` \core_ai\manager::user_policy_accepted(int $userid, int $contextid): bool `
151+
152+ Given a user ID and a Context ID (of the context the policy was displayed in) set the policy
153+ acceptance.
154+
155+ #### Webservices
156+
157+ The manager class also makes available webservices to be used for policy setting and getting.
158+ This helps Placements set policy acceptance via ajax as part of a UI workflow:
159+
160+ ##### ` core_ai_get_policy_status `
161+
162+ Gets the policy status for a user. Calls: ` core_ai\external\get_policy_status `
163+
164+ ##### ` core_ai_set_policy_status `
165+
166+ Sets the policy status for a user. Calls: ` core_ai\external\set_policy_status `
135167
136168### Actions
137169
@@ -142,17 +174,19 @@ Actions are defined as classes in the `\core_ai\aiactions` namespace.
142174The naming convention for Action classes is ` <verb>_<noun singular> ` ,
143175for example: ` generate_image ` , ` translate_text ` .
144176
145- Each action ** MUST** inherit from the ` \core_ai\aiactions\base() ` abstract class.
177+ #### Base abstract class
178+
179+ Each action ** MUST** inherit from the ` \core_ai\aiactions\base ` abstract class.
146180They must also implement two methods:
147181
148- - ` __construct(...args): void `
149- - The constructor method is allowed to have a variable signature, so that each action can define its own
150- configuration requirements.
151- - The method ** MUST ** take a ` contextid ` as one of the variables .
152- - An Action ** MUST** be correctly instantiated before it can be used and passed onto the AI manager .
153- For example the constructor method for the generate_image Action is:
182+ ##### 1. ` __construct() `
183+
184+ - The constructor method is allowed to have a variable signature, so that each action can define its own
185+ configuration requirements .
186+ - The method ** MUST** take a ` contextid ` as one of the variables .
187+ - An Action ** MUST ** be correctly instantiated before it can be used and passed onto the AI manager.
154188
155- ``` php
189+ ``` php title="Example: The __construct() method for the generate_image Action"
156190public function __construct(
157191 int $contextid,
158192 /** @var int The user id requesting the action. */
@@ -172,14 +206,14 @@ public function __construct(
172206}
173207```
174208
175- - ` store(response_base $response): int `
176- - This method is responsible for storing any action specific data related to the action in the
177- LMS database.
178- - Each Action must store its own data can that can be referenced later.
179- - It takes a matching response class, that contains the result of the action call.
180- - For example the store() call form the generate_image Action is:
209+ ##### 2. ` store() `
181210
182- ``` php
211+ - This method is responsible for storing any action specific data related to the action in the
212+ LMS database.
213+ - Each Action must store its own data can that can be referenced later.
214+ - It takes a matching response class, that contains the result of the action call.
215+
216+ ``` php title="Example: The store() method for the generate_image Action"
183217#[\Override]
184218public function store(response_base $response): int {
185219 global $DB;
@@ -200,13 +234,13 @@ public function store(response_base $response): int {
200234```
201235
202236It is up to the action to define its own database schema and stored data, that is relevant to
203- what the action does. For example the database table definition for the generate_image Action is:
237+ what the action does.
204238
205- ``` xml
206- <TABLE NAME =" ai_action_generate_image" COMMENT =" stores specific data about generate image actions" >
239+ ``` xml title="Example: The database table definition for the generate_image Action"
240+ <TABLE NAME =" ai_action_generate_image" COMMENT =" Stores specific data about generate image actions" >
207241 <FIELDS >
208242 <FIELD NAME =" id" TYPE =" int" LENGTH =" 10" NOTNULL =" true" SEQUENCE =" true" />
209- <FIELD NAME =" prompt" TYPE =" text" NOTNULL =" true " SEQUENCE =" false" COMMENT =" The text from the user that was used to generate the image" />
243+ <FIELD NAME =" prompt" TYPE =" text" NOTNULL =" false " SEQUENCE =" false" COMMENT =" The text from the user that was used to generate the image" />
210244 <FIELD NAME =" numberimages" TYPE =" int" LENGTH =" 10" NOTNULL =" true" SEQUENCE =" false" COMMENT =" The number of images requested to be generated" />
211245 <FIELD NAME =" quality" TYPE =" char" LENGTH =" 21" NOTNULL =" true" SEQUENCE =" false" COMMENT =" The quality of the image, e.g. hd." />
212246 <FIELD NAME =" aspectratio" TYPE =" char" LENGTH =" 20" NOTNULL =" false" SEQUENCE =" false" COMMENT =" The aspect ratio of the generate image, e.g landscape" />
@@ -230,18 +264,15 @@ This allows Placements to receive an expected response for any Action call.
230264Each Action has a matching response class. The provider that processes the Action will instantiate
231265an instance of this response class and populate it with the data required for this type of response.
232266
233- Each Action response MUST inherit from the ` \core_ai\aiactions\responses\response_base ` abstract
267+ Each Action response ** MUST** inherit from the ` \core_ai\aiactions\responses\response_base ` abstract
234268class. They must also implement two methods:
235269
236- - ` set_response(array $response): void `
237- - Taking an array of response variables (which must be defined as class variables),
238- it sets these against class variables so they can be retrieved by the Manager and calling Placement.
239- - ` get_response(): array `
240- - Returns the set response data.
270+ ##### 1. ` set_response_data() `
241271
242- For example the ` set_response() ` for the generate_image Action response is:
272+ Taking an array of response variables (which must be defined as class variables),
273+ it sets these against class variables so they can be retrieved by the Manager and calling Placement.
243274
244- ``` php
275+ ``` php title="Example: The set_response_data() for the generate_image Action response"
245276 #[\Override]
246277 public function set_response_data(array $response): void {
247278 $this->draftfile = $response['draftfile'] ?? null;
@@ -250,9 +281,11 @@ For example the `set_response()` for the generate_image Action response is:
250281 }
251282```
252283
253- And the ` get_response() ` for the generate_image Action response is:
284+ ##### 2. ` get_response_data() `
285+
286+ Returns the set response data.
254287
255- ``` php
288+ ``` php title="Example: The get_response_data() for the generate_image Action response"
256289 #[\Override]
257290 public function get_response_data(): array {
258291 return [
0 commit comments