ComponentsChat loadersFine-Tuning on LangSmith Chat DatasetsOn this pageFine-Tuning on LangSmith Chat DatasetsThis 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 library.import openaiimport timeimport jsonfrom io import BytesIOmy_file = BytesIO()for dialog in training_data:    my_file.write((json.dumps({"messages": dialog}) + "\n").encode('utf-8'))my_file.seek(0)training_file = openai.File.create(  file=my_file,  purpose='fine-tune')job = openai.FineTuningJob.create(    training_file=training_file.id,    model="gpt-3.5-turbo",)# Wait for the fine-tuning to complete (this may take some time)status = openai.FineTuningJob.retrieve(job.id).statusstart_time = time.time()while status != "succeeded":    print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)    time.sleep(5)    status = openai.FineTuningJob.retrieve(job.id).status# Now your model is fine-tuned!    Status=[running]... 302.42s. 143.85s4. Use in LangChain​After fine-tuning, use the resulting model ID with the ChatOpenAI model class in your LangChain app.# Get the fine-tuned model IDjob = openai.FineTuningJob.retrieve(job.id)model_id = job.fine_tuned_model# Use the fine-tuned model in LangChainmodel = ChatOpenAI(    model=model_id,    temperature=1,)model.invoke("There were three ravens sat on a tree.")Now you have successfully fine-tuned a model using data from LangSmith LLM runs!PreviousiMessageNextFine-Tuning on LangSmith LLM RunsPrerequisites1. Select dataset2. Prepare Data3. Fine-tune the Model4. Use in LangChain