探索人工智能的世界:構(gòu)建智能問答系統(tǒng)之環(huán)境篇
引言
在之前的一篇文章中,我們已經(jīng)對項目所需的知識點進行了簡單的了解。今天,我們將開始搭建整個項目的環(huán)境。在接下來的文章中,我們將詳細介紹如何配置項目所需的各種工具和環(huán)境,以確保項目的順利進行。
部署
接下來,你可能需要多次重啟電腦來確保環(huán)境變量的生效。這是必要的,因為在進行開發(fā)之前,我們必須確保環(huán)境設(shè)置正確。
Python
你可以在官方網(wǎng)站的下載頁面找到Python的最新版本(3.10.*)下載地址:https://www.python.org/downloads/windows/
下載完成后,你可以按照默認設(shè)置一直點擊"下一步"進行安裝。如果你不想立即重啟電腦,可以繼續(xù)安裝Docker Desktop。
圖片
Docker Desktop
Docker Desktop是一個在Windows上運行的可視化工具,它可以讓你更方便地管理和運行docker容器。安裝Docker的目的是為了將數(shù)據(jù)庫環(huán)境獨立運行起來,以便更好地進行開發(fā)和測試。
你可以在官方網(wǎng)站上找到Docker Desktop的下載地址:https://docs.docker.com/desktop/install/windows-install/
在該頁面上,你可以找到適用于Windows的Docker Desktop的安裝文件。下載完成后,你可以按照安裝向?qū)б徊讲竭M行安裝。
安裝完成后,你可以使用Docker Desktop來創(chuàng)建、啟動和停止容器,以及管理容器的網(wǎng)絡(luò)和存儲等設(shè)置。通過將數(shù)據(jù)庫環(huán)境單獨運行在docker容器中,你可以更好地隔離和管理數(shù)據(jù)庫,使開發(fā)流程更加高效和可靠。
圖片
安裝完成后,你可以進行一次電腦重啟,以使環(huán)境變量生效。請注意,Python和Docker的環(huán)境變量無需手動配置,它們在安裝過程中已經(jīng)自動配置好了。所以,你只需要重啟電腦即可讓這些環(huán)境變量生效。重啟后,你就可以開始使用Python和Docker了。
Visual Studio Code
Visual Studio Code是一個免費的源代碼編輯器,適用于Windows、macOS和Linux操作系統(tǒng)。它是一款輕量級但功能強大的工具,被廣泛用于開發(fā)各種編程語言和技術(shù)。
官方下載地址:https://code.visualstudio.com/Download
圖片
ps:首先,我本想嘗試使用cursor進行開發(fā),因為它具備智能AI編程功能,可以提高效率。然而,在啟動dev container的過程中,我遇到了一系列錯誤,盡管我進行了多次資料搜索,但仍然沒有找到解決方法。因此,我決定放棄使用cursor。
百度飛槳
現(xiàn)在,我們可以根據(jù)我們上一篇文章中已經(jīng)建立好的目錄和文件開始安裝開發(fā)環(huán)境了。有些同學(xué)可能會認為百度飛槳平臺已經(jīng)預(yù)裝了開發(fā)環(huán)境,可以直接開始開發(fā),但是我想說,如果只是進行一些簡單的練習(xí),這樣確實沒有問題,但是如果是個人或企業(yè)級的開發(fā)項目,通常還是需要在本地進行開發(fā)和部署。因此,我將按照較為復(fù)雜的方式進行說明。如果你對百度飛槳平臺感興趣,也可以去嘗試,我這里就不再演示了,因為他們提供了快速開發(fā)的詳細文檔。
百度飛槳平臺的官方地址是:https://aistudio.baidu.com/index
圖片
如果你在cmd命令行中使用docker version命令,你會在控制臺中看到相應(yīng)的輸出,這意味著我們可以開始發(fā)布數(shù)據(jù)庫容器了。
圖片
如果你對此感興趣,也可以去看一下官方提供的快速開發(fā)示例。你可以通過訪問官方網(wǎng)站來獲取更多相關(guān)信息:https://milvus.io/
我已經(jīng)將官方提供的配置文件拉到我們的配置文件中了,所以你不需要再去查看官方的示例了。此外,我們的配置文件中還包含了一個我們自己的容器。將來,我們計劃將我們開發(fā)的程序作為一個接口供其他組件調(diào)用,所以我們先把這部分工作先完成了。
配置文件
以下是docker-compose.yaml的配置文件
version: '3'
services:
xiaoyu-chat:
build:
context: .
dockerfile: Dockerfile
target: ${TARGET:-dev} # Default value is "dev"
args:
- MAKE=${MAKE}
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
etcd:
container_name: milvus-etcd
image: quay.io/coreos/etcd:v3.5.0
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1000
- ETCD_QUOTA_BACKEND_BYTES=4294967296
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd
command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
minio:
container_name: milvus-minio
image: minio/minio:RELEASE.2020-12-03T00-03-10Z
environment:
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data
command: minio server /minio_data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
standalone:
container_name: milvus-standalone
image: milvusdb/milvus:v2.3.2
command: ["milvus", "run", "standalone"]
environment:
ETCD_ENDPOINTS: etcd:2379
MINIO_ADDRESS: minio:9000
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
ports:
- "19530:19530"
depends_on:
- "etcd"
- "minio"
networks:
default:
name: milvus
下面是 Dockerfile,由于我沒有編寫具體的業(yè)務(wù)代碼,所以最后一行僅創(chuàng)建了一個空的容器,沒有運行任何文件。請在完成后直接替換此行為您的實際運行命令。
FROM python:3.10 as base
WORKDIR /app
# setup code
COPY . .
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ poetry==1.4.2
# Install dependencies using Poetry
RUN poetry config virtualenvs.create true && \
poetry install --no-interaction --no-ansi
# image to dev
FROM base as dev
CMD sh -c "while sleep 1000; do :; done"
下面是pyproject.toml的配置文件
[tool.poetry]
name = "xiaoyu-chat"
version = "0.1.0"
description = "AI QA"
authors = ["xiaoyu"]
[tool.poetry.dependencies]
python = "^3.10, <3.11"
fastapi = "^0.104.1"
ipykernel = "^6.26.0"
langchain = "^0.0.326"
openai = "^0.28.1"
[[tool.poetry.source]]
name = "aliyun"
url = "https://mirrors.aliyun.com/pypi/simple/"
default = true
secondary = false
如果您使用了 Git,我已經(jīng)為您寫好了 .gitignore 文件,內(nèi)容如下:
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
volumes/
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
還差最后一個環(huán)境變量env的文件了,這一步非常簡單。我們需要設(shè)置一個調(diào)用openai的key,如果你不知道如何獲取,可以參考我之前在公眾號上發(fā)布的文章。
OPENAI_API_KEY='sk-UvHNty93g44iGO1ydfgNT12lbkFJYK6WqgOTxjtIIL6xxxd9'
非常好!現(xiàn)在我們已經(jīng)完成了環(huán)境的搭建,接下來可以安裝VS Code中的Dev Container插件了。你可以選擇不使用Dev Container插件來編排容器,也可以直接使用Docker命令進行操作,只是使用Dev Container插件可以讓你在容器內(nèi)部直接進行代碼和環(huán)境的操作等等。
下面是devcontainers.json配置文件的內(nèi)容:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"dockerComposeFile": "../docker-compose.yaml",
"service": "xiaoyu-chat",
"workspaceFolder": "/app",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter"
]
}
},
"forwardPorts": [3000,4444,7900,5900],
"shutdownAction": "stopCompose"
}
現(xiàn)在,當(dāng)你使用Ctrl+Shift+P快捷鍵打開命令面板,并選擇"Reopen in Container"命令來啟動Dev Container時,它就會開始編排容器了。請耐心等待,直到容器啟動完成。
在啟動的容器中,你可以添加一個控制臺,以便直接進入容器內(nèi)部并修改環(huán)境。這樣你就可以輕松地進行容器環(huán)境的調(diào)整和配置了。
圖片
在下方的圖示中,你可以看到兩個命令。第一個命令是用來進入容器的,而第二個命令則是用來退出容器的。
圖片
此時,你可以查看Docker Desktop的運行情況,以便了解其狀態(tài)和性能。以后,無論你是否進入Dev Container,你都可以直接運行你的容器,就像開啟了一個數(shù)據(jù)庫服務(wù)一樣簡單。這樣可以讓你更方便地管理和使用Docker容器。
圖片
總結(jié)
經(jīng)過上述步驟,我們的項目已經(jīng)成功啟動。在明天的文章中,我將帶大家一起了解代碼案例,并演示如何使用整個流程。通過這些代碼案例,我們將深入了解項目的具體實現(xiàn)細節(jié),并掌握如何在實際項目中使用這些工具和環(huán)境。