File size: 2,150 Bytes
cd73f6e
 
 
 
 
 
be04987
cd73f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aa9e50
 
cd73f6e
 
 
 
0aa9e50
 
cd73f6e
 
 
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
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 custom_llm import CustomLLM, custom_chain_with_history


API_TOKEN = os.getenv('HF_INFER_API')

#API_URL = "https://api-inference.huggingface.co/models/gpt2"

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


if 'memory' not in st.session_state:
    st.session_state['memory'] = ConversationBufferMemory(return_messages=True)

if 'chain' not in st.session_state:
    st.session_state['chain'] = CustomChainWithHistory(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|"]), memory=memory)

st.title("Chat With Me")
st.subheader("by Jonathan Jordan")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# 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(prompt)

    # Display assistant response in chat message container
    with st.chat_message("Jojo"):
        st.markdown(response)
    memory.save_context({"question":prompt}, {"output":prompt})
    memory.chat_memory.messages = memory.chat_memory.messages[-15:]
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "Jojo", "content": response})