跳到主要内容
Open In ColabOpen on GitHub

LangSmith 聊天数据集

本笔记本演示了加载 LangSmith 聊天数据集并在此数据上微调模型的简便方法。该过程简单,包括 3 个步骤。

  1. 创建聊天数据集。
  2. 使用 LangSmithDatasetChatLoader 加载示例。
  3. 微调您的模型。

然后您可以在您的 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()

加载聊天会话后,将其转换为适合微调的格式。

from langchain_community.adapters.openai import convert_messages_for_finetuning

training_data = convert_messages_for_finetuning(chat_sessions)

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 中使用

微调后,在您的 LangChain 应用程序中使用生成的模型 ID 和 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 参考: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 运行的数据微调了模型!


此页对您有帮助吗?