Wajahat698 commited on
Commit
30c50db
·
verified ·
1 Parent(s): 77cf474

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -1,11 +1,16 @@
1
  import os
2
  from dotenv import load_dotenv
3
  import streamlit as st
4
- from langchain.chat_models import ChatOpenAI
 
 
5
  from langchain.schema import HumanMessage, AIMessage
6
 
7
  # Load environment variables
8
  load_dotenv()
 
 
 
9
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
10
 
11
  # Initialize OpenAI chat model using LangChain
@@ -14,6 +19,7 @@ chat = ChatOpenAI(
14
  model="gpt-4o",
15
  temperature=0.7,
16
  max_retries=3 # Automatically retry failed requests
 
17
  )
18
 
19
  # Streamlit UI setup
@@ -21,6 +27,7 @@ st.set_page_config(
21
  page_title="ChatGPT ",
22
  page_icon="",
23
  layout="centered",
 
24
  )
25
 
26
  # Chat history initialization
@@ -36,6 +43,7 @@ for msg in st.session_state.messages:
36
  st.write(f"**Assistant:** {msg.content}")
37
 
38
  # User input box
 
39
  user_input = st.text_input("Ask me anything:")
40
 
41
  if user_input:
@@ -46,7 +54,8 @@ if user_input:
46
  # Generate assistant response
47
  with st.spinner("Thinking..."):
48
  try:
49
- response = chat(st.session_state.messages)
 
50
  st.write(f"**Assistant:** {response.content}")
51
  st.session_state.messages.append(AIMessage(content=response.content))
52
  except Exception as e:
 
1
  import os
2
  from dotenv import load_dotenv
3
  import streamlit as st
4
+ # from langchain.chat_models import ChatOpenAI # <-- remove or comment out
5
+ from langchain_community.chat_models import ChatOpenAI
6
+
7
  from langchain.schema import HumanMessage, AIMessage
8
 
9
  # Load environment variables
10
  load_dotenv()
11
+
12
+
13
+
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
 
16
  # Initialize OpenAI chat model using LangChain
 
19
  model="gpt-4o",
20
  temperature=0.7,
21
  max_retries=3 # Automatically retry failed requests
22
+
23
  )
24
 
25
  # Streamlit UI setup
 
27
  page_title="ChatGPT ",
28
  page_icon="",
29
  layout="centered",
30
+
31
  )
32
 
33
  # Chat history initialization
 
43
  st.write(f"**Assistant:** {msg.content}")
44
 
45
  # User input box
46
+
47
  user_input = st.text_input("Ask me anything:")
48
 
49
  if user_input:
 
54
  # Generate assistant response
55
  with st.spinner("Thinking..."):
56
  try:
57
+ # Use .invoke() instead of calling chat directly
58
+ response = chat.invoke(st.session_state.messages)
59
  st.write(f"**Assistant:** {response.content}")
60
  st.session_state.messages.append(AIMessage(content=response.content))
61
  except Exception as e: