File size: 4,056 Bytes
dfbf21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import src.srf_bot as sb
import prompts.system_prompts as sp
from langchain_core.messages import HumanMessage

# Initialize the chatbot
chatbot = sb.SRFChatbot()

# Dictionary to store passages with identifiers
retrieved_passages = {}

# Define the respond function
def respond(query, history):
    formatted_query = [HumanMessage(content=query)]
    # Invoke the chatbot
    result = chatbot.graph.invoke({"messages": formatted_query}, chatbot.config)
    
    # Extract the assistant's response
    response = result["messages"][-1].content

    # Retrieve passages from your vector database based on the query
    # For the example, we'll use dummy passages
    passages = [
        "This is the full text of Passage 1.",
        "This is the full text of Passage 2.",
        "This is the full text of Passage 3."
    ]

    # Store passages with identifiers
    passage_ids = []
    for idx, passage in enumerate(passages):
        identifier = f"Passage {idx+1}"
        retrieved_passages[identifier] = passage
        passage_ids.append(identifier)

    # Reference passages in the response
    linked_response = f"{response}\n\nReferences:"
    for pid in passage_ids:
        linked_response += f" [{pid}]"

    # Append to history
    history.append((query, linked_response))
    return history, ""

# Function to get passage content based on selection
def get_passage_content(passage_id):
    return retrieved_passages.get(passage_id, "Passage not found.")

# Function to update the system prompt
def update_system_prompt(selected_prompt):
    # Update the chatbot's system prompt
    chatbot.reset_system_prompt(selected_prompt)
    # Update the displayed system prompt text
    return sp.system_prompt_templates[selected_prompt]

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# SRF Chatbot")

    with gr.Row():
        with gr.Column(scale=4):
            # Chatbot interface
            chatbot_output = gr.Chatbot()
            user_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
            submit_button = gr.Button("Submit")

        with gr.Column(scale=1):
            # Dropdown to select system prompts
            system_prompt_dropdown = gr.Dropdown(
                choices=list(sp.system_prompt_templates.keys()),
                label="Select Chatbot Instructions",
                value=list(sp.system_prompt_templates.keys())[0]
            )
            # Display the selected system prompt
            system_prompt_display = gr.Textbox(
                value=sp.system_prompt_templates[list(sp.system_prompt_templates.keys())[0]],
                label="Current Chatbot Instructions",
                lines=5,
                interactive=False
            )
            
            # Update system prompt display when a new prompt is selected
            system_prompt_dropdown.change(
                fn=update_system_prompt,
                inputs=[system_prompt_dropdown],
                outputs=[system_prompt_display]
            )
            
            # Passage selection and display
            gr.Markdown("### References")
            passage_selector = gr.Dropdown(label="Select a passage to view", choices=[])
            passage_display = gr.Markdown()

    # Update the chatbot when the submit button is clicked
    submit_button.click(
        fn=respond,
        inputs=[user_input, chatbot_output],
        outputs=[chatbot_output, user_input]
    )

    # Update the passage selector options when the chatbot output changes
    def update_passage_selector(chat_history):
        # Get the latest passages
        choices = list(retrieved_passages.keys())
        return gr.update(choices=choices)
    
    chatbot_output.change(
        fn=update_passage_selector,
        inputs=[chatbot_output],
        outputs=[passage_selector]
    )

    # Display the selected passage
    passage_selector.change(
        fn=get_passage_content,
        inputs=[passage_selector],
        outputs=[passage_display]
    )

demo.launch()