Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[editor] Fix Model Parsers prompt_metadata remember_chat_context #1209

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,21 @@ function AIConfigEditorBase({
async (
promptName: string,
newPrompt: Prompt,
onSuccess: (aiconfigRes: AIConfig) => void,
onError: (err: unknown) => void
callbacks: {
onSuccess?: (aiconfigRes: AIConfig) => void;
onError?: (err: unknown) => void;
}
) => {
try {
const serverConfigRes = await updatePromptCallback(
promptName,
newPrompt
);
if (serverConfigRes?.aiconfig) {
onSuccess(serverConfigRes.aiconfig);
callbacks?.onSuccess?.(serverConfigRes.aiconfig);
}
} catch (err: unknown) {
onError(err);
callbacks?.onError?.(err);
}
},
DEBOUNCE_MS
Expand Down Expand Up @@ -302,13 +304,15 @@ function AIConfigEditorBase({
...prompt,
input: newPromptInput,
},
(config) =>
dispatch({
type: "CONSOLIDATE_AICONFIG",
action,
config,
}),
onError
{
onSuccess: (config) =>
dispatch({
type: "CONSOLIDATE_AICONFIG",
action,
config,
}),
onError,
}
);
} catch (err: unknown) {
onError(err);
Expand Down Expand Up @@ -350,15 +354,17 @@ function AIConfigEditorBase({
// PromptName component maintains local state for the name to show in the UI
// We cannot update client config state until the name is successfully set server-side
// or else we could end up referencing a prompt name that is not set server-side
() => {
dispatch({
type: "UPDATE_PROMPT_NAME",
id: promptId,
name: newName,
});
logEventHandler?.("UPDATE_PROMPT_NAME");
},
onError
{
onSuccess: () => {
dispatch({
type: "UPDATE_PROMPT_NAME",
id: promptId,
name: newName,
});
logEventHandler?.("UPDATE_PROMPT_NAME");
},
onError,
}
);
} catch (err: unknown) {
onError(err);
Expand Down Expand Up @@ -392,6 +398,57 @@ function AIConfigEditorBase({
);
}, [updateModelCallback]);

const onUpdatePromptMetadata = useCallback(
async (promptId: string, newMetadata: JSONObject) => {
if (!debouncedUpdatePrompt) {
// Just no-op if no callback specified. We could technically perform
// client-side updates but that might be confusing
return;
}

dispatch({
type: "UPDATE_PROMPT_METADATA",
id: promptId,
metadata: newMetadata,
});

const onError = (err: unknown) => {
const message = (err as RequestCallbackError).message ?? null;
showNotification({
title: "Error updating prompt metadata",
message,
type: "error",
});
};

try {
const statePrompt = getPrompt(stateRef.current, promptId);
if (!statePrompt) {
throw new Error(`Could not find prompt with id ${promptId}`);
}
const prompt = clientPromptToAIConfigPrompt(statePrompt);

await debouncedUpdatePrompt(
prompt.name,
{
...prompt,
metadata: {
...newMetadata,
model: prompt.metadata?.model,
parameters: prompt.metadata?.parameters,
},
},
{
onError,
}
);
} catch (err: unknown) {
onError(err);
}
},
[debouncedUpdatePrompt, showNotification]
);

const onUpdatePromptModelSettings = useCallback(
async (promptId: string, newModelSettings: JSONObject) => {
if (!debouncedUpdateModel) {
Expand Down Expand Up @@ -1098,6 +1155,7 @@ function AIConfigEditorBase({
onChangePromptName={onChangePromptName}
onDeletePrompt={onDeletePrompt}
onRunPrompt={onRunPrompt}
onUpdatePromptMetadata={onUpdatePromptMetadata}
onUpdatePromptModel={onUpdatePromptModel}
onUpdatePromptModelSettings={onUpdatePromptModelSettings}
onUpdatePromptParameters={onUpdatePromptParameters}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export default function SettingsPropertyRenderer({
setAndPropagateValue(event.currentTarget.checked)
}
disabled={readOnly}
styles={{ inner: { alignSelf: "center" } }}
/>
);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {
promptSchema?: PromptSchema;
onUpdateModelSettings: (settings: Record<string, unknown>) => void;
onUpdateParameters: (parameters: JSONObject) => void;
onUpdatePromptMetadata: (metadata: JSONObject) => void;
};

// Don't default to config-level model settings since that could be confusing
Expand All @@ -27,6 +28,22 @@ function getModelSettings(prompt: ClientPrompt) {
}
}

function getMetadata(prompt: ClientPrompt) {
const metadata = { ...prompt.metadata };

// Model and parameters handled as their own tabs
delete metadata.model;
delete metadata.parameters;

// Tags are set to null by default by python sdk. For now, exclude if null
// We may want to add special UI for tags later
if (metadata.tags === null) {
delete metadata.tags;
}

return metadata;
}

function getPromptParameters(prompt: ClientPrompt) {
return prompt.metadata?.parameters;
}
Expand All @@ -45,8 +62,10 @@ export default memo(function PromptActionBar({
promptSchema,
onUpdateModelSettings,
onUpdateParameters,
onUpdatePromptMetadata,
}: Props) {
const [isExpanded, setIsExpanded] = useState(false);
const [selectedTab, setSelectedTab] = useState<string | null>("settings");
// TODO: Handle drag-to-resize
const modelSettingsSchema = promptSchema?.model_settings;
const promptMetadataSchema = promptSchema?.prompt_metadata;
Expand All @@ -67,45 +86,57 @@ export default memo(function PromptActionBar({
<ActionIcon size="sm" onClick={() => setIsExpanded(false)} mt="0.5em">
<IconClearAll />
</ActionIcon>
<Tabs defaultValue="settings" mb="1em">
<Tabs
defaultValue="settings"
value={selectedTab}
mb="1em"
onTabChange={setSelectedTab}
>
<Tabs.List>
<Tabs.Tab value="settings">Settings</Tabs.Tab>
{checkParametersSupported(prompt) && (
<Tabs.Tab value="parameters">Local Parameters</Tabs.Tab>
)}
</Tabs.List>

<Tabs.Panel value="settings" className="actionTabsPanel">
<ScrollArea
h={maxContentHeightPx}
type="auto"
style={{ overflowY: "auto" }}
>
<ModelSettingsRenderer
settings={getModelSettings(prompt)}
schema={modelSettingsSchema}
onUpdateModelSettings={onUpdateModelSettings}
/>
<PromptMetadataRenderer
prompt={prompt}
schema={promptMetadataSchema}
/>
</ScrollArea>
</Tabs.Panel>

{checkParametersSupported(prompt) && (
<Tabs.Panel value="parameters" className="actionTabsPanel">
<ParametersRenderer
initialValue={getPromptParameters(prompt)}
onUpdateParameters={onUpdateParameters}
maxHeight={
maxContentHeightPx
? maxContentHeightPx - ADD_PARAMETER_BOTTOM_HEIGHT
: undefined
}
/>
{selectedTab === "settings" && (
<Tabs.Panel value="settings" className="actionTabsPanel">
<ScrollArea
h={maxContentHeightPx}
type="auto"
style={{ overflowY: "auto" }}
>
<ModelSettingsRenderer
settings={getModelSettings(prompt)}
schema={modelSettingsSchema}
onUpdateModelSettings={onUpdateModelSettings}
/>
{/* For now, just render metadata if a schema is explicitly provided */}
{promptMetadataSchema && (
<PromptMetadataRenderer
metadata={getMetadata(prompt)}
onUpdatePromptMetadata={onUpdatePromptMetadata}
schema={promptMetadataSchema}
/>
)}
</ScrollArea>
</Tabs.Panel>
)}

{selectedTab === "parameters" &&
checkParametersSupported(prompt) && (
<Tabs.Panel value="parameters" className="actionTabsPanel">
<ParametersRenderer
initialValue={getPromptParameters(prompt)}
onUpdateParameters={onUpdateParameters}
maxHeight={
maxContentHeightPx
? maxContentHeightPx - ADD_PARAMETER_BOTTOM_HEIGHT
: undefined
}
/>
</Tabs.Panel>
)}
</Tabs>
</Container>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Props = {
promptId: string,
newParameters: Record<string, unknown>
) => void;
onUpdatePromptMetadata: (promptId: string, newMetadata: JSONObject) => void;
defaultConfigModelName?: string;
isRunButtonDisabled?: boolean;
};
Expand All @@ -48,6 +49,7 @@ export default memo(function PromptContainer({
onUpdateModel,
onUpdateModelSettings,
onUpdateParameters,
onUpdatePromptMetadata,
isRunButtonDisabled = false,
}: Props) {
const promptId = prompt._ui.id;
Expand All @@ -72,6 +74,11 @@ export default memo(function PromptContainer({
[promptId, onUpdateParameters]
);

const updatePromptMetadata = useCallback(
(metadata: JSONObject) => onUpdatePromptMetadata(promptId, metadata),
[promptId, onUpdatePromptMetadata]
);

const runPrompt = useCallback(
async () => await onRunPrompt(promptId),
[promptId, onRunPrompt]
Expand Down Expand Up @@ -171,6 +178,7 @@ export default memo(function PromptContainer({
promptSchema={promptSchema}
onUpdateModelSettings={updateModelSettings}
onUpdateParameters={updateParameters}
onUpdatePromptMetadata={updatePromptMetadata}
/>
</div>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type Props = {
onChangePromptName: (promptId: string, newName: string) => Promise<void>;
onDeletePrompt: (promptId: string) => Promise<void>;
onRunPrompt: (promptId: string) => Promise<void>;
onUpdatePromptMetadata: (
promptId: string,
newMetadata: JSONObject
) => Promise<void>;
onUpdatePromptModel: (promptId: string, newModel?: string) => Promise<void>;
onUpdatePromptModelSettings: (
promptId: string,
Expand Down Expand Up @@ -76,6 +80,7 @@ export default memo(function PromptsContainer(props: Props) {
onUpdateModel={props.onUpdatePromptModel}
onUpdateModelSettings={props.onUpdatePromptModelSettings}
onUpdateParameters={props.onUpdatePromptParameters}
onUpdatePromptMetadata={props.onUpdatePromptMetadata}
defaultConfigModelName={props.defaultModel}
isRunButtonDisabled={isAnotherPromptRunning}
/>
Expand Down
Loading
Loading