File size: 2,602 Bytes
c379a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from uuid import uuid4

from langgraph.checkpoint.memory import MemorySaver
from langgraph.store.memory import InMemoryStore

from src.rag_lanchain import graph_builder


memory = MemorySaver()
in_memory_store = InMemoryStore()
graph = graph_builder.compile(checkpointer=memory, store=in_memory_store)


def respond(msg, config):
    role_dict = {"ai": "assistant", "human": "user"}
    if len(msg) == 0:
        gr.Warning("Chat messages cannot be empty")
        history = []
        for hist in graph.get_state_history(config):
            history = [{"role": role_dict.get(i.type, i.type), "content": i.content} for i in
                       hist.values["messages"]]
            break
        return "", history
    events = graph.stream(
        {"messages": [{"role": "user", "content": msg}]},
        config,
        stream_mode="values",
    )
    events = list(events)
    conversation = events[-1]["messages"]
    conversation = [{"role": role_dict.get(i.type, i.type), "content": i.content} for i in conversation]
    return "", conversation


def init_chat_state():
    return {"configurable": {"thread_id": str(uuid4()).replace('-', '_')}}


css = """
.centered-container {
    max-width: 1000px;
    margin: 0 auto;
}
"""

THEME = gr.themes.Ocean()

demo = gr.Blocks(theme=THEME, fill_width=False, fill_height=True, css=css)

with demo:
    config_state = gr.State(init_chat_state)
    with gr.Column(elem_classes="centered-container"):
        gr.Markdown("""
        # 💬 Polars Python Chatbot
        ### Ask anything about the [Polars](https://pola-rs.github.io/polars/) Python package!  
        ### This chatbot uses a database of embeddings generated from the official documentation to help you find accurate and relevant answers about using Polars for data manipulation in Python.
        """)

        chatbot = gr.Chatbot(
            label=None,
            type="messages",
            show_label=False,
            height=400,
        )

        with gr.Row(equal_height=True):
            msg = gr.Textbox(
                placeholder="Type your message here...",
                show_label=False,
                lines=3,
                max_lines=3,
                scale=5,
            )
            send_btn = gr.Button("Send", variant="primary", scale=1)

        with gr.Row():
            clear = gr.ClearButton([msg, chatbot], value="Clear Chat", variant="secondary")

    send_btn.click(respond, [msg, config_state], [msg, chatbot])
    msg.submit(respond, [msg, config_state], [msg, chatbot])

if __name__ == '__main__':
    demo.launch()