-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathusing_streamlit_chat_ui.py
81 lines (47 loc) · 2.48 KB
/
using_streamlit_chat_ui.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
""" This example provides a basic framework to build a Chatbot UI interface in conjunction with LLMWare
using Streamlit Chat UI.
To run this example requires an install of Streamlit, e.g., `pip3 install streamlit`
To execute the script, run from the command line with: `streamlit run using_with_streamlit_ui.py`
Also, please note that the first time you run with a new model, the model will be downloaded and cached locally,
so expect a delay on the 'first run' which will be much faster on every successive run.
All components of the chatbot will be running locally, so the speed will be determined greatly by the
CPU/GPU capacities of your machine.
We have set the max_output at 250 tokens - for faster, set lower ...
For more information on the Streamlit Chat UI,
see https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps
"""
import streamlit as st
from llmware.models import ModelCatalog
def simple_chat_ui_app (model_name):
st.title(f"Simple Chat with {model_name}")
model = ModelCatalog().load_model(model_name, temperature=0.3, sample=True, max_output=250)
# initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# accept user input
prompt = st.chat_input("Say something")
if prompt:
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
model_response = model.inference(prompt)
# insert additional error checking / post-processing of output here
bot_response = model_response["llm_response"]
st.markdown(bot_response)
st.session_state.messages.append({"role": "user", "content": prompt})
st.session_state.messages.append({"role": "assistant", "content": bot_response})
return 0
if __name__ == "__main__":
# a few representative good chat models that can run locally
# note: will take a minute for the first time it is downloaded and cached locally
chat_models = ["phi-3-gguf",
"llama-2-7b-chat-gguf",
"llama-3-instruct-bartowski-gguf",
"openhermes-mistral-7b-gguf",
"zephyr-7b-gguf"]
model_name = chat_models[0]
simple_chat_ui_app(model_name)