2024-06-21 03:32:15 -04:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { Input } from "@ember/component";
|
2024-10-23 16:58:27 -04:00
|
|
|
import { concat, get, hash } from "@ember/helper";
|
2024-06-21 03:32:15 -04:00
|
|
|
import { on } from "@ember/modifier";
|
2024-06-24 18:26:30 -04:00
|
|
|
import { action, computed } from "@ember/object";
|
2024-06-21 03:32:15 -04:00
|
|
|
import { LinkTo } from "@ember/routing";
|
|
|
|
import { later } from "@ember/runloop";
|
2024-11-19 05:57:40 -05:00
|
|
|
import { service } from "@ember/service";
|
2024-08-21 10:41:55 -04:00
|
|
|
import { eq } from "truth-helpers";
|
2024-06-21 03:32:15 -04:00
|
|
|
import DButton from "discourse/components/d-button";
|
|
|
|
import Avatar from "discourse/helpers/bound-avatar-template";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import icon from "discourse-common/helpers/d-icon";
|
|
|
|
import i18n from "discourse-common/helpers/i18n";
|
|
|
|
import I18n from "discourse-i18n";
|
|
|
|
import AdminUser from "admin/models/admin-user";
|
|
|
|
import ComboBox from "select-kit/components/combo-box";
|
|
|
|
import DTooltip from "float-kit/components/d-tooltip";
|
|
|
|
|
|
|
|
export default class AiLlmEditorForm extends Component {
|
|
|
|
@service toasts;
|
|
|
|
@service router;
|
|
|
|
@service dialog;
|
|
|
|
|
|
|
|
@tracked isSaving = false;
|
|
|
|
|
|
|
|
@tracked testRunning = false;
|
|
|
|
@tracked testResult = null;
|
|
|
|
@tracked testError = null;
|
|
|
|
@tracked apiKeySecret = true;
|
|
|
|
|
|
|
|
get selectedProviders() {
|
|
|
|
const t = (provName) => {
|
|
|
|
return I18n.t(`discourse_ai.llms.providers.${provName}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.args.llms.resultSetMeta.providers.map((prov) => {
|
|
|
|
return { id: prov, name: t(prov) };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get adminUser() {
|
|
|
|
return AdminUser.create(this.args.model?.user);
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:26:30 -04:00
|
|
|
get testErrorMessage() {
|
|
|
|
return I18n.t("discourse_ai.llms.tests.failure", { error: this.testError });
|
|
|
|
}
|
|
|
|
|
|
|
|
get displayTestResult() {
|
|
|
|
return this.testRunning || this.testResult !== null;
|
|
|
|
}
|
|
|
|
|
2024-08-21 10:41:55 -04:00
|
|
|
@computed("args.model.provider")
|
2024-06-24 18:26:30 -04:00
|
|
|
get canEditURL() {
|
2024-08-22 10:31:28 -04:00
|
|
|
return this.args.model.provider !== "aws_bedrock";
|
2024-06-24 18:26:30 -04:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:35:35 -04:00
|
|
|
get modulesUsingModel() {
|
2024-10-21 20:16:02 -04:00
|
|
|
const usedBy = this.args.model.used_by?.filter((m) => m.type !== "ai_bot");
|
|
|
|
|
|
|
|
if (!usedBy || usedBy.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const localized = usedBy.map((m) => {
|
|
|
|
return I18n.t(`discourse_ai.llms.usage.${m.type}`, {
|
|
|
|
persona: m.name,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: this is not perfectly localized
|
|
|
|
return localized.join(", ");
|
2024-08-06 13:35:35 -04:00
|
|
|
}
|
|
|
|
|
2024-08-28 14:57:58 -04:00
|
|
|
get seeded() {
|
|
|
|
return this.args.model.id < 0;
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:35:35 -04:00
|
|
|
get inUseWarning() {
|
|
|
|
return I18n.t("discourse_ai.llms.in_use_warning", {
|
|
|
|
settings: this.modulesUsingModel,
|
|
|
|
count: this.args.model.used_by.length,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:26:30 -04:00
|
|
|
@computed("args.model.provider")
|
|
|
|
get metaProviderParams() {
|
|
|
|
return (
|
|
|
|
this.args.llms.resultSetMeta.provider_params[this.args.model.provider] ||
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-21 03:32:15 -04:00
|
|
|
@action
|
|
|
|
async save() {
|
|
|
|
this.isSaving = true;
|
|
|
|
const isNew = this.args.model.isNew;
|
|
|
|
|
|
|
|
try {
|
2024-09-30 03:15:11 -04:00
|
|
|
await this.args.model.save();
|
2024-06-21 03:32:15 -04:00
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
this.args.llms.addObject(this.args.model);
|
|
|
|
this.router.transitionTo("adminPlugins.show.discourse-ai-llms.index");
|
|
|
|
} else {
|
|
|
|
this.toasts.success({
|
|
|
|
data: { message: I18n.t("discourse_ai.llms.saved") },
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
popupAjaxError(e);
|
|
|
|
} finally {
|
|
|
|
later(() => {
|
|
|
|
this.isSaving = false;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async test() {
|
|
|
|
this.testRunning = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const configTestResult = await this.args.model.testConfig();
|
|
|
|
this.testResult = configTestResult.success;
|
|
|
|
|
|
|
|
if (this.testResult) {
|
|
|
|
this.testError = null;
|
|
|
|
} else {
|
|
|
|
this.testError = configTestResult.error;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
popupAjaxError(e);
|
|
|
|
} finally {
|
|
|
|
later(() => {
|
|
|
|
this.testRunning = false;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
makeApiKeySecret() {
|
|
|
|
this.apiKeySecret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleApiKeySecret() {
|
|
|
|
this.apiKeySecret = !this.apiKeySecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
delete() {
|
|
|
|
return this.dialog.confirm({
|
|
|
|
message: I18n.t("discourse_ai.llms.confirm_delete"),
|
|
|
|
didConfirm: () => {
|
|
|
|
return this.args.model
|
|
|
|
.destroyRecord()
|
|
|
|
.then(() => {
|
|
|
|
this.args.llms.removeObject(this.args.model);
|
|
|
|
this.router.transitionTo(
|
|
|
|
"adminPlugins.show.discourse-ai-llms.index"
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
<template>
|
2024-08-28 14:57:58 -04:00
|
|
|
{{#if this.seeded}}
|
|
|
|
<div class="alert alert-info">
|
2024-12-03 21:18:08 -05:00
|
|
|
{{icon "circle-exclamation"}}
|
2024-08-28 14:57:58 -04:00
|
|
|
{{i18n "discourse_ai.llms.seeded_warning"}}
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2024-08-06 13:35:35 -04:00
|
|
|
{{#if this.modulesUsingModel}}
|
|
|
|
<div class="alert alert-info">
|
2024-12-03 21:18:08 -05:00
|
|
|
{{icon "circle-exclamation"}}
|
2024-08-06 13:35:35 -04:00
|
|
|
{{this.inUseWarning}}
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2024-08-28 14:57:58 -04:00
|
|
|
<form class="form-horizontal ai-llm-editor {{if this.seeded 'seeded'}}">
|
2024-06-21 03:32:15 -04:00
|
|
|
<div class="control-group">
|
|
|
|
<label>{{i18n "discourse_ai.llms.display_name"}}</label>
|
|
|
|
<Input
|
|
|
|
class="ai-llm-editor-input ai-llm-editor__display-name"
|
|
|
|
@type="text"
|
|
|
|
@value={{@model.display_name}}
|
2024-10-23 16:58:27 -04:00
|
|
|
disabled={{this.seeded}}
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="control-group">
|
|
|
|
<label>{{i18n "discourse_ai.llms.name"}}</label>
|
|
|
|
<Input
|
|
|
|
class="ai-llm-editor-input ai-llm-editor__name"
|
|
|
|
@type="text"
|
|
|
|
@value={{@model.name}}
|
2024-10-23 16:58:27 -04:00
|
|
|
disabled={{this.seeded}}
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
|
|
|
<DTooltip
|
2024-12-05 15:45:05 -05:00
|
|
|
@icon="circle-question"
|
2024-10-23 16:58:27 -04:00
|
|
|
@content={{i18n "discourse_ai.llms.hints.name"}}
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="control-group">
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.provider"}}</label>
|
2024-06-21 03:32:15 -04:00
|
|
|
<ComboBox
|
|
|
|
@value={{@model.provider}}
|
|
|
|
@content={{this.selectedProviders}}
|
2024-08-22 10:31:28 -04:00
|
|
|
@class="ai-llm-editor__provider"
|
2024-10-23 16:58:27 -04:00
|
|
|
@options={{hash disabled=this.seeded}}
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
|
|
|
</div>
|
2024-08-28 14:57:58 -04:00
|
|
|
{{#unless this.seeded}}
|
|
|
|
{{#if this.canEditURL}}
|
|
|
|
<div class="control-group">
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.url"}}</label>
|
2024-08-28 14:57:58 -04:00
|
|
|
<Input
|
|
|
|
class="ai-llm-editor-input ai-llm-editor__url"
|
|
|
|
@type="text"
|
|
|
|
@value={{@model.url}}
|
|
|
|
required="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2024-06-21 03:32:15 -04:00
|
|
|
<div class="control-group">
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.api_key"}}</label>
|
2024-08-28 14:57:58 -04:00
|
|
|
<div class="ai-llm-editor__secret-api-key-group">
|
|
|
|
<Input
|
|
|
|
@value={{@model.api_key}}
|
|
|
|
class="ai-llm-editor-input ai-llm-editor__api-key"
|
|
|
|
@type={{if this.apiKeySecret "password" "text"}}
|
|
|
|
required="true"
|
|
|
|
{{on "focusout" this.makeApiKeySecret}}
|
|
|
|
/>
|
|
|
|
<DButton
|
|
|
|
@action={{this.toggleApiKeySecret}}
|
|
|
|
@icon="far-eye-slash"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{#each-in this.metaProviderParams as |field type|}}
|
|
|
|
<div class="control-group ai-llm-editor-provider-param__{{type}}">
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n
|
2024-08-28 14:57:58 -04:00
|
|
|
(concat "discourse_ai.llms.provider_fields." field)
|
|
|
|
}}</label>
|
|
|
|
{{#if (eq type "checkbox")}}
|
|
|
|
<Input
|
|
|
|
@type={{type}}
|
|
|
|
@checked={{mut (get @model.provider_params field)}}
|
|
|
|
/>
|
|
|
|
{{else}}
|
|
|
|
<Input
|
|
|
|
@type={{type}}
|
|
|
|
@value={{mut (get @model.provider_params field)}}
|
|
|
|
/>
|
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
{{/each-in}}
|
|
|
|
<div class="control-group">
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.tokenizer"}}</label>
|
2024-08-28 14:57:58 -04:00
|
|
|
<ComboBox
|
|
|
|
@value={{@model.tokenizer}}
|
|
|
|
@content={{@llms.resultSetMeta.tokenizers}}
|
|
|
|
@class="ai-llm-editor__tokenizer"
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
|
|
|
</div>
|
2024-08-28 14:57:58 -04:00
|
|
|
<div class="control-group">
|
|
|
|
<label>{{i18n "discourse_ai.llms.max_prompt_tokens"}}</label>
|
2024-06-21 03:32:15 -04:00
|
|
|
<Input
|
2024-08-28 14:57:58 -04:00
|
|
|
@type="number"
|
|
|
|
class="ai-llm-editor-input ai-llm-editor__max-prompt-tokens"
|
|
|
|
step="any"
|
|
|
|
min="0"
|
|
|
|
lang="en"
|
|
|
|
@value={{@model.max_prompt_tokens}}
|
2024-06-24 18:26:30 -04:00
|
|
|
required="true"
|
2024-06-21 03:32:15 -04:00
|
|
|
/>
|
2024-08-28 14:57:58 -04:00
|
|
|
<DTooltip
|
2024-12-05 15:45:05 -05:00
|
|
|
@icon="circle-question"
|
2024-10-23 16:58:27 -04:00
|
|
|
@content={{i18n "discourse_ai.llms.hints.max_prompt_tokens"}}
|
2024-08-28 14:57:58 -04:00
|
|
|
/>
|
2024-06-21 03:32:15 -04:00
|
|
|
</div>
|
2024-08-28 14:57:58 -04:00
|
|
|
<div class="control-group ai-llm-editor__vision-enabled">
|
|
|
|
<Input @type="checkbox" @checked={{@model.vision_enabled}} />
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.vision_enabled"}}</label>
|
2024-08-28 14:57:58 -04:00
|
|
|
<DTooltip
|
2024-12-05 15:45:05 -05:00
|
|
|
@icon="circle-question"
|
2024-10-23 16:58:27 -04:00
|
|
|
@content={{i18n "discourse_ai.llms.hints.vision_enabled"}}
|
2024-08-28 14:57:58 -04:00
|
|
|
/>
|
2024-06-24 18:26:30 -04:00
|
|
|
</div>
|
2024-10-21 20:16:02 -04:00
|
|
|
<div class="control-group ai-llm-editor__enabled-chat-bot">
|
|
|
|
<Input @type="checkbox" @checked={{@model.enabled_chat_bot}} />
|
2024-10-23 16:58:27 -04:00
|
|
|
<label>{{i18n "discourse_ai.llms.enabled_chat_bot"}}</label>
|
2024-10-21 20:16:02 -04:00
|
|
|
<DTooltip
|
2024-12-05 15:45:05 -05:00
|
|
|
@icon="circle-question"
|
2024-10-23 16:58:27 -04:00
|
|
|
@content={{i18n "discourse_ai.llms.hints.enabled_chat_bot"}}
|
2024-08-28 14:57:58 -04:00
|
|
|
/>
|
2024-06-21 03:32:15 -04:00
|
|
|
</div>
|
2024-10-23 16:58:27 -04:00
|
|
|
|
2024-10-23 19:36:17 -04:00
|
|
|
{{#if @model.user}}
|
|
|
|
<div class="control-group">
|
|
|
|
<label>{{i18n "discourse_ai.llms.ai_bot_user"}}</label>
|
|
|
|
<a
|
|
|
|
class="avatar"
|
|
|
|
href={{@model.user.path}}
|
|
|
|
data-user-card={{@model.user.username}}
|
|
|
|
>
|
|
|
|
{{Avatar @model.user.avatar_template "small"}}
|
|
|
|
</a>
|
|
|
|
<LinkTo @route="adminUser" @model={{this.adminUser}}>
|
|
|
|
{{@model.user.username}}
|
|
|
|
</LinkTo>
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2024-10-23 16:58:27 -04:00
|
|
|
|
2024-08-28 14:57:58 -04:00
|
|
|
<div class="control-group ai-llm-editor__action_panel">
|
|
|
|
<DButton
|
|
|
|
class="ai-llm-editor__test"
|
|
|
|
@action={{this.test}}
|
|
|
|
@disabled={{this.testRunning}}
|
2024-10-23 16:58:27 -04:00
|
|
|
@label="discourse_ai.llms.tests.title"
|
|
|
|
/>
|
2024-06-21 03:32:15 -04:00
|
|
|
|
|
|
|
<DButton
|
2024-08-28 14:57:58 -04:00
|
|
|
class="btn-primary ai-llm-editor__save"
|
|
|
|
@action={{this.save}}
|
|
|
|
@disabled={{this.isSaving}}
|
2024-10-23 16:58:27 -04:00
|
|
|
@label="discourse_ai.llms.save"
|
|
|
|
/>
|
2024-08-28 14:57:58 -04:00
|
|
|
{{#unless @model.isNew}}
|
|
|
|
<DButton
|
|
|
|
@action={{this.delete}}
|
|
|
|
class="btn-danger ai-llm-editor__delete"
|
2024-10-23 16:58:27 -04:00
|
|
|
@label="discourse_ai.llms.delete"
|
|
|
|
/>
|
2024-08-28 14:57:58 -04:00
|
|
|
{{/unless}}
|
|
|
|
</div>
|
|
|
|
{{/unless}}
|
2024-06-21 03:32:15 -04:00
|
|
|
|
|
|
|
<div class="control-group ai-llm-editor-tests">
|
|
|
|
{{#if this.displayTestResult}}
|
|
|
|
{{#if this.testRunning}}
|
|
|
|
<div class="spinner small"></div>
|
2024-10-23 16:58:27 -04:00
|
|
|
{{i18n "discourse_ai.llms.tests.running"}}
|
2024-06-21 03:32:15 -04:00
|
|
|
{{else}}
|
|
|
|
{{#if this.testResult}}
|
|
|
|
<div class="ai-llm-editor-tests__success">
|
|
|
|
{{icon "check"}}
|
2024-10-23 16:58:27 -04:00
|
|
|
{{i18n "discourse_ai.llms.tests.success"}}
|
2024-06-21 03:32:15 -04:00
|
|
|
</div>
|
|
|
|
{{else}}
|
|
|
|
<div class="ai-llm-editor-tests__failure">
|
2024-12-03 21:18:08 -05:00
|
|
|
{{icon "xmark"}}
|
2024-06-21 03:32:15 -04:00
|
|
|
{{this.testErrorMessage}}
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
|
|
|
{{/if}}
|
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</template>
|
|
|
|
}
|