2023-12-08 08:42:56 +11:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2024-02-15 16:37:59 +11:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2023-11-21 16:56:43 +11:00
|
|
|
import RestModel from "discourse/models/rest";
|
|
|
|
|
|
2024-04-12 23:32:46 +10:00
|
|
|
const CREATE_ATTRIBUTES = [
|
2024-04-09 11:03:07 -03:00
|
|
|
"id",
|
2023-11-21 16:56:43 +11:00
|
|
|
"name",
|
|
|
|
|
"description",
|
2024-06-11 18:14:14 +10:00
|
|
|
"tools",
|
2023-11-21 16:56:43 +11:00
|
|
|
"system_prompt",
|
|
|
|
|
"allowed_group_ids",
|
|
|
|
|
"enabled",
|
|
|
|
|
"system",
|
|
|
|
|
"priority",
|
2024-02-03 07:09:34 +11:00
|
|
|
"top_p",
|
|
|
|
|
"temperature",
|
2024-02-15 16:37:59 +11:00
|
|
|
"user_id",
|
|
|
|
|
"mentionable",
|
|
|
|
|
"default_llm",
|
|
|
|
|
"user",
|
|
|
|
|
"max_context_posts",
|
2024-03-27 14:30:11 +11:00
|
|
|
"vision_enabled",
|
|
|
|
|
"vision_max_pixels",
|
2024-04-01 13:43:34 -03:00
|
|
|
"rag_uploads",
|
2024-04-12 23:32:46 +10:00
|
|
|
"rag_chunk_tokens",
|
|
|
|
|
"rag_chunk_overlap_tokens",
|
|
|
|
|
"rag_conversation_chunks",
|
2024-04-30 13:49:21 +10:00
|
|
|
"question_consolidator_llm",
|
2024-05-06 09:49:02 +10:00
|
|
|
"allow_chat",
|
2024-06-11 18:14:14 +10:00
|
|
|
"tool_details",
|
2024-04-15 23:22:06 +10:00
|
|
|
];
|
2024-04-12 23:32:46 +10:00
|
|
|
|
2024-03-04 09:56:59 +11:00
|
|
|
const SYSTEM_ATTRIBUTES = [
|
2024-04-09 11:03:07 -03:00
|
|
|
"id",
|
2024-03-04 09:56:59 +11:00
|
|
|
"allowed_group_ids",
|
|
|
|
|
"enabled",
|
|
|
|
|
"system",
|
|
|
|
|
"priority",
|
|
|
|
|
"user_id",
|
|
|
|
|
"mentionable",
|
|
|
|
|
"default_llm",
|
|
|
|
|
"user",
|
|
|
|
|
"max_context_posts",
|
2024-03-27 14:30:11 +11:00
|
|
|
"vision_enabled",
|
|
|
|
|
"vision_max_pixels",
|
2024-04-01 13:43:34 -03:00
|
|
|
"rag_uploads",
|
2024-04-12 23:32:46 +10:00
|
|
|
"rag_chunk_tokens",
|
|
|
|
|
"rag_chunk_overlap_tokens",
|
|
|
|
|
"rag_conversation_chunks",
|
2024-04-30 13:49:21 +10:00
|
|
|
"question_consolidator_llm",
|
2024-05-06 09:49:02 +10:00
|
|
|
"allow_chat",
|
2024-06-11 18:14:14 +10:00
|
|
|
"tool_details",
|
2024-03-04 09:56:59 +11:00
|
|
|
];
|
|
|
|
|
|
2024-06-11 18:14:14 +10:00
|
|
|
class ToolOption {
|
2023-12-08 08:42:56 +11:00
|
|
|
@tracked value = null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 16:56:43 +11:00
|
|
|
export default class AiPersona extends RestModel {
|
2023-12-08 08:42:56 +11:00
|
|
|
// this code is here to convert the wire schema to easier to work with object
|
2024-06-11 18:14:14 +10:00
|
|
|
// on the wire we pass in/out tools as an Array.
|
|
|
|
|
// [[ToolName, {option1: value, option2: value}], ToolName2, ToolName3]
|
|
|
|
|
// So we rework this into a "tools" property and nested toolOptions
|
2023-12-08 08:42:56 +11:00
|
|
|
init(properties) {
|
2024-06-11 18:14:14 +10:00
|
|
|
if (properties.tools) {
|
|
|
|
|
properties.tools = properties.tools.map((tool) => {
|
|
|
|
|
if (typeof tool === "string") {
|
|
|
|
|
return tool;
|
2023-12-08 08:42:56 +11:00
|
|
|
} else {
|
2024-06-11 18:14:14 +10:00
|
|
|
let [toolId, options] = tool;
|
2023-12-08 08:42:56 +11:00
|
|
|
for (let optionId in options) {
|
|
|
|
|
if (!options.hasOwnProperty(optionId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-06-11 18:14:14 +10:00
|
|
|
this.getToolOption(toolId, optionId).value = options[optionId];
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
2024-06-11 18:14:14 +10:00
|
|
|
return toolId;
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
super.init(properties);
|
2024-06-11 18:14:14 +10:00
|
|
|
this.tools = properties.tools;
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
async createUser() {
|
|
|
|
|
const result = await ajax(
|
|
|
|
|
`/admin/plugins/discourse-ai/ai-personas/${this.id}/create-user.json`,
|
|
|
|
|
{
|
|
|
|
|
type: "POST",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
this.user = result.user;
|
|
|
|
|
this.user_id = this.user.id;
|
|
|
|
|
return this.user;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 18:14:14 +10:00
|
|
|
getToolOption(toolId, optionId) {
|
|
|
|
|
this.toolOptions ||= {};
|
|
|
|
|
this.toolOptions[toolId] ||= {};
|
|
|
|
|
return (this.toolOptions[toolId][optionId] ||= new ToolOption());
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 18:14:14 +10:00
|
|
|
populateToolOptions(attrs) {
|
|
|
|
|
if (!attrs.tools) {
|
2023-12-08 08:42:56 +11:00
|
|
|
return;
|
|
|
|
|
}
|
2024-06-11 18:14:14 +10:00
|
|
|
let toolsWithOptions = [];
|
|
|
|
|
attrs.tools.forEach((toolId) => {
|
|
|
|
|
if (typeof toolId !== "string") {
|
|
|
|
|
toolId = toolId[0];
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
2024-06-11 18:14:14 +10:00
|
|
|
if (this.toolOptions && this.toolOptions[toolId]) {
|
|
|
|
|
let options = this.toolOptions[toolId];
|
2023-12-08 08:42:56 +11:00
|
|
|
let optionsWithValues = {};
|
|
|
|
|
for (let optionId in options) {
|
|
|
|
|
if (!options.hasOwnProperty(optionId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let option = options[optionId];
|
|
|
|
|
optionsWithValues[optionId] = option.value;
|
|
|
|
|
}
|
2024-06-11 18:14:14 +10:00
|
|
|
toolsWithOptions.push([toolId, optionsWithValues]);
|
2023-12-08 08:42:56 +11:00
|
|
|
} else {
|
2024-06-11 18:14:14 +10:00
|
|
|
toolsWithOptions.push(toolId);
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
});
|
2024-06-11 18:14:14 +10:00
|
|
|
attrs.tools = toolsWithOptions;
|
2023-12-08 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 16:56:43 +11:00
|
|
|
updateProperties() {
|
2024-03-04 09:56:59 +11:00
|
|
|
let attrs = this.system
|
|
|
|
|
? this.getProperties(SYSTEM_ATTRIBUTES)
|
2024-04-15 23:22:06 +10:00
|
|
|
: this.getProperties(CREATE_ATTRIBUTES);
|
2023-11-21 16:56:43 +11:00
|
|
|
attrs.id = this.id;
|
2024-06-11 18:14:14 +10:00
|
|
|
this.populateToolOptions(attrs);
|
2024-04-12 23:32:46 +10:00
|
|
|
|
2023-11-21 16:56:43 +11:00
|
|
|
return attrs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createProperties() {
|
2024-04-12 23:32:46 +10:00
|
|
|
let attrs = this.getProperties(CREATE_ATTRIBUTES);
|
2024-06-11 18:14:14 +10:00
|
|
|
this.populateToolOptions(attrs);
|
2023-12-08 08:42:56 +11:00
|
|
|
return attrs;
|
2023-11-21 16:56:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workingCopy() {
|
2024-04-15 23:22:06 +10:00
|
|
|
let attrs = this.getProperties(CREATE_ATTRIBUTES);
|
2024-06-11 18:14:14 +10:00
|
|
|
this.populateToolOptions(attrs);
|
2024-04-12 23:32:46 +10:00
|
|
|
return AiPersona.create(attrs);
|
2023-11-21 16:56:43 +11:00
|
|
|
}
|
|
|
|
|
}
|