Chainlit 原生 UI,可通过端口 8000 访问。当你运行 chainlit run 时,应在你的默认浏览器中打开。

支持的功能

消息流式传输元素音频询问用户聊天历史聊天配置文件反馈

预览

窗口消息传递

当在 iframe 中运行 Web 应用时,服务器和父窗口可以使用窗口消息进行通信。这对于向 Chainlit 服务器发送上下文信息以及根据服务器响应更新父窗口非常有用。

在你的 Chainlit 服务器中添加一个使用 @cl.on_window_message 装饰器的函数,以接收来自父窗口的消息。

import chainlit as cl

@cl.on_window_message
async def window_message(message: str):
  if message.startswith("Client: "):
    await cl.Message(content=f"Window message received: {message}").send()

然后,在你的应用/网站中,你可以像这样发出窗口消息:

const iframe = document.getElementById('the-iframe');
iframe.contentWindow.postMessage('Client: Hello from parent window', '*');

要从服务器向父窗口发送消息,请使用 cl.send_window_message

import chainlit as cl

@cl.on_message
async def message():
  await cl.send_window_message("Server: Hello from Chainlit")

父窗口可以像这样监听消息:

window.addEventListener('message', (event) => {
  if (event.data.startsWith("Server: ")) {
    console.log('Parent window received:', event.data);
  }
});

示例

查看 cookbook 中使用窗口消息传递功能的示例:https://github.com/Chainlit/cookbook/tree/main/window-message