首页 > 后端开发 > Python教程 > 在 Upsun 上使用 RAG 试验 Chainlit AI 界面

在 Upsun 上使用 RAG 试验 Chainlit AI 界面

Patricia Arquette
发布: 2025-01-21 00:14:14
原创
460 人浏览过

Chainlit:可扩展的对话式人工智能框架

Chainlit 是一个开源异步 Python 框架,旨在构建强大且可扩展的对话式 AI 应用程序。 它提供了灵活的基础,允许开发人员无缝集成外部 API、自定义逻辑和本地模型。

Experiment with Chainlit AI interface with RAG on Upsun

本教程演示了 Chainlit 中的两种检索增强生成 (RAG) 实现:

  1. 利用 OpenAI 助手上传文档。
  2. 将 llama_index 与本地文档文件夹结合使用。

本地 Chainlit 设置

虚拟环境

创建虚拟环境:

mkdir chainlit && cd chainlit
python3 -m venv venv
source venv/bin/activate
登录后复制
登录后复制

安装依赖项

安装所需的包并保存依赖项:

pip install chainlit
pip install llama_index  # For implementation #2
pip install openai
pip freeze > requirements.txt
登录后复制
登录后复制

测试 Chainlit

启动Chainlit:

chainlit hello
登录后复制

访问占位符https://www.php.cn/link/2674cea93e3214abce13e072a2dc2ca5

Experiment with Chainlit AI interface with RAG on Upsun

Upsun 部署

Git 初始化

初始化 Git 存储库:

git init .
登录后复制

创建.gitignore文件:

<code>.env
database/**
data/**
storage/**
.chainlit
venv
__pycache__</code>
登录后复制

Upsun 项目创建

使用 CLI 创建 Upsun 项目(按照提示操作)。 Upsun 将自动配置远程存储库。

配置

Chainlit 的 Upsun 配置示例:

applications:
  chainlit:
    source:
      root: "/"
    type: "python:3.11"
    mounts:
      "/database":
        source: "storage"
        source_path: "database"
      ".files":
        source: "storage"
        source_path: "files"
      "__pycache__":
        source: "storage"
        source_path: "pycache"
      ".chainlit":
        source: "storage"
        source_path: ".chainlit"
    web:
      commands:
        start: "chainlit run app.py --port $PORT --host 0.0.0.0"
      upstream:
        socket_family: tcp
      locations:
        "/":
          passthru: true
        "/public":
          passthru: true
    build:
      flavor: none
    hooks:
      build: |
        set -eux
        pip install -r requirements.txt
      deploy: |
        set -eux
      # post_deploy: |
routes:
  "https://{default}/":
    type: upstream
    upstream: "chainlit:http"
  "https://www.{default}":
    type: redirect
    to: "https://{default}/"
登录后复制

通过 Upsun CLI 设置 OPENAI_API_KEY 环境变量:

upsun variable:create env:OPENAI_API_KEY --value=sk-proj[...]
登录后复制

部署

提交并部署:

git add .
git commit -m "First chainlit example"
upsun push
登录后复制

查看部署状态。 成功部署将显示 Chainlit 在您的主环境中运行。

Experiment with Chainlit AI interface with RAG on Upsun

实现一:OpenAI助手及上传文件

此实现使用 OpenAI 助手来处理上传的文档。

辅助创作

在 OpenAI 平台上创建一个新的 OpenAI 助手。设置系统指令,选择模型(带有文本响应格式),并保持较低的温度(例如0.10)。 复制助手 ID (asst_[xxx]) 并将其设置为环境变量:

upsun variable:create env:OPENAI_ASSISTANT_ID --value=asst_[...]
登录后复制

内容上传

将您的文档(首选 Markdown)上传到助手。 OpenAI 将创建一个矢量存储。

Experiment with Chainlit AI interface with RAG on Upsun

Experiment with Chainlit AI interface with RAG on Upsun

助理逻辑(app.py)

app.py 内容替换为提供的代码。 关键部分:@cl.on_chat_start 创建一个新的 OpenAI 线程,@cl.on_message 将用户消息发送到该线程并流式传输响应。

提交并部署更改。测试助手。

Experiment with Chainlit AI interface with RAG on Upsun

实现2:OpenAI llama_index

此实现使用 llama_index 进行本地知识管理,并使用 OpenAI 进行响应生成。

创建分支

创建一个新分支:

mkdir chainlit && cd chainlit
python3 -m venv venv
source venv/bin/activate
登录后复制
登录后复制

文件夹创建和安装

创建 datastorage 文件夹。将安装添加到 Upsun 配置中。

app.py 更新

使用提供的 llama_index 代码更新 app.py。 此代码加载文档,创建 VectorStoreIndex,并使用它通过 OpenAI 回答查询。

部署新环境并上传data文件夹。测试应用程序。

Experiment with Chainlit AI interface with RAG on Upsun

奖励:身份验证

使用 SQLite 数据库添加身份验证。

数据库设置

创建一个 database 文件夹并将挂载添加到 Upsun 配置中。为数据库路径创建环境变量:

pip install chainlit
pip install llama_index  # For implementation #2
pip install openai
pip freeze > requirements.txt
登录后复制
登录后复制

身份验证逻辑 (app.py)

使用 app.py 将身份验证逻辑添加到 @cl.password_auth_callback。 这会添加一个登录表单。

创建一个脚本来生成哈希密码。将用户添加到数据库(使用散列密码)。部署身份验证和测试登录。

Experiment with Chainlit AI interface with RAG on Upsun

结论

本教程演示了如何在 Upsun 上部署 Chainlit 应用程序,并使用两个 RAG 实现和身份验证。 灵活的架构允许各种适应和集成。

以上是在 Upsun 上使用 RAG 试验 Chainlit AI 界面的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板