Spaces:
Runtime error
Runtime error
Commit
·
24b913e
1
Parent(s):
f0ab69f
add postgre integration to store chat history in database
Browse files
app.py
CHANGED
|
@@ -3,34 +3,42 @@ from langchain_community.llms import HuggingFaceTextGenInference
|
|
| 3 |
import os
|
| 4 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 5 |
from langchain.schema import StrOutputParser
|
|
|
|
| 6 |
|
| 7 |
from custom_llm import CustomLLM, custom_chain_with_history
|
| 8 |
|
| 9 |
-
|
| 10 |
-
API_TOKEN = os.getenv('HF_INFER_API')
|
| 11 |
-
|
| 12 |
-
|
| 13 |
from typing import Optional
|
| 14 |
|
| 15 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 16 |
-
from langchain_community.chat_models import ChatAnthropic
|
| 17 |
from langchain_core.chat_history import BaseChatMessageHistory
|
| 18 |
-
from langchain.memory import ConversationBufferMemory
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
if 'memory' not in st.session_state:
|
| 23 |
-
st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if 'chain' not in st.session_state:
|
| 26 |
-
st.session_state['chain'] = custom_chain_with_history(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
st.title("Chat With Me")
|
| 29 |
st.subheader("by Jonathan Jordan")
|
| 30 |
|
| 31 |
# Initialize chat history
|
| 32 |
if "messages" not in st.session_state:
|
| 33 |
-
st.session_state.messages = []
|
| 34 |
|
| 35 |
# Display chat messages from history on app rerun
|
| 36 |
for message in st.session_state.messages:
|
|
|
|
| 3 |
import os
|
| 4 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 5 |
from langchain.schema import StrOutputParser
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
from custom_llm import CustomLLM, custom_chain_with_history
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from typing import Optional
|
| 11 |
|
| 12 |
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 |
+
|
| 19 |
+
API_TOKEN = os.getenv('HF_INFER_API')
|
| 20 |
+
POSTGRE_URL = os.environ('POSTGRE_URL')
|
| 21 |
+
|
| 22 |
|
| 23 |
|
| 24 |
if 'memory' not in st.session_state:
|
| 25 |
+
# st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
|
| 26 |
+
|
| 27 |
+
st.session_state.memory = PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
| 28 |
|
| 29 |
if 'chain' not in st.session_state:
|
| 30 |
+
st.session_state['chain'] = custom_chain_with_history(
|
| 31 |
+
llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"]),
|
| 32 |
+
# memory=st.session_state.memory.chat_memory,
|
| 33 |
+
memory=st.session_state.memory
|
| 34 |
+
)
|
| 35 |
|
| 36 |
st.title("Chat With Me")
|
| 37 |
st.subheader("by Jonathan Jordan")
|
| 38 |
|
| 39 |
# Initialize chat history
|
| 40 |
if "messages" not in st.session_state:
|
| 41 |
+
st.session_state.messages = [{"role":"assistant", "content":"Hello, My name is Jonathan Jordan. You can call me Jojo. How can I help you today?"}]
|
| 42 |
|
| 43 |
# Display chat messages from history on app rerun
|
| 44 |
for message in st.session_state.messages:
|