In the intro, we learned how the .app()
method launches a web app with a simple chat interface, for example:
from chatlas import ChatAnthropic
= ChatAnthropic()
chat chat.app()
This is a great way to quickly test your model, but you’ll likely want to embed similar functionality into a larger web app. Here’s how you can do that we different web frameworks.
Shiny
Using Shiny’s ui.Chat
component, you can simply pass user input from the component into the chat.stream()
method. This generate a response stream that can then be passed to .append_message_stream()
.
from chatlas import ChatAnthropic
from shiny.express import ui
= ui.Chat(
chat id="ui_chat",
=["Hi! How can I help you today?"],
messages
)
chat.ui()
= ChatAnthropic()
chat_model
@chat_ui.on_user_submit
async def handle_user_input():
= chat_model.stream(chat.user_input())
response await chat.append_message_stream(response)
Streamlit
Streamlit is a popular Python library for building web apps. You can use the st.chat_input()
and st.chat_message()
methods to create a chat interface. Here’s an example of how you can use Chatlas with Streamlit:
import streamlit as st
from chatlas import ChatOpenAI, Turn
with st.sidebar:
= st.text_input(
openai_api_key "OpenAI API Key", key="chatbot_api_key", type="password"
)"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/Chatbot.py)"
"💬 Chatbot")
st.title(
if "turns" not in st.session_state:
"turns"] = [
st.session_state[="assistant", contents="How can I help you?"),
Turn(role
]
list[Turn] = st.session_state.turns
turns:
for turn in turns:
st.chat_message(turn.role).write(turn.text)
if prompt := st.chat_input():
if not openai_api_key:
"Please add your OpenAI API key to continue.")
st.info(
st.stop()
"user").write(prompt)
st.chat_message(
= ChatOpenAI(api_key=openai_api_key, turns=turns)
chat = chat.stream(prompt)
response
with st.chat_message("assistant"):
st.write_stream(response)
"turns"] = chat.get_turns() st.session_state[