利用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.")

运行之后界面如下所示

相关推荐
元Y亨H4 小时前
大模型技术 模型调用结果解析 概述
google·langchain·llm
mingo_敏8 小时前
DeepAgents : 权限(Permissions)
人工智能·深度学习·langchain
眼泪划过的星空14 小时前
基于 LangChain 框架构建本地化 RAG 系统:无需调用云端 API 的完整实践
人工智能·langchain
YUS云生16 小时前
大模型学习·第41天:LangChain进阶——提示词模板与Chain链式调用
学习·langchain·c#
国际云,接待21 小时前
AWS RDS 备份与 PITR 恢复演练:从保留期、恢复命令到应用切换验证
运维·云计算·aws·灾难恢复·数据库备份·rds
颜酱2 天前
13 | 使用 LangChain 生成 SQL
人工智能·python·langchain
小猪咪piggy2 天前
【AI 赋能测试】Langchain 框架的使用
人工智能·chrome·langchain
艾斯特_2 天前
langGraph
人工智能·python·langchain
未知违规用户2 天前
大模型项目:知识库构建工具与 LangChain 操作ChromaDB
人工智能·python·langchain·numpy
chaors2 天前
DeepResearchSystem 0x04:MAS 进阶
langchain·agent·ai编程