如何实现LLM输出的Callback可视化

如何实现LLM输出的Callback可视化

背景

当我们是用React大模型进行输出时,由于React这类推理的大模型它的输出不是一次性的,内部会有一个完整的推理过程。

实现

准备环境

导入需要的包

import streamlit as st # 导入streamlit
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler 导入callback
创建streamlit配置
st.set_page_config(page_title="Code Agent",layout="wide")
st.title("Code Agent")
实现可在界面上配置的API KEY

我这里使用的是通义千问的模型,为了不用每次都要输入,设置默认获取本地环境变量中的key,没找到再获取sidebar.text_input控件的值,都没获取到就不往下走了

input_api_key = st.sidebar.text_input(
    label="通义千问API KEY",
    placeholder="输入后点击回车键生效",
    type="password",
)

dashscope_api_key = os.environ.get("DASHSCOPE_API_KEY",input_api_key)

# 检查API KEY
if not dashscope_api_key:
    st.info("请配置通义千问的API KEY")
    st.stop()
实现React Agent

实现react agent,并绑定PythonREPLTool,让大模型能够通过编程来回答用户提问,下面的代码同时实现了模型记忆功能。

instructions = """You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question. 
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
"""

base_prompt_template = """
{instructions}

TOOLS:
------

You have access to the following tools:

{tools}

To use a tool, please use the following format:

‍```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
‍```

When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:

‍```
Thought: Do I need to use a tool? No
Final Answer: [your response here]
‍```

Begin!

Previous conversation history:
{chat_history}

New input: {input}
{agent_scratchpad}"""

base_prompt = PromptTemplate.from_template(base_prompt_template)

prompt = base_prompt.partial(instructions=instructions)

llm = ChatTongyi(model="qwen-max",)

tools = [PythonREPLTool()]

msgs = StreamlitChatMessageHistory()
memory = ConversationBufferMemory(
    chat_memory=msgs, return_messages=True, memory_key="chat_history", output_key="output"
)

agent = create_react_agent(llm,tools,prompt)

if "messages" not in st.session_state or st.sidebar.button("清空聊天记录"):
    st.session_state["messages"] = [{"role": "assistant", "content": "您好,我是助手小C,我会编程。"}]


# 加载历史聊天记录
for msg in st.session_state.messages:
    st.chat_message(msg["role"]).write(msg["content"])

agent_executor = AgentExecutor(agent=agent, tools=tools,memory=memory,verbose=False)
创建agent执行,并实现回调

因为langchain迭代比较快,目前在网络上找到的、书里面的,甚至官方demo,这个回调的写法都过时了

以前可能写法:直接在agent_executor.run方法中传callbacks参数,但是最新代码不支持run方法,只能用invoke,

st_cb = StreamlitCallbackHandler(st.container())
response = agent_executor.run({"input":user_query}, callbacks=[st_cb])

现在则需要构造一个config参数,写法如下:

# 创建聊天输入框
user_query = st.chat_input(placeholder="请开始提问吧!")

if user_query:
    st.session_state.messages.append({"role": "user", "content": user_query})
    st.chat_message("user").write(user_query)

    with st.chat_message("assistant"):
        st_cb = StreamlitCallbackHandler(st.container())
        config = {"callbacks":[st_cb]}
        response = agent_executor.invoke({"input":user_query}, config=config)
        st.session_state.messages.append({"role": "assistant", "content": response["output"]})
        st.write(response["output"])
运行效果

可以看到,大模型的输出通过streamlit的callback显示了整个推理过程。

image

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值