banner
Intro to AI engineering
Please be gentle
#️⃣   ⌛  ~1 h 🤓  Intermediate
14.02.2025
upd:
#149

views-badgeviews-badge
banner
Intro to AI engineering
Please be gentle
⌛  ~1 h
#149


🎓 155/167

This post is a part of the AI engineering educational series from my free course. Please keep in mind that the correct sequence of posts is outlined on the course page, while it can be arbitrary in Research.

I'm also happy to announce that I've started working on standalone paid courses, so you could support my work and get cheap educational material. These courses will be of completely different quality, with more theoretical depth and niche focus, and will feature challenging projects, quizzes, exercises, video lectures and supplementary stuff. Stay tuned!


Artificial intelligence has emerged as a driving force behind myriad transformative applications, from interactive chatbots that provide health advice to cutting-edge recommendation systems that adapt to changing user preferences. As organizations across industries embrace AI to optimize processes and unlock new business value, the strategic deployment of these intelligent systems has become a top priority. In this realm, the role of AI engineering has gained special significance.

Unlike AI researchers and ML engineers who create novel algorithms and investigate theoretical frontiers, AI engineers focus on the practical aspects of applying these breakthroughs to solve real-world challenges. They deploy and integrate pre-trained models, configure pipelines, ensure production-level robustness, maintain system performance over time, and collaborate with cross-functional teams to design solutions that can handle the complexities of real-world data. AI engineering is very much about using existing and proven AI assets — particularly large pre-trained models — and molding them into powerful applications that bring direct value to business and society.

In this article, I define what AI engineering entails and illustrate how it differs from ML engineering and AI research. I then explore the growing impact of AI engineering across diverse domains — from automating repetitive tasks to delivering personalized user experiences. Next, I outline the broad responsibilities of AI engineers, which include data pipeline development, infrastructure design, model fine-tuning, and system deployment at scale. Throughout the discussion, I highlight both the opportunities and pitfalls, touching on the importance of robust safety measures, careful management of biases, and the critical significance of cross-functional collaboration.

By the end, you will have a comprehensive understanding of the essential skill set and knowledge base for AI engineering, the core challenges you might encounter, and the advanced techniques — from leveraging open-source models to building multimodal AI applications — that power many of the most innovative use cases today.


Pre-trained models: benefits, limitations, and applications

Pre-trained models are at the heart of AI engineering. Rather than training an AI system from scratch using massive datasets (a resource- and time-intensive task), AI engineers can leverage models that have already learned general-purpose representations from large corpora. By infoThe process of tuning the model's parameters on a specialized dataset to adapt it to a particular domain.fine-tuning these models for a targeted application or domain, organizations can achieve powerful results with a fraction of the effort.

Pre-trained models in context

A pre-trained model is typically trained on extensive datasets — for instance, hundreds of gigabytes of text for an NLP model or millions of labeled images for a computer vision model. The model learns to encode fundamental patterns, features, and relationships in that data. Then, to tackle a domain-specific task (e.g., cancer detection from radiography scans, legal text summarization, or credit default prediction), AI engineers can fine-tune the general-purpose model on a much smaller domain-specific dataset. This process exploits the broad knowledge captured by the original pre-training, often reducing both development time and the volume of data needed.

Example: BERT for text classification

If you want to create a text classification engine to detect spam emails, you might start with a language model like BERT, which was trained on a massive corpus of English text. You then fine-tune BERT's layers to distinguish spam from non-spam emails using a labeled dataset of your own. Rather than training a deep neural network from scratch, which could require millions of labeled examples, you might only need tens of thousands or even fewer, because the model already "understands" a great deal about language structures, word contexts, and grammar.

