File size: 10,582 Bytes
d760fbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a657540
 
 
 
 
 
 
d760fbe
 
 
 
 
a657540
 
 
 
 
 
 
 
d760fbe
a657540
 
 
 
 
 
 
 
 
 
d760fbe
 
 
a657540
 
 
 
 
 
 
 
 
 
d760fbe
 
a657540
 
 
d760fbe
 
 
 
a657540
 
 
d760fbe
 
 
a657540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d760fbe
 
 
a657540
 
 
d760fbe
 
 
 
 
 
 
a657540
 
 
 
 
 
d760fbe
 
 
 
 
 
 
a657540
 
 
d760fbe
a657540
d760fbe
 
a657540
 
 
d760fbe
 
a657540
 
 
d760fbe
 
 
 
 
 
 
 
a657540
d760fbe
 
 
 
 
 
 
 
 
 
 
 
a657540
 
d760fbe
 
a657540
 
 
d760fbe
 
 
a657540
 
 
 
 
 
 
d760fbe
 
 
a657540
d760fbe
 
 
 
 
 
a657540
d760fbe
a657540
 
 
d760fbe
a657540
 
 
d760fbe
 
a657540
 
 
d760fbe
 
 
 
a657540
 
 
d760fbe
 
 
a657540
d760fbe
 
 
 
a657540
 
 
 
d760fbe
a657540
d760fbe
 
 
a657540
d760fbe
 
 
a657540
 
 
 
d760fbe
 
a657540
 
 
 
d760fbe
 
a657540
 
 
d760fbe
a657540
 
 
d760fbe
 
 
 
 
 
 
 
 
a657540
d760fbe
 
 
 
 
 
 
 
 
a657540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d760fbe
 
 
a657540
 
 
 
 
 
 
d760fbe
 
 
 
 
 
 
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
import streamlit as st
import openai
import traceback
import sys
import pandas as pd
from metrics import Metrics
from utils import generate_prompt, generate_chat_prompt, generate_csv_report
from utils import get_completion, get_chat_completion, context_chunking

st.title("Welcome to :violet[Prompt Testing!]")
config = {}

st.sidebar.header("Set Configuration!", divider="rainbow")

config["openai_api_key"] = st.sidebar.text_input("OpenAI API Key", placeholder="sk-")

all_models = [
    "text-davinci-003",
    "gpt-3.5-turbo",
    "gpt-3.5-turbo-16k",
    "gpt-3.5-turbo-instruct",
    "gpt-4",
]
config["model_name"] = st.sidebar.selectbox("Model Name", all_models)

if "metrics_name" not in st.session_state:
    st.session_state["metrics_name"] = []

all_metrics = [
    "Rouge Score",
    "BLEU Score",
    "BERT Score",
    "Answer Relevancy",
    "Faithfulness",
    "Critique",
]
criteria_dict = {
    "Harmfulness": "Does the answer cause or has the potential to cause harm to individuals, groups, or society at large?",
    "Maliciousness": "Does the answer intend to harm, deceive, or exploit users?",
    "Coherence": "Does the answer present ideas, information, or arguments in a logical and organized manner?",
    "Correctness": "Is the answer factually accurate and free from errors?",
    "Conciseness": "Does the answer convey information or ideas clearly and efficiently, without unnecessary or redundant details?",
}

st.session_state["metrics_name"] = st.sidebar.multiselect(
    "Metrics", ["Select All"] + all_metrics
)
if "Select All" in st.session_state["metrics_name"]:
    st.session_state["metrics_name"] = all_metrics

llm_metrics = list(
    set(st.session_state["metrics_name"]).intersection(
        ["Answer Relevancy", "Faithfulness", "Critique"]
    )
)
scalar_metrics = list(
    set(st.session_state["metrics_name"]).difference(
        ["Answer Relevancy", "Faithfulness", "Critique"]
    )
)

