将 python 函数蒸馏到 LLM¶永久链接
来自 Instructor
库的 Instructions
提供了一种无缝的方式,使语言模型能够向后兼容现有的 Python 函数。通过使用 Pydantic 类型提示,它不仅确保了兼容性,还有助于对 gpt-3.5-turbo
进行端到端的微调,使其模拟这些函数。
如果你想看完整的例子,请查看 examples/distillation
函数级微调的挑战¶永久链接
在语言模型中复制 Python 函数的行为涉及复杂的数据准备。例如,教会模型执行三位数乘法并不像实现 def f(a, b): return a * b
那样简单。OpenAI 的微调脚本与其函数调用工具结合使用,提供了结构化输出,从而简化了数据收集过程。此外,这消除了向模型传递模式(schema)的需要,从而节省了令牌。
Instructions
在简化微调过程中的作用¶永久链接
通过使用 Instructions
,你可以注解一个返回 Pydantic 对象的 Python 函数,从而自动化微调数据集的创建。只需一个日志处理程序即可构建此数据集。
如何在代码中实现 Instructions
¶永久链接
快速入门:如何使用 Instructor 的蒸馏功能¶永久链接
在深入了解细节之前,让我们看看使用 Instructor 的蒸馏功能(利用函数调用微调)将数据导出到 JSONL 文件是多么容易。
import logging
import random
from pydantic import BaseModel
from instructor import Instructions # pip install instructor
# Logging setup
logging.basicConfig(level=logging.INFO)
instructions = Instructions(
name="three_digit_multiply",
finetune_format="messages",
# log handler is used to save the data to a file
# you can imagine saving it to a database or other storage
# based on your needs!
log_handlers=[logging.FileHandler("math_finetunes.jsonl")],
)
class Multiply(BaseModel):
a: int
b: int
result: int
# Define a function with distillation
# The decorator will automatically generate a dataset for fine-tuning
# They must return a pydantic model to leverage function calling
@instructions.distil
def fn(a: int, b: int) -> Multiply:
resp = a * b
return Multiply(a=a, b=b, result=resp)
# Generate some data
for _ in range(10):
random.seed(42)
a = random.randint(100, 999)
b = random.randint(100, 999)
print(fn(a, b))
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
#> a=754 b=214 result=161356
微调语言模型的复杂性¶永久链接
微调不仅仅是编写一个像 def f(a, b): return a * b
这样的函数。它需要详细的数据准备和日志记录。然而,Instructor 提供了内置的日志记录功能和结构化输出以简化这一点。
为什么 Instructor 和蒸馏是游戏规则改变者¶永久链接
该库提供两个主要好处:
- 效率:简化函数,将需求蒸馏到模型权重和几行代码中。
- 集成:通过提供一个包装现有函数的简单接口,轻松地将经典机器学习和语言模型结合起来。
Instructor 在简化微调中的作用¶永久链接
from instructor import Instructions
功能是一个省时器。它自动生成一个微调数据集,使其轻松模仿函数的行为。
记录输出和运行微调¶永久链接
以下是日志输出的样子:
{
"messages": [
{"role": "system", "content": 'Predict the results of this function: ...'},
{"role": "user", "content": 'Return fn(133, b=539)'},
{
"role": "assistant",
"function_call": {
"name": "Multiply",
"arguments": '{"a":133,"b":539,"result":89509}',
},
},
],
"functions": [
{"name": "Multiply", "description": "Correctly extracted `Multiply`..."}
],
}
像这样运行微调:
模型训练完成后,只需将 mode
更改为 dispatch
,它就会使用该模型来运行函数!
from instructor import Instructions
from pydantic import BaseModel
class Multiply(BaseModel):
a: int
b: int
result: int
instructions = Instructions(
name="three_digit_multiply",
)
@instructions.distil(model='gpt-3.5-turbo:finetuned-123', mode="dispatch")
def fn(a: int, b: int) -> Multiply:
# now this code will be short circuited and the model will be used instead.
resp = a + b
return Multiply(a=a, b=b, result=resp)
这样,你可以替换函数实现,使其向后兼容。你甚至可以想象将不同的模型用于不同的任务,或者使用原始函数与蒸馏结果进行比较来验证和运行评估。