Benefits of pre-trained models

  1. Reduced data requirements: Traditional supervised machine learning requires large curated datasets. Pre-trained models alleviate this requirement, as they have already digested large amounts of data. This opens doors for organizations with limited labeled data.

  2. Faster deployment: Training large-scale models from scratch can take days or weeks even on top-tier GPU clusters. By starting from a pre-trained checkpoint, you can drastically reduce training time and get your model into production much sooner.

  3. Improved generalization: Pre-training on a diverse dataset often confers robust generalization to new data distributions, especially if the downstream domain has at least some resemblance to what the model saw in pre-training.

  4. Access to advanced architectures: State-of-the-art model designs (e.g., Transformer-based architectures like GPT-4 or T5) can be quite complex. When you rely on pre-trained versions, you can instantly harness cutting-edge AI research without building these architectures from scratch.

Limitations and considerations

  1. Inherited biases: Pre-trained models can inherit biases from the data used for pre-training. For instance, if the model is trained on text that reflects certain stereotypes, those biases can carry over into downstream tasks.

  2. Lack of domain specificity: A model trained for general text understanding may not have direct knowledge of specialized domains, such as chemical patents or historical legal documents. Fine-tuning helps but may not completely overcome certain domain gaps.

  3. Opacity and interpretability challenges: Deep neural networks, especially large language models or complicated vision architectures, act as black boxes, making it difficult to interpret how they arrive at their predictions.

  4. Maintenance overhead: Although pre-trained models save time initially, they may still need updates if the domain or the data distribution shifts over time, requiring continuous monitoring and retraining or re-fine-tuning.

Industry applications

Pre-trained models power many real-world deployments:

  • Text: GPT-4 for generating and summarizing documents, BERT-based architectures for sentiment analysis and classification.
  • Images: DALL-E for generating marketing visuals, pre-trained ResNets or Vision Transformers for defect detection in manufacturing.
  • Speech: Whisper for call center transcription, followed by further classification or sentiment analysis of the resulting text.

These models have become the foundation of numerous AI pipelines in various sectors — from e-commerce (product recommendation and personalization) to healthcare (medical image analysis and patient triage).


The OpenAI ecosystem: APIs and customization

OpenAI has made advanced models accessible through straightforward APIs, revolutionizing how developers and organizations adopt state-of-the-art AI capabilities. Here, I outline some of the most relevant OpenAI offerings for AI engineers, including GPT-4, Codex, and the Embeddings API. While OpenAI stands out as a leading commercial provider, the fundamental concepts apply to many other large-language-model ecosystems.

GPT-4 for text understanding and generation

GPT-4 is a powerful language model capable of producing coherent, context-aware text for tasks ranging from casual conversation to specialized technical writing. It has a large context window (some variants support up to 32,768 tokens), which enables it to handle long-form tasks such as summarizing lengthy documents or analyzing multi-turn chat dialogues.

Chat completions API

A popular interface to GPT-based models is the Chat Completions API. Instead of sending raw text, you structure your conversation into roles like infoSystem messages set instructions or context for the entire conversation, user messages contain the user's query, and assistant messages contain the AI's responses. system, user, and assistant. This approach is especially handy when creating conversational applications or interactive systems, since the model maintains context across multiple turns. For instance:


import openai

openai.api_key = "YOUR_API_KEY"

messages = [
    {"role": "system", "content": "You are a helpful financial assistant."},
    {"role": "user", "content": "What are the main steps to apply for a personal loan?"}
]

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=messages,
    max_tokens=500
)

print(response["choices"][0]["message"]["content"])

This approach ensures the conversation remains cohesive. You can keep appending user and assistant messages to maintain state. For advanced use cases, you might store previous messages in a conversation database, especially if you want to handle multi-turn dialogues across sessions or incorporate advanced logic (like responding to user queries in multiple languages).

Fine-tuning GPT-based models

OpenAI offers fine-tuning capabilities that let you adapt a base model to your specific domain. While GPT-4 has had limited fine-tuning options historically, earlier GPT-3.5 series models have well-documented fine-tuning endpoints. The process entails supplying a curated dataset of (prompt, completion) pairs. For instance, if you want a GPT-based model to speak like a legal assistant, you can compile a dataset of legal questions and sample correct answers, along with the desired style.


import openai

