Spaces:
Configuration error
Configuration error
File size: 17,080 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
import React, { useState, useEffect } from 'react';
import { Card, Form, Typography, Select, Input, Switch, Tooltip, Modal, message, Divider, Space, Tag, Image, Steps } from 'antd';
import { Button, TextInput } from '@tremor/react';
import type { FormInstance } from 'antd';
import { GuardrailProviders, guardrail_provider_map, shouldRenderPIIConfigSettings, guardrailLogoMap } from './guardrail_info_helpers';
import { createGuardrailCall, getGuardrailUISettings, getGuardrailProviderSpecificParams } from '../networking';
import PiiConfiguration from './pii_configuration';
import GuardrailProviderFields from './guardrail_provider_fields';
const { Title, Text, Link } = Typography;
const { Option } = Select;
const { Step } = Steps;
// Define human-friendly descriptions for each mode
const modeDescriptions = {
pre_call: "Before LLM Call - Runs before the LLM call and checks the input (Recommended)",
during_call: "During LLM Call - Runs in parallel with the LLM call, with response held until check completes",
post_call: "After LLM Call - Runs after the LLM call and checks only the output",
logging_only: "Logging Only - Only runs on logging callbacks without affecting the LLM call"
};
interface AddGuardrailFormProps {
visible: boolean;
onClose: () => void;
accessToken: string | null;
onSuccess: () => void;
}
interface GuardrailSettings {
supported_entities: string[];
supported_actions: string[];
supported_modes: string[];
pii_entity_categories: Array<{
category: string;
entities: string[];
}>;
}
interface LiteLLMParams {
guardrail: string;
mode: string;
default_on: boolean;
[key: string]: any; // Allow additional properties for specific guardrails
}
// Mapping of provider -> list of param descriptors
interface ProviderParam {
param: string;
description: string;
required: boolean;
default_value?: string;
options?: string[];
type?: string;
}
interface ProviderParamsResponse {
[provider: string]: ProviderParam[];
}
const AddGuardrailForm: React.FC<AddGuardrailFormProps> = ({
visible,
onClose,
accessToken,
onSuccess
}) => {
const [form] = Form.useForm();
const [loading, setLoading] = useState(false);
const [selectedProvider, setSelectedProvider] = useState<string | null>(null);
const [guardrailSettings, setGuardrailSettings] = useState<GuardrailSettings | null>(null);
const [selectedEntities, setSelectedEntities] = useState<string[]>([]);
const [selectedActions, setSelectedActions] = useState<{[key: string]: string}>({});
const [currentStep, setCurrentStep] = useState(0);
const [providerParams, setProviderParams] = useState<ProviderParamsResponse | null>(null);
// Fetch guardrail UI settings + provider params on mount / accessToken change
useEffect(() => {
if (!accessToken) return;
const fetchData = async () => {
try {
// Parallel requests for speed
const [uiSettings, providerParamsResp] = await Promise.all([
getGuardrailUISettings(accessToken),
getGuardrailProviderSpecificParams(accessToken),
]);
setGuardrailSettings(uiSettings);
setProviderParams(providerParamsResp);
} catch (error) {
console.error('Error fetching guardrail data:', error);
message.error('Failed to load guardrail configuration');
}
};
fetchData();
}, [accessToken]);
const handleProviderChange = (value: string) => {
setSelectedProvider(value);
// Reset form fields that are provider-specific
form.setFieldsValue({
config: undefined,
presidio_analyzer_api_base: undefined,
presidio_anonymizer_api_base: undefined
});
// Reset PII selections when changing provider
setSelectedEntities([]);
setSelectedActions({});
};
const handleEntitySelect = (entity: string) => {
setSelectedEntities(prev => {
if (prev.includes(entity)) {
return prev.filter(e => e !== entity);
} else {
return [...prev, entity];
}
});
};
const handleActionSelect = (entity: string, action: string) => {
setSelectedActions(prev => ({
...prev,
[entity]: action
}));
};
const nextStep = async () => {
try {
// Validate current step fields
if (currentStep === 0) {
await form.validateFields(['guardrail_name', 'provider', 'mode', 'default_on']);
// Also validate provider-specific fields if applicable
if (selectedProvider) {
// This will automatically validate any required fields for the selected provider
const fieldsToValidate = ['guardrail_name', 'provider', 'mode', 'default_on'];
if (selectedProvider === 'PresidioPII') {
fieldsToValidate.push('presidio_analyzer_api_base', 'presidio_anonymizer_api_base');
}
await form.validateFields(fieldsToValidate);
}
}
setCurrentStep(currentStep + 1);
} catch (error) {
console.error("Form validation failed:", error);
}
};
const prevStep = () => {
setCurrentStep(currentStep - 1);
};
const resetForm = () => {
form.resetFields();
setSelectedProvider(null);
setSelectedEntities([]);
setSelectedActions({});
setCurrentStep(0);
};
const handleClose = () => {
resetForm();
onClose();
};
const handleSubmit = async () => {
try {
setLoading(true);
// First validate currently visible fields
await form.validateFields();
// After validation, fetch *all* form values (including those from previous steps)
const values = form.getFieldsValue(true);
// Get the guardrail provider value from the map
const guardrailProvider = guardrail_provider_map[values.provider];
// Prepare the guardrail data with proper typings
const guardrailData: {
guardrail_name: string;
litellm_params: {
guardrail: string;
mode: string;
default_on: boolean;
[key: string]: any; // Allow dynamic properties
};
guardrail_info: any;
} = {
guardrail_name: values.guardrail_name,
litellm_params: {
guardrail: guardrailProvider,
mode: values.mode,
default_on: values.default_on
},
guardrail_info: {}
};
// For Presidio PII, add the entity and action configurations
if (values.provider === 'PresidioPII' && selectedEntities.length > 0) {
const piiEntitiesConfig: {[key: string]: string} = {};
selectedEntities.forEach(entity => {
piiEntitiesConfig[entity] = selectedActions[entity] || 'MASK'; // Default to MASK if no action selected
});
guardrailData.litellm_params.pii_entities_config = piiEntitiesConfig;
// Add Presidio API bases if provided
if (values.presidio_analyzer_api_base) {
guardrailData.litellm_params.presidio_analyzer_api_base = values.presidio_analyzer_api_base;
}
if (values.presidio_anonymizer_api_base) {
guardrailData.litellm_params.presidio_anonymizer_api_base = values.presidio_anonymizer_api_base;
}
}
// Add config values to the guardrail_info if provided
else if (values.config) {
try {
const configObj = JSON.parse(values.config);
// For some guardrails, the config values need to be in litellm_params
guardrailData.guardrail_info = configObj;
} catch (error) {
message.error('Invalid JSON in configuration');
setLoading(false);
return;
}
}
/******************************
* Add provider-specific params
* ----------------------------------
* The backend exposes exactly which extra parameters a provider
* accepts via `/guardrails/ui/provider_specific_params`.
* Instead of copying every unknown form field, we fetch the list for
* the selected provider and ONLY pass those recognised params.
******************************/
// Use pre-fetched provider params to copy recognised params
if (providerParams && selectedProvider) {
const providerKey = guardrail_provider_map[selectedProvider]?.toLowerCase();
const providerSpecificParams = providerParams[providerKey] || [];
const allowedParams = new Set<string>(
providerSpecificParams.map((p) => p.param)
);
allowedParams.forEach((paramName) => {
const paramValue = values[paramName];
if (paramValue !== undefined && paramValue !== null && paramValue !== '') {
guardrailData.litellm_params[paramName] = paramValue;
}
});
}
if (!accessToken) {
throw new Error("No access token available");
}
console.log("Sending guardrail data:", JSON.stringify(guardrailData));
await createGuardrailCall(accessToken, guardrailData);
message.success('Guardrail created successfully');
// Reset form and close modal
resetForm();
onSuccess();
onClose();
} catch (error) {
console.error("Failed to create guardrail:", error);
message.error('Failed to create guardrail: ' + (error instanceof Error ? error.message : String(error)));
} finally {
setLoading(false);
}
};
const renderBasicInfo = () => {
return (
<>
<Form.Item
name="guardrail_name"
label="Guardrail Name"
rules={[{ required: true, message: 'Please enter a guardrail name' }]}
>
<TextInput placeholder="Enter a name for this guardrail" />
</Form.Item>
<Form.Item
name="provider"
label="Guardrail Provider"
rules={[{ required: true, message: 'Please select a provider' }]}
>
<Select
placeholder="Select a guardrail provider"
onChange={handleProviderChange}
labelInValue={false}
optionLabelProp="label"
dropdownRender={menu => menu}
showSearch={true}
>
{Object.entries(GuardrailProviders).map(([key, value]) => (
<Option
key={key}
value={key}
label={
<div style={{ display: 'flex', alignItems: 'center' }}>
{guardrailLogoMap[value] && (
<img
src={guardrailLogoMap[value]}
alt=""
style={{
height: '20px',
width: '20px',
marginRight: '8px',
objectFit: 'contain'
}}
onError={(e) => {
// Hide broken image icon if image fails to load
e.currentTarget.style.display = 'none';
}}
/>
)}
<span>{value}</span>
</div>
}
>
<div style={{ display: 'flex', alignItems: 'center' }}>
{guardrailLogoMap[value] && (
<img
src={guardrailLogoMap[value]}
alt=""
style={{
height: '20px',
width: '20px',
marginRight: '8px',
objectFit: 'contain'
}}
onError={(e) => {
// Hide broken image icon if image fails to load
e.currentTarget.style.display = 'none';
}}
/>
)}
<span>{value}</span>
</div>
</Option>
))}
</Select>
</Form.Item>
<Form.Item
name="mode"
label="Mode"
tooltip="How the guardrail should be applied"
rules={[{ required: true, message: 'Please select a mode' }]}
>
<Select
optionLabelProp="label"
mode="multiple"
>
{guardrailSettings?.supported_modes?.map(mode => (
<Option key={mode} value={mode} label={mode}>
<div>
<div>
<strong>{mode}</strong>
{mode === 'pre_call' && <Tag color="green" style={{ marginLeft: '8px' }}>Recommended</Tag>}
</div>
<div style={{ fontSize: '12px', color: '#888' }}>{modeDescriptions[mode as keyof typeof modeDescriptions]}</div>
</div>
</Option>
)) || (
<>
<Option value="pre_call" label="pre_call">
<div>
<div><strong>pre_call</strong> <Tag color="green">Recommended</Tag></div>
<div style={{ fontSize: '12px', color: '#888' }}>{modeDescriptions.pre_call}</div>
</div>
</Option>
<Option value="during_call" label="during_call">
<div>
<div><strong>during_call</strong></div>
<div style={{ fontSize: '12px', color: '#888' }}>{modeDescriptions.during_call}</div>
</div>
</Option>
<Option value="post_call" label="post_call">
<div>
<div><strong>post_call</strong></div>
<div style={{ fontSize: '12px', color: '#888' }}>{modeDescriptions.post_call}</div>
</div>
</Option>
<Option value="logging_only" label="logging_only">
<div>
<div><strong>logging_only</strong></div>
<div style={{ fontSize: '12px', color: '#888' }}>{modeDescriptions.logging_only}</div>
</div>
</Option>
</>
)}
</Select>
</Form.Item>
<Form.Item
name="default_on"
label="Always On"
tooltip="If enabled, this guardrail will be applied to all requests by default."
>
<Select>
<Select.Option value={true}>Yes</Select.Option>
<Select.Option value={false}>No</Select.Option>
</Select>
</Form.Item>
{/* Use the GuardrailProviderFields component to render provider-specific fields */}
<GuardrailProviderFields
selectedProvider={selectedProvider}
accessToken={accessToken}
providerParams={providerParams}
/>
</>
);
};
const renderPiiConfiguration = () => {
if (!guardrailSettings || selectedProvider !== 'PresidioPII') return null;
return (
<PiiConfiguration
entities={guardrailSettings.supported_entities}
actions={guardrailSettings.supported_actions}
selectedEntities={selectedEntities}
selectedActions={selectedActions}
onEntitySelect={handleEntitySelect}
onActionSelect={handleActionSelect}
entityCategories={guardrailSettings.pii_entity_categories}
/>
);
};
const renderStepContent = () => {
switch (currentStep) {
case 0:
return renderBasicInfo();
case 1:
if (shouldRenderPIIConfigSettings(selectedProvider)) {
return renderPiiConfiguration();
}
default:
return null;
}
};
const renderStepButtons = () => {
return (
<div className="flex justify-end space-x-2 mt-4">
{currentStep > 0 && (
<Button
variant="secondary"
onClick={prevStep}
>
Previous
</Button>
)}
{currentStep < 1 && (
<Button
onClick={nextStep}
>
Next
</Button>
)}
{currentStep === 1 && (
<Button
onClick={handleSubmit}
loading={loading}
>
Create Guardrail
</Button>
)}
<Button
variant="secondary"
onClick={handleClose}
>
Cancel
</Button>
</div>
);
};
return (
<Modal
title="Add Guardrail"
open={visible}
onCancel={handleClose}
footer={null}
width={700}
>
<Form
form={form}
layout="vertical"
initialValues={{
mode: "pre_call",
default_on: false
}}
>
<Steps current={currentStep} className="mb-6">
<Step title="Basic Info" />
<Step title={selectedProvider === 'PresidioPII' ? "PII Configuration" : "Provider Configuration"} />
</Steps>
{renderStepContent()}
{renderStepButtons()}
</Form>
</Modal>
);
};
export default AddGuardrailForm; |