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

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
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具