Skip to main content

Cortex Fine-Tuning

Cortex Fine-Tuning lets you customize a foundation model on your own data to improve accuracy for domain-specific tasks. The fine-tuned model runs inside Snowflake and is called through AI_COMPLETE just like any built-in model.

What Is Fine-Tuning?​

Fine-tuning takes a pre-trained language model and continues training it on a smaller, task-specific dataset. The model learns patterns, terminology, and output formats relevant to your use case while retaining its general language abilities.

When to Fine-Tune vs Prompt Engineering​

ApproachBest ForTradeoffs
Prompt engineeringGeneral tasks, quick iteration, low data volumeNo training cost; limited by context window
Few-shot promptingTasks where examples clarify the desired outputNo training cost; adds tokens to every call
Fine-tuningConsistent style, domain jargon, structured output at scaleTraining cost and time; requires labeled data

Choose fine-tuning when prompt engineering alone cannot achieve the required accuracy, or when you need consistent behavior across thousands of calls without repeating lengthy prompts.

Preparing Training Data​

Training data must be a table or view with two columns in a prompt/completion conversational format.

Required Format​

ColumnTypeDescription
promptSTRINGThe input text or instruction
completionSTRINGThe desired model output
CREATE OR REPLACE TABLE FINE_TUNE_DATA (
prompt STRING,
completion STRING
);

INSERT INTO FINE_TUNE_DATA VALUES
('Classify this support ticket: "My account was charged twice for the same order."',
'{"category": "billing", "priority": "high", "sentiment": "negative"}'),
('Classify this support ticket: "When will the new dashboard feature be available?"',
'{"category": "feature_request", "priority": "low", "sentiment": "neutral"}'),
('Classify this support ticket: "I cannot log in after resetting my password."',
'{"category": "authentication", "priority": "high", "sentiment": "negative"}');

Data Quality Guidelines​

  • Minimum rows: At least 100 examples; 500+ recommended for strong results.
  • Balanced classes: Ensure each category has roughly equal representation.
  • Consistent format: Use identical output structure across all completions.
  • Clean text: Remove HTML tags, excessive whitespace, and encoding artifacts.
  • No data leakage: Do not include test examples in the training set.

Creating a Fine-Tuning Job​

SELECT SNOWFLAKE.CORTEX.FINETUNE(
'CREATE',
'TICKET_CLASSIFIER_FT',
'llama3.1-8b',
'SELECT prompt, completion FROM FINE_TUNE_DATA',
'SELECT prompt, completion FROM FINE_TUNE_VALIDATION'
);

Parameters​

ParameterDescription
Operation'CREATE' to start a new job
Job nameUnique identifier for this fine-tuning run
Base modelFoundation model to fine-tune (e.g., llama3.1-8b, mistral-7b)
Training querySELECT returning prompt and completion columns
Validation queryOptional holdout set for evaluation during training

Monitoring Fine-Tuning Jobs​

-- Check job status
SELECT SNOWFLAKE.CORTEX.FINETUNE('DESCRIBE', 'TICKET_CLASSIFIER_FT');

The response includes:

FieldDescription
statusPENDING, IN_PROGRESS, SUCCESS, FAILED
progressPercentage complete
base_modelThe foundation model used
trained_modelThe output model name (available after SUCCESS)
training_lossFinal training loss value
validation_lossFinal validation loss (if validation data was provided)

Listing All Jobs​

SELECT SNOWFLAKE.CORTEX.FINETUNE('SHOW');

Using the Fine-Tuned Model​

Once the job status is SUCCESS, use the output model name with AI_COMPLETE.

SELECT SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT', -- fine-tuned model name
'Classify this support ticket: "Your product broke after one week of use."'
) AS classification;

Batch Inference​

SELECT
TICKET_ID,
TICKET_TEXT,
SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT',
'Classify this support ticket: "' || TICKET_TEXT || '"'
) AS classification
FROM SUPPORT_TICKETS
WHERE CREATED_AT >= DATEADD('day', -1, CURRENT_DATE());

Evaluation and Iteration​

After deploying a fine-tuned model, evaluate it against a held-out test set.

-- Compare model output to ground truth
SELECT
t.TICKET_ID,
t.EXPECTED_CATEGORY,
SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT',
'Classify this support ticket: "' || t.TICKET_TEXT || '"'
) AS predicted,
CASE WHEN predicted ILIKE '%' || t.EXPECTED_CATEGORY || '%' THEN 1 ELSE 0 END AS correct
FROM TEST_TICKETS t;

If accuracy is insufficient:

  1. Add more training examples for underperforming categories.
  2. Improve prompt consistency in training data.
  3. Try a larger base model (e.g., move from 8B to 70B parameters).
  4. Clean noisy labels — a small set of high-quality data outperforms a large set of noisy data.

Cost Considerations​

Cost FactorDetails
Training computeBilled per token processed during training
Model storageFine-tuned model weights stored in your account
InferencePer-token cost when calling AI_COMPLETE with the model

Fine-tuning a smaller model (8B) on a focused dataset is often more cost-effective than repeatedly sending long few-shot prompts to a larger model.

Cancelling a Job​

SELECT SNOWFLAKE.CORTEX.FINETUNE('CANCEL', 'TICKET_CLASSIFIER_FT');

Key Takeaways​

  • Fine-tuning customizes a foundation model to your domain using your data inside Snowflake.
  • Prepare clean, balanced training data with prompt and completion columns.
  • Monitor jobs with FINETUNE('DESCRIBE', ...) and use the output model via AI_COMPLETE.
  • Start with prompt engineering; move to fine-tuning only when prompting alone falls short.
  • Evaluate rigorously and iterate on data quality before scaling up model size.