File size: 5,689 Bytes
b0b485a
 
 
 
 
 
 
8776349
b0b485a
 
 
 
 
 
 
 
 
 
 
 
 
 
321b65c
b0b485a
baf43d1
b0b485a
 
 
 
32f7b36
 
 
6e2cc63
b0b485a
 
 
 
 
 
 
 
 
 
d11a7ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b485a
 
 
 
 
 
 
71a59eb
b0b485a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f435b5e
 
 
26ff4af
f435b5e
 
26ff4af
 
f435b5e
b0b485a
 
f435b5e
b0b485a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
# from langchain_community.llms import HuggingFaceTextGenInference
import os, pickle
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import StrOutputParser

from custom_llm import CustomLLM, custom_chain_with_history, custom_combined_chain, custom_dataframe_chain, format_df,custom_unique_df_chain
import pandas as pd


API_TOKEN = os.getenv('HF_INFER_API')


from typing import Optional

from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_models import ChatAnthropic
from langchain_core.chat_history import BaseChatMessageHistory
from langchain.memory import ConversationBufferMemory
from langchain_core.runnables.history import RunnableWithMessageHistory


@st.cache_data(persist=False)
def get_df():
    return pickle.load(open("ebesha_ticket_df.pkl", "rb"))


@st.cache_resource
def get_llm_chain():
    llm = CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"])
    dataframe_chain = custom_dataframe_chain(llm=llm, df=st.session_state.df, unique_values=st.session_state.unique_values)
    memory_chain = custom_chain_with_history(llm=llm, memory=st.session_state.memory)
    return custom_combined_chain(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], max_new_tokens=4), df_chain=dataframe_chain, memory_chain=memory_chain)

if 'memory' not in st.session_state:
    st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
    st.session_state.memory.chat_memory.add_ai_message("Hello there! I'm AI assistant of Lintas Media Danawa. How can I help you today?")

if 'df' not in st.session_state:
    st.session_state['df'] = get_df()


if 'unique_values' not in st.session_state:
    # exec(custom_unique_df_chain(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"]), df=st.session_state.df).invoke({"df_example":format_df(st.session_state.df.head(4))}))
    # st.session_state.unique_values = response
    df = st.session_state.df
    st.session_state.unique_values =  {
        'request_mode': df['request_mode'].unique().tolist(),
        'service_category': df['service_category'].unique().tolist(),
        'child_service_1': df['child_service_1'].unique().tolist(),
        'child_service_2': df['child_service_2'].unique().tolist(),
        'child_service_3': df['child_service_3'].unique().tolist(),
        'child_service_4': df['child_service_4'].unique().tolist(),
        'request_status': df['request_status'].unique().tolist(),
        'request_type': df['request_type'].unique().tolist(),
        'priority': df['priority'].unique().tolist(),
        'fcr': df['fcr'].unique().tolist(),
    }



if 'chain' not in st.session_state:
    # st.session_state['chain'] = custom_chain_with_history(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"]), memory=st.session_state.memory)
    st.session_state['chain'] = get_llm_chain()
    # st.session_state['chain'] = custom_chain_with_history(llm=InferenceClient("https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1", headers = {"Authorization": f"Bearer {API_TOKEN}"}, stream=True, max_new_tokens=512, temperature=0.01), memory=st.session_state.memory)
st.title("LMD Chatbot V3")
st.subheader("Combination of Ticket Submission and WI/User Guide Knowledge")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = [{"role":"assistant", "content":"Hello there! I'm AI assistant of Lintas Media Danawa. How can I help you today?"}]

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])


# React to user input
if prompt := st.chat_input("Ask me anything.."):
    # Display user message in chat message container
    st.chat_message("User").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "User", "content": prompt})
    
    # full_response = st.session_state.chain.invoke(prompt).split("\n<|")[0]
    full_response = st.session_state.chain.invoke({"question":prompt, "memory":st.session_state.memory, "df_example":format_df(st.session_state.df.head(4))}).split("\n<|")[0]

    print(len(full_response))
    
    try :
        df = st.session_state.df
        exec(full_response)
        full_response = "Here is the python code: \n\n```python"+ full_response +"\n```\n\nGenerated Response: \n\n"+ str(response)
    except Exception as e:
        print(e)
        
    with st.chat_message("assistant"):
        st.markdown(full_response)
    

    # Display assistant response in chat message container
    # with st.chat_message("assistant"):
    #     message_placeholder = st.empty()
    #     full_response = ""
    #     for chunk in st.session_state.chain.stream(prompt):
    #         full_response += chunk + " "
    #         message_placeholder.markdown(full_response + " ")
    #         if full_response[-4:] == "\n<|":
    #             break
        # st.markdown(full_response)
    st.session_state.memory.save_context({"question":prompt}, {"output":full_response})
    st.session_state.memory.chat_memory.messages = st.session_state.memory.chat_memory.messages[-15:]
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": full_response})