490 lines
16 KiB
TypeScript
490 lines
16 KiB
TypeScript
import type { ParsedModelInferenceRow } from "~/utils/clickhouse/inference";
|
|
import type {
|
|
FeedbackRow,
|
|
FeedbackBounds,
|
|
StoredInference,
|
|
} from "~/types/tensorzero";
|
|
import { useEffect, useState } from "react";
|
|
import type { ReactNode } from "react";
|
|
import { useConfig, useFunctionConfig } from "~/context/config";
|
|
import { getTotalInferenceUsage } from "~/utils/clickhouse/helpers";
|
|
import { BasicInfo } from "~/routes/observability/inferences/$inference_id/BasicInfo";
|
|
import { ChatOutputElement } from "~/components/input_output/ChatOutputElement";
|
|
import { JsonOutputElement } from "~/components/input_output/JsonOutputElement";
|
|
import FeedbackTable from "~/components/feedback/FeedbackTable";
|
|
import { ParameterCard } from "~/routes/observability/inferences/$inference_id/ParameterCard";
|
|
import { ToolParametersSection } from "~/components/inference/ToolParametersSection";
|
|
import { TagsTable } from "~/components/tags/TagsTable";
|
|
import { ModelInferencesContent } from "~/routes/observability/inferences/$inference_id/ModelInferencesSection";
|
|
import {
|
|
SectionHeader,
|
|
SectionLayout,
|
|
SectionsGroup,
|
|
} from "~/components/layout/PageLayout";
|
|
import { useToast } from "~/hooks/use-toast";
|
|
import {
|
|
prepareInferenceActionRequest,
|
|
useInferenceActionFetcher,
|
|
prepareDemonstrationFromVariantOutput,
|
|
} from "~/routes/api/tensorzero/inference.utils";
|
|
import { ActionBar } from "~/components/layout/ActionBar";
|
|
import { TryWithSelect } from "~/components/inference/TryWithSelect";
|
|
import { AddToDatasetButton } from "~/components/dataset/AddToDatasetButton";
|
|
import { HumanFeedbackButton } from "~/components/feedback/HumanFeedbackButton";
|
|
import { HumanFeedbackModal } from "~/components/feedback/HumanFeedbackModal";
|
|
import { HumanFeedbackForm } from "~/components/feedback/HumanFeedbackForm";
|
|
import { DemonstrationFeedbackButton } from "~/components/feedback/DemonstrationFeedbackButton";
|
|
import { AskAutopilotButton } from "~/components/autopilot/AskAutopilotButton";
|
|
import { logger } from "~/utils/logger";
|
|
import { useFetcherWithReset } from "~/hooks/use-fetcher-with-reset";
|
|
import { DEFAULT_FUNCTION } from "~/utils/constants";
|
|
import { CopyMessagesButton } from "~/components/inference/CopyMessagesButton";
|
|
import { VariantResponseModal } from "~/components/inference/VariantResponseModal";
|
|
import { InputElement } from "../input_output/InputElement";
|
|
import type { Input } from "~/types/tensorzero";
|
|
|
|
export interface InferenceDetailData {
|
|
inference: StoredInference;
|
|
// TODO: remove
|
|
input?: Input;
|
|
model_inferences: ParsedModelInferenceRow[];
|
|
feedback: FeedbackRow[];
|
|
feedback_bounds: FeedbackBounds;
|
|
hasDemonstration: boolean;
|
|
latestFeedbackByMetric: Record<string, string>;
|
|
usedVariants: string[];
|
|
}
|
|
|
|
/**
|
|
* Props for rendering the header section (BasicInfo + ActionBar).
|
|
* This allows the parent component to customize how the header is rendered,
|
|
* e.g., wrapping it in a PageHeader for the full page view.
|
|
*/
|
|
export interface InferenceHeaderProps {
|
|
basicInfo: ReactNode;
|
|
actionBar: ReactNode;
|
|
}
|
|
|
|
interface InferenceDetailContentProps {
|
|
data: InferenceDetailData;
|
|
onFeedbackAdded?: (redirectUrl?: string) => void;
|
|
feedbackFooter?: ReactNode;
|
|
/**
|
|
* Optional render function for customizing the header layout.
|
|
* If not provided, BasicInfo and ActionBar are rendered directly.
|
|
* Use this to wrap the header in a PageHeader for the full page view.
|
|
*/
|
|
renderHeader?: (props: InferenceHeaderProps) => ReactNode;
|
|
}
|
|
|
|
type ModalType = "human-feedback" | "variant-response" | null;
|
|
|
|
type ActionData =
|
|
| { redirectTo: string; error?: never }
|
|
| { error: string; redirectTo?: never };
|
|
|
|
export function InferenceDetailContent({
|
|
data,
|
|
onFeedbackAdded,
|
|
feedbackFooter,
|
|
renderHeader,
|
|
}: InferenceDetailContentProps) {
|
|
const {
|
|
inference,
|
|
input,
|
|
model_inferences,
|
|
feedback,
|
|
feedback_bounds,
|
|
hasDemonstration,
|
|
latestFeedbackByMetric,
|
|
usedVariants,
|
|
} = data;
|
|
|
|
const [openModal, setOpenModal] = useState<ModalType | null>(null);
|
|
const [selectedVariant, setSelectedVariant] = useState<string | null>(null);
|
|
|
|
const inferenceUsage = getTotalInferenceUsage(model_inferences);
|
|
const functionConfig = useFunctionConfig(inference.function_name);
|
|
const variants = Object.keys(functionConfig?.variants || {});
|
|
|
|
const { toast } = useToast();
|
|
|
|
// Demonstration feedback fetcher
|
|
const demonstrationFeedbackFetcher = useFetcherWithReset<ActionData>();
|
|
const {
|
|
data: demonstrationFeedbackData,
|
|
state: demonstrationFeedbackState,
|
|
reset: resetDemonstrationFeedbackFetcher,
|
|
} = demonstrationFeedbackFetcher;
|
|
const demonstrationFeedbackFormError =
|
|
demonstrationFeedbackState === "idle"
|
|
? (demonstrationFeedbackData?.error ?? null)
|
|
: null;
|
|
useEffect(() => {
|
|
const currentState = demonstrationFeedbackState;
|
|
const fetcherData = demonstrationFeedbackData;
|
|
if (currentState === "idle" && fetcherData?.redirectTo) {
|
|
onFeedbackAdded?.(fetcherData.redirectTo);
|
|
setOpenModal(null);
|
|
setSelectedVariant(null);
|
|
resetDemonstrationFeedbackFetcher();
|
|
}
|
|
}, [
|
|
demonstrationFeedbackData,
|
|
demonstrationFeedbackState,
|
|
resetDemonstrationFeedbackFetcher,
|
|
onFeedbackAdded,
|
|
]);
|
|
|
|
// Variant inference fetcher
|
|
const variantInferenceFetcher = useInferenceActionFetcher();
|
|
const [lastRequestArgs, setLastRequestArgs] = useState<
|
|
Parameters<typeof prepareInferenceActionRequest>[0] | null
|
|
>(null);
|
|
const variantSource = "inference";
|
|
const variantInferenceIsLoading =
|
|
openModal === "variant-response" &&
|
|
(variantInferenceFetcher.state === "submitting" ||
|
|
variantInferenceFetcher.state === "loading");
|
|
|
|
const { submit } = variantInferenceFetcher;
|
|
const processRequest = (
|
|
option: string,
|
|
args: Parameters<typeof prepareInferenceActionRequest>[0],
|
|
) => {
|
|
try {
|
|
const request = prepareInferenceActionRequest(args);
|
|
|
|
setSelectedVariant(option);
|
|
setOpenModal("variant-response");
|
|
setLastRequestArgs(args);
|
|
|
|
try {
|
|
void submit({ data: JSON.stringify(request) });
|
|
} catch (stringifyError) {
|
|
logger.error("Failed to stringify request:", stringifyError);
|
|
toast.error({
|
|
title: "Request Error",
|
|
description: "Failed to prepare the request. Please try again.",
|
|
});
|
|
setSelectedVariant(null);
|
|
setOpenModal(null);
|
|
}
|
|
} catch (error) {
|
|
logger.error("Failed to prepare inference request:", error);
|
|
|
|
let errorMessage = "Failed to prepare the request. Please try again.";
|
|
if (error instanceof Error) {
|
|
if (error.message.includes("Extra body is not supported")) {
|
|
errorMessage =
|
|
"This inference contains extra body parameters which are not supported in the UI.";
|
|
} else if (error.message) {
|
|
errorMessage = error.message;
|
|
}
|
|
}
|
|
|
|
toast.error({
|
|
title: "Request Preparation Error",
|
|
description: errorMessage,
|
|
});
|
|
}
|
|
};
|
|
|
|
const onVariantSelect = (variant: string) => {
|
|
if (!input) {
|
|
toast.error({
|
|
title: "Input Unavailable",
|
|
description: "Cannot retry inference without input data.",
|
|
});
|
|
return;
|
|
}
|
|
processRequest(variant, {
|
|
resource: inference,
|
|
input,
|
|
source: variantSource,
|
|
variant,
|
|
});
|
|
};
|
|
|
|
const onModelSelect = (model: string) => {
|
|
if (!input) {
|
|
toast.error({
|
|
title: "Input Unavailable",
|
|
description: "Cannot retry inference without input data.",
|
|
});
|
|
return;
|
|
}
|
|
processRequest(model, {
|
|
resource: inference,
|
|
input,
|
|
source: variantSource,
|
|
model_name: model,
|
|
});
|
|
};
|
|
|
|
const handleRefresh = () => {
|
|
if (!lastRequestArgs) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const request = prepareInferenceActionRequest(lastRequestArgs);
|
|
request.cache_options = {
|
|
...request.cache_options,
|
|
enabled: "write_only",
|
|
};
|
|
submit({ data: JSON.stringify(request) });
|
|
} catch (error) {
|
|
logger.error("Failed to prepare inference request for refresh:", error);
|
|
toast.error({
|
|
title: "Request Preparation Error",
|
|
description: "Failed to refresh inference. Please try again.",
|
|
});
|
|
}
|
|
};
|
|
|
|
// Human feedback fetcher
|
|
const humanFeedbackFetcher = useFetcherWithReset<ActionData>();
|
|
const {
|
|
data: humanFeedbackData,
|
|
state: humanFeedbackState,
|
|
reset: resetHumanFeedbackFetcher,
|
|
} = humanFeedbackFetcher;
|
|
const humanFeedbackFormError =
|
|
humanFeedbackState === "idle" ? (humanFeedbackData?.error ?? null) : null;
|
|
useEffect(() => {
|
|
const currentState = humanFeedbackState;
|
|
const fetcherData = humanFeedbackData;
|
|
if (currentState === "idle" && fetcherData?.redirectTo) {
|
|
onFeedbackAdded?.(fetcherData.redirectTo);
|
|
setOpenModal(null);
|
|
resetHumanFeedbackFetcher();
|
|
}
|
|
}, [
|
|
humanFeedbackData,
|
|
humanFeedbackState,
|
|
onFeedbackAdded,
|
|
resetHumanFeedbackFetcher,
|
|
]);
|
|
|
|
const config = useConfig();
|
|
|
|
const isDefault = inference.function_name === DEFAULT_FUNCTION;
|
|
|
|
const modelsSet = new Set<string>([...usedVariants, ...config.model_names]);
|
|
const models = [...modelsSet].sort();
|
|
|
|
const options = isDefault ? models : variants;
|
|
const onSelect = isDefault ? onModelSelect : onVariantSelect;
|
|
|
|
const variantType =
|
|
functionConfig?.variants[inference.variant_name]?.inner.type ??
|
|
(inference.function_name === "tensorzero::default"
|
|
? "chat_completion"
|
|
: "unknown");
|
|
|
|
// Build the header components
|
|
const basicInfoElement = (
|
|
<BasicInfo
|
|
inference={inference}
|
|
variantType={variantType}
|
|
modelInferences={model_inferences}
|
|
/>
|
|
);
|
|
|
|
const actionBarElement = (
|
|
<ActionBar>
|
|
<TryWithSelect
|
|
options={options}
|
|
onSelect={onSelect}
|
|
isLoading={variantInferenceIsLoading}
|
|
isDefaultFunction={isDefault}
|
|
/>
|
|
<AddToDatasetButton
|
|
inferenceId={inference.inference_id}
|
|
functionName={inference.function_name}
|
|
variantName={inference.variant_name}
|
|
episodeId={inference.episode_id}
|
|
hasDemonstration={hasDemonstration}
|
|
/>
|
|
<HumanFeedbackModal
|
|
onOpenChange={(isOpen) => {
|
|
if (humanFeedbackState !== "idle") {
|
|
return;
|
|
}
|
|
|
|
if (!isOpen) {
|
|
resetHumanFeedbackFetcher();
|
|
}
|
|
setOpenModal(isOpen ? "human-feedback" : null);
|
|
}}
|
|
isOpen={openModal === "human-feedback"}
|
|
trigger={<HumanFeedbackButton />}
|
|
>
|
|
<humanFeedbackFetcher.Form method="post" action="/api/feedback">
|
|
<HumanFeedbackForm
|
|
inferenceId={inference.inference_id}
|
|
inferenceOutput={inference.output}
|
|
formError={humanFeedbackFormError}
|
|
isSubmitting={
|
|
humanFeedbackState === "submitting" ||
|
|
humanFeedbackState === "loading"
|
|
}
|
|
/>
|
|
</humanFeedbackFetcher.Form>
|
|
</HumanFeedbackModal>
|
|
<AskAutopilotButton
|
|
message={`Inference ID: ${inference.inference_id}\n\n`}
|
|
/>
|
|
{/* Keep at end of row — conditionally hidden, so trailing position avoids jitter */}
|
|
<CopyMessagesButton input={input} output={inference.output} />
|
|
</ActionBar>
|
|
);
|
|
|
|
// Allow parent to customize header rendering, or use default layout
|
|
const headerContent = renderHeader ? (
|
|
renderHeader({ basicInfo: basicInfoElement, actionBar: actionBarElement })
|
|
) : (
|
|
<>
|
|
{basicInfoElement}
|
|
{actionBarElement}
|
|
</>
|
|
);
|
|
|
|
return (
|
|
<>
|
|
{headerContent}
|
|
|
|
<SectionsGroup>
|
|
<SectionLayout>
|
|
<SectionHeader heading="Input" />
|
|
{input && <InputElement input={input} />}
|
|
</SectionLayout>
|
|
|
|
<SectionLayout>
|
|
<SectionHeader heading="Output" />
|
|
{inference.type === "json" ? (
|
|
<JsonOutputElement
|
|
output={inference.output}
|
|
outputSchema={inference.output_schema}
|
|
/>
|
|
) : (
|
|
<ChatOutputElement output={inference.output} />
|
|
)}
|
|
</SectionLayout>
|
|
|
|
<SectionLayout>
|
|
<SectionHeader
|
|
heading="Feedback"
|
|
count={feedback.length}
|
|
badge={{
|
|
name: "inference",
|
|
tooltip:
|
|
"This table only includes inference-level feedback. To see episode-level feedback, open the detail page for that episode.",
|
|
}}
|
|
/>
|
|
<FeedbackTable
|
|
feedback={feedback}
|
|
latestCommentId={feedback_bounds.by_type.comment.last_id!}
|
|
latestDemonstrationId={
|
|
feedback_bounds.by_type.demonstration.last_id!
|
|
}
|
|
latestFeedbackIdByMetric={latestFeedbackByMetric}
|
|
/>
|
|
{feedbackFooter}
|
|
</SectionLayout>
|
|
|
|
<SectionLayout>
|
|
<SectionHeader heading="Inference Parameters" />
|
|
{inference.inference_params ? (
|
|
<ParameterCard
|
|
parameters={JSON.stringify(inference.inference_params, null, 2)}
|
|
/>
|
|
) : (
|
|
<div className="text-fg-muted flex items-center justify-center py-12 text-sm">
|
|
Parameters missing
|
|
</div>
|
|
)}
|
|
</SectionLayout>
|
|
|
|
{inference.type === "chat" && (
|
|
<SectionLayout>
|
|
<SectionHeader heading="Tool Parameters" />
|
|
<ToolParametersSection
|
|
allowed_tools={inference.allowed_tools}
|
|
additional_tools={inference.additional_tools}
|
|
tool_choice={inference.tool_choice}
|
|
parallel_tool_calls={inference.parallel_tool_calls}
|
|
provider_tools={inference.provider_tools}
|
|
/>
|
|
</SectionLayout>
|
|
)}
|
|
|
|
<SectionLayout>
|
|
<SectionHeader heading="Tags" />
|
|
<TagsTable
|
|
tags={Object.fromEntries(
|
|
Object.entries(inference.tags).filter(
|
|
(entry): entry is [string, string] => entry[1] !== undefined,
|
|
),
|
|
)}
|
|
isEditing={false}
|
|
/>
|
|
</SectionLayout>
|
|
|
|
<SectionLayout>
|
|
<SectionHeader heading="Model Inferences" />
|
|
<ModelInferencesContent modelInferences={model_inferences} />
|
|
</SectionLayout>
|
|
</SectionsGroup>
|
|
|
|
{selectedVariant && (
|
|
<VariantResponseModal
|
|
isOpen={openModal === "variant-response"}
|
|
isLoading={variantInferenceIsLoading}
|
|
error={variantInferenceFetcher.error?.message}
|
|
variantResponse={variantInferenceFetcher.data?.info ?? null}
|
|
rawResponse={variantInferenceFetcher.data?.raw ?? null}
|
|
onClose={() => {
|
|
setOpenModal(null);
|
|
setSelectedVariant(null);
|
|
setLastRequestArgs(null);
|
|
}}
|
|
item={inference}
|
|
inferenceUsage={inferenceUsage}
|
|
selectedVariant={selectedVariant}
|
|
source={variantSource}
|
|
onRefresh={lastRequestArgs ? handleRefresh : null}
|
|
>
|
|
{variantInferenceFetcher.data?.info && (
|
|
<demonstrationFeedbackFetcher.Form
|
|
method="post"
|
|
action="/api/feedback"
|
|
>
|
|
<input type="hidden" name="metricName" value="demonstration" />
|
|
<input
|
|
type="hidden"
|
|
name="inferenceId"
|
|
value={inference.inference_id}
|
|
/>
|
|
<input
|
|
type="hidden"
|
|
name="value"
|
|
value={JSON.stringify(
|
|
prepareDemonstrationFromVariantOutput(
|
|
variantInferenceFetcher.data.info,
|
|
),
|
|
)}
|
|
/>
|
|
<DemonstrationFeedbackButton
|
|
isSubmitting={demonstrationFeedbackState === "submitting"}
|
|
submissionError={demonstrationFeedbackFormError}
|
|
/>
|
|
</demonstrationFeedbackFetcher.Form>
|
|
)}
|
|
</VariantResponseModal>
|
|
)}
|
|
</>
|
|
);
|
|
}
|