利用streamlit结合langchain_aws实现claud3的页面交互

测试使用的代码如下

复制代码
import streamlit as st
from langchain_aws import ChatBedrock

def chat_with_model(prompt, model_id):
    llm = ChatBedrock(credentials_profile_name="default", model_id=model_id, region_name="us-east-1")
    res = llm.invoke(prompt)
    return res.content

# Streamlit app
st.title("LangChain AWS Chat Interface")

# Sidebar for model selection
models = {
    "Claude-3 Sonnet": "anthropic.claude-3-sonnet-20240229-v1:0",
    "titan": "amazon.titan-text-express-v1",
    "Claude-3 haiku": "anthropic.claude-3-haiku-20240307-v1:0"
}

model_choice = st.sidebar.selectbox("Select a Model", list(models.keys()))
model_id = models[model_choice]

# Text area for user input
prompt = st.text_area("Enter your prompt here:", "")

if st.button("Send"):
    if prompt:
        response = chat_with_model(prompt, model_id)
        st.write(f"模型返回的结果:\n{response}")
    else:
        st.write("Please enter a prompt.")

运行代码

streamlit run app.py

运行结果如下

为了实现更友好的界面,修改源码如下

复制代码
import streamlit as st
from langchain_aws import ChatBedrock

# 定义与模型交互的函数
def chat_with_model(prompt, model_id, history):
    llm = ChatBedrock(credentials_profile_name="default", model_id=model_id, region_name="us-east-1")
    full_prompt = "\n".join(history + [prompt])
    res = llm.invoke(full_prompt)
    return res.content

# 初始化 Streamlit 应用
st.set_page_config(page_title="Bedrock Client", layout="wide")
st.title("Bedrock Client")

# 定义 CSS 样式
st.markdown(
    """
    <style>
    .sidebar .sidebar-content {
        background-color: #f0f0f5;
    }
    .main .block-container {
        padding: 2rem;
    }
    .chat-history {
        max-height: 400px;
        overflow-y: auto;
        padding: 1rem;
        background-color: #ffffff;
        border: 1px solid #ddd;
        border-radius: 8px;
        margin-bottom: 1rem;
    }
    .chat-input {
        width: 100%;
        padding: 1rem;
        border: 1px solid #ddd;
        border-radius: 8px;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# 模型字典
models = {
    "Claude-3 Sonnet": "anthropic.claude-3-sonnet-20240229-v1:0",
    "titan": "amazon.titan-text-express-v1",
    "Claude-3 haiku": "anthropic.claude-3-haiku-20240307-v1:0"
}

# 侧边栏选择模型
st.sidebar.header("选择模型")
model_choice = st.sidebar.selectbox("Select a Model", list(models.keys()))
model_id = models[model_choice]

# 检查 session_state 中是否有对话历史记录
if "history" not in st.session_state:
    st.session_state.history = []

# 主界面布局
st.sidebar.title("会话")
st.sidebar.button("新建对话", on_click=lambda: st.session_state.update({"history": []}))
st.sidebar.markdown(f"**当前会话** ({len(st.session_state.history) // 2} 条对话)")

# 显示对话历史记录
st.markdown("### 对话记录")
chat_history_container = st.container()
with chat_history_container:
    if st.session_state.history:
        for message in st.session_state.history:
            st.markdown(f"<div class='chat-history'>{message}</div>", unsafe_allow_html=True)
    else:
        st.markdown("<div class='chat-history'>暂无对话记录</div>", unsafe_allow_html=True)

# 输入区域和发送按钮
prompt = st.text_input("Enter your prompt here:", "", key="input", placeholder="输入消息,Shift + Enter 换行 / 触发补全,触发命令", label_visibility="collapsed")

if st.button("发送"):
    if prompt:
        response = chat_with_model(prompt, model_id, st.session_state.history)
        st.session_state.history.append(f"User: {prompt}")
        st.session_state.history.append(f"Model: {response}")
        st.experimental_rerun()  # 刷新页面以显示新对话
    else:
        st.write("Please enter a prompt.")

运行之后界面如下所示

相关推荐
Tbisnic6 小时前
LangChain的 六大核心组件与 RAG 知识库构建
人工智能·python·ai·langchain·rag·langgraph
gb42152878 小时前
python中pypdf库和langchain-unstructured库在解析pdf文件的时候的区别?
python·langchain·pdf
badhope10 小时前
用RAG做了个智能客服,上线第一天就被用户骂了——我的7天实战复盘
人工智能·langchain
Wang's Blog16 小时前
AI Agent白手起家44: LangChain 文档切分实战 — 长度、文本架构与语义切片
人工智能·langchain
jaboo1216 小时前
postgresql从入门到精通
数据库·postgresql·langchain
math_hongfan16 小时前
鸿蒙多模态AI交互高级:图文+语音+手势融合交互/多模态大模型端侧适配/跨模态检索高阶实战
人工智能·学习·华为·交互·语音识别·harmonyos·鸿蒙
闲猫1 天前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain
程序员AI工坊1 天前
Agent 开发:ReAct 循环与工具调用实战——从单次调用到自主 Agent
人工智能·后端·python·langchain·agent·react
Wang's Blog1 天前
AI Agent白手起家41: LangChain 链的高级应用:函数、记忆、路由与容错
langchain