Skip to content

Commit

Permalink
fix: notebook creates new config (#4458)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnyama authored Aug 15, 2024
1 parent e772de8 commit 303877e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,8 @@ const initialize = async () => {
if (state.interventionPolicy?.id) {
// copy the state into the knobs if it exists
selectedPolicy.value = await getInterventionPolicyById(state.interventionPolicy.id);
knobs.value.transientInterventionPolicy = cloneDeep(state.interventionPolicy);
} else {
knobs.value.transientInterventionPolicy = cloneDeep(state.interventionPolicy);
}
knobs.value.transientInterventionPolicy = cloneDeep(state.interventionPolicy);
};
const applyInterventionPolicy = (interventionPolicy: InterventionPolicy) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { WorkflowOperationTypes } from '@/types/workflow';
import type { Operation, BaseState } from '@/types/workflow';
import type { ModelConfiguration } from '@/types/Types';
import { NotebookHistory } from '@/services/notebook';
import configureModel from '@assets/svg/operator-images/configure-model.svg';

export const name = 'ModelConfigOperation';

export interface ModelEditCode {
code: string;
timestamp: number;
}

export interface ModelConfigOperationState extends BaseState {
transientModelConfig: ModelConfiguration;
modelEditCodeHistory: ModelEditCode[];
hasCodeBeenRun: boolean;
notebookHistory: NotebookHistory[];
hasCodeRun: boolean;
}

export const blankModelConfig: ModelConfiguration = {
Expand Down Expand Up @@ -43,9 +39,9 @@ export const ModelConfigOperation: Operation = {
action: async () => ({}),
initState: () => {
const init: ModelConfigOperationState = {
modelEditCodeHistory: [],
hasCodeBeenRun: false,
transientModelConfig: blankModelConfig
transientModelConfig: blankModelConfig,
notebookHistory: [],
hasCodeRun: false
};
return init;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</template>
<template #header-controls-right>
<Button label="Reset" @click="resetConfiguration" outlined severity="secondary" />
<Button class="mr-3" :disabled="isSaveDisabled" label="Save" @click="() => createConfiguration()" />
<Button class="mr-3" :disabled="isSaveDisabled" label="Save" @click="createConfiguration" />
</template>

<Accordion multiple :active-index="[0, 1]">
Expand Down Expand Up @@ -135,8 +135,7 @@
</template>
</tera-drilldown-section>
<tera-columnar-panel :tabName="ConfigTabs.Notebook">
<tera-drilldown-section id="notebook-section">
<div class="toolbar-right-side"></div>
<tera-drilldown-section class="notebook-section">
<div class="toolbar">
<Suspense>
<tera-notebook-jupyter-input
Expand All @@ -145,7 +144,7 @@
:context-language="contextLanguage"
@llm-output="(data: any) => appendCode(data, 'code')"
@llm-thought-output="(data: any) => llmThoughts.push(data)"
@question-asked="llmThoughts = []"
@question-asked="updateLlmQuery"
>
<template #toolbar-right-side>
<tera-input-text v-model="knobs.transientModelConfig.name" placeholder="Configuration Name" />
Expand Down Expand Up @@ -175,7 +174,7 @@
</tera-drilldown-preview>
</tera-columnar-panel>
</tera-drilldown>
<tera-modal v-if="sanityCheckErrors.length > 0">
<tera-modal v-if="!isEmpty(sanityCheckErrors)">
<template #header>
<h4>Warning, these settings may cause errors</h4>
</template>
Expand All @@ -188,11 +187,7 @@
</template>
<template #footer>
<Button label="Ok" class="p-button-primary" @click="sanityCheckErrors = []" />
<Button
label="Ignore warnings and use configuration"
class="p-button-secondary"
@click="() => createConfiguration()"
/>
<Button label="Ignore warnings and use configuration" class="p-button-secondary" @click="createConfiguration" />
</template>
</tera-modal>

Expand Down Expand Up @@ -250,6 +245,7 @@ import TeraSliderPanel from '@/components/widgets/tera-slider-panel.vue';
import { useConfirm } from 'primevue/useconfirm';
import Dropdown from 'primevue/dropdown';
import TeraToggleableEdit from '@/components/widgets/tera-toggleable-edit.vue';
import { saveCodeToState } from '@/services/notebook';
import TeraModelConfigurationItem from './tera-model-configuration-item.vue';
import { ModelConfigOperation, ModelConfigOperationState, blankModelConfig } from './model-config-operation';

Expand Down Expand Up @@ -316,6 +312,7 @@ const buildJupyterContext = () => {
};
};
const codeText = ref('# This environment contains the variable "model_config" to be read and updated');
const llmQuery = ref('');
const llmThoughts = ref<any[]>([]);
const notebookResponse = ref();
const executeResponse = ref({
Expand Down Expand Up @@ -379,7 +376,8 @@ const runFromCode = () => {
knobs.value.transientModelConfig = data.content;

if (executedCode) {
saveCodeToState(executedCode, true);
updateCodeState(executedCode, true);
createConfiguration();
}
})
.register('any_execute_reply', (data) => {
Expand All @@ -395,21 +393,15 @@ const runFromCode = () => {
});
};

// FIXME: Copy pasted in 3 locations, could be written cleaner and in a service
const saveCodeToState = (code: string, hasCodeBeenRun: boolean) => {
const state = cloneDeep(props.node.state);
state.hasCodeBeenRun = hasCodeBeenRun;
function updateLlmQuery(query: string) {
llmThoughts.value = [];
llmQuery.value = query;
}

// for now only save the last code executed, may want to save all code executed in the future
const codeHistoryLength = props.node.state.modelEditCodeHistory.length;
const timestamp = Date.now();
if (codeHistoryLength > 0) {
state.modelEditCodeHistory[0] = { code, timestamp };
} else {
state.modelEditCodeHistory.push({ code, timestamp });
}
function updateCodeState(code: string = codeText.value, hasCodeRun: boolean = true) {
const state = saveCodeToState(props.node, code, hasCodeRun, llmQuery.value, llmThoughts.value);
emit('update-state', state);
};
}

const initializeEditor = (editorInstance: any) => {
editor = editorInstance;
Expand Down Expand Up @@ -497,27 +489,22 @@ const downloadConfiguredModel = async (configuration: ModelConfiguration = knobs
const createConfiguration = async () => {
if (!model.value || isSaveDisabled.value) return;
const state = cloneDeep(props.node.state);
const modelConfig = cloneDeep(knobs.value.transientModelConfig);
const data = await createModelConfiguration(modelConfig);
if (!data) {
logger.error('Failed to create model configuration');
return;
}
knobs.value.transientModelConfig = cloneDeep(data);
state.transientModelConfig = knobs.value.transientModelConfig;
useToastService().success('', 'Created model configuration');
emit('append-output', {
type: ModelConfigOperation.outputs[0].type,
label: state.transientModelConfig.name,
label: data.name,
value: data.id,
isSelected: false,
state
state: cloneDeep(props.node.state)
});
};
Expand Down Expand Up @@ -575,7 +562,6 @@ const onSelectConfiguration = (config: ModelConfiguration) => {
};
const applyConfigValues = (config: ModelConfiguration) => {
const state = cloneDeep(props.node.state);
knobs.value.transientModelConfig = cloneDeep(config);
// Update output port:
Expand All @@ -593,13 +579,12 @@ const applyConfigValues = (config: ModelConfiguration) => {
// If the output does not already exist
else {
// Append this config to the output.
state.transientModelConfig = knobs.value.transientModelConfig;
emit('append-output', {
type: ModelConfigOperation.outputs[0].type,
label: config.name,
value: config.id,
isSelected: false,
state
state: cloneDeep(props.node.state)
});
}
logger.success(`Configuration applied ${config.name}`);
Expand All @@ -621,9 +606,7 @@ const resetConfiguration = () => {
message: 'This will reset all values original values of the configuration.',
accept: () => {
const originalConfig = suggestedConfigurationContext.value.tableData.find((c) => c.id === selectedConfigId.value);
if (originalConfig) {
applyConfigValues(originalConfig);
}
if (originalConfig) applyConfigValues(originalConfig);
},
acceptLabel: 'Confirm',
rejectLabel: 'Cancel'
Expand Down Expand Up @@ -687,7 +670,7 @@ onUnmounted(() => {
padding-bottom: var(--gap-2);
}
#notebook-section:deep(main) {
.notebook-section:deep(main) {
gap: var(--gap-small);
position: relative;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/hmi-client/src/services/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface NotebookHistory {
}

// A common pattern used to save code from a notebook within an operator
// This is ready to be ported to nodes such as tera-model-config and tera-stratify-mira
// One more operator to port this to: tera-stratify-mira
// Not ported yet since this will ruin the states of the nodes that already exist due to their differently named properties
export const saveCodeToState = (
node: WorkflowNode<any>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public ResponseEntity<ModelConfiguration> createModelConfiguration(
}

return ResponseEntity.status(HttpStatus.CREATED).body(
modelConfigurationService.createAsset(modelConfiguration, projectId, permission)
modelConfigurationService.createAsset(modelConfiguration.clone(), projectId, permission)
);
} catch (final IOException e) {
log.error("Unable to get model configuration from postgres db", e);
Expand Down

0 comments on commit 303877e

Please sign in to comment.