
回复
本文将帮助你使用低代码前端、用于会话管理的LangChain以及用于生成响应的Bedrock LLM来创建聊天机器人。
在不断发展的AI领域,聊天机器人已成为一种不可或缺的工具,用于增强用户参与度和简化信息传递。本文将逐步介绍构建交互式聊天机器人的具体过程,使用Streamlit作为前端、使用LangChain用于协调交互,以及使用基于Amazon Bedrock的Anthropic Claude模型作为大语言模型(LLM)后端。我们将深入研究前端和后端的代码片段,并解释使这个聊天机器人切实可行的关键组件。
图1. 聊天机器人体系结构
Python
import streamlit
import chatbot_backend
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
import boto3
from langchain_aws import ChatBedrock
import pandas as pd
# 2 Set Title for Chatbot - streamlit.title("Hi, This is your Chatbott")
# 3 LangChain memory to the session cache - Session State -
if 'memory' not in streamlit.session_state:
streamlit.session_state.memory = demo.demo_memory()
# 4 Add the UI chat history to the session cache - Session State
if 'chat_history' not in streamlit.session_state:
streamlit.session_state.chat_history = []
# 5 Re-render the chat history
for message in streamlit.session_state.chat_history:
with streamlit.chat_message(message["role"]):
streamlit.markdown(message["text"])
# 6 Enter the details for chatbot input box
input_text = streamlit.chat_input("Powered by Bedrock")
if input_text:
with streamlit.chat_message("user"):
streamlit.markdown(input_text)
streamlit.session_state.chat_history.append({"role": "user", "text": input_text})
chat_response = demo.demo_conversation(input_text=input_text,
memory=streamlit.session_state.memory)
with streamlit.chat_message("assistant"):
streamlit.markdown(chat_response)
streamlit.session_state.chat_history.append({"role": "assistant", "text": chat_response})
Python
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
import boto3
from langchain_aws import ChatBedrock
# 2a Write a function for invoking model- client connection with Bedrock with profile, model_id
def demo_chatbot():
boto3_session = boto3.Session(
# Your aws_access_key_id,
# Your aws_secret_access_key,
region_name='us-east-1'
)
llm = ChatBedrock(
model_id="anthropic.claude-3-sonnet-20240229-v1:0",
client=boto3_session.client('bedrock-runtime'),
model_kwargs={
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 20000,
"temperature": .3,
"top_p": 0.3,
"stop_sequences": ["\n\nHuman:"]
}
)
return llm
# 3 Create a Function for ConversationSummaryBufferMemory (llm and max token limit)
def demo_memory():
llm_data = demo_chatbot()
memory = ConversationSummaryBufferMemory(llm=llm_data, max_token_limit=20000)
return memory
# 4 Create a Function for Conversation Chain - Input text + Memory
def demo_conversation(input_text, memory):
llm_chain_data = demo_chatbot()
# Initialize ConversationChain with proper llm and memory
llm_conversation = ConversationChain(llm=llm_chain_data, memory=memory, verbose=True)
# Call the invoke method
full_input = f" \nHuman: {input_text}"
llm_start_time = time.time()
chat_reply = llm_conversation.invoke({"input": full_input})
llm_end_time = time.time()
llm_elapsed_time = llm_end_time - llm_start_time
memory.save_context({"input": input_text}, {"output": chat_reply.get('response', 'No Response')})
return chat_reply.get('response', 'No Response')
我们在上面探讨了用Streamlit、LangChain和强大的LLM后端构建的交互式聊天机器人的基本模块。这个基础为从客户支持自动化到个性化学习体验的无限可能打开了大门。你可以随意试验、改进和部署这个聊天机器人,以满足自己的特定需求和使用场景。
原文标题:Building an Interactive Chatbot With Streamlit, LangChain, and Bedrock,作者:Karan Bansal