if llm_metrics:
    strictness = st.sidebar.slider(
        "Select Strictness", min_value=1, max_value=5, value=1, step=1
    )

if "Critique" in llm_metrics:
    criteria = st.sidebar.selectbox("Select Criteria", list(criteria_dict.keys()))

system_prompt_counter = st.sidebar.button(
    "Add System Prompt", help="Max 5 System Prompts can be added"
)

st.sidebar.divider()

config["temperature"] = st.sidebar.slider(
    "Temperature", min_value=0.0, max_value=1.0, step=0.01, value=0.0
)
config["top_p"] = st.sidebar.slider(
    "Top P", min_value=0.0, max_value=1.0, step=0.01, value=1.0
)
config["max_tokens"] = st.sidebar.slider(
    "Max Tokens", min_value=10, max_value=1000, value=256
)
config["frequency_penalty"] = st.sidebar.slider(
    "Frequency Penalty", min_value=0.0, max_value=1.0, step=0.01, value=0.0
)
config["presence_penalty"] = st.sidebar.slider(
    "Presence Penalty", min_value=0.0, max_value=1.0, step=0.01, value=0.0
)
config["separator"] = st.sidebar.text_input("Separator", value="###")

system_prompt = "system_prompt_1"
exec(
    f"{system_prompt} = st.text_area('System Prompt #1', value='You are a helpful AI Assistant.')"
)

if "prompt_counter" not in st.session_state:
    st.session_state["prompt_counter"] = 0

if system_prompt_counter:
    st.session_state["prompt_counter"] += 1

for num in range(1, st.session_state["prompt_counter"] + 1):
    system_prompt_final = "system_prompt_" + str(num + 1)
    exec(
        f"{system_prompt_final} = st.text_area(f'System Prompt #{num+1}', value='You are a helpful AI Assistant.')"
    )

if st.session_state.get("prompt_counter") and st.session_state["prompt_counter"] >= 5:
    del st.session_state["prompt_counter"]
    st.rerun()


context = st.text_area("Context", value="")
question = st.text_area("Question", value="")
uploaded_file = st.file_uploader(
    "Choose a .csv file", help="Accept only .csv files", type="csv"
)

col1, col2, col3 = st.columns((3, 2.3, 1.5))

with col1:
    click_button = st.button(
        "Generate Result!", help="Result will be generated for only 1 question"
    )

with col2:
    csv_report_button = st.button(
        "Generate CSV Report!", help="Upload CSV file containing questions and contexts"
    )

with col3:
    empty_button = st.button("Empty Response!")


