[AI Embedchain] 集成 Streamlit

简介: [AI Embedchain] 集成 Streamlit

在这个例子中,我们将学习如何使用mistralai/Mixtral-8x7B-Instruct-v0.1和Embedchain与Streamlit一起构建一个简单的RAG聊天机器人。

Streamlit + Embedchain Demo

设置

安装Embedchain和Streamlit。

pip install embedchain streamlit

app.py

import os
from embedchain import App
import streamlit as st

with st.sidebar:
    huggingface_access_token = st.text_input("Hugging face Token", key="chatbot_api_key", type="password")
    "[获取Hugging Face访问令牌](https://huggingface.co/settings/tokens)"
    "[查看源代码](https://github.com/embedchain/examples/mistral-streamlit)"

st.title("💬 聊天机器人")
st.caption("🚀 一个由Mistral提供支持的Embedchain应用!")
if "messages" not in st.session_state:
    st.session_state.messages = [
        {
   
   
            "role": "assistant",
            "content": """
        嗨!我是一个聊天机器人。我可以回答问题并学习新事物!\n
        问我任何事情,如果你想让我学习新东西,请输入`/add <source>`。\n
        我几乎可以学习一切。:)
        """,
        }
    ]

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("问我任何事情!"):
    if not st.session_state.chatbot_api_key:
        st.error("请输入你的Hugging Face访问令牌")
        st.stop()

    os.environ["HUGGINGFACE_ACCESS_TOKEN"] = st.session_state.chatbot_api_key
    app = App.from_config(config_path="config.yaml")

    if prompt.startswith("/add"):
        with st.chat_message("user"):
            st.markdown(prompt)
            st.session_state.messages.append({
   
   "role": "user", "content": prompt})
        prompt = prompt.replace("/add", "").strip()
        with st.chat_message("assistant"):
            message_placeholder = st.empty()
            message_placeholder.markdown("添加到知识库中...")
            app.add(prompt)
            message_placeholder.markdown(f"已将{prompt}添加到知识库!")
            st.session_state.messages.append({
   
   "role": "assistant", "content": f"已将{prompt}添加到知识库!"})
            st.stop()

    with st.chat_message("user"):
        st.markdown(prompt)
        st.session_state.messages.append({
   
   "role": "user", "content": prompt})

    with st.chat_message("assistant"):
        msg_placeholder = st.empty()
        msg_placeholder.markdown("思考中...")
        full_response = ""

        for response in app.chat(prompt):
            msg_placeholder.empty()
            full_response += response

        msg_placeholder.markdown(full_response)
        st.session_state.messages.append({
   
   "role": "assistant", "content": full_response})

config.yaml

app:
    config:
        name: 'mistral-streamlit-app'

llm:
    provider: huggingface
    config:
        model: 'mistralai/Mixtral-8x7B-Instruct-v0.1'
        temperature: 0.1
        max_tokens: 250
        top_p: 0.1
        stream: true

embedder:
    provider: huggingface
    config:
        model: 'sentence-transformers/all-mpnet-base-v2'

在本地运行

streamlit run app.py

引用

相关文章
|
6天前
|
机器学习/深度学习 人工智能 搜索推荐
如何让你的Uno Platform应用秒变AI大神?从零开始,轻松集成机器学习功能,让应用智能起来,用户惊呼太神奇!
【9月更文挑战第8天】随着技术的发展,人工智能与机器学习已融入日常生活,特别是在移动应用开发中。Uno Platform 是一个强大的框架,支持使用 C# 和 XAML 开发跨平台应用(涵盖 Windows、macOS、iOS、Android 和 Web)。本文探讨如何在 Uno Platform 中集成机器学习功能,通过示例代码展示从模型选择、训练到应用集成的全过程,并介绍如何利用 Onnx Runtime 等库实现在 Uno 平台上的模型运行,最终提升应用智能化水平和用户体验。
18 1
|
28天前
|
人工智能 iOS开发 UED
详解苹果和微软的AI集成策略
详解苹果和微软的AI集成策略
详解苹果和微软的AI集成策略
|
1月前
|
人工智能 前端开发 API
[AI Embedchain] 开始使用 - 全栈
[AI Embedchain] 开始使用 - 全栈
[AI Embedchain] 开始使用 - 全栈
|
1月前
|
人工智能
[AI Embedchain] 集成 Chainlit
[AI Embedchain] 集成 Chainlit
[AI Embedchain] 集成 Chainlit
|
1月前
|
人工智能 监控 Python
[AI Embedchain] 集成 Langsmith
[AI Embedchain] 集成 Langsmith
[AI Embedchain] 集成 Langsmith
|
29天前
|
人工智能 监控 数据可视化
[AI Embedchain] 集成 OpenLIT
[AI Embedchain] 集成 OpenLIT
|
29天前
|
人工智能 监控 测试技术
[AI Embedchain] 集成 Helicone
[AI Embedchain] 集成 Helicone
|
1月前
|
人工智能 Python
[AI Embedchain] 开始使用 - 快速开始
[AI Embedchain] 开始使用 - 介绍
|
1月前
|
人工智能 搜索推荐 物联网
移动应用开发的未来趋势:从跨平台到AI集成
在数字化浪潮的推动下,移动应用已成为我们日常生活不可或缺的一部分。本文将探讨移动应用开发领域的最新进展,特别是跨平台框架和人工智能技术的融合如何塑造这一行业。通过分析当前技术栈、工具和最佳实践,我们将揭示未来移动应用开发的趋势,并讨论这些变化对开发者、企业和最终用户的意义。
56 1
|
18天前
|
存储 人工智能 关系型数据库
PolarDB 与 AI/ML 集成的应用案例
【8月更文第27天】随着大数据和人工智能技术的发展,越来越多的企业开始探索将关系型数据库与 AI/ML 技术相结合的方式,以提高数据分析效率和业务智能化水平。阿里云的 PolarDB 是一款高性能的关系型数据库服务,支持多种数据库引擎,如 MySQL、PostgreSQL 和 Oracle。通过与阿里云的其他 AI/ML 服务集成,PolarDB 能够为企业提供端到端的数据处理和分析解决方案。
56 0

热门文章

最新文章