Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (68 loc) · 3.07 KB
/
Copy pathapp.py
File metadata and controls
89 lines (68 loc) · 3.07 KB
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
from pathlib import PurePath
import gradio as gr
from dotenv import load_dotenv
from config import KNOWLEDGE_BASE_DIR_NAME
from pipelines import DEFAULT_PIPELINE, PIPELINE_CHOICES, get_pipeline
load_dotenv(override=True)
def display_source(source: str) -> str:
"""
Show the source path relative to the knowledge base folder, hiding the local directory it was ingested from.
"""
parts = PurePath(source).parts
if KNOWLEDGE_BASE_DIR_NAME not in parts:
return source
return "/".join(parts[parts.index(KNOWLEDGE_BASE_DIR_NAME) + 1:])
def format_context(context_chunks):
formatted = "<h2 style='color: #ff7800;'>Relevant Context</h2>\n\n"
for chunk in context_chunks:
formatted += f"<span style='color: #ff7800;'>Source: {display_source(chunk.metadata['source'])}</span>\n\n"
formatted += chunk.page_content + "\n\n"
return formatted
def content_to_text(content) -> str:
"""
Gradio 6 stores message content as a list of blocks, e.g. [{"type": "text", "text": "..."}].
"""
if isinstance(content, str):
return content
return "\n".join(block["text"] for block in content if block.get("type") == "text")
def chat(history, pipeline_name):
messages = [{"role": message["role"], "content": content_to_text(message["content"])} for message in history]
latest_question = messages[-1]["content"]
prior_messages = messages[:-1]
answer, context_chunks = get_pipeline(pipeline_name).answer_question(latest_question, prior_messages)
history.append({"role": "assistant", "content": answer})
return history, format_context(context_chunks)
def main():
def put_message_in_chatbot(message, history):
return "", history + [{"role": "user", "content": message}]
theme = gr.themes.Soft(font=["Inter", "system-ui", "sans-serif"])
with gr.Blocks(title="Insurellm Expert Assistant") as ui:
gr.Markdown("# Insurellm Expert Assistant\nAsk me anything about Insurellm!")
pipeline_selector = gr.Radio(
choices=PIPELINE_CHOICES,
value=DEFAULT_PIPELINE,
label="RAG Pipeline",
)
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(
label="Conversation", height=600, buttons=["copy"]
)
question_box = gr.Textbox(
label="Your Question",
placeholder="Ask anything about Insurellm...",
show_label=False,
)
with gr.Column(scale=1):
context_markdown = gr.Markdown(
label="Retrieved Context",
value="*Retrieved context will appear here*",
container=True,
height=600,
)
question_box.submit(
put_message_in_chatbot, inputs=[question_box, chatbot], outputs=[question_box, chatbot]
).then(chat, inputs=[chatbot, pipeline_selector], outputs=[chatbot, context_markdown])
ui.launch(inbrowser=True, theme=theme)
if __name__ == "__main__":
main()