LLM 的实用指南:从基础知识到使用 LLMOps 最佳实践将高级 LLM 和 RAG 应用部署到 AWS
LLM 的实用指南:从基础知识到使用 LLMOps 最佳实践将高级 LLM 和 RAG 应用部署到 AWS
Find the book on Amazon or Packt
The goal of this book is to create your own end-to-end LLM-based system using best practices:
You can download and use the final trained model on Hugging Face.
[!IMPORTANT] The code in this GitHub repository is actively maintained and may contain updates not reflected in the book. Always refer to this repository for the latest version of the code.
To install and run the project locally, you need the following dependencies.
| Tool | Version | Purpose | Installation Link |
|---|---|---|---|
| pyenv | ≥2.3.36 | Multiple Python versions (optional) | Install Guide |
| Python | 3.11 | Runtime environment | Download |
| Poetry | >= 1.8.3 and < 2.0 | Package management | Install Guide |
| Docker | ≥27.1.1 | Containerization | Install Guide |
| AWS CLI | ≥2.15.42 | Cloud management | Install Guide |
| Git | ≥2.44.0 | Version control | Download |
The code also uses and depends on the following cloud services. For now, you don't have to do anything. We will guide you in the installation and deployment sections on how to use them:
| Service | Purpose |
|---|---|
| HuggingFace | Model registry |
| Comet ML | Experiment tracker |
| Opik | Prompt monitoring |
| ZenML | Orchestrator and artifacts layer |
| AWS | Compute and storage |
| MongoDB | NoSQL database |
| Qdrant | Vector database |
| GitHub Actions | CI/CD pipeline |
In the LLM Engineer's Handbook, Chapter 2 will walk you through each tool. Chapters 10 and 11 provide step-by-step guides on how to set up everything you need.
Here is the directory overview:
.
├── code_snippets/ # Standalone example code
├── configs/ # Pipeline configuration files
├── llm_engineering/ # Core project package
│ ├── application/
│ ├── domain/
│ ├── infrastructure/
│ ├── model/
├── pipelines/ # ML pipeline definitions
├── steps/ # Pipeline components
├── tests/ # Test examples
├── tools/ # Utility scripts
│ ├── run.py
│ ├── ml_service.py
│ ├── rag.py
│ ├── data_warehouse.pyllm_engineering/ is the main Python package implementing LLM and RAG functionality. It follows Domain-Driven Design (DDD) principles:
domain/: Core business entities and structuresapplication/: Business logic, crawlers, and RAG implementationmodel/: LLM training and inferenceinfrastructure/: External service integrations (AWS, Qdrant, MongoDB, FastAPI)The code logic and imports flow as follows: infrastructure → model → application → domain
pipelines/: Contains the ZenML ML pipelines, which serve as the entry point for all the ML pipelines. Coordinates the data processing and model training stages of the ML lifecycle.
steps/: Contains individual ZenML steps, which are reusable components for building and customizing ZenML pipelines. Steps perform specific tasks (e.g., data loading, preprocessing) and can be combined within the ML pipelines.
tests/: Covers a few sample tests used as examples within the CI pipeline.
tools/: Utility scripts used to call the ZenML pipelines and inference code:
run.py: Entry point script to run ZenML pipelines.ml_service.py: Starts the REST API inference server.rag.py: Demonstrates usage of the RAG retrieval module.data_warehouse.py: Used to export or import data from the MongoDB data warehouse through JSON files.configs/: ZenML YAML configuration files to control the execution of pipelines and steps.
code_snippets/: Independent code examples that can be executed independently.
[!NOTE] If you are experiencing issues while installing and running the repository, consider checking the Issues GitHub section for other people who solved similar problems or directly asking us for help.
Start by cloning the repository and navigating to the project directory:
git clone https://github.com/PacktPublishing/LLM-Engineers-Handbook.git
cd LLM-Engineers-Handbook Next, we have to prepare your Python environment and its adjacent dependencies.
The project requires Python 3.11. You can either use your global Python installation or set up a project-specific version using pyenv.
Verify your Python version:
python --version # Should show Python 3.11.xpyenv --version # Should show pyenv 2.3.36 or laterpyenv install 3.11.8python --version # Should show Python 3.11.8python --version
# Output: Python 3.11.8[!NOTE]
The project includes a.python-versionfile that automatically sets the correct Python version when you're in the project directory.
The project uses Poetry for dependency management.
poetry --version # Should show Poetry version 1.8.3 or laterpoetry env use 3.11
poetry install --without aws
poetry run pre-commit installThis will:
As our task manager, we run all the scripts using Poe the Poet.
poetry shellpoetry poe ...If you're experiencing issues with poethepoet, you can still run the project commands directly through Poetry. Here's how:
pyproject.tomlpoetry run with the underlying commandInstead of:
poetry poe local-infrastructure-upUse the direct command from pyproject.toml:
poetry run <actual-command-from-pyproject-toml>Note: All project commands are defined in the [tool.poe.tasks] section of pyproject.toml
Now, let's configure our local project with all the necessary credentials and tokens to run the code locally.
After you have installed all the dependencies, you must create and fill a .env file with your credentials to appropriately interact with other services and run the project. Setting your sensitive credentials in a .env file is a good security practice, as this file won't be committed to GitHub or shared with anyone else.
cp .env.example .env # The file must be at your repository's root!.env file to get you started. The following are the mandatory settings we must complete when working locally:To authenticate to OpenAI's API, you must fill out the OPENAI_API_KEY env var with an authentication token.
OPENAI_API_KEY=your_api_key_here→ Check out this tutorial to learn how to provide one from OpenAI.
To authenticate to Hugging Face, you must fill out the HUGGINGFACE_ACCESS_TOKEN env var with an authentication token.
HUGGINGFACE_ACCESS_TOKEN=your_token_here→ Check out this tutorial to learn how to provide one from Hugging Face.
To authenticate to Comet ML (required only during training) and Opik, you must fill out the COMET_API_KEY env var with your authentication token.
COMET_API_KEY=your_api_key_here→ Check out this tutorial to learn how to get started with Opik. You can also access Opik's dashboard using this link.
When deploying the project to the cloud, we must set additional settings for Mongo, Qdrant, and AWS. If you are just working locally, the default values of these env vars will work out of the box. Detailed deployment instructions are available in Chapter 11 of the LLM Engineer's Handbook.
We must change the DATABASE_HOST env var with the URL pointing to your cloud MongoDB cluster.
DATABASE_HOST=your_mongodb_url→ Check out this tutorial to learn how to create and host a MongoDB cluster for free.
Change USE_QDRANT_CLOUD to true, QDRANT_CLOUD_URL with the URL point to your cloud Qdrant cluster, and QDRANT_APIKEY with its API key.
USE_QDRANT_CLOUD=true
QDRANT_CLOUD_URL=your_qdrant_cloud_url
QDRANT_APIKEY=your_qdrant_api_key→ Check out this tutorial to learn how to create a Qdrant cluster for free
For your AWS set-up to work correctly, you need the AWS CLI installed on your local machine and properly configured with an admin user (or a user with enough permissions to create new SageMaker, ECR, and S3 resources; using an admin user will make everything more straightforward).
Chapter 2 provides step-by-step instructions on how to install the AWS CLI, create an admin user on AWS, and get an access key to set up the AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables. If you already have an AWS admin user in place, you have to configure the following env vars in your .env file:
AWS_REGION=eu-central-1 # Change it with your AWS region.
AWS_ACCESS_KEY=your_aws_access_key
AWS_SECRET_KEY=your_aws_secret_keyAWS credentials are typically stored in ~/.aws/credentials. You can view this file directly using cat or similar commands:
暂无开放 Issues,或尚未同步最近议题。