File size: 7,325 Bytes
a4200f5
 
 
 
 
 
 
0090bd8
a4200f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b58084d
2b8356a
f185462
2b8356a
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from utlis.helper import *

initialize_session_state()

with st.sidebar:
    st.title("Smart Retrieval")
    # Get List of models
    llms = ['Cohere','Gemini-Pro','Mistral-7B-Instruct-v0.3','gemma-2b','Meta-Llama-3-8B-Instruct','Phi-3-mini-4k-instruct','zephyr-7b-beta']
    st.session_state.llm = st.selectbox("Choose LLM",llms)
    genre = st.radio(
    "Choose option",
    ["Select document(s)", "Add document(s)","Delete service(s)", "Delete document(s)"])
    
    if genre=="Add document(s)":
        st.title('Add Document(s)')
        # Check service status
        # Get all available services
        add_new_service = st.checkbox("Add new service")
        if add_new_service:
            new_service = st.text_input("Enter service name")
            # Get list of Embedding models
            res_request= requests.get(EMBEDDING_MODELS_API)
            embidding_models =json.loads(res_request.text)
            embdding_model = st.selectbox("Choose Embidding model",embidding_models["Model_Names_paid"])
            if  new_service and st.button('Add'):
                add_service(st.session_state.token,new_service, embdding_model)

        services =  requests.get(SERVICES_API+st.session_state.token)
        services =json.loads(services.text)
        if len(services)>0:
           st.session_state.service = st.selectbox("Choose Service",services)

        # Get list of Indexing methods
        # indexing_method_list = ['FLAT','HSNW']
        # st.session_state.indexing_method = st.selectbox("Choose Indexing method",indexing_method_list)
        # Send Document to API
        if st.session_state.service:
            st.session_state.uploaded_files = st.file_uploader("Upload PDF files",  type=["pdf", "png", "jpg", "jpeg"], accept_multiple_files=True)
            if st.session_state.uploaded_files:
                st.session_state.process = st.button('Process')
                if st.session_state.process:
                    add_document(st.session_state.token,st.session_state.service)

    elif genre=="Select document(s)":
        st.title('Chat with Document(s)')
        services =  requests.get(SERVICES_API+st.session_state.token)
        services =json.loads(services.text)

        if len(services)>0:
            st.session_state.service_slected_to_chat = st.selectbox("Choose Service",services)
            st.session_state.top_k = st.number_input("Top k ", min_value=1, value=5)
            history_document = requests.get(DOCUMENT_API+f'/{st.session_state.token}/{st.session_state.service_slected_to_chat}')
            history_document =json.loads(history_document.text).get("documents",[])
            if len(history_document)>=2:
                history_document.append("ALL")
            # Get list of documents from histrory
            if "ALL" in history_document:
                st.session_state.doument_slected_to_chat = st.multiselect(
                    "",history_document ,default="ALL"
                    )
            elif len(history_document)==1:
                st.session_state.doument_slected_to_chat = st.multiselect(
                    "",history_document,default=history_document[0]
                    )
            else:
                st.session_state.doument_slected_to_chat = st.multiselect(
                    "",history_document
                    )
            if "ALL" in st.session_state.doument_slected_to_chat:
                st.session_state.doument_slected_to_chat = history_document
                st.session_state.doument_slected_to_chat.remove("ALL")
            st.write("You selected:", st.session_state.doument_slected_to_chat)
    elif genre == "Delete service(s)":
        st.title('Delete Service(s)')
        services =  requests.get(SERVICES_API+st.session_state.token)
        services =json.loads(services.text)
        if len(services)>=2:
            services.append("ALL")
            # Get list of documents from histrory
        if "ALL" in services:
            service_slected = st.multiselect(
                    "",services ,default="ALL"
                    )
        elif len(services)==1:
            service_slected = st.multiselect(
                    "",services,default=services[0]
                    )
        else:
            service_slected = st.multiselect(
                    "",services
                    )
        if "ALL" in service_slected:
            service_slected = services
            service_slected.remove("ALL")
        st.write("You selected:", service_slected)

        if len(service_slected) > 0:
            st.session_state.delete = st.button('Delete')
            if st.session_state.delete:
                delete_service(st.session_state.token ,service_slected)
        
    elif genre == "Delete document(s)":
        st.title('Delete Document(s)')
        services =  requests.get(SERVICES_API+st.session_state.token)
        services =json.loads(services.text)
        if len(services)>0:
            service = st.selectbox("Choose Service",services)
            history_document = requests.get(DOCUMENT_API+f'/{st.session_state.token}/{service}')
            history_document =json.loads(history_document.text).get("documents",[])

            if len(history_document)>=2:
                history_document.append("ALL")
            # Get list of documents from histrory
            if "ALL" in history_document:
                document_slected_to_delete = st.multiselect(
                    "",history_document ,default="ALL"
                    )
            elif len(history_document)==1:
                document_slected_to_delete = st.multiselect(
                    "",history_document,default=history_document[0]
                    )
            else:
                document_slected_to_delete = st.multiselect(
                    "",history_document
                    )
            if "ALL" in document_slected_to_delete:
                document_slected_to_delete = history_document
                document_slected_to_delete.remove("ALL")

            st.write("You selected:", document_slected_to_delete)
            if len(document_slected_to_delete) > 0:
                st.session_state.delete = st.button('Delete')
                if st.session_state.delete:
                    delete_document(st.session_state.token,st.session_state.service ,document_slected_to_delete)




for msg in st.session_state.messages:
    if msg["role"] == "user":
        st.chat_message(msg["role"], avatar="πŸ§‘β€πŸ’»").write(msg["content"])
    else:
        st.chat_message(msg["role"], avatar="πŸ€–").write(msg["content"])

if prompt := st.chat_input():
    st.session_state.messages.append({"role": "user", "content": prompt})
    st.chat_message("user", avatar="πŸ§‘β€πŸ’»").write(prompt)

    context = get_context(prompt,st.session_state.token,st.session_state.service_slected_to_chat,st.session_state.top_k)
    template = " "
    for i in range(0,len(context)):
        template += f"Chunk{i}: "+context[i] + "\n"
    print(template)
    response=generate_response(st.session_state.llm, prompt, context = template)
        
    #response = generate_response(st.session_state.llm,prompt, context)
    st.session_state.messages.append({"role": "assistant", "content": response})

    st.chat_message("assistant", avatar="πŸ€–").write(response)