
LANGCHAIN — Can Langsmith and Lilac Help You Fine-Tune Your LLMs?
For a successful technology, reality must take precedence over public relations, for nature cannot be fooled. — Richard Feynman
Langsmith and Lilac are powerful tools that can help you fine-tune your LLMs. They provide complementary capabilities, enabling you to efficiently collect, connect, manage datasets, and offer advanced analytics to structure, filter, and refine datasets for continuously improving your data pipeline.
In the following tutorial, we will go through the process of capturing traces from a prototype using LangChain, importing the traces into Lilac, curating the dataset, and finally fine-tuning the model.
Capture Traces
When building with LangChain, you can easily trace all the execution steps to LangSmith by setting a couple of environment variables. Once the traces are captured, you can select runs to add to a candidate dataset in the UI or programmatically.
Import to Lilac
Lilac provides a native integration with LangSmith datasets. After installing Lilac locally and setting the LANGCHAIN_API_KEY in the environment, you will see a list of LangSmith datasets auto-populated in the Lilac UI. You can then select the dataset earmarked for fine-tuning, and Lilac will handle the rest.
Curate Your Dataset
Once the dataset is in Lilac, you can run Lilac’s signals, concepts, and labels to help organize and filter the dataset. Signals such as near-duplicates and PII detection, concepts for organizing data, and labels for custom tagging can all be applied to the dataset.
Export the Dataset
After computing the information needed for filtering, you can export the enriched dataset via Python or through Lilac’s UI, which will create a browser download of a JSON file. The Python API is recommended for downloading large amounts of data or for better control over the selection of data.
Fine-tune
With the dataset in hand, it’s time to fine-tune the model. It’s easy to convert from LangChain’s message format to the formats expected by OpenAI, HuggingFace, or other training frameworks. Once the model is fine-tuned, it can be used in your Chain by updating the “model” argument in your LLM.
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model="ft:gpt-3.5-turbo-0613:{openaiOrg}::{modelId}")Assuming the data is structured appropriately, this model will have more awareness of the structure and style required for generating responses.
This overview demonstrates the process for going from traces to a fine-tuned model by integrating Lilac and LangSmith. With the data process in place, you can continuously improve each component in your contextual reasoning application. LangSmith makes it easy to collect user and model-assisted feedback to save time when capturing data, and Lilac helps you analyze, label, and organize all the text data so you can refine your model appropriately.
By following this process, you can fine-tune your LLMs with LangSmith and Lilac, ensuring more consistent and high-quality behavior in your applications.
