pratikshahp commited on
Commit
6fd8a6a
·
verified ·
1 Parent(s): 6e5e4ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -1,48 +1,57 @@
1
  import streamlit as st
2
- import os
3
- from dotenv import load_dotenv
4
  from langchain.chains import ConversationChain
5
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
6
  from langchain.memory import ConversationBufferMemory
7
- from langchain.chat_models import ChatOpenAI
8
-
9
- # Load API key
10
  load_dotenv()
 
 
11
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
 
13
- # Initialize model
 
14
  llm = ChatOpenAI(
15
  openai_api_key=OPENAI_API_KEY,
16
- model_name="gpt-4o-mini",
17
  temperature=0.7,
18
- max_tokens=50,
19
  )
20
 
21
- # Page config
22
- st.set_page_config(page_title="LangChain Chatbot")
23
- st.title("🤖 LangChain Chatbot (OpenAI)")
24
 
25
- # History and memory
26
  history = StreamlitChatMessageHistory(key="chat_messages")
27
- memory = ConversationBufferMemory(chat_memory=history, return_messages=True)
28
- conversation = ConversationChain(llm=llm, memory=memory)
29
 
30
- # 💬 Display chat history (before new user input)
31
  for msg in history.messages:
32
  if msg.type == "human":
33
  st.chat_message("user").write(msg.content)
34
- elif msg.type == "ai":
35
  st.chat_message("assistant").write(msg.content)
36
 
37
- # 🔥 Chat input AFTER displaying history
 
 
 
 
 
 
38
  if prompt := st.chat_input("Say something..."):
39
- # Display + store user message
40
- st.chat_message("user").write(prompt)
41
  history.add_user_message(prompt)
 
42
 
43
- # Response with word limit
44
- response = conversation.predict(input=f"{prompt}\n\nPlease respond in complete sentence in no more than 50 words.")
45
-
46
- # Display + store assistant message
47
- st.chat_message("assistant").write(response)
48
  history.add_ai_message(response)
 
 
 
 
 
1
  import streamlit as st
 
 
2
  from langchain.chains import ConversationChain
3
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
4
  from langchain.memory import ConversationBufferMemory
5
+ import os
6
+ from dotenv import load_dotenv
7
+ from langchain.chat_models import ChatOpenAI # Use ChatOpenAI instead of HuggingFaceEndpoint
8
  load_dotenv()
9
+
10
+ # Set your HF_TOKEN
11
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
 
13
+
14
+ # Initialize the OpenAI model
15
  llm = ChatOpenAI(
16
  openai_api_key=OPENAI_API_KEY,
17
+ model_name="gpt-4o-mini", # or "gpt-3.5-turbo"
18
  temperature=0.7,
19
+ max_tokens=50
20
  )
21
 
22
+ # Initialize Streamlit app
23
+ st.set_page_config(page_title="LangChain Chatbot with Memory")
24
+ st.title("🤖 LangChain Chatbot with Memory")
25
 
26
+ # Initialize chat message history
27
  history = StreamlitChatMessageHistory(key="chat_messages")
 
 
28
 
29
+ # Display chat history
30
  for msg in history.messages:
31
  if msg.type == "human":
32
  st.chat_message("user").write(msg.content)
33
+ else:
34
  st.chat_message("assistant").write(msg.content)
35
 
36
+ # Initialize memory with chat history
37
+ memory = ConversationBufferMemory(chat_memory=history, return_messages=True)
38
+
39
+ # Create conversation chain
40
+ conversation = ConversationChain(llm=llm, memory=memory)
41
+
42
+ # Chat input
43
  if prompt := st.chat_input("Say something..."):
44
+ # Add user message to history
 
45
  history.add_user_message(prompt)
46
+ st.chat_message("user").write(prompt)
47
 
48
+ # Generate AI response with word limit instruction
49
+ limited_prompt = f"{prompt}\n\nPlease respond in complete sentence in no more than 50 words."
50
+ response = conversation.predict(input=limited_prompt)
51
+
52
+ # Add AI message to history
53
  history.add_ai_message(response)
54
+ st.chat_message("assistant").write(response)
55
+
56
+ use following instead of huggingface endpoints
57
+ from langchain.chat_models import ChatOpenAI