本文档介绍了设置或重命名消息作者的两种方法,以便在用户界面中显示更友好的作者名称:author_rename 装饰器以及在消息创建时指定消息作者。

方法 1: author_rename

用于在消息处理过程中动态重命名消息作者。

参数

orig_author
str
必需

原始作者名称。

返回值

author
str
必需

重命名后的作者

用法

from langchain import OpenAI, LLMMathChain
import chainlit as cl


@cl.author_rename
def rename(orig_author: str):
    rename_dict = {"LLMMathChain": "Albert Einstein", "Chatbot": "Assistant"}
    return rename_dict.get(orig_author, orig_author)


@cl.on_message
async def main(message: cl.Message):
    llm = OpenAI(temperature=0)
    llm_math = LLMMathChain.from_llm(llm=llm)
    res = await llm_math.acall(message.content, callbacks=[cl.AsyncLangchainCallbackHandler()])

    await cl.Message(content="Hello").send()

方法 2: 消息作者

允许在创建消息时命名消息作者。

用法

创建新的消息对象时,您可以直接指定作者。

from langchain import OpenAI, LLMMathChain
import chainlit as cl

@cl.on_message
async def main(message: cl.Message):
    llm = OpenAI(temperature=0)
    llm_math = LLMMathChain.from_llm(llm=llm)
    res = await llm_math.acall(message.content, callbacks=[cl.AsyncLangchainCallbackHandler()])

    # Specify the author at message creation
    response_message = cl.Message(content="Hello", author="NewChatBotName")
    await response_message.send()