首页 互联网资讯 探索人工智能的世界:构建智能问答系统之环境篇

探索人工智能的世界:构建智能问答系统之环境篇

互联网资讯 244
2023-11-09,引言

在之前的一篇文章中,我们已经对项目所需的知识点进行了简单的了解。今天,我们将开始搭建整个项目的环境。在接下来的文章中,我们将详细介绍如何配置项目所需的各种工具和环境,以确保项目的顺利进行。

部署

接下来,你可能需要多次重启电脑来确保环境变量的生效。这是必要的,因为在进行开发之前,我们必须确保环境设置正确。

Python

你可以在官方网站的下载页面找到Python的最新版本(3.10.*)下载地址:https://www.python.org/downloads/windows/

下载完成后,你可以按照默认设置一直点击"下一步"进行安装。如果你不想立即重启电脑,可以继续安装Docker Desktop。

图片

Docker Desktop

Docker Desktop是一个在Windows上运行的可视化工具,它可以让你更方便地管理和运行docker容器。安装Docker的目的是为了将数据库环境独立运行起来,以便更好地进行开发和测试。

你可以在官方网站上找到Docker Desktop的下载地址:https://docs.docker.com/desktop/install/windows-install/

在该页面上,你可以找到适用于Windows的Docker Desktop的安装文件。下载完成后,你可以按照安装向导一步步进行安装。

安装完成后,你可以使用Docker Desktop来创建、启动和停止容器,以及管理容器的网络和存储等设置。通过将数据库环境单独运行在docker容器中,你可以更好地隔离和管理数据库,使开发流程更加高效和可靠。

图片

安装完成后,你可以进行一次电脑重启,以使环境变量生效。请注意,Python和Docker的环境变量无需手动配置,它们在安装过程中已经自动配置好了。所以,你只需要重启电脑即可让这些环境变量生效。重启后,你就可以开始使用Python和Docker了。

Visual Studio Code

Visual Studio Code是一个免费的源代码编辑器,适用于Windows、macOS和Linux操作系统。它是一款轻量级但功能强大的工具,被广泛用于开发各种编程语言和技术。

官方下载地址:https://code.visualstudio.com/Download

图片

ps:首先,我本想尝试使用cursor进行开发,因为它具备智能AI编程功能,可以提高效率。然而,在启动dev container的过程中,我遇到了一系列错误,尽管我进行了多次资料搜索,但仍然没有找到解决方法。因此,我决定放弃使用cursor。

百度飞桨

现在,我们可以根据我们上一篇文章中已经建立好的目录和文件开始安装开发环境了。有些同学可能会认为百度飞桨平台已经预装了开发环境,可以直接开始开发,但是我想说,如果只是进行一些简单的练习,这样确实没有问题,但是如果是个人或企业级的开发项目,通常还是需要在本地进行开发和部署。因此,我将按照较为复杂的方式进行说明。如果你对百度飞桨平台感兴趣,也可以去尝试,我这里就不再演示了,因为他们提供了快速开发的详细文档。

百度飞桨平台的官方地址是:https://aistudio.baidu.com/index

图片

如果你在cmd命令行中使用docker version命令,你会在控制台中看到相应的输出,这意味着我们可以开始发布数据库容器了。

图片

如果你对此感兴趣,也可以去看一下官方提供的快速开发示例。你可以通过访问官方网站来获取更多相关信息:https://milvus.io/

我已经将官方提供的配置文件拉到我们的配置文件中了,所以你不需要再去查看官方的示例了。此外,我们的配置文件中还包含了一个我们自己的容器。将来,我们计划将我们开发的程序作为一个接口供其他组件调用,所以我们先把这部分工作先完成了。

配置文件

以下是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,由于我没有编写具体的业务代码,所以最后一行仅创建了一个空的容器,没有运行任何文件。请在完成后直接替换此行为您的实际运行命令。

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,我已经为您写好了 .gitignore 文件,内容如下:

# 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/

还差最后一个环境变量env的文件了,这一步非常简单。我们需要设置一个调用openai的key,如果你不知道如何获取,可以参考我之前在公众号上发布的文章。

OPENAI_API_KEY='sk-UvHNty93g44iGO1ydfgNT12lbkFJYK6WqgOTxjtIIL6xxxd9'

非常好!现在我们已经完成了环境的搭建,接下来可以安装VS Code中的Dev Container插件了。你可以选择不使用Dev Container插件来编排容器,也可以直接使用Docker命令进行操作,只是使用Dev Container插件可以让你在容器内部直接进行代码和环境的操作等等。

下面是devcontainers.json配置文件的内容:

// 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" }

现在,当你使用Ctrl+Shift+P快捷键打开命令面板,并选择"Reopen in Container"命令来启动Dev Container时,它就会开始编排容器了。请耐心等待,直到容器启动完成。

在启动的容器中,你可以添加一个控制台,以便直接进入容器内部并修改环境。这样你就可以轻松地进行容器环境的调整和配置了。

图片

在下方的图示中,你可以看到两个命令。第一个命令是用来进入容器的,而第二个命令则是用来退出容器的。

图片

此时,你可以查看Docker Desktop的运行情况,以便了解其状态和性能。以后,无论你是否进入Dev Container,你都可以直接运行你的容器,就像开启了一个数据库服务一样简单。这样可以让你更方便地管理和使用Docker容器。

图片

总结

经过上述步骤,我们的项目已经成功启动。在明天的文章中,我将带大家一起了解代码案例,并演示如何使用整个流程。通过这些代码案例,我们将深入了解项目的具体实现细节,并掌握如何在实际项目中使用这些工具和环境。

PS:本文来源:探索人工智能的世界:构建智能问答系统之环境篇,人工智能,代码,系统,人工智能,作者:努力的小雨