Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,8 @@ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
|
13 |
from langchain_core.chat_history import BaseChatMessageHistory
|
14 |
from langchain.memory import ConversationBufferMemory, PostgresChatMessageHistory
|
15 |
|
|
|
|
|
16 |
|
17 |
|
18 |
|
@@ -29,18 +31,40 @@ def get_llm_chain():
|
|
29 |
|
30 |
|
31 |
@st.cache_resource
|
32 |
-
def
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if 'memory' not in st.session_state:
|
37 |
-
|
38 |
|
39 |
# st.session_state.memory = PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
40 |
|
41 |
-
st.
|
42 |
-
|
43 |
-
st.session_state.memory.add_ai_message("Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?")
|
44 |
|
45 |
if 'chain' not in st.session_state:
|
46 |
# st.session_state['chain'] = custom_chain_with_history(
|
@@ -55,7 +79,7 @@ if 'chain' not in st.session_state:
|
|
55 |
|
56 |
st.title("Chat With Me")
|
57 |
st.subheader("by Jonathan Jordan")
|
58 |
-
st.markdown("Note :
|
59 |
|
60 |
# Initialize chat history
|
61 |
if "messages" not in st.session_state:
|
@@ -78,10 +102,20 @@ if prompt := st.chat_input("Ask me anything.."):
|
|
78 |
# Display assistant response in chat message container
|
79 |
with st.chat_message("assistant"):
|
80 |
st.markdown(response)
|
81 |
-
|
82 |
-
st.session_state.memory.
|
83 |
-
# st.session_state.memory.
|
84 |
-
|
|
|
85 |
# Add assistant response to chat history
|
86 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
from langchain_core.chat_history import BaseChatMessageHistory
|
14 |
from langchain.memory import ConversationBufferMemory, PostgresChatMessageHistory
|
15 |
|
16 |
+
import psycopg2
|
17 |
+
import urllib.parse as up
|
18 |
|
19 |
|
20 |
|
|
|
31 |
|
32 |
|
33 |
@st.cache_resource
|
34 |
+
def get_db_connection(conn_url, password=None):
|
35 |
+
|
36 |
+
url = up.urlparse(conn_url)
|
37 |
+
|
38 |
+
conn = psycopg2.connect(
|
39 |
+
database=url.path[1:],
|
40 |
+
user=url.username,
|
41 |
+
password=password if password is not None else url.password,
|
42 |
+
host=url.hostname,
|
43 |
+
port=url.port
|
44 |
+
)
|
45 |
+
|
46 |
+
print("Connection to database succesfull!")
|
47 |
+
return conn
|
48 |
+
|
49 |
+
# @st.cache_resource
|
50 |
+
# def get_memory():
|
51 |
+
# return PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
52 |
+
|
53 |
|
54 |
+
if 'conn' not in st.session_state:
|
55 |
+
st.session_state.conn = get_db_connection(POSTGRE_URL)
|
56 |
+
|
57 |
+
if 'cursor' not in st.session_state:
|
58 |
+
st.session_state.cursor = st.session_state.conn.cursor()
|
59 |
|
60 |
if 'memory' not in st.session_state:
|
61 |
+
st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
|
62 |
|
63 |
# st.session_state.memory = PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
64 |
|
65 |
+
# st.session_state.memory = get_memory()
|
66 |
+
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?")
|
67 |
+
# st.session_state.memory.add_ai_message("Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?")
|
68 |
|
69 |
if 'chain' not in st.session_state:
|
70 |
# st.session_state['chain'] = custom_chain_with_history(
|
|
|
79 |
|
80 |
st.title("Chat With Me")
|
81 |
st.subheader("by Jonathan Jordan")
|
82 |
+
st.markdown("Note : This conversation will be recorded in our private Database, thank you :)")
|
83 |
|
84 |
# Initialize chat history
|
85 |
if "messages" not in st.session_state:
|
|
|
102 |
# Display assistant response in chat message container
|
103 |
with st.chat_message("assistant"):
|
104 |
st.markdown(response)
|
105 |
+
|
106 |
+
# st.session_state.memory.add_user_message(prompt)
|
107 |
+
# st.session_state.memory.add_ai_message(response)
|
108 |
+
st.session_state.memory.save_context({"question":prompt}, {"output":response})
|
109 |
+
st.session_state.memory.chat_memory.messages = st.session_state.memory.chat_memory.messages[-15:]
|
110 |
# Add assistant response to chat history
|
111 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
112 |
+
|
113 |
+
# Insert data into the table
|
114 |
+
st.session_state.cursor.execute(
|
115 |
+
"INSERT INTO chat_history (input_text, response_text, created_at) VALUES (%s, %s, %s)",
|
116 |
+
(prompt, response, datetime.now())
|
117 |
+
)
|
118 |
+
|
119 |
+
# Commit the transaction
|
120 |
+
st.session_state.conn.commit()
|
121 |
+
|