LangSmith 聊天数据集
本笔记本演示了一个简单的方法来加载 LangSmith 聊天数据集并在此数据上微调模型。 该过程很简单,包含 3 个步骤。
- 创建聊天数据集。
- 使用
LangSmithDatasetChatLoader加载示例。 - 微调模型。
然后,您可以在 LangChain 应用中使用微调后的模型。
在深入研究之前,让我们先安装所需的依赖项。
先决条件
请确保您已安装 langchain >= 0.0.311,并已将环境配置为您的 LangSmith API 密钥。
%pip install --upgrade --quiet langchain langchain-openai
import os
import uuid
uid = uuid.uuid4().hex[:6]
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = "YOUR API KEY"
1. 选择一个数据集
此笔记本直接在选择要微调的运行上进行微调。通常,您会从跟踪的运行中精心挑选。您可以在文档 docs 中了解有关 LangSmith 数据集的更多信息。
在本教程中,我们将在此处上传一个现有的数据集供您使用。
from langsmith.client import Client
client = Client()
import requests
url = "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. 准备数据
现在我们可以创建一个 LangSmithRunChatLoader 实例,并使用其 lazy_load() 方法加载聊天会话。
from langchain_community.chat_loaders.langsmith import LangSmithDatasetChatLoader
loader = LangSmithDatasetChatLoader(dataset_name=dataset_name)
chat_sessions = loader.lazy_load()
API Reference:LangSmithDatasetChatLoader
加载会话后,将其转换为适合微调的格式。
from langchain_community.adapters.openai import convert_messages_for_finetuning
training_data = convert_messages_for_finetuning(chat_sessions)
API Reference:convert_messages_for_finetuning
3. 微调模型
现在,使用 OpenAI 库启动微调过程。
import json
import time
from io import BytesIO
import openai
my_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.files.create(file=my_file, purpose="fine-tune")
job = openai.fine_tuning.jobs.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.fine_tuning.jobs.retrieve(job.id).status
start_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.fine_tuning.jobs.retrieve(job.id).status
# Now your model is fine-tuned!
Status=[running]... 429.55s. 46.34s
4. 在 LangChain 中使用
微调完成后,将生成的模型 ID 与 LangChain 应用中的 ChatOpenAI 模型类一起使用。
# Get the fine-tuned model ID
job = openai.fine_tuning.jobs.retrieve(job.id)
model_id = job.fine_tuned_model
# Use the fine-tuned model in LangChain
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
model=model_id,
temperature=1,
)
API Reference:ChatOpenAI
model.invoke("There were three ravens sat on a tree.")
AIMessage(content='[{"s": "There were three ravens", "object": "tree", "relation": "sat on"}, {"s": "three ravens", "object": "a tree", "relation": "sat on"}]')
您现在已经成功地使用来自 LangSmith LLM 运行的数据微调了一个模型!