-
Notifications
You must be signed in to change notification settings - Fork 0
/
scouting_streamlit.py
60 lines (41 loc) · 1.58 KB
/
scouting_streamlit.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
"""Simple example of a chatbot (with memory) with Streamlit interface."""
# please put your API key in os.environ["OPENAI_API_KEY"]
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.memory import StreamlitChatMessageHistory
from langchain.memory import ConversationBufferMemory
# MODEL
# =====
# prompts setup
template = """You are an AI chatbot having a conversation with a human.
Please provide responses only in JSON format.
{history}
Human: {human_input}
AI: """
prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)
# memory setup
msgs = StreamlitChatMessageHistory(key="chat_messages")
memory = ConversationBufferMemory(memory_key="history", chat_memory=msgs)
# if there are no messages in the chat history, add a welcome message
if len(msgs.messages) == 0:
msgs.add_ai_message("How can I help you?")
# AI model setup
chain = LLMChain(llm=OpenAI(), prompt=prompt, memory=memory)
# INTERFACE
# =========
import streamlit as st # pylint: disable=wrong-import-position
# writes all messages to the screen
for msg in msgs.messages:
st.chat_message(msg.type).write(msg.content)
# asks for user input
prompt = st.chat_input()
# if the user has entered something
if prompt:
# shows the user's input on the screen
st.chat_message("human").write(prompt)
# generates a response from the AI
# adds prompt and response to the chat history
response = chain.run(prompt)
# show the AI's response on the screen
st.chat_message("ai").write(response)