4
博客免费使用oceanbase cloud搭建rag聊天机器人

免费使用oceanbase cloud搭建rag聊天机器人-c7电子娱乐

2024-11-21用户实践

最近官方推出了免费试用365天的云数据库,版本也升级到了4.3.支持了向量功能.

官方推出了活动体验ai的活动, 教程中使用了docker单机版数据库,既然有免费的云数据库,就优先使用云数据库体验一下.

1. 云环境申请

在c7电子娱乐官网的c7电子娱乐主页有有一个大大的标题,ob cloud 365天免费试用.经过简单的操作后,大约等5分钟,就创建了一个免费的数据库实例了.

1732173867

点击右上方的"三个点",依次创建用户、创建数据库、获取连接串,就可以通过公网连接云上数据库了,要谨慎添加白名单,避免资源被非法连接.

进入实例控制台后,点击"参数管理",设置ob_vector_memory_limit_percentage,启用向量检索功能,将参数值设置为30.


2. 安装python

要求的python版本大于等于3.9,小于4.0.我使用的是3.9.6.

# yum install python39


3. clone项目

# git clone https://github.com/oceanbase-devhub/ai-workshop-2024.git

4. 安装poetry

poetry是python的依赖和包管理工具,安装包更简单也更方便.

# python3 -m pip install poetry
# cd ~/ai-workshop-2024
# poetry install

如果下载包比较慢,可以将官方源换为阿里源

# cd ai-workshop-2024
# vi pyproject.toml
// 删除下面的源信息
[[tool.poetry.source]]
name = "pypi"
priority = "primary"
			
[[tool.poetry.source]]
name = "tuna"
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
priority = "supplemental"
// 添加下面的源信息
[[tool.poetry.source]]
name = "ali"
url = "https://mirrors.aliyun.com/pypi/simple/"
priority = "primary"
			
[[tool.poetry.source]]
name = "tuna"
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
priority = "supplemental"
//使配置生效
# poetry lock


接下来配置环境变量

# cp .env.example .env
# vi .env
api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx	# 替换https://open.bigmodel.cn/usercenter/apikeys 智谱ai的api key
llm_model="glm-4-flash"
llm_base_url="https://open.bigmodel.cn/api/paas/v4/" # bigmodel (zhipuai)
# llm_base_url="https://api.openai.com/v1/" # openai
# llm_base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" # dashscope (alibaba)
hf_endpoint=https://hf-mirror.com
bge_model_path=baai/bge-m3
ollama_url=
ollama_token=
openai_api_key=
openai_base_url=
openai_embedding_model=
db_host="127.0.0.1"	# 数据库的ip或域名
db_port="2881"		# 数据库的端口
db_user="root@test"	# 连接的用户名
db_name="test"		# 连接的数据库名
db_password=""		# 连接的密码

5. 准备bge-m3 模型

# poetry run python utils/prepare_bgem3.py
		
===================================
bgem3flagmodel loaded successfully!
===================================

出现以上的输出,就成功了.

6. 准备文档数据

从github克隆文档数据

# cd doc_repos
# git config --global http.postbuffer 16000m  // 增加修改buffer大小
# git config --global core.compression -1 // 启动压缩
# git clone --single-branch --branch v4.3.3 https://github.com/oceanbase/oceanbase-doc.git --depth 1  //如果git报错,添加后面的参数
# git clone --single-branch --branch v4.3.0 https://github.com/oceanbase/ocp-doc.git
# git clone --single-branch --branch v4.3.1 https://github.com/oceanbase/odc-doc.git
# git clone --single-branch --branch v4.2.5 https://github.com/oceanbase/oms-doc.git
# git clone --single-branch --branch v2.10.0 https://github.com/oceanbase/obd-doc.git
# git clone --single-branch --branch v4.3.0 https://github.com/oceanbase/oceanbase-proxy-doc.git
# cd ..


把文档的标题转换为标准的 markdown 格式

# poetry run python convert_headings.py \
  doc_repos/oceanbase-doc/zh-cn \
  doc_repos/ocp-doc/zh-cn \
  doc_repos/odc-doc/zh-cn \
  doc_repos/oms-doc/zh-cn \
  doc_repos/obd-doc/zh-cn \
  doc_repos/oceanbase-proxy-doc/zh-cn

生成文档向量和元数据,等待时间比较长,并且相当看硬件性能,是个不错的性能压测工具

# poetry run python embed_docs.py --doc_base doc_repos/oceanbase-doc/zh-cn
# poetry run python embed_docs.py --doc_base doc_repos/ocp-doc/zh-cn --component ocp
# poetry run python embed_docs.py --doc_base doc_repos/odc-doc/zh-cn --component odc
# poetry run python embed_docs.py --doc_base doc_repos/oms-doc/zh-cn --component oms
# poetry run python embed_docs.py --doc_base doc_repos/obd-doc/zh-cn --component obd
# poetry run python embed_docs.py --doc_base doc_repos/oceanbase-proxy-doc/zh-cn --component odp

保存加载数据

# poetry run python utils/extract.py --output_file ~/my-data.json


加载预处理的文档数据

# poetry run python utils/load.py --source_file ~/my-data.json

7. 验证数据库和数据

进入.env配置文件中的数据库,会有一个新表,表名是corpus,表结构中的embedding列的数据类型是vector(1024),这个类型就是向量类型,

mysql> desc corpus;
 ---------------- --------------- ------ ----- --------- ------- 
| field          | type          | null | key | default | extra |
 ---------------- --------------- ------ ----- --------- ------- 
| id             | varchar(4096) | no   | pri | null    |       |
| embedding      | vector(1024)  | yes  |     | null    |       |
| document       | longtext      | yes  |     | null    |       |
| metadata       | json          | yes  |     | null    |       |
| component_code | int(11)       | no   | pri | null    |       |
 ---------------- --------------- ------ ----- --------- ------- 
5 rows in set (0.04 sec)
mysql> select count(*) from corpus;
 ---------- 
| count(*) |
 ---------- 
|     6500 |
 ---------- 
1 row in set (0.05 sec)


8.启动web界面

上面的准备工作已经全部完成,接下来就是激动人心的时刻了,原神启动!!!(走错片场了)

# poetry run streamlit run --server.runonsave false chat_ui.py
		
    you can now view your streamlit app in your browser.
		
	local url: http://localhost:8501
	network url: http://172.xxx.xxx.xxx:8501
	external url: http://xxx.xxx.xxx.xxx:8501 # 这是您可以从浏览器访问的 url

刚好streamlit提供服务的ip都不是对外的,修改.streamlit/config.toml,指定对外服务的ip和端口

[server]
port = 8501
enablecors = false
		 
[browser]
serveraddress = "192.168.56.110"
gatherusagestats = false

重启后,ip被绑定到了192.168.56.110上.

接下来就是一个尖锐的问题(开玩笑)


1732176929

又提问了一个问题,总体来说还是不错的

1732176981


参考文档:


点赞4
收藏

声明

本网站下的“博客”等板块为技术爱好者提供分享、交流的平台。发布者发布的任何内容、信息等,并不反映或代表本网站的观点、立场或政策。本网站不对其任何内容和信息的错误以及由此产生的损失或损坏承担任何责任。

尊重知识产权是本网站的基本原则之一,如您在使用本网站过程中发现本网站中存在侵犯您或其他第三人合法知识产权的情况,请您即可将侵权材料及初步证据提交至下述邮箱:obcompliance@oceanbase.com 。本网站将在收到材料后尽快进行审核及处理。

已发布 3 篇博文

网站地图