File size: 5,346 Bytes
cd73f6e
 
 
 
 
9b6d908
 
cd73f6e
be04987
cd73f6e
 
 
 
 
9cbe2ec
24b913e
811654f
 
24b913e
64900b8
24b913e
 
 
60a2e4b
24b913e
20d5ffa
 
 
5dcb2d3
 
4aa0194
 
20d5ffa
 
 
 
811654f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd73f6e
811654f
 
 
3d00f79
 
cd73f6e
 
811654f
24b913e
b492afd
20d5ffa
811654f
 
 
cd73f6e
 
20d5ffa
 
 
 
 
 
 
 
 
cd73f6e
 
 
d5ef1c5
cd73f6e
 
 
24b913e
cd73f6e
 
 
 
 
 
 
 
 
 
 
 
0aa9e50
d7d7f0d
cd73f6e
 
f9a4cc4
cd73f6e
811654f
 
 
 
 
cd73f6e
f9a4cc4
811654f
 
8da4159
66b7b89
 
 
 
 
 
 
 
 
 
 
8da4159
66b7b89
 
 
 
 
 
 
 
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
import streamlit as st 
from langchain_community.llms import HuggingFaceTextGenInference
import os
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import StrOutputParser
# from datetime import datetime
from datetime import datetime, timezone, timedelta

from custom_llm import CustomLLM, custom_chain_with_history

from typing import Optional

from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.chat_history import BaseChatMessageHistory
from langchain.memory import ConversationBufferMemory#, PostgresChatMessageHistory

import psycopg2
import urllib.parse as up

os.environ['LANGCHAIN_TRACING_V2'] = "true"


API_TOKEN = os.getenv('HF_INFER_API')
POSTGRE_URL = os.environ['POSTGRE_URL']

@st.cache_resource
def get_llm_chain():
    return custom_chain_with_history(
        # llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], temperature=0.001), 
        llm=CustomLLM(repo_id="google/gemma-7b", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"], temperature=0.001), 
        # memory=st.session_state.memory.chat_memory,
        memory=st.session_state.memory
    )


@st.cache_resource
def get_db_connection(conn_url, password=None):
    
    url = up.urlparse(conn_url)

    conn = psycopg2.connect(
        database=url.path[1:],
        user=url.username,
        password=password if password is not None else url.password,
        host=url.hostname,
        port=url.port
    )

    print("Connection to database succesfull!")
    return conn

# @st.cache_resource
# def get_memory():
#     return PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))


if 'conn' not in st.session_state:
    st.session_state.conn = get_db_connection(POSTGRE_URL)

# if 'cursor' not in st.session_state:
#     st.session_state.cursor = st.session_state.conn.cursor()

if 'memory' not in st.session_state:
    st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
    
    # st.session_state.memory = PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))

    # st.session_state.memory = get_memory()
    st.session_state.memory.chat_memory.add_ai_message("Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?")
    # st.session_state.memory.add_ai_message("Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?")

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<|","<|"], temperature=0.001), 
    #     memory=st.session_state.memory.chat_memory,
    #     # memory=st.session_state.memory
    # )

    st.session_state['chain'] = get_llm_chain()



st.title("Chat With Me")
st.subheader("by Jonathan Jordan")
st.markdown("""<p style="color: yellow;">Note : This conversation will be recorded in our private Database, thank you :)</p>""", unsafe_allow_html=True)

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = [{"role":"assistant", "content":"Hello, My name is Jonathan Jordan. You can call me Jojo. 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})
    
    response = st.session_state.chain.invoke({"question":prompt, "memory":st.session_state.memory}).split("\n<|")[0]

    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        st.markdown(response)
        
    # st.session_state.memory.add_user_message(prompt)
    # st.session_state.memory.add_ai_message(response)
    st.session_state.memory.save_context({"question":prompt}, {"output":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": response})

    # Insert data into the table
    try :
        try :
            cur = st.session_state.conn.cursor()
        except:
            get_db_connection.clear()
            st.session_state.conn = get_db_connection(POSTGRE_URL)
            cur = st.session_state.conn.cursor()
            
        cur.execute(
            f"INSERT INTO chat_history (input_text, response_text, created_at) VALUES (%s, %s, %s)",
            (prompt, response, datetime.now(timezone.utc) + timedelta(hours=7))
        )
        
        # Commit the transaction
        st.session_state.conn.commit()
        cur.close()
    except Exception as e:
        print("ERROR!!!\n", str(e))
        print("User Input :", prompt)
        print("Chatbot Response :", response)