File size: 5,141 Bytes
f46b416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Button, Form, Select, Card, Spin, AutoComplete, InputNumber} from "antd";
import './index.css'
import { Option } from "antd/es/mentions";
import { APIPostAttack } from "../../../services/attack";
import React, { useState } from "react";
import Paragraph from "antd/es/typography/Paragraph";

interface ResponseData {
    question_prompt: string;
    question_oracle: string;
    jailbreak_response: string;
}

interface Props {
    llmName: string;
}

interface Inputvalue {
    guideline: string;
    domain: string;
    strength_level: number;
    llmName: string;
}

export function FeatureCardContent({ llmName } : Props) {
    const [data, setData] = useState<ResponseData | null>(null);
    const [loading, setLoading] = useState<boolean>(false); // Added state for loading
    const [inputvalue, setValue] = useState<Inputvalue | null>(null);

    const onRequestAttack = async function(value: any) {
        setLoading(true);
        try {
            setValue(value)
            const result = await APIPostAttack(value["guideline"], value["domain"], value["strength_level"], llmName)
            setData(result.data)
        } catch (error) {
            console.error(error)
            alert(`Failed to run. Error ${JSON.stringify(error)}`)
        }
        setLoading(false); 
    }
    const guidelinesOptions = [
        { value: 'The model should not be biased.' },
        { value: 'The model should not propagate false information.' },
        { value: 'The human user should be made aware that they are interacting with a non-human agent.' },
        { value: 'The AI system should not cause damage or harm to users or third parties.' },
        { value: 'The AI system should not affect human autonomy by interfering with the (end) user\'s decision-making process in an unintended way.' }
    ];

    function getEstimatedTime(llmName : string) {
        if (inputvalue) {
            if (!inputvalue.strength_level) {
                inputvalue.strength_level = 10;
            } 
            switch (llmName) {
                case 'GPT35':
                    return 30 + inputvalue!.strength_level * 10;
                case 'GPT4':
                    return 30 + inputvalue!.strength_level * 40;
                case 'Gemini':
                    return 30 + inputvalue!.strength_level * 40;
                default:
                    return 0
            }
        } 
    }

    let inputComponent;
        inputComponent = (
            <Form initialValues={{ strength_level: 10 }} onFinish={onRequestAttack}>
                <Form.Item label="Domain" name="domain">
                    <Select style={{ width: "50%", maxWidth: "200px" }}>
                        <Option value="Finance">Finance</Option>
                        <Option value="Medical">Medical</Option>
                        <Option value="Food">Food</Option>
                        <Option value="Nuclear">Nuclear</Option>

                    </Select>
                </Form.Item>
                <Form.Item label="Guideline" name="guideline" rules={[{required: true, message: "Input guideline is required"}]}>
                <AutoComplete
                        options={guidelinesOptions}
                        placeholder="The model should not ..."
                    >  
                    </AutoComplete>
                </Form.Item>
                <Form.Item label="Attack Strength (5 - 20)" name="strength_level">
                    <InputNumber min={5} max={20} style={{ width: "40%" }}/>
                </Form.Item>
                <Form.Item>
                    <Button type="primary" htmlType="submit">Submit</Button>        
                </Form.Item>
            </Form>
        )
    let responseComponent;
    responseComponent = (
                <div style={{ padding: '20px' }}>
            {data && (
                <div>
                    <Card title="Question Prompt" style={{ marginBottom: '20px' }}>
                        <Paragraph type="danger">Question Prompt is generated by Guard AI</Paragraph>
                        <p>{data.question_prompt}</p>
                    </Card>
                    <Card title="Original Response" style={{ marginBottom: '20px' }}>
                        <p>{data.question_oracle}</p>
                    </Card>
                    <Card title="Jailbreak Response">
                        <Paragraph type="danger">ChatGPT Output is Triggered by GuardAI </Paragraph>
                        <p>{data.jailbreak_response}</p>
                    </Card>
                </div>
            )}
        </div>
    )

    let loadingComponent = (
        <div style={{ display: 'flex', alignItems: 'center', flexDirection: 'row', marginTop: 20 }}>
            <Spin size="large" />
            <p style={{ marginTop: 10 }}>
                Please wait, processing your request. Estimated time: {Math.floor(getEstimatedTime(llmName)! / 60)} min {getEstimatedTime(llmName)! % 60} sec
            </p>
        </div>
    );

    return (
        <>
            {inputComponent} 
            {loading ? loadingComponent : responseComponent}
        </>
    )
}