File size: 4,454 Bytes
60d3f7a
 
 
 
 
 
6fe12fb
 
60d3f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19e7a62
60d3f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fb3630
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
122
123
124
125
126
127
128
129
130
import streamlit as st
from run_model import generate_response, generate_RAG_response
from web_search import search_web
from deep_research import perform_deep_research
import tempfile

st.set_page_config(page_title="Chat with AI",
    page_icon="🚀",layout="wide")

def main():
    st.title("Chat with AI")
    

    with st.sidebar:
        st.title("Tools")
        
        option = st.selectbox(
            "Choose tools",
            ("Simple Chat", "Web Search", "Upload PDF", "Deep Web Search"),
        )

        temperature = st.slider(
            label="Temperature (controls randomness)",
            min_value=0.0,
            max_value=2.0,
            value=1.0,
            step=0.01,
            help="Lower = more deterministic, Higher = more random"
        )
        # Top-k sampling
        top_k = st.slider(
            label="Top-k (limits to top K tokens by probability)",
            min_value=0,
            max_value=100,
            value=50,
            step=1,
            help="0 = disable top-k filtering"
        )

        # Top-p (nucleus sampling)
        top_p = st.slider(
            label="Top-p (nucleus sampling cutoff)",
            min_value=0.0,
            max_value=1.0,
            value=0.9,
            step=0.01,
            help="0.0 = conservative, 1.0 = more random"
        )

        if option == "Upload PDF":
            file = st.file_uploader(label="Uploaded file will provide context to LLM", type="pdf")
            

        if option == "Web Search":
            st.write("Web Search Enabled for next query")
            # WB_SEARCH = True

        if option == "None":
            st.warning("You are not using any tool")

        if option == "Deep Web Search":
            st.write("Deep Web Research Enabled for next query")



    # col1, col2 = st.columns([6, 1], gap="small")

    # column 1
    # with col1:
    if "messages" not in st.session_state:
        st.session_state.messages = []
    
    for msg in st.session_state.messages:
        with st.chat_message(msg["role"]):
            st.markdown(msg["content"])

    # prompt = st.chat_input("Say something...")

    if prompt:= st.chat_input("Say something..."):
        # Display user message
        st.chat_message("user").markdown(prompt)
        st.session_state.messages.append({"role": "user", "content": prompt})
        if option == "Simple Chat":

            response = generate_response(history=st.session_state.messages, temperature=temperature, top_k=top_k, top_p=top_p)
            st.chat_message("assistant").markdown(response)
            # st.session_state.messages.append({"role": "assistant", "content": response})
        
        if option == "Web Search":
            st.session_state.messages.append([{"role": "user", "content" : prompt}])

            response, sources = search_web(prompt)
            # asnswer = response
            # st.chat_message("assistant").markdown(f"{response}\n\n###Sources\n{'\n'.join([source for source in sources])}")
            with st.chat_message("assistant"):
                st.markdown(f"{response}\n\n### Sources\n" + "\n".join(sources))

        if option == "Deep Web Search":
            st.session_state.messages.append([{"role": "user", "content" : prompt}])

            response = perform_deep_research(prompt)
            # asnswer = response
            # st.chat_message("assistant").markdown(f"{response}\n\n###Sources\n{'\n'.join([source for source in sources])}")
            with st.chat_message("assistant"):
                st.markdown(response)

        if option == "Upload PDF":
            st.session_state.messages.append([{"role": "user", "content" : prompt}])

            with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
                tmp_file.write(file.read())
                tmp_path = tmp_file.name
            print(tmp_path)
            response = generate_RAG_response(prompt, tmp_path, st.session_state.messages)
            # print(file)
            with st.chat_message("assistant"):
                st.markdown(response)
        
        st.session_state.messages.append({"role": "assistant", "content": response})
        
        
    # # Slider in column 2
    # with col2:
    #     temperature = st.slider("Temperature", 0.0, 1.0, 0.0)
    #     top_k = st.slider("Top k", 0.0, 100.0, 40.0)
    #     top_p = st.slider("Top p", 0.0, 1.0, 0.95)


if __name__ == "__main__":
    main()