Online · Cluj-Napoca · Romania

Python Coding Classes for Children in Cluj-Napoca

Python coding classes for children in Cluj-Napoca build software foundations—variables, branching, loops and reusable functions—suited to a city known for its tech industry.

Python Cluj-Napoca

Chapter 176 — Build Practical AI Workflows with LangChain Chains and Tools

1. Introduction: Fine-Tuning Pre-Trained LLMs for Custom AI Workflows

Large technology organizations pre-train massive Large Language Models (LLMs) on vast general datasets and make them publicly accessible. These pre-trained base models possess strong general knowledge and perform reasonably well across standard tasks. However, when building enterprise AI applications that require specialized domain expertise, relying solely on general-purpose models often falls short.

To tailor language models to custom client data and specialized domains, developers turn to Fine-Tuning.

  ┌─────────────────────────────────────────────────────────────┐
  │                 Pre-Trained vs. Fine-Tuned Models           │
  ├───────────────────────────────┬─────────────────────────────┤
  │ Pre-Trained General LLM       │ Fine-Tuned Domain Model     │
  ├───────────────────────────────┼─────────────────────────────┤
  │ • Trained on massive web data │ • Trained on small, custom  │
  │   from scratch.               │   client datasets.          │
  │ • General-purpose answers     │ • High accuracy on domain-  │
  │   across diverse topics.      │   specific tasks.           │
  │ • Average performance on      │ • Optimized for enterprise  │
  │   niche business data.        │   workflows & tools.        │
  └───────────────────────────────┴─────────────────────────────┘
    

