đ Fine-Tuning FastTrack
Build a financial sentiment classifier in minutes using Factory and Qwen 2.5
This tutorial demonstrates how to fine-tune a small language model (Qwen 2.5 0.5B) for financial sentiment analysis using the Factory SDK. We'll use the Financial PhraseBank dataset to train a model that can classify financial statements as positive, negative, or neutral.
What You'll Learn
Setup & Data Prep
- Factory SDK configuration
- Loading small foundation models
- Preparing financial datasets
Training & Evaluation
- Creating recipes for training
- Parameter-efficient fine-tuning
- Multi-metric model evaluation
Deployment & Testing
- Creating OpenAI-compatible APIs
- Optimized model deployment
- Performance testing & monitoring
Prerequisites
- Basic Python knowledge
- Google Colab or a local environment with GPU support
âšī¸ Create Your Factory Account
To use this tutorial, you'll need to create a free Factory account at factory.manufactai.com.
For detailed instructions on setting up your account, please visit our account creation guide.
Once you have an account, you'll be able to access your tenant name and API key needed for the FactoryClient
setup in this tutorial.
Step 1: Installation and Setup
Let's start by installing the necessary packages:
Now initialize the Factory client:
This connects your local computing resource to the Factory Hub, enabling seamless integration of your development environment with Factory's services for versioning, tracking, and deployment.
Step 2: Load the Base Model
We'll use Qwen 2.5 0.5B Instruct, a small but powerful foundation model:
After this step, a revision of the model is securely stored in Factory for further use and deployment. The model is only 0.5B parameters in size, making it efficient to run on modest hardware while still providing good performance for our task.
Step 3: Prepare the Dataset
We'll use the Financial PhraseBank dataset, which contains financial statements labeled with sentiment:
The dataset contains financial messages categorized as:
- 0: negative
- 1: neutral
- 2: positive
Step 4: Create a Recipe
We need to format our data into a chat format the model can understand:
During this process, Factory automatically:
- Analyzes data for IID (Independent and Identically Distributed) characteristics
- Detects potential data shifts between training and test sets
- Provides statistical analysis of these results in the Factory Hub
Our recipe creates a chat format where:
- The financial statement is the user's message
- The sentiment label is the assistant's response
Step 5: Fine-tune the Model
Now we can fine-tune our model using parameter-efficient techniques:
Factory automatically:
- Measures the response of layers in the network to the training data
- Selects the best possible layers for fine-tuning
- Determines and sets the optimal LoRA parameters for fine-tuning
- Stores all metrics and measurement results in the Factory Hub
We're using several techniques to make training efficient:
- Small batch sizes with gradient accumulation (effective batch size of 16)
- Only tuning 50% of the model layers (parameter-efficient fine-tuning)
- Regular evaluation during training to monitor progress
Step 6: Evaluate the Model
After training, we evaluate our model's performance:
We're using multiple metrics to get a comprehensive view of performance:
- Exact Match: Measures if predictions match the expected labels exactly
- Levenshtein Distance: Measures character-level differences between predictions and expected labels
- Precision and Recall: Classification metrics for each class (positive, neutral, negative)
The evaluation results are stored in the Factory Hub for comparison and analysis.
Step 7: Deploy the Model
Now we can deploy our fine-tuned model as an API:
This deployment:
- Uses FP16 precision for efficiency
- Runs on port 9777
- Uses 80% of available GPU memory
- Uses no CPU swap space (ideal for environments with limited memory)
The deployment automatically connects with the Factory Hub, transmitting data and metrics. New queries are embedded and compared with the training data through statistical tests in the latent space, providing continuous monitoring of potential data shifts.
Step 8: Use the Deployed Model
To communicate with our deployed model, we'll use the OpenAI client which works with our API:
A Factory deployment implements exactly the same interface as OpenAI. This allows you to simply use the OpenAI package, and existing applications can be easily switched over.
Step 9: Test the Model
Let's stress test our API with multiple concurrent requests:
This test:
- Sends 500 requests to our API
- Uses up to 32 concurrent workers
- Measures response times and success rates
The comparison of these test data is stored in the Factory Hub, allowing you to check if any data shifts have occurred. This provides continuous monitoring of your model's performance in production.
Next Steps
Now that you've built and deployed a financial sentiment analysis model, you could:
- Improve the model - Experiment with different base models or hyperparameters
- Expand the dataset - Add more financial statements or different types of financial data
- Enhance the recipe - Modify the preprocessing to improve model performance
- Integrate with applications - Connect your model to financial analysis tools or dashboards
Complete Code
The complete code for this tutorial is available in this Colab notebook.
Ready to build your own AI models?
Factory makes it easy to fine-tune, evaluate, and deploy production-ready models in minutes.