File size: 1,423 Bytes
8b6196b
ca9a177
871255a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca9a177
 
 
 
871255a
ca9a177
 
871255a
 
 
 
ca9a177
 
 
 
 
 
 
 
 
871255a
8b6196b
871255a
 
 
 
ca9a177
871255a
 
 
ca9a177
 
 
 
8b6196b
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
from search import SemanticSearch, GoogleSearch, Document
import streamlit as st
from model import RAGModel, load_configs


def run_on_start():
    global r
    global configs
    configs = load_configs(config_file="rag.configs.yml")
    r = RAGModel(configs)


def search(query):
    g = GoogleSearch(query)
    data = g.all_page_data
    d = Document(data, min_char_len=configs["document"]["min_char_length"])
    st.session_state.doc = d.doc()[0]


st.title("LLM powred Google search")

if "messages" not in st.session_state:
    run_on_start()
    st.session_state.messages = []

if "doc" not in st.session_state:
    st.session_state.doc = None


for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])


if prompt := st.chat_input("Search Here insetad of Google"):
    st.chat_message("user").markdown(prompt)
    st.session_state.messages.append({"role": "user", "content": prompt})

    search(prompt)
    s, u = SemanticSearch(
        prompt,
        st.session_state.doc,
        configs["model"]["embeding_model"],
        configs["model"]["device"],
    )
    topk = s.semantic_search(query=prompt, k=32)
    output = r.answer_query(query=prompt, topk_items=topk)
    response = output
    with st.chat_message("assistant"):
        st.markdown(response)

    st.session_state.messages.append({"role": "assistant", "content": response})