star

提示

阅读构建一个基本的LLM聊天应用教程,学习如何使用st.chat_messagest.chat_input来构建基于聊天的应用。

插入一个聊天消息容器。

要向返回的容器添加元素,您可以使用with表示法(推荐)或直接在返回的对象上调用方法。请参见下面的示例。

函数签名[source]

st.chat_message(name, *, avatar=None)

参数

name ("user", "assistant", "ai", "human", or str)

消息作者的名字。可以是"human"/"user"或"ai"/"assistant",以启用预设的样式和头像。

目前,名字不会在用户界面中显示,但会设置为无障碍标签。出于无障碍考虑,不应使用空字符串。

avatar (Anything supported by st.image (except list), str, or None)

The avatar shown next to the message.

If avatar is None (default), the icon will be determined from name as follows:

  • If name is "user" or "human", the message will have a default user icon.
  • If name is "ai" or "assistant", the message will have a default bot icon.
  • For all other values of name, the message will show the first letter of the name.

In addition to the types supported by st.image (except list), the following strings are valid:

  • A single-character emoji. For example, you can set avatar="🧑‍💻" or avatar="🦖". Emoji short codes are not supported.

  • An icon from the Material Symbols library (rounded style) in the format ":material/icon_name:" where "icon_name" is the name of the icon in snake case.

    For example, icon=":material/thumb_up:" will display the Thumb Up icon. Find additional icons in the Material Symbols font library.

返回

(Container)

一个可以容纳多个元素的单一容器。

示例

你可以使用with符号将任何元素插入到扩展器中

import streamlit as st
import numpy as np

with st.chat_message("user"):
    st.write("Hello 👋")
    st.line_chart(np.random.randn(30, 3))

或者你可以直接在返回的对象中调用方法:

import streamlit as st
import numpy as np

message = st.chat_message("assistant")
message.write("Hello human")
message.bar_chart(np.random.randn(30, 3))

有关st.chat_messagest.chat_input API的概述,请查看Streamlit的高级开发者倡导者Chanin Nantasenamat(@dataprofessor)提供的视频教程。

forum

还有问题吗?

我们的 论坛 充满了有用的信息和Streamlit专家。