pratikshahp commited on
Commit
57cd8e9
·
verified ·
1 Parent(s): 22b9db0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.chains import ConversationChain
4
+ from langchain_community.chat_message_histories import StreamlitChatMessageHistory
5
+ from langchain.memory import ConversationBufferMemory
6
+ import os
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ # Set your HF_TOKEN
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
13
+
14
+
15
+ # Initialize the HuggingFace inference endpoint
16
+ llm = HuggingFaceEndpoint(
17
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
18
+ huggingfacehub_api_token=HF_TOKEN.strip(),
19
+ temperature=0.7,
20
+ max_new_tokens=200
21
+ )
22
+
23
+ # Initialize Streamlit app
24
+ st.set_page_config(page_title="LangChain Chatbot with Memory")
25
+ st.title("🤖 LangChain Chatbot with Memory")
26
+
27
+ # Initialize chat message history
28
+ history = StreamlitChatMessageHistory(key="chat_messages")
29
+
30
+ # Display chat history
31
+ for msg in history.messages:
32
+ if msg.type == "human":
33
+ st.chat_message("user").write(msg.content)
34
+ else:
35
+ st.chat_message("assistant").write(msg.content)
36
+
37
+ # Initialize memory with chat history
38
+ memory = ConversationBufferMemory(chat_memory=history, return_messages=True)
39
+
40
+ # Create conversation chain
41
+ conversation = ConversationChain(llm=llm, memory=memory)
42
+
43
+ # Chat input
44
+ if prompt := st.chat_input("Say something..."):
45
+ # Add user message to history
46
+ history.add_user_message(prompt)
47
+ st.chat_message("user").write(prompt)
48
+
49
+ # Generate AI response
50
+ response = conversation.predict(input=prompt)
51
+
52
+ # Add AI message to history
53
+ history.add_ai_message(response)
54
+ st.chat_message("assistant").write(response)