# Prepare your data as a JSONL file with lines like:
# {"prompt": "<question>", "completion": "<answer>"}

openai.api_key = "YOUR_API_KEY"
openai.FineTune.create(
    training_file="file-abc123",
    model="gpt-3.5-turbo"
)

Once the model is fine-tuned, you can invoke it by specifying your fine-tuned model name. This allows you to create domain-specific variations of GPT models that are more aligned with specialized tasks (e.g., drafting finance reports, discussing legal opinions, or analyzing genomic data).

Codex for code generation

OpenAI's Codex model is specialized for code-related tasks, supporting dozens of programming languages. It can generate code completions, debug errors, and even comment code snippets with natural language explanations. While GPT-4 also handles code, Codex is often a strong choice for code-centric tasks like building AI-based development assistants or automated testing tools.

Embeddings API for similarity and clustering

The Embeddings API converts text into high-dimensional vectors that capture semantic meaning. You can use these embeddings for:

  • Semantic search: Finding documents related to a query based on vector similarity (e.g., cosine similarity).
  • Clustering: Grouping semantically similar items (e.g., user feedback, product descriptions).
  • Recommendation systems: Matching user interests to relevant content.

import openai
import numpy as np

openai.api_key = "YOUR_API_KEY"

response = openai.Embedding.create(
    input=["The capital of France is Paris.", "The Earth is round."],
    model="text-embedding-ada-002"
)

embeddings = [r["embedding"] for r in response["data"]]
similarity = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
print("Cosine similarity:", similarity)

These vectors serve as the backbone of more advanced capabilities, such as building your own semantic search engine or powering classification workflows for large-scale text corpora. Tools like infoVector databases store embeddings in specialized data structures, enabling efficient nearest-neighbor searches and real-time retrieval.vector databases commonly integrate these embeddings to achieve lightning-fast similarity queries.

Token limits and pricing trade-offs

As you design a production application, be aware of token limits. GPT-3.5 models often have a 4k or 16k token context window, while some GPT-4 variants go up to 32k tokens. Longer contexts can handle bigger inputs but also cost more. On top of that, you pay for both input and output tokens. Balancing cost and performance is a critical piece of AI engineering. You might rely on smaller models for cost-sensitive tasks, reserving GPT-4 for tasks that demand higher accuracy and reasoning capacity.


AI safety: mitigating risks in real-world deployments

When AI systems shift from research labs to real-world settings, new considerations emerge. Large language models can produce harmful or biased content, particularly if manipulated by adversarial prompts. They can inadvertently disclose private or sensitive information. AI engineers have a pivotal role in mitigating such risks.

Prompt injection and adversarial inputs

Prompt injection is a form of adversarial attack where a malicious user deliberately crafts input to trick the model into disclosing sensitive information, producing disallowed content, or deviating from intended guidelines. For instance, an attacker might instruct the model to ignore or override instructions about not revealing proprietary data.

Mitigation strategies:

  • Sanitize user inputs before passing them to the model.
  • Create robust prompt structures with system instructions that reduce the risk of accidental override.
  • Use output validation to check if the model's response is suspicious or violates policy.

Bias mitigation and fairness

Pre-trained models learn from large data corpora that reflect real-world biases around race, gender, or other protected characteristics. Left unaddressed, these biases can lead to discriminatory outcomes, such as systematically lower loan approvals for certain demographic groups.

Mitigation strategies:

  • Conduct an audit on the training data to identify potential sources of bias.
  • Consider fine-tuning the model with fairness-aware techniques or more diverse datasets.
  • Implement post-processing filters (e.g., ensuring certain demographic labels are not treated as negative signals).

Content moderation

When you build systems that generate user-facing text, image, or audio, you must ensure the content is not harmful. OpenAI's Moderation API classifies user-submitted content and can flag or block it if it violates safety guidelines. This can help you moderate content related to harassment, hate speech, or other sensitive categories.


import openai

openai.api_key = "YOUR_API_KEY"

response = openai.Moderation.create(
    input="Some user input that might be offensive."
)

print(response["results"])