Core Concept of Fine-Tuning:

  • Avoiding From-Scratch Training: Fine-tuning does not require training a multi-billion parameter network from scratch on huge datasets.
  • Adapting Pre-Trained Weights: Instead, fine-tuning takes an existing pre-trained model (such as OpenAI's gpt-3.5-turbo or open-source Hugging Face models) and updates its parameters using a smaller, clean, domain-specific dataset.
  • LangChain Integration: Once fine-tuned, the specialized model can be deployed as a standalone API or plugged directly into a LangChain workflow as a custom model component.

2. The 4-Step Fine-Tuning & Deployment Pipeline

Fine-tuning an LLM and integrating it into an AI pipeline involves four structured stages:

  ┌─────────────────────────────────────────────────────────────┐
  │               The 4-Step Fine-Tuning Workflow               │
  ├─────────────────────────────────────────────────────────────┤
  │  Step 1: Dataset Preparation & Cleaning                     │
  │          (JSONL format, cleaning, deduplication)            │
  │                             │                               │
  │                             ▼                               │
  │  Step 2: Training Execution & Hyperparameter Tuning         │
  │          (Asynchronous API job, metric tracking)            │
  │                             │                               │
  │                             ▼                               │
  │  Step 3: Model Evaluation & Testing                         │
  │          (Test on unseen prompts, compare results)          │
  │                             │                               │
  │                             ▼                               │
  │  Step 4: Deployment & LangChain Integration                 │
  │          (Standalone API or LangChain component)            │
  └─────────────────────────────────────────────────────────────┘
    

Step 1: Dataset Preparation & Data Cleaning

The success of fine-tuning relies heavily on training data quality. Preparing a dataset requires formatting, cleaning, and balancing:

  • Format Adjustment: Training data must match the input format expected by the model API (typically JSONL format containing system, user, and assistant message pairs).
  • Data Diversity: The dataset must be diverse and representative of all target problem scenarios.
  • Consistency: Identical inputs must yield consistent output patterns to avoid confusing the model.
  • Deduplication & Balancing: Duplicate records must be removed, and samples across different categories must be balanced to prevent bias.

Step 2: Training Execution & Hyperparameter Tuning

  • API & Library Fine-Tuning: Fine-tuning jobs are submitted via APIs (such as OpenAI's client.fine_tuning.jobs.create) or open-source libraries like Hugging Face.
  • Asynchronous Execution: Because fine-tuning takes time, jobs run asynchronously on remote servers, returning a tracking Job ID.
  • Hyperparameter Tracking: Developers track loss metrics and adjust hyperparameters across multiple training runs to identify the best-performing model.

Step 3: Model Evaluation & Testing

  • Unseen Test Prompts: The fine-tuned model must be tested using prompts that were not included in the training dataset.
  • Comparative Analysis: Performance on test prompts is evaluated before and after fine-tuning to verify that output quality and accuracy have improved.

Step 4: Deployment & LangChain Integration

Once evaluation confirms superior performance, the fine-tuned model is deployed:

  • Standalone API: Hosted as a cloud microservice endpoint.
  • LangChain Workflow Component: Placed directly inside a LangChain chain or agent pipeline by passing the fine-tuned model ID to model wrappers like ChatOpenAI(model_name="ft:gpt-3.5-turbo:...").

3. Hands-On Python Implementation in VS Code

Below is the Python implementation demonstrating how to upload a domain training dataset (train.jsonl) and launch an asynchronous fine-tuning job using the OpenAI Python SDK.

  ┌─────────────────────────────────────────────────────────────┐
  │                OpenAI Fine-Tuning Code Flow                 │
  ├─────────────────────────────────────────────────────────────┤
  │  1. Read File in Binary Mode ──► open("train.jsonl", "rb")  │
  │                                           │                 │
  │                                           ▼                 │
  │  2. Upload File to OpenAI    ──► client.files.create(...)   │
  │                                           │                 │
  │                                           ▼                 │
  │  3. Retrieve File ID         ──► file.id                    │
  │                                           │                 │
  │                                           ▼                 │
  │  4. Submit Fine-Tuning Job   ──► jobs.create(...)           │
  │                                           │                 │
  │                                           ▼                 │
  │  5. Retrieve Server Job ID   ──► job.id                     │
  └─────────────────────────────────────────────────────────────┘
    

Complete Executable Python Script:

# =====================================================================
# Fine-Tuning an OpenAI LLM for Custom Domain Workflows (Topic 176)
# =====================================================================

import os
from openai import OpenAI
from dotenv import load_dotenv

# 1. Load API Key from .env file into environment variables
load_dotenv()

# 2. Instantiate OpenAI Client Object
# API key is automatically picked up from OPENAI_API_KEY environment variable
client = OpenAI()

# 3. Open Training Dataset File in Binary Read Mode ("rb")
# 'train.jsonl' contains client domain data formatted for fine-tuning
training_file_path = "train.jsonl"

with open(training_file_path, "rb") as file_object:
    # 4. Upload File to OpenAI API for Fine-Tuning
    uploaded_file = client.files.create(
        file=file_object,
        purpose="fine-tune"  # Specifies purpose as fine-tuning
    )

# 5. Display Uploaded File ID
print(f"Successfully uploaded dataset!")
print(f"Uploaded File ID: {uploaded_file.id}")

# 6. Submit Asynchronous Fine-Tuning Job to Server
# Fine-tuning runs asynchronously on remote GPU servers
fine_tune_job = client.fine_tuning.jobs.create(
    training_file=uploaded_file.id,  # Pass uploaded file ID
    model="gpt-3.5-turbo"            # Base model to fine-tune
)

# 7. Display Submitted Fine-Tuning Job ID
print(f"\nFine-tuning job successfully submitted!")
print(f"Fine-Tuning Job ID: {fine_tune_job.id}")
    

Code Execution Output:

When executed in VS Code, the script outputs the unique File ID and long Job ID returned by the OpenAI server:

Successfully uploaded dataset!
Uploaded File ID: file-abc123xyz456

Fine-tuning job successfully submitted!
Fine-Tuning Job ID: ftjob-987654321fedcba
    

This Job ID confirms that the server accepted the job and is executing the fine-tuning process asynchronously in the background.

4. Summary Comparison Table

Workflow Stage Operational Process Key Parameters / Functions Primary Objective
Data Preparation Cleaning, deduplication, & balancing. JSONL format, binary read mode (rb). Ensures high-quality, non-confusing data.
File Upload Uploading training dataset. client.files.create(purpose="fine-tune"). Registers dataset on API server.
Job Submission Launching asynchronous fine-tuning. client.fine_tuning.jobs.create(). Updates base model parameters on custom data.
Evaluation Testing fine-tuned model. Testing on unseen prompts. Verifies performance gain over base model.
Deployment Integrating into production. Standalone API or LangChain component. Powers domain-specific AI workflows.

Lesson Q&A

Definition & Purpose: Fine-tuning involves customizing and adapting a pre-trained Large Language Model (LLM)—which has already been trained on massive generic datasets by large AI companies—using a smaller, client-specific domain dataset. It avoids the enormous time and resource cost of training a model from scratch.

Data Preparation Requirements: Fine-tuning requires clean, diverse, and representative data. It needs consistent examples (where identical inputs produce consistent target outputs without confusing the model), removal of duplicate entries, and balanced samples across categories.

General-Purpose vs. Fine-Tuned Models: Pre-trained general-purpose LLMs provide reasonably good answers across broad, generic tasks. In contrast, fine-tuned models excel exceptionally within a specific target domain, though their accuracy on external, unrelated domains may be lower.

Data Formatting & Loading: Domain-specific client data is formatted into a .jsonl file (e.g., train.jsonl). It is opened in binary read mode ("rb") and uploaded using OpenAI's file API (client.files.create(file=f, purpose="fine-tune")), returning a unique file_id.

Asynchronous Job Submission: The fine-tuning job is initialized using client.fine_tuning.jobs.create(training_file=file.id, model="gpt-3.5-turbo"). Because fine-tuning takes time, the job executes asynchronously on server infrastructure, returning a unique, long job_id string while freeing up client execution.

Monitoring & Tracking: Developers track fine-tuning progress via server responses and adjust hyperparameters across runs to identify the best-performing model.

Testing with Unseen Prompts: Once fine-tuning completes, the model must be evaluated using test prompts that were not included in the original training dataset.

Performance Comparison: Testing prompts are evaluated on the model both before and after fine-tuning to compare performance metrics directly—verifying whether responses improved, remained identical, or degraded.

Deployment Options: If testing metrics are satisfactory, the fine-tuned model can be deployed into production through two main paths:

  1. As a standalone API service.
  2. Integrated as a custom model component within a LangChain pipeline or workflow.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

Python for Kids – 12 Pillars on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Silicon Valley of Transylvania: Advanced Python Syntax for Cluj Kids

Cluj-Napoca’s tech reputation pairs well with Python’s readable structure. Classes cover variables, branching, loops and functions. Academic tutoring can be explored via pakhometutor.com. Enquire +92 322 4217059. Foundations first—no inflated career claims.

Frequently asked questions

Why learn Python in a tech hub like Cluj-Napoca?

Learning Python builds a solid foundation in software engineering principles used throughout the tech industry.

What logic structures are covered in Python classes?

Variables, data types, if-else branching, loops, and reusable functions.

Are general academic tutoring services available?

Yes, partner networks like pakhometutor.com offer comprehensive online academic tutoring.

Explore related learning

BucharestRobotics · Bucharest DebrecenPython · Debrecen TimisoaraAI · Timisoara