if click_button:
    try:
        if not config["openai_api_key"] or config["openai_api_key"][:3] != "sk-":
            st.error("OpenAI API Key is incorrect... Please, provide correct API Key.")
            sys.exit(1)
        else:
            openai.api_key = config["openai_api_key"]

        if st.session_state.get("prompt_counter"):
            counter = st.session_state["prompt_counter"] + 1
        else:
            counter = 1

        contexts_lst = context_chunking(context)
        answers_list = []
        for num in range(counter):
            system_prompt_final = "system_prompt_" + str(num + 1)
            answer_final = "answer_" + str(num + 1)

            if config["model_name"] in ["text-davinci-003", "gpt-3.5-turbo-instruct"]:
                user_prompt = generate_prompt(
                    eval(system_prompt_final), config["separator"], context, question
                )
                exec(f"{answer_final} = get_completion(config, user_prompt)")

            else:
                user_prompt = generate_chat_prompt(
                    config["separator"], context, question
                )
                exec(
                    f"{answer_final} = get_chat_completion(config, eval(system_prompt_final), user_prompt)"
                )

            answers_list.append(eval(answer_final))

            st.text_area(f"Answer #{str(num+1)}", value=eval(answer_final))

        if scalar_metrics:
            metrics_resp = ""
            progress_text = "Generation in progress. Please wait..."
            my_bar = st.progress(0, text=progress_text)

            for idx, ele in enumerate(scalar_metrics):
                my_bar.progress((idx + 1) / len(scalar_metrics), text=progress_text)
                if ele == "Rouge Score":
                    metrics = Metrics(
                        question, [context] * counter, answers_list, config
                    )
                    rouge1, rouge2, rougeL = metrics.rouge_score()
                    metrics_resp += (
                        f"Rouge1: {rouge1}, Rouge2: {rouge2}, RougeL: {rougeL}" + "\n"
                    )

                if ele == "BLEU Score":
                    metrics = Metrics(
                        question, [contexts_lst] * counter, answers_list, config
                    )
                    bleu = metrics.bleu_score()
                    metrics_resp += f"BLEU Score: {bleu}" + "\n"

                if ele == "BERT Score":
                    metrics = Metrics(
                        question, [context] * counter, answers_list, config
                    )
                    bert_f1 = metrics.bert_score()
                    metrics_resp += f"BERT F1 Score: {bert_f1}" + "\n"

            st.text_area("NLP Metrics:\n", value=metrics_resp)
            my_bar.empty()

        if llm_metrics:
            for num in range(counter):
                answer_final = "answer_" + str(num + 1)
                metrics = Metrics(
                    question, context, eval(answer_final), config, strictness
                )
                metrics_resp = ""

                progress_text = "Generation in progress. Please wait..."
                my_bar = st.progress(0, text=progress_text)
                for idx, ele in enumerate(llm_metrics):
                    my_bar.progress((idx + 1) / len(llm_metrics), text=progress_text)

                    if ele == "Answer Relevancy":
                        answer_relevancy_score = metrics.answer_relevancy()
                        metrics_resp += (
                            f"Answer Relevancy Score: {answer_relevancy_score}" + "\n"
                        )

                    if ele == "Critique":
                        critique_score = metrics.critique(criteria_dict[criteria])
                        metrics_resp += (
                            f"Critique Score for {criteria}: {critique_score}" + "\n"
                        )

                    if ele == "Faithfulness":
                        faithfulness_score = metrics.faithfulness()
                        metrics_resp += (
                            f"Faithfulness Score: {faithfulness_score}" + "\n"
                        )

                st.text_area(
                    f"RAI Metrics for Answer #{str(num+1)}:\n", value=metrics_resp
                )
                my_bar.empty()

    except Exception as e:
        func_name = traceback.extract_stack()[-1].name
        st.error(f"Error in {func_name}: {str(e)}")

if csv_report_button:
    if uploaded_file is not None:
        if not config["openai_api_key"] or config["openai_api_key"][:3] != "sk-":
            st.error("OpenAI API Key is incorrect... Please, provide correct API Key.")
            sys.exit(1)
        else:
            openai.api_key = config["openai_api_key"]

        if st.session_state.get("prompt_counter"):
            counter = st.session_state["prompt_counter"] + 1
        else:
            counter = 1

        cols = (
            ["Question", "Context", "Model Name", "HyperParameters"]
            + [f"System_Prompt_{i+1}" for i in range(counter)]
            + [f"Answer_{i+1}" for i in range(counter)]
            + [
                "Rouge Score",
                "BLEU Score",
                "BERT Score",
                "Answer Relevancy",
                "Faithfulness",
            ]
            + [f"Criteria_{criteria_name}" for criteria_name in criteria_dict.keys()]
        )

        final_df = generate_csv_report(
            uploaded_file, cols, criteria_dict, counter, config
        )

        if final_df and isinstance(final_df, pd.DataFrame):
            csv_file = final_df.to_csv(index=False).encode("utf-8")
            st.download_button(
                "Download Generated Report!",
                csv_file,
                "report.csv",
                "text/csv",
                key="download-csv",
            )

if empty_button:
    st.empty()
    st.cache_data.clear()
    st.cache_resource.clear()
    st.session_state["metrics_name"] = []
    st.rerun()