jonathanjordan21 commited on
Commit
cd73f6e
1 Parent(s): 7d8da9a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ 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, CustomChainWithHistory
8
+
9
+
10
+ API_TOKEN = os.getenv('HF_INFER_API')
11
+
12
+ #API_URL = "https://api-inference.huggingface.co/models/gpt2"
13
+
14
+ from typing import Optional
15
+
16
+ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
17
+ from langchain_community.chat_models import ChatAnthropic
18
+ from langchain_core.chat_history import BaseChatMessageHistory
19
+ from langchain.memory import ConversationBufferMemory
20
+ from langchain_core.runnables.history import RunnableWithMessageHistory
21
+
22
+
23
+ if 'memory' not in st.session_state:
24
+ st.session_state['memory'] = ConversationBufferMemory(return_messages=True)
25
+
26
+ if 'chain' not in st.session_state:
27
+ 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)
28
+
29
+ st.title("Chat With Me")
30
+ st.subheader("by Jonathan Jordan")
31
+
32
+ # Initialize chat history
33
+ if "messages" not in st.session_state:
34
+ st.session_state.messages = []
35
+
36
+ # Display chat messages from history on app rerun
37
+ for message in st.session_state.messages:
38
+ with st.chat_message(message["role"]):
39
+ st.markdown(message["content"])
40
+
41
+ # React to user input
42
+ if prompt := st.chat_input("Ask me anything.."):
43
+ # Display user message in chat message container
44
+ st.chat_message("User").markdown(prompt)
45
+ # Add user message to chat history
46
+ st.session_state.messages.append({"role": "User", "content": prompt})
47
+
48
+ response = st.session_state.chain.invoke({"question":prompt},
49
+ config={"configurable": {"session_id": "foobar"}},)
50
+
51
+ # Display assistant response in chat message container
52
+ with st.chat_message("Jojo"):
53
+ st.markdown(response)
54
+ # Add assistant response to chat history
55
+ st.session_state.messages.append({"role": "Jojo", "content": response})
56
+