Gen AI/LLMs
Jun 9, 2024
Snowflake Cortex Finetuning LLMs
Background
Large Language Models (LLMs) have demonstrated impressive capabilities in understanding and generating text. However, in specialized domains like healthcare, life sciences and medical devices, where proprietary data and nuanced terminology are common, the effectiveness of general LLMs can be limited.
Consider the scenario of MediSnow, a medical device company facing an influx of customer complaints about various products. Analyzing these complaints requires an understanding of the company's specific device names (like Xerocore 2000, MediLink Pro, SynthoPulse 3000), also specific device models, and potential issues related to those devices—information that isn't readily available in publicly trained models.
What is Fine-Tuning
Fine-tuning is a crucial technique for tailoring a large language model (LLM) to your specific domain or task. It involves training the model on a smaller, more focused dataset that aligns with your specific needs. This not only improves the model's performance on your targeted tasks but also potentially reduces the computational cost of inference compared to using a massive, general-purpose LLM.
Why Fine-Tuning Matters for MediSnow
Proprietary Knowledge: MediSnow's devices and associated terminology are unique to their product line. Fine-tuning allows them to inject this domain-specific knowledge into the LLM, making it more accurate in understanding and responding to customer feedback.
Cost Efficiency: Large, general-purpose LLMs can be computationally expensive to run. Fine-tuning a smaller model on MediSnow's data allows them to achieve comparable performance for their specific use case at a fraction of the cost.
Reduced Latency: Smaller, fine-tuned models generally provide faster responses, crucial for real-time applications like customer support interactions.
RAG Limitations: While Retrieval Augmented Generation (RAG) can provide additional context, it still relies on the base LLM's ability to understand the domain. If the base model is unfamiliar with medical device terminology, RAG's effectiveness will be limited.
Fine Tuning With Snowflake Cortex
Snowflake Cortex allows you to harness the power of Large Language Models (LLMs) and tailor them to your specific domain. In this guide, we'll demonstrate how to fine-tune a pre-trained model using customer complaints and device information from a medical device database (MediSnowDB). This fine-tuned model can then categorize complaints, extract sentiments, or perform other domain-specific tasks.
Note: In addition to scripting, you can also create and fine-tune LLM models using Snowsight's intuitive UI, which streamlines the process by guiding you through each step and generating the corresponding SQL script.
A Practical Example: Medical Devices Complaint Data
The following sections will guide you through a step-by-step process of how MediSnow can leverage Snowflake Cortex to fine-tune an LLM for their specific needs, maximizing the benefits of AI while maintaining cost-effectiveness.
First, run and setup the MediSnowDB sample data model and proceed with the rest of the exercises.
⛁ Sample Data Model: medisnowdb-data-model
The provided database, "MediSnowDB," with its two tables ("devices" and "customer_complaints"), offers a foundation for tracking and analyzing essential aspects of MediSnow's business:
>_ SQL
Let's delve into how fine-tuning LLMs in Snowflake Cortex can supercharge your data workflow:
1. Setting Up Your Training (and Validation) Data
Create training and validation tables that will guide the fine-tuning process:
SQL
3. Fine-Tuning Your LLM Model with Snowflake Cortex
Fine-tuning large language models (LLMs) like Llama3–8B within Snowflake Cortex is a powerful way to customize their performance for your specific needs. This involves training the model on a focused dataset tailored to your task, resulting in improved accuracy and efficiency for your application.
Snowflake Cortex simplifies the process by handling resource management and optimizing the workflow, allowing you to concentrate on your data and objectives.
After fine-tuning, you can seamlessly deploy and integrate your customized model for tasks like text summarization, question answering, or content generation within your domain, enhancing its capabilities and delivering superior results.
The example below shows how to initiate the fine-tuning process using Snowflake Cortex. This example fine-tunes a Llama3–8B model, but you can choose from a variety of available models (see the Snowflake documentation):
SQL
After the job is complete, the fine-tuned model is available in your environment and also visible in the SnowSight UI. With the right privileges, it can be securely governed and democratized across the organization for usage.
4. Testing and Evaluating Your Fine-Tuned Model
After the fine-tuning process is complete, rigorously test the model for performance and accuracy, compared to the base model:
SQL
Improved Results with Fine-Tuned Models, answered correctly based on knowledge of proprietary data in the database:
Compare this with the less accurate and potentially more costly responses from the Llama-70b base model. Even with larger size and cost, the results are inaccurate for this "specific" task.
By comparing results between the base and fine-tuned models, you can assess the improvements gained from fine-tuning. Beyond the complaint data, you can then use the fine-tuned model to perform specific tasks on your medical device data from the data model.
Fine-tuning as a Strategic Advantage
By fine-tuning a smaller, more affordable LLM, MediSnow can create a highly specialized model that excels at understanding customer complaints, categorizing issues, and potentially even generating automated responses. This not only improves the efficiency of their customer support processes but also allows them to gain deeper insights into product performance and customer sentiment.
Conclusion
Snowflake Cortex enables you to fine-tune powerful Large Language Models for your domain-specific needs. This empowers you to extract valuable insights from your data, automate tasks, and enhance decision-making in the healthcare domain.
Resources
Fine-tuning (Snowflake Cortex): https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-finetuning
Large Language Model (LLM) Functions (Snowflake Cortex): https://docs.snowflake.com/en/user-guide/snowflake-cortex/llm-functions
Is Your Data Ready to Fine-Tune your Business Specific Large Language Models?: https://medium.com/snowflake/is-your-data-ready-to-fine-tune-your-business-specific-large-language-models-bfda147bfd62
Demo: LLM Serverless Fine-Tuning With Snowflake Cortex AI | Summit 2024: Demo: LLM Serverless Fine-Tuning With Snowflake Cortex AI | Summit 2024