The process is simple and comprises 3 steps.Create the chat dataset.Use the LangSmithDatasetChatLoader to load examples.Fine-tune your model.Then you can use the fine-tuned model in your LangChain app.Before diving in, let's install our prerequisites.Prerequisites‚ÄãEnsure you've installed langchain >= 0.0.311 and have configured your environment with your LangSmith API key.%pip install -U langchain openaiimport osimport uuiduid = uuid.uuid4().hex[:6]os.environ["LANGCHAIN_TRACING_V2"] = "true"os.environ["LANGCHAIN_API_KEY"] = "YOUR API KEY"1. Select dataset‚ÄãThis notebook fine-tunes a model directly on a selecting which runs to fine-tune on. You will often curate these from traced runs. You can learn more about LangSmith datasets in the docs docs.For the sake of this tutorial, we will upload an existing dataset here that you can use.from langsmith.client import Clientclient = Client()import requestsurl = "https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/docs/integrations/chat_loaders/example_data/langsmith_chat_dataset.json"response = requests.get(url)response.raise_for_status()data = response.json()dataset_name = f"Extraction Fine-tuning Dataset {uid}"ds = client.create_dataset(dataset_name=dataset_name, data_type="chat")_ = client.create_examples(    inputs = [e['inputs'] for e in data],    outputs = [e['outputs'] for e in data],    dataset_id=ds.id,)2. Prepare Data‚ÄãNow we can create an instance of LangSmithRunChatLoader and load the chat sessions using its lazy_load() method.from langchain.chat_loaders.langsmith import LangSmithDatasetChatLoaderloader = LangSmithDatasetChatLoader(dataset_name=dataset_name)chat_sessions = loader.lazy_load()With the chat sessions loaded, convert them into a format suitable for fine-tuning.‚Äãfrom langchain.adapters.openai import convert_messages_for_finetuningtraining_data = convert_messages_for_finetuning(chat_sessions)3. Fine-tune the Model‚ÄãNow, initiate the fine-tuning process using the OpenAI
This notebook demonstrates an easy way to load a LangSmith chat dataset fine-tune a model on that data.
This notebook demonstrates an easy way to load a LangSmith chat dataset fine-tune a model on that data. ->: The process is simple and comprises 3 steps.Create the chat dataset.Use the LangSmithDatasetChatLoader to load examples.Fine-tune your model.Then you can use the fine-tuned model in your LangChain app.Before diving in, let's install our prerequisites.Prerequisites‚ÄãEnsure you've installed langchain >= 0.0.311 and have configured your environment with your LangSmith API key.%pip install -U langchain openaiimport osimport uuiduid = uuid.uuid4().hex[:6]os.environ["LANGCHAIN_TRACING_V2"] = "true"os.environ["LANGCHAIN_API_KEY"] = "YOUR API KEY"1. Select dataset‚ÄãThis notebook fine-tunes a model directly on a selecting which runs to fine-tune on. You will often curate these from traced runs. You can learn more about LangSmith datasets in the docs docs.For the sake of this tutorial, we will upload an existing dataset here that you can use.from langsmith.client import Clientclient = Client()import requestsurl = "https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/docs/integrations/chat_loaders/example_data/langsmith_chat_dataset.json"response = requests.get(url)response.raise_for_status()data = response.json()dataset_name = f"Extraction Fine-tuning Dataset {uid}"ds = client.create_dataset(dataset_name=dataset_name, data_type="chat")_ = client.create_examples(    inputs = [e['inputs'] for e in data],    outputs = [e['outputs'] for e in data],    dataset_id=ds.id,)2. Prepare Data‚ÄãNow we can create an instance of LangSmithRunChatLoader and load the chat sessions using its lazy_load() method.from langchain.chat_loaders.langsmith import LangSmithDatasetChatLoaderloader = LangSmithDatasetChatLoader(dataset_name=dataset_name)chat_sessions = loader.lazy_load()With the chat sessions loaded, convert them into a format suitable for fine-tuning.‚Äãfrom langchain.adapters.openai import convert_messages_for_finetuningtraining_data = convert_messages_for_finetuning(chat_sessions)3. Fine-tune the Model‚ÄãNow, initiate the fine-tuning process using the OpenAI