Privacy safeguards

Many AI applications process user queries that may contain personally identifiable information (PII), sensitive financial data, or health records. When dealing with such data:

  1. Anonymize user prompts by removing names and IDs.
  2. Avoid logging raw prompts in production databases.
  3. Use end-user identification tags or pseudonyms so you can track usage without storing direct user data.

Adversarial testing

A critical step is running adversarial tests before deployment. By intentionally probing the model with boundary cases, contradictory instructions, or manipulative inputs, you can detect vulnerabilities. This proactive approach helps ensure your AI application does not expose private data or produce outputs that run counter to your organization's ethical standards.


Open-source AI: tools and collaborative innovation

While commercial providers like OpenAI, Anthropic, and Google deliver advanced APIs, the open-source AI community has exploded with new frameworks, model checkpoints, and collaborative platforms. Open-source AI fosters transparency, fuels innovation, and offers a cost-effective route for organizations seeking complete control over their AI stack.

Hugging Face Hub

A cornerstone of open-source AI is the Hugging Face Hub, which hosts over 900,000 models spanning various tasks, from NLP classification to vision-based object detection. Whether you need a GPT-like language model, a stable diffusion model for generative art, or specialized BERT variants in obscure languages, chances are you will find something relevant.

  • Transformers.js: Enables running transformer models in JavaScript environments, including web browsers.
  • Inference SDK: A simpler interface to run inference on Hugging Face models hosted on the platform.

# Example using the Hugging Face Inference Endpoint in Python

from huggingface_hub.inference_api import InferenceApi

inference = InferenceApi(repo_id="bert-base-uncased", token="YOUR_HF_TOKEN")
input_text = "Hello, world!"
result = inference(inputs=input_text)
print(result)

Here, you can trivially test or integrate pre-trained models, and if performance or domain specificity is insufficient, you can upload your fine-tuned variations back to the Hub for sharing with the community.

Ollama for local LLMs

Ollama is a platform focusing on running large language models locally, prioritizing privacy and offline capabilities. This is particularly advantageous for organizations whose compliance requirements forbid sending data to third-party APIs. Ollama optimizes resource usage, making LLM deployment possible even on laptops or edge devices. With the Ollama SDK, you can easily integrate local LLMs into your applications for tasks like on-premise question answering, summarizing confidential documents, or real-time language analysis on a factory floor with limited internet connectivity.

Open-source embeddings

Open-source embeddings like Word2Vec, GloVe, and more recent Sentence-BERT or CLIP can serve as powerful drop-in alternatives to proprietary services for tasks like semantic search, recommendation, or zero-shot classification. These embeddings are freely available, can be hosted on your own infrastructure, and can be fine-tuned for domain-specific tasks. If you're working with multilingual data or specialized jargon, custom fine-tuning of open-source embeddings can significantly boost performance.

Community contributions and model repositories

Open-source AI thrives on a culture of collaboration. Not only can you download pre-trained weights, but you can also contribute your improvements:

  • Health care–specific BERT: Fine-tune BERT on medical notes for more accurate symptom analysis, then share it publicly for other health providers.
  • Special domain customizations: For example, a BERT that excels in analyzing financial regulatory texts, or a GPT-2 that has been retrained on tens of thousands of legal cases.

This communal approach encourages reproducibility, speeds up progress, and ensures that improvements in model architectures and training recipes disseminate rapidly through the AI community.


Multimodal AI: bridging text, images, and speech

Modern applications do not always involve just text or just images in isolation; many real-world scenarios require multimodal AI that interprets and generates text, vision, or audio data simultaneously. For example, a customer service system might process both spoken queries and product images to troubleshoot a device.

OpenAI Vision API

Alongside textual models, OpenAI Vision API solutions allow the analysis of images (e.g., detecting defects in manufacturing lines or identifying brand logos in user-generated content). Combined with GPT's language skills, the model can produce textual summaries of what it "sees" in images, enabling advanced tasks like automated alt-text generation for accessibility.

mysterious_frog

