Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,30 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
-
#As Langchain team has been working aggresively on improving the tool, we can see a lot of changes happening every weeek,
|
| 5 |
-
#As a part of it, the below import has been depreciated
|
| 6 |
-
#from langchain.chat_models import ChatOpenAI
|
| 7 |
-
|
| 8 |
-
#New import from langchain, which replaces the above
|
| 9 |
-
from langchain_openai import ChatOpenAI
|
| 10 |
-
|
| 11 |
-
#import os
|
| 12 |
-
#os.environ["OPENAI_API_KEY"] = "sk-PLfFw23dd932dfg34446dftyvvdfgdfgmvXr2dL8hVowXdt"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
from langchain.schema import (
|
| 16 |
-
AIMessage,
|
| 17 |
-
HumanMessage,
|
| 18 |
-
SystemMessage
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# From here down is all the StreamLit UI
|
| 22 |
-
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
|
| 23 |
-
st.header("Hey, I'm your Chat GPT")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if "sessionMessages" not in st.session_state:
|
| 28 |
-
st.session_state.sessionMessages = [
|
| 29 |
-
SystemMessage(content="You are a helpful assistant.")
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def load_answer(question):
|
| 35 |
-
|
| 36 |
-
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
| 37 |
-
|
| 38 |
-
assistant_answer = chat.invoke(st.session_state.sessionMessages )
|
| 39 |
-
|
| 40 |
-
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
|
| 41 |
-
|
| 42 |
-
return assistant_answer.content
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def get_text():
|
| 46 |
-
input_text = st.text_input("You: ")
|
| 47 |
-
return input_text
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
chat = ChatOpenAI(temperature=0, model="GPT-4o" )
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
user_input=get_text()
|
| 56 |
-
submit = st.button('Generate')
|
| 57 |
-
|
| 58 |
-
if submit:
|
| 59 |
-
|
| 60 |
-
response = load_answer(user_input)
|
| 61 |
-
st.subheader("Answer:")
|
| 62 |
-
|
| 63 |
-
st.write(response)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
import streamlit as st
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def chat(user_message):
|
| 7 |
+
response = openai.ChatCompletion.create(
|
| 8 |
+
model="gpt-4",
|
| 9 |
+
messages=[
|
| 10 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 11 |
+
{"role": "user", "content": user_message}
|
| 12 |
+
],
|
| 13 |
+
temperature=0.7,
|
| 14 |
+
max_tokens=150,
|
| 15 |
+
top_p=1,
|
| 16 |
+
frequency_penalty=0,
|
| 17 |
+
presence_penalty=0
|
| 18 |
+
)
|
| 19 |
+
return response.choices[0].message['content']
|
| 20 |
+
|
| 21 |
+
st.title("GPT-4 Chatbot")
|
| 22 |
+
st.write("Chat with GPT-4 using OpenAI API")
|
| 23 |
+
|
| 24 |
+
user_message = st.text_input("You:", "")
|
| 25 |
+
if st.button("Send"):
|
| 26 |
+
if user_message:
|
| 27 |
+
response = chat(user_message)
|
| 28 |
+
st.write(f"GPT-4: {response}")
|
| 29 |
+
else:
|
| 30 |
+
st.write("Please enter a message.")
|