-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.py
81 lines (73 loc) · 2.81 KB
/
8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import streamlit as st
from langchain import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate)
from langchain.document_loaders import *
from langchain.chains.summarize import load_summarize_chain
import tempfile
from langchain.docstore.document import Document
import time
from langchain.memory import ConversationBufferMemory
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
openai_api_key = st.sidebar.text_input(
"OpenAI API Key",
placeholder="sk-...",
value=os.getenv("OPENAI_API_KEY", ""),
type="password",
)
st.title('Chat App')
#Get message from the user for chat-based system
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if message := st.chat_input("Type your message"):
with st.chat_message("user"):
st.markdown(message)
st.session_state.messages.append({"role": "user", "content": message})
#Generate a response in the style of Elon Musk
def elonMuskResponse(message):
prompt = PromptTemplate(
input_variables=['chat_history', 'message'], template='''You are Elon Musk. Craft a response in your unique style.
{chat_history}
Message: {message}
Elon Musk:'''
)
memory = ConversationBufferMemory(memory_key="chat_history", input_key="message")
llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k", openai_api_key=openai_api_key, temperature=0.7)
chat_llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=False,
memory=memory,
)
return chat_llm_chain
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
response = ""
elif message:
if 'chat_llm_chain' not in st.session_state:
st.session_state.chat_llm_chain = elonMuskResponse(message)
response = st.session_state.chat_llm_chain.run(message=message)
else:
response = ""
#Display the generated response to the user in a chat interface
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# Simulate stream of response with milliseconds delay
for chunk in response.split():
full_response += chunk + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
# Add assistant response to chat history
if full_response:
st.session_state.messages.append({"role": "assistant", "content": full_response})