TillLangbein commited on
Commit
c3057bf
·
1 Parent(s): 80e25da

Add application file

Browse files
.cache.db ADDED
Binary file (332 kB). View file
 
__pycache__/app.cpython-312.pyc ADDED
Binary file (173 Bytes). View file
 
__pycache__/prompts.cpython-312.pyc ADDED
Binary file (3.56 kB). View file
 
app.py ADDED
@@ -0,0 +1,494 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import getpass
2
+ import os
3
+ import random
4
+
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain_core.globals import set_llm_cache
7
+ from langchain_community.cache import SQLiteCache
8
+ from langchain_community.vectorstores import FAISS
9
+ from langchain_openai import OpenAIEmbeddings
10
+ from langgraph.graph import END, StateGraph, START
11
+ from langchain_core.output_parsers import StrOutputParser
12
+
13
+ from typing import List
14
+ from typing_extensions import TypedDict
15
+ import gradio as gr
16
+ from pydantic import BaseModel, Field
17
+
18
+ from prompts import IMPROVE_PROMPT, RELEVANCE_PROMPT, ANSWER_PROMPT, HALLUCINATION_PROMPT, RESOLVER_PROMPT, REWRITER_PROMPT
19
+
20
+ TOPICS = [
21
+ "ICT strategy management",
22
+ "IT governance management & internal controls system",
23
+ "Internal audit & compliance management",
24
+ "ICT asset & architecture management",
25
+ "ICT risk management",
26
+ "Information security & human resource security management",
27
+ "IT configuration management",
28
+ "Cryptography, certificates & key management",
29
+ "Secure network & infrastructure management",
30
+ "Backup",
31
+ "Security testing",
32
+ "Threat-led penetration testing",
33
+ "Logging",
34
+ "Data and ICT system security",
35
+ "Physical and environmental security",
36
+ "Vulnerability & patch management",
37
+ "Identity and access management",
38
+ "ICT change management",
39
+ "IT project & project portfolio management",
40
+ "Acquisition, development & maintenance of ICT systems & EUA",
41
+ "ICT incident management",
42
+ "Monitoring, availability, capacity & performance management",
43
+ "ICT outsourcing & third-party risk management",
44
+ "Subcontracting management",
45
+ "ICT provider & service level management",
46
+ "ICT business continuity management"
47
+ ]
48
+
49
+ class GradeDocuments(BaseModel):
50
+ """Binary score for relevance check on retrieved documents."""
51
+
52
+ binary_score: str = Field(
53
+ description="Documents are relevant to the question, 'yes' or 'no'"
54
+ )
55
+
56
+ class GradeHallucinations(BaseModel):
57
+ """Binary score for hallucination present in generation answer."""
58
+
59
+ binary_score: str = Field(
60
+ description="Answer is grounded in the facts, 'yes' or 'no'"
61
+ )
62
+
63
+ class GradeAnswer(BaseModel):
64
+ """Binary score to assess answer addresses question."""
65
+
66
+ binary_score: str = Field(
67
+ description="Answer addresses the question, 'yes' or 'no'"
68
+ )
69
+
70
+ class GraphState(TypedDict):
71
+ """
72
+ Represents the state of our graph.
73
+
74
+ Attributes:
75
+ question: question
76
+ generation: LLM generation
77
+ documents: list of documents
78
+ """
79
+
80
+ question: str
81
+ selected_sources: List[List[bool]]
82
+ generation: str
83
+ documents: List[str]
84
+ fitting_documents: List[str]
85
+ dora_docs: List[str]
86
+ dora_rts_docs: List[str]
87
+ dora_news_docs: List[str]
88
+
89
+ def _set_env(var: str):
90
+ if os.environ.get(var):
91
+ return
92
+ os.environ[var] = getpass.getpass(var + ":")
93
+
94
+ def load_vectorstores(paths: list):
95
+ # The dora vectorstore
96
+ embd = OpenAIEmbeddings()
97
+
98
+ vectorstores = [FAISS.load_local(path, embd, allow_dangerous_deserialization=True) for path in paths]
99
+ retrievers = [vectorstore.as_retriever(search_type="mmr", search_kwargs={
100
+ "k": 7,
101
+ "fetch_k": 10,
102
+ "score_threshold": 0.7,
103
+ }) for vectorstore in vectorstores]
104
+
105
+ return retrievers
106
+
107
+ # Put all chains in fuctions
108
+ def dora_rewrite(state):
109
+ """
110
+ Rewrites the question to fit dora wording
111
+
112
+ Args:
113
+ state (dict): The current graph state
114
+
115
+ Returns:
116
+ state (dict): New key added to state, documents, that contains retrieved documents
117
+ """
118
+ print("---TRANSLATE TO DORA---")
119
+ question = state["question"]
120
+
121
+ new_question = dora_question_rewriter.invoke({"question": question, "topics": TOPICS})
122
+
123
+ if new_question == "Thats an interesting question, but I dont think I can answer it based on my Dora knowledge.":
124
+ return {"question": new_question, "generation": new_question}
125
+ else:
126
+ return {"question": new_question}
127
+
128
+ def retrieve(state):
129
+ """
130
+ Retrieve documents
131
+
132
+ Args:
133
+ state (dict): The current graph state
134
+
135
+ Returns:
136
+ state (dict): New key added to state, documents, that contains retrieved documents
137
+ """
138
+ print("---RETRIEVE---")
139
+ question = state["question"]
140
+ selected_sources = state["selected_sources"]
141
+
142
+ # Retrieval
143
+ documents = []
144
+ if selected_sources[0]:
145
+ documents.extend(dora_retriever.invoke(question))
146
+ if selected_sources[1]:
147
+ documents.extend(dora_rts_retriever.invoke(question))
148
+ if selected_sources[2]:
149
+ documents.extend(dora_news_retriever.invoke(question))
150
+
151
+ return {"documents": documents, "question": question}
152
+
153
+ def grade_documents(state):
154
+ """
155
+ Determines whether the retrieved documents are relevant to the question.
156
+
157
+ Args:
158
+ state (dict): The current graph state
159
+
160
+ Returns:
161
+ state (dict): Updates documents key with only filtered relevant documents
162
+ """
163
+
164
+ print("---CHECK DOCUMENTS RELEVANCE TO QUESTION---")
165
+ question = state["question"]
166
+ documents = state["documents"]
167
+ fitting_documents = state["fitting_documents"] if "fitting_documents" in state else []
168
+
169
+
170
+ # Score each doc
171
+ for d in documents:
172
+ score = retrieval_grader.invoke(
173
+ {"question": question, "document": d.page_content}
174
+ )
175
+ grade = score.binary_score
176
+ if grade == "yes":
177
+ #print("---GRADE: DOCUMENT RELEVANT---")
178
+ if d in fitting_documents:
179
+ #print(f"---Document {d.page_content} already in fitting documents---")
180
+ continue
181
+ fitting_documents.append(d)
182
+ else:
183
+ #print("---GRADE: DOCUMENT NOT RELEVANT---")
184
+ continue
185
+
186
+ return {"fitting_documents": fitting_documents}
187
+
188
+ def generate(state):
189
+ """
190
+ Generate answer
191
+
192
+ Args:
193
+ state (dict): The current graph state
194
+
195
+ Returns:
196
+ state (dict): New key added to state, generation, that contains LLM generation
197
+ """
198
+ print("---GENERATE---")
199
+ question = state["question"]
200
+ fitting_documents = state["fitting_documents"]
201
+
202
+ dora_docs = [d for d in fitting_documents if d.metadata["source"].startswith("Dora")]
203
+ dora_rts_docs = [d for d in fitting_documents if d.metadata["source"].startswith("Commission")]
204
+ dora_news_docs = [d for d in fitting_documents if d.metadata["source"].startswith("https")]
205
+
206
+ # RAG generation
207
+ generation = answer_chain.invoke({"context": fitting_documents, "question": question})
208
+ return {"generation": generation, "dora_docs": dora_docs, "dora_rts_docs": dora_rts_docs, "dora_news_docs": dora_news_docs}
209
+
210
+ def transform_query(state):
211
+ """
212
+ Transform the query to produce a better question.
213
+
214
+ Args:
215
+ state (dict): The current graph state
216
+
217
+ Returns:
218
+ state (dict): Updates question key with a re-phrased question
219
+ """
220
+
221
+ print("---TRANSFORM QUERY---")
222
+ question = state["question"]
223
+
224
+ # Re-write question
225
+ better_question = question_rewriter.invoke({"question": question})
226
+ print(f"{better_question =}")
227
+ return {"question": better_question}
228
+
229
+ ### Edges ###
230
+ def suitable_question(state):
231
+ """
232
+ Determines whether the question is suitable.
233
+
234
+ Args:
235
+ state (dict): The current graph state
236
+
237
+ Returns:
238
+ str: Binary decision for next node to call
239
+ """
240
+
241
+ print("---ASSESSING THE QUESTION---")
242
+ question = state["question"]
243
+ #print(f"{question = }")
244
+ if question == "Thats an interesting question, but I dont think I can answer it based on my Dora knowledge.":
245
+ return "end"
246
+ else:
247
+ return "retrieve"
248
+
249
+ def decide_to_generate(state):
250
+ """
251
+ Determines whether to generate an answer, or re-generate a question.
252
+
253
+ Args:
254
+ state (dict): The current graph state
255
+
256
+ Returns:
257
+ str: Binary decision for next node to call
258
+ """
259
+
260
+ print("---ASSESS GRADED DOCUMENTS---")
261
+ fitting_documents = state["fitting_documents"]
262
+
263
+ if not fitting_documents:
264
+ # All documents have been filtered check_relevance
265
+ # We will re-generate a new query
266
+ print(
267
+ "---DECISION: ALL DOCUMENTS ARE IRRELEVANT TO QUESTION, TRANSFORM QUERY---"
268
+ )
269
+ return "transform_query"
270
+ else:
271
+ # We have relevant documents, so generate answer
272
+ print(f"---DECISION: GENERATE WITH {len(fitting_documents)} DOCUMENTS---")
273
+ return "generate"
274
+
275
+ def grade_generation_v_documents_and_question(state):
276
+ """
277
+ Determines whether the generation is grounded in the document and answers question.
278
+
279
+ Args:
280
+ state (dict): The current graph state
281
+
282
+ Returns:
283
+ str: Decision for next node to call
284
+ """
285
+
286
+ print("---CHECK HALLUCINATIONS---")
287
+ question = state["question"]
288
+ fitting_documents = state["fitting_documents"]
289
+ generation = state["generation"]
290
+
291
+ score = hallucination_grader.invoke(
292
+ {"documents": fitting_documents, "generation": generation}
293
+ )
294
+ grade = score.binary_score
295
+
296
+ # Check hallucination
297
+ if grade == "yes":
298
+ print("---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---")
299
+ # Check question-answering
300
+ print("---GRADE GENERATION vs QUESTION---")
301
+ score = answer_grader.invoke({"question": question, "generation": generation})
302
+ grade = score.binary_score
303
+ if grade == "yes":
304
+ print("---DECISION: GENERATION ADDRESSES QUESTION---")
305
+ return "useful"
306
+ else:
307
+ print("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
308
+ return "not useful"
309
+ else:
310
+ for document in fitting_documents:
311
+ print(document.page_content)
312
+ print("---DECISION: THOSE DOCUMENTS ARE NOT GROUNDING THIS GENERATION---")
313
+ print(f"{generation = }")
314
+ return "not supported"
315
+
316
+ def compile_graph():
317
+ workflow = StateGraph(GraphState)
318
+ # Define the nodes
319
+ workflow.add_node("dora_rewrite", dora_rewrite) # retrieve
320
+ workflow.add_node("retrieve", retrieve) # retrieve
321
+ workflow.add_node("grade_documents", grade_documents) # grade documents
322
+ workflow.add_node("generate", generate) # generate
323
+ workflow.add_node("transform_query", transform_query) # transform_query
324
+ # Define the edges
325
+ workflow.add_edge(START, "dora_rewrite")
326
+ workflow.add_conditional_edges(
327
+ "dora_rewrite",
328
+ suitable_question,
329
+ {
330
+ "retrieve": "retrieve",
331
+ "end": END,
332
+ },
333
+ )
334
+ workflow.add_edge("retrieve", "grade_documents")
335
+ workflow.add_conditional_edges(
336
+ "grade_documents",
337
+ decide_to_generate,
338
+ {
339
+ "transform_query": "transform_query",
340
+ "generate": "generate",
341
+ },
342
+ )
343
+ workflow.add_edge("transform_query", "retrieve")
344
+ workflow.add_conditional_edges(
345
+ "generate",
346
+ grade_generation_v_documents_and_question,
347
+ {
348
+ "not supported": "generate",
349
+ "useful": END,
350
+ "not useful": "transform_query",
351
+ },
352
+ )
353
+ # Compile
354
+ app = workflow.compile()
355
+ return app
356
+
357
+ # Function to interact with Gradio
358
+ def generate_response(question: str, dora: bool, rts: bool, news: bool):
359
+ selected_sources = [dora, rts, news] if any([dora, rts, news]) else [True, False, False]
360
+ state = app.invoke({"question": question, "selected_sources": selected_sources})
361
+ return (
362
+ state["generation"],
363
+ ('\n\n'.join([f"***{doc.metadata["source"]} section {doc.metadata['section']}***: {doc.page_content}" for doc in state["dora_docs"]])) if "dora_docs" in state and state["dora_docs"] else 'No documents available.',
364
+ ('\n\n'.join([f"***{doc.metadata["source"]}, section {doc.metadata['section']}***: {doc.page_content}" for doc in state["dora_rts_docs"]])) if "dora_rts_docs" in state and state["dora_rts_docs"] else 'No documents available.',
365
+ ('\n\n'.join([f"***{doc.metadata["source"]}***: {doc.page_content}" for doc in state["dora_news_docs"]])) if "dora_news_docs" in state and state["dora_news_docs"] else 'No documents available.',
366
+ )
367
+
368
+ def show_loading(prompt: str):
369
+ return [prompt, "loading", "loading", "loading", "loading"]
370
+
371
+ def on_click():
372
+ return "I would love to hear your opinion: \[email protected]"
373
+
374
+ def clear_results():
375
+ return "", "", "", "", ""
376
+
377
+ def random_prompt():
378
+ return random.choice([
379
+ "Was ist der Unterschied zwischen TIBER-EU und DORA TLPT?",
380
+ "Ich möchte ein SIEM einführen. Bitte gib mir eine Checkliste, was ich beachten muss.",
381
+ "Was ist der Geltungsbereich der DORA? Bin ich als Finanzdienstleister im Leasinggeschäft betroffen?",
382
+ "Ich hatte einen Ransomwarevorfall mit erheblichen Auswirkungen auf den Geschäftsbetrieb. Muss ich etwas melden?",
383
+ "Was ist dieses DORA überhaupt?"
384
+ ])
385
+
386
+ def load_css():
387
+ with open('style.css', 'r') as file:
388
+ return file.read()
389
+
390
+ def run_gradio():
391
+ with gr.Blocks(title='Artificial Compliance', theme=gr.themes.Monochrome(), css=load_css(), fill_width=True, fill_height=True,) as gradio_ui:
392
+ # Adding a sliding navbar
393
+ with gr.Column(scale=1, elem_id='navbar'):
394
+ gr.Image(
395
+ '..\\deployment\\logo.png',
396
+ interactive=False,
397
+ show_label=False,
398
+ scale=1,
399
+ width="50%",
400
+ height="50%"
401
+ )
402
+ with gr.Column():
403
+ dora_chatbot_button = gr.Checkbox(label="Dora", value=True, elem_classes=["navbar-button"])
404
+ document_workbench_button = gr.Checkbox(label="Published RTS documents", value=True, elem_classes=["navbar-button"])
405
+ newsfeed_button = gr.Checkbox(label="Bafin documents", value=True, elem_classes=["navbar-button"])
406
+ question_prompt = gr.Textbox(
407
+ value=random_prompt(),
408
+ label='What you always wanted to know about Dora:',
409
+ elem_classes=['textbox'],
410
+ lines=6
411
+ )
412
+ with gr.Row():
413
+ clear_results_button = gr.Button('Clear Results', variant='secondary', size="m")
414
+ submit_button = gr.Button('Submit', variant='primary', size="m")
415
+
416
+ # Adding a header
417
+ gr.Markdown("# The Doracle", elem_id="header")
418
+ gr.Markdown("----------------------------------------------------------------------------")
419
+ display_prompt = gr.Markdown(
420
+ value="",
421
+ label="question_prompt",
422
+ elem_id="header"
423
+ )
424
+ gr.Markdown("----------------------------------------------------------------------------")
425
+
426
+ with gr.Column(scale=1):
427
+ with gr.Row(elem_id='text_block'):
428
+ llm_generation = gr.Markdown(label="LLM Generation", elem_id="llm_generation")
429
+
430
+ gr.Markdown("----------------------------------------------------------------------------")
431
+
432
+ with gr.Row(elem_id='text_block'):
433
+ dora_documents = gr.Markdown(label="DORA Documents")
434
+ dora_rts_documents = gr.Markdown(label="DORA RTS Documents")
435
+ dora_news_documents = gr.Markdown(label="Bafin supporting Documents")
436
+
437
+ # Adding a footer with impressum and contact
438
+ with gr.Row(elem_classes="footer"):
439
+ gr.Markdown("Contact", elem_id="clickable_markdown")
440
+ invisible_btn = gr.Button("", elem_id="invisible_button")
441
+
442
+ gr.on(
443
+ triggers=[question_prompt.submit, submit_button.click],
444
+ inputs=[question_prompt],
445
+ outputs=[display_prompt, llm_generation, dora_documents, dora_rts_documents, dora_news_documents],
446
+ fn=show_loading
447
+ ).then(
448
+ outputs=[llm_generation, dora_documents, dora_rts_documents, dora_news_documents],
449
+ inputs=[question_prompt, dora_chatbot_button, document_workbench_button, newsfeed_button],
450
+ fn=generate_response
451
+ )
452
+
453
+ # Use gr.on() with the invisible button's click event
454
+ gr.on(
455
+ triggers=[invisible_btn.click],
456
+ fn=on_click,
457
+ outputs=[llm_generation]
458
+ )
459
+
460
+ # Clearing out all results when the appropriate button is clicked
461
+ clear_results_button.click(fn=clear_results, outputs=[display_prompt, llm_generation, dora_documents, dora_rts_documents, dora_news_documents])
462
+
463
+ gradio_ui.launch()
464
+
465
+
466
+ if __name__ == "__main__":
467
+ _set_env("OPENAI_API_KEY")
468
+ set_llm_cache(SQLiteCache(database_path=".cache.db"))
469
+
470
+ dora_retriever, dora_rts_retriever, dora_news_retriever = load_vectorstores(
471
+ ["./dora_vectorstore_data_faiss.vst",
472
+ "./rts_eur_lex_vectorstore_faiss.vst",
473
+ "./bafin_news_vectorstore_faiss.vst",]
474
+ )
475
+
476
+ fast_llm = ChatOpenAI(model="gpt-3.5-turbo")
477
+ smart_llm = ChatOpenAI(model="gpt-4-turbo", temperature=0.2, max_tokens=4096)
478
+ tool_llm = ChatOpenAI(model="gpt-4o")
479
+ rewrite_llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=1, cache=False)
480
+
481
+ dora_question_rewriter = IMPROVE_PROMPT | tool_llm | StrOutputParser()
482
+ retrieval_grader = RELEVANCE_PROMPT | fast_llm.with_structured_output(GradeDocuments)
483
+ answer_chain = ANSWER_PROMPT | smart_llm | StrOutputParser() #former RAG chain
484
+ hallucination_grader = HALLUCINATION_PROMPT | fast_llm.with_structured_output(GradeHallucinations)
485
+ answer_grader = RESOLVER_PROMPT | fast_llm.with_structured_output(GradeAnswer)
486
+ question_rewriter = REWRITER_PROMPT | rewrite_llm | StrOutputParser()
487
+
488
+ app = compile_graph()
489
+
490
+ # And finally, run the app
491
+ run_gradio()
492
+
493
+
494
+
logo.png ADDED
prompts.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate
2
+
3
+ # Rewriting process using ChatPromptTemplate
4
+ IMPROVE_PROMPT = ChatPromptTemplate.from_messages(
5
+ [
6
+ ("system", """
7
+ You are a question rewriter that optimizes an input question for better retrieval from vectorstore data containing information security regulatory texts, especially tha digital operations security acrt (DORA).
8
+ The regulatory texts in the vectorstore mainly address the following topics: {topics}.
9
+ Your goal is to understand the underlying semantic intent of the input question and reformulate it to improve clarity and relevance for retrieving content on {topics}.
10
+ If the question is not related to any of the topics or information security regulatory texts, simply answer: "Thats an interesting question, but I dont think I can answer it based on my Dora knowledge."
11
+ """),
12
+ (
13
+ "human",
14
+ "Here is the initial question: \n\n{question} \nPlease formulate an improved version of the question, focusing on clarity and retrieval optimization."
15
+ ),
16
+ ]
17
+ )
18
+
19
+ RELEVANCE_PROMPT = ChatPromptTemplate.from_messages(
20
+ [
21
+ ("system", """You are a grader assessing relevance of a retrieved document to a user question. \n
22
+ If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant. \n
23
+ It does not need to be a stringent test. The goal is to filter out erroneous retrievals. \n
24
+ Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question."""
25
+ ),
26
+ ("human", "Retrieved document: \n\n {document} \n\n User question: {question}"),
27
+ ]
28
+ )
29
+
30
+ ANSWER_PROMPT = ChatPromptTemplate.from_messages(
31
+ [
32
+ (
33
+ "system",
34
+ "You are a highly experienced IT auditor, specializing in information security and regulatory compliance. Your task is to assist a colleague who has approached you with a question."
35
+ " You have access to relevant context, provided here: {context}."
36
+ " Please respond with a clear, concise, and precise answer, strictly based on the provided context. Ensure your response is accurate and always cite sources from the context."
37
+ " Do not introduce any new information or alter the context in any way."
38
+ ),
39
+ ("user", "{question}"),
40
+ ]
41
+ )
42
+
43
+ HALLUCINATION_PROMPT = ChatPromptTemplate.from_messages(
44
+
45
+ [
46
+ ("system", """You are a grader assessing whether an LLM generation is grounded in / supported by a set of retrieved facts. \n
47
+ Give a binary score 'yes' or 'no'. 'Yes' means that the answer is grounded in / supported by the set of facts."""),
48
+ ("human", "Set of facts: \n\n {documents} \n\n LLM generation: {generation}"),
49
+ ]
50
+ )
51
+
52
+ RESOLVER_PROMPT = ChatPromptTemplate.from_messages(
53
+ [
54
+ ("system", """You are a grader assessing whether an answer addresses / resolves a question \n
55
+ Give a binary score 'yes' or 'no'. Yes' means that the answer resolves the question."""),
56
+ ("human", "User question: \n\n {question} \n\n LLM generation: {generation}"),
57
+ ]
58
+ )
59
+
60
+ REWRITER_PROMPT = ChatPromptTemplate.from_messages(
61
+ [
62
+ ("system", """You a question re-writer that converts an input question to a better version that is optimized \n
63
+ for vectorstore retrieval. Look at the input and try to reason about the underlying semantic intent / meaning."""),
64
+ (
65
+ "human",
66
+ "Here is the initial question: \n\n {question} \n Formulate an improved question.",
67
+ ),
68
+ ]
69
+ )
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio==5.4.0
2
+ langchain_community==0.3.4
3
+ langchain_core==0.3.14
4
+ langchain_openai==0.2.4
5
+ langgraph==0.2.41
6
+ pydantic==2.9.2
7
+ typing_extensions==4.12.2
style.css ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* style.css */
2
+ .gradio-container {
3
+ width: 100% !important;
4
+ max-width: none !important;
5
+ background-color: #D3D3D3;
6
+ }
7
+ #navbar {
8
+ position: fixed;
9
+ left: -190px;
10
+ top: 0;
11
+ width: 200px;
12
+ height: 100%;
13
+ background-color: #444;
14
+ transition: left 0.3s ease;
15
+ padding: 10px;
16
+ box-shadow: 2px 0 5px rgba(0, 0, 0, 0.5);
17
+ z-index: 1000;
18
+ }
19
+ #navbar:hover {
20
+ left: 0;
21
+ }
22
+ .navbar-button {
23
+ background-color: #D3D3D3;
24
+ color: #fff;
25
+ margin-bottom: 10px;
26
+ width: 100%;
27
+ }
28
+ .footer {
29
+ text-align: center;
30
+ padding: 10px;
31
+ background-color: #ffffff;
32
+ margin-top: 20px;
33
+ }
34
+ #header {
35
+ text-align: center;
36
+ font-size: 1.8em;
37
+ font-weight: bold;
38
+ margin-top: 20px;
39
+ }
40
+ #text_block {
41
+ margin-left: 150px;
42
+ margin-right: 150px;
43
+ max-width: calc(100% - 300px);
44
+ box-sizing: border-box;
45
+ overflow-x: hidden;
46
+ word-wrap: break-word;
47
+ }
48
+ #llm_output {
49
+ width: 100%;
50
+ box-sizing: border-box;
51
+ padding: 10px;
52
+ }
53
+ #clickable_markdown {
54
+ position: relative;
55
+ cursor: pointer;
56
+ }
57
+ #invisible_button {
58
+ position: absolute;
59
+ top: 0;
60
+ left: 0;
61
+ width: 100%;
62
+ height: 100%;
63
+ opacity: 0;
64
+ cursor: pointer;
65
+ }