Views
No views yet
| 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 | 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 |
| 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 |
1.
2├── code_snippets/ # Standalone example code
3├── configs/ # Pipeline configuration files
4├── llm_engineering/ # Core project package
5│ ├── application/
6│ ├── domain/
7│ ├── infrastructure/
8│ ├── model/
9├── pipelines/ # ML pipeline definitions
10├── steps/ # Pipeline components
11├── tests/ # Test examples
12├── tools/ # Utility scripts
13│ ├── run.py
14│ ├── ml_service.py
15│ ├── rag.py
16│ ├── 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)infrastructure → model → application → domainpipelines/: 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.1git clone https://github.com/PacktPublishing/LLM-Engineers-Handbook.git
2cd LLM-Engineers-Handbook 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.81python --version
2# 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.
poetry --version # Should show Poetry version 1.8.3 or later1poetry env use 3.11
2poetry install --without aws
3poetry run pre-commit installpoetry shellpoetry poe ...poethepoet, you can still run the project commands directly through Poetry. Here's how:pyproject.tomlpoetry run with the underlying commandpoetry poe local-infrastructure-uppoetry run <actual-command-from-pyproject-toml>.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:OPENAI_API_KEY env var with an authentication token.OPENAI_API_KEY=your_api_key_hereHUGGINGFACE_ACCESS_TOKEN env var with an authentication token.HUGGINGFACE_ACCESS_TOKEN=your_token_hereCOMET_API_KEY env var with your authentication token.COMET_API_KEY=your_api_key_hereDATABASE_HOST env var with the URL pointing to your cloud MongoDB cluster.DATABASE_HOST=your_mongodb_urlUSE_QDRANT_CLOUD to true, QDRANT_CLOUD_URL with the URL point to your cloud Qdrant cluster, and QDRANT_APIKEY with its API key.1USE_QDRANT_CLOUD=true
2QDRANT_CLOUD_URL=your_qdrant_cloud_url
3QDRANT_APIKEY=your_qdrant_api_keyAWS_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:1AWS_REGION=eu-central-1 # Change it with your AWS region.
2AWS_ACCESS_KEY=your_aws_access_key
3AWS_SECRET_KEY=your_aws_secret_key~/.aws/credentials. You can view this file directly using cat or similar commands:cat ~/.aws/credentials[!IMPORTANT] Additional configuration options are available in settings.py. Any variable in theSettingsclass can be configured through the.envfile.
[!WARNING] You need Docker installed (>= v27.1.1)
poetry poe local-infrastructure-uppoetry poe local-infrastructure-down[!WARNING]
When running on MacOS, before starting the server, export the following environment variable:export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YESOtherwise, the connection between the local server and pipeline will break. 🔗 More details in this issue. This is done by default when using Poe the Poet.
poetry poe run-inference-ml-service[!IMPORTANT] The LLM microservice, called by the RESTful API, will work only after deploying the LLM to AWS SageMaker.
localhost:8237username: defaultpassword:localhost:6333localhost:6333/dashboardmongodb://llm_engineering:llm_engineering@127.0.0.1:27017twinusername: llm_engineeringpassword: llm_engineeringmongodb://llm_engineering:llm_engineering@127.0.0.1:27017[!IMPORTANT] Everything related to training or running the LLMs (e.g., training, evaluation, inference) can only be run if you set up AWS SageMaker, as explained in the next section on cloud infrastructure.
poetry install --with aws[!NOTE] Chapter 10 provides step-by-step instructions in the section "Implementing the LLM microservice using AWS SageMaker".
.env file) properly configured with an AWS admin user.poetry poe create-sagemaker-rolesagemaker_user_credentials.json file at the root of your repository with your new AWS_ACCESS_KEY and AWS_SECRET_KEY values. But before replacing your new AWS credentials, also run the following command to create the execution role (to create it using your admin credentials).poetry poe create-sagemaker-execution-rolesagemaker_execution_role.json file at the root of your repository with your new AWS_ARN_ROLE value. Add it to your .env file.AWS_ACCESS_KEY, AWS_SECRET_KEY, and AWS_ARN_ROLE values in your .env file, you can use AWS SageMaker. Note that this step is crucial to complete the AWS setup.poetry poe run-training-pipelineconfigs/training.yaml directly in SageMaker. You can visualize the results in Comet ML's dashboard.poetry poe run-evaluation-pipelineconfigs/evaluating.yaml directly in SageMaker. You can visualize the results in *-results datasets saved to your Hugging Face profile.poetry poe deploy-inference-endpointpoetry poe test-sagemaker-endpointpoetry poe delete-inference-endpointAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_ECR_NAMEAWS_REGIONCOMET_API_KEY env var. As Opik is powered by Comet, you don't have to set up anything else along Comet:pipelines/ folder. Thus, when you want to understand or debug a workflow, starting with the ZenML pipeline is the best approach.Pipelines sectionfeature_engineering)feature_engineering_run_2024_06_20_18_40_24)poetry poe run-digital-data-etl[!WARNING] You must have Chrome (or another Chromium-based browser) installed on your system for LinkedIn and Medium crawlers to work (which use Selenium under the hood). Based on your Chrome version, the Chromedriver will be automatically installed to enable Selenium support. Another option is to run everything using our Docker image if you don't want to install Chrome. For example, to run all the pipelines combined you can runpoetry poe run-docker-end-to-end-data-pipeline. Note that the command can be tweaked to support any other pipeline.If, for any other reason, you don't have a Chromium-based browser installed and don't want to use Docker, you have two other options to bypass this Selenium issue:
- Comment out all the code related to Selenium, Chrome and all the links that use Selenium to crawl them (e.g., Medium), such as the
chromedriver_autoinstaller.install()command from application.crawlers.base and other static calls that check for Chrome drivers and Selenium.- Install Google Chrome using your CLI in environments such as GitHub Codespaces or other cloud VMs using the same command as in our Docker file.
configs/digital_data_etl_[author_name].yaml and add them to the links field. Also, you can create a completely new file and specify it at run time, like this: python -m llm_engineering.interfaces.orchestrator.run --run-etl --etl-config-filename configs/digital_data_etl_[your_name].yamlpoetry poe run-feature-engineering-pipelinepoetry poe run-generate-instruct-datasets-pipelinepoetry poe run-generate-preference-datasets-pipelinepoetry poe run-end-to-end-data-pipelinepoetry poe run-export-data-warehouse-to-jsondata/data_warehouse_raw_data directory):poetry poe run-import-data-warehouse-from-jsonpoetry poe run-export-artifact-to-json-pipelineoutput folder as JSON files (it will take their latest version):configs/export_artifact_to_json.yaml configuration file.poetry poe run-training-pipelinepoetry poe run-evaluation-pipeline[!WARNING] For this to work, make sure you properly configured AWS SageMaker as described in Set up cloud infrastructure (for production).
poetry poe call-rag-retrieval-modulepoetry poe run-inference-ml-servicepoetry poe call-inference-ml-service[!WARNING] For the inference service to work, you must have the LLM microservice deployed to AWS SageMaker, as explained in the setup cloud infrastructure section.
1poetry poe lint-check
2poetry poe lint-fix1poetry poe format-check
2poetry poe format-fixpoetry poe gitleaks-checkpoetry poe test.env is filled as expected, follow the next steps to run the LLM system end-to-end:poetry poe run-digital-data-etlpoetry poe run-feature-engineering-pipelinepoetry poe run-generate-instruct-datasets-pipelinepoetry poe run-generate-preference-datasets-pipeline[!IMPORTANT] From now on, for these steps to work, you need to properly set up AWS SageMaker, such as runningpoetry install --with awsand filling in the AWS-related environment variables and configs.
poetry poe run-training-pipelineconfigs/training.yaml, change finetuning_type to dpo, and run poetry poe run-training-pipeline againpoetry poe run-evaluation-pipeline[!IMPORTANT] From now on, for these steps to work, you need to properly set up AWS SageMaker, such as runningpoetry install --with awsand filling in the AWS-related environment variables and configs.
poetry poe call-rag-retrieval-modulepoetry poe deploy-inference-endpointpoetry poe test-sagemaker-endpointpoetry poe run-inference-ml-servicepoetry poe call-inference-ml-service