An image was requested, but the frog was found.

Alt: "AI analyzing product images"

Caption: "AI vision systems are used to detect defects, identify brand elements, and power accessibility solutions."

Error type: missing path

DALL-E for creative image generation

DALL-E is a generative model that turns textual prompts into images. Marketers and designers can rapidly prototype visuals from textual specifications ("Show me a futuristic living room with neon lighting"), while product teams can use DALL-E to generate mock-ups. This drastically speeds up concept ideation and can even produce user interface sketches for app designs.

Whisper for speech-to-text

Whisper is OpenAI's speech recognition model that supports multiple languages. It can handle transcriptions for call centers, live captioning for events, or accessibility features in software products. By pairing Whisper with GPT-4, you can create an end-to-end pipeline that ingests user speech, transcribes it, and then processes the text to generate meaningful responses.

LangChain and LlamaIndex for multimodal workflows

  • LangChain: A framework that chains multiple AI calls together, enabling you to combine large language models with image classifiers, speech recognition, or other specialized modules. For instance, you can build a medical application that analyzes patient X-rays with a vision model, then calls GPT-4 to produce an integrated textual report for doctors.
  • LlamaIndex: Allows you to index heterogeneous data sources (e.g., text and images) and feed relevant segments to a language model for analysis. This helps unify textual and visual data into a single information-retrieval pipeline.

AI development tools: accelerating workflows

AI engineering is not just about picking the right model. It also involves using a variety of development tools that streamline coding, debugging, testing, and collaboration.

GitHub Copilot

Powered by Codex, GitHub Copilot provides real-time code suggestions as you type in popular editors like Visual Studio Code. It can autocomplete function names, propose entire blocks of code, and reduce repetitive coding chores. While it's not perfect, Copilot speeds up the development cycle by offering a quick starting point for many coding tasks.

Cursor IDE

Cursor IDE integrates AI-driven debugging and refactoring. By interpreting error messages or code patterns, it can propose fixes in natural language, highlight suspicious code blocks, and even recommend structural improvements. This approach transforms the typical test-debug cycle into a more dynamic, AI-assisted process.

Replicate for scalable model deployment

Replicate is a platform that simplifies hosting custom models, offering versioning, monitoring, and resource scaling out of the box. Rather than spinning up your own GPU-accelerated servers for each new model, you can push your fine-tuned models to Replicate, which exposes them via an API endpoint. This is particularly useful if you have a suite of smaller specialized models that you want to track over time.

Pieces for knowledge sharing

Pieces is a snippet manager enhanced with AI capabilities, making it easier to capture, tag, and reuse code across large teams. Its AI-generated metadata classifies code snippets by language, functionality, and context, helping developers quickly retrieve the right snippet to solve a particular problem.


Concepts

In AI engineering, you will often encounter a wide array of specialized terms. While many of these are covered in depth throughout the broader course, here is a quick reference:

  • LLMs (Large Language Models): Transformer-based neural networks (e.g., GPT-4, PaLM, Claude) pre-trained on extensive textual data.
  • Inference: The process of running a trained model to generate predictions or responses.
  • Training: The process of optimizing model parameters using data. For AI engineers, this is often limited to fine-tuning rather than training from scratch.
  • Embeddings: Numeric vector representations of text (or images, audio) used to measure semantic similarity and support tasks like search or clustering.
  • Prompt engineering: The art of crafting instructions to guide a model's output effectively. This can involve carefully chosen words, context structuring, or style constraints.
  • Vector databases: Specialized databases (e.g., Pinecone, Milvus) that store embeddings in a way that facilitates nearest-neighbor queries over millions or billions of vectors.
  • RAG (Retrieval-Augmented Generation): A technique where external documents or knowledge bases are retrieved at query time and combined with a language model's context to enhance factual accuracy and reduce hallucinations.

Optimization and deployment strategies

Once you have developed and tested your AI solution, the final hurdle is getting it into production reliably. This involves not only shipping the model but also ensuring it maintains performance and user satisfaction under real-world demands.

Token efficiency and context management

In solutions using large language models, token usage can balloon quickly with lengthy user inputs or multi-turn conversations. Because many vendors bill per token, you must strategize:

  1. Input truncation: If user messages exceed a certain length, summarize them first.
  2. Context compression: Use embeddings to store chat context and only re-inject the most relevant pieces into the prompt.
  3. Caching frequent queries: If you repeatedly see the same or similar requests, store previously computed responses to reduce cost and latency.

Cost control

Balancing cost against performance is an ongoing exercise. High-end models like GPT-4 are more accurate but more expensive. Some tips:

  • Hybrid approach: Use GPT-3.5 for standard requests and only escalate to GPT-4 for complex tasks (e.g., legal or medical queries).
  • Batch requests: Where possible, group multiple similar user queries into a single API call to reduce overhead.
  • Monitoring usage: Set up dashboards that track token consumption, enabling quick interventions if usage spikes unexpectedly.

Scalability and containerization

Scalability is essential when your system might experience thousands of requests per second (RPS). Containerization tools such as Docker and orchestration platforms like Kubernetes help you horizontally scale your AI service. Containerizing your model inference server ensures:

  1. Portability: You can deploy the same container image on different environments (e.g., cloud, on-premises).
  2. Isolation: Resource allocation (CPU, GPU, memory) can be more carefully managed across nodes.
  3. Rollback: If a new model version or code update causes production issues, you can quickly revert to a stable container image.
mysterious_frog

An image was requested, but the frog was found.

Alt: "Containerized AI deployment"

Caption: "Docker container images can bundle AI models, libraries, and code, simplifying deployment and version control."

Error type: missing path

Monitoring performance

Once deployed, continuous monitoring is crucial:

  • Latency tracking: Use a tool like Prometheus to capture response times. If inference latency spikes, you may need more GPU instances or improved network bandwidth.
  • Model accuracy: Track a real-time accuracy proxy (e.g., user satisfaction scores, acceptance rates, or a small holdout test set run periodically).
  • Logging and alerting: Capture inputs (with caution for privacy) and outputs to identify anomalies or drift. Alerts can be triggered if the system starts producing an unusual volume of negative or flagged content.

A/B testing for safe rollouts

Rather than instantly rolling out a new fine-tuned model to all users, A/B testing compares multiple model variants. You might sample 10% of users on a new model while keeping 90% on the existing version. Then measure performance on key metrics: user engagement, cost per query, error rates, or domain-specific measures like click-through rates. If the new model outperforms, you gradually increase its share until full deployment.


# Pseudocode for A/B testing logic in Python

import random

def route_request(user_id):
    # For demonstration, a 10% chance to route to "Model B"
    if random.random() < 0.1:
        return "ModelB"
    else:
        return "ModelA"

This ensures you do not disrupt your entire user base with an untested model, mitigating risks and giving you quantifiable insights into performance differences.


In summary, AI engineering represents a vibrant intersection of advanced AI techniques and robust software engineering best practices. By leveraging pre-trained models effectively and orchestrating them with the right infrastructure, AI engineers can deliver scalable, accurate, and safe AI applications to production. From harnessing OpenAI's GPT-4 for text-based tasks to integrating open-source solutions like Hugging Face or Ollama for specialized or offline use cases, the key is to balance performance, cost, and reliability in an ever-evolving landscape of data-driven innovation.

AI engineers stand as the champions of real-world AI adoption: weaving powerful models into existing products, ensuring that systems remain fair, transparent, and robust under adversity, and continuously refining the architecture to meet dynamic business and user needs. As more enterprises embrace AI, the demand for skilled AI engineers who can seamlessly bring these solutions to life has never been higher. The potential for breakthroughs is immense — and with the right strategies, technologies, and collaboration, these breakthroughs can translate into impactful, sustainable outcomes in every industry.

kofi_logopaypal_logopatreon_logobtc-logobnb-logoeth-logo
kofi_logopaypal_logopatreon_logobtc-logobnb-logoeth-logo