LLM Engine API详解:完整掌握Completion与FineTune接口使用
2026/6/10 20:57:31 网站建设 项目流程

LLM Engine API详解:完整掌握Completion与FineTune接口使用

【免费下载链接】llm-engineScale LLM Engine public repository项目地址: https://gitcode.com/gh_mirrors/ll/llm-engine

LLM Engine是一款功能强大的开源工具,提供了高效的Completion(文本生成)和FineTune(模型微调)接口,帮助开发者轻松构建和定制大型语言模型应用。本文将详细介绍这两个核心接口的使用方法,让你快速掌握从文本生成到模型定制的全流程。

Completion接口:快速实现文本生成

基础使用方法

Completion接口是LLM Engine最核心的功能之一,它允许你通过简单的API调用实现文本生成。以下是一个基本的使用示例:

from llmengine import Completion response = Completion.create( model="llama-2-7b", prompt="Hello, my name is", max_new_tokens=10, temperature=0.2, ) print(response.json()) # '{"request_id": "c4bf0732-08e0-48a8-8b44-dfe8d4702fb0", "output": {"text": "________ and I am a ________", "num_completion_tokens": 10}}' print(response.output.text) # ________ and I am a ________

主要参数说明:

  • model:指定要使用的LLM模型(详见Model Zoo)
  • prompt:模型输入的提示文本
  • max_new_tokens:生成文本的最大token数量
  • temperature:采样温度,值越高输出越随机,值越低输出越确定

流式输出功能

为了减少感知延迟,Completion API支持流式输出功能。当设置stream=True时,模型会以服务器发送事件(SSE)的形式逐块返回结果:

import sys from llmengine import Completion stream = Completion.create( model="llama-2-7b", prompt="Give me a 200 word summary on the current economic events in the US.", max_new_tokens=1000, temperature=0.2, stream=True, ) for response in stream: if response.output: print(response.output.text, end="") sys.stdout.flush() else: print(response.error) break

异步请求处理

LLM Engine的Python客户端支持异步处理,通过Completion.acreate方法可以实现非阻塞的文本生成:

import asyncio from llmengine import Completion async def main(): response = await Completion.acreate( model="llama-2-7b", prompt="Hello, my name is", max_new_tokens=10, temperature=0.2, ) print(response.json()) asyncio.run(main())

高级功能:引导式解码

LLM Engine支持多种引导式解码方式,让模型生成符合特定格式的输出:

正则表达式引导
response = Completion.create( model="llama-2-7b", prompt="Hello, my name is", max_new_tokens=10, temperature=0.2, guided_regex="Sean.*", )
选项列表引导
response = Completion.create( model="llama-2-7b", prompt="Hello, my name is", max_new_tokens=10, temperature=0.2, guided_choice=["Sean", "Brian", "Tim"], )
JSON模式引导
response = Completion.create( model="llama-2-7b", prompt="Hello, my name is", max_new_tokens=10, temperature=0.2, guided_json={"properties":{"myString":{"type":"string"}},"required":["myString"]}, )

FineTune接口:定制专属模型

微调简介

FineTune接口允许你在自己的数据上微调开源LLM,以提高特定领域的性能。微调的优势包括:

  • 比单纯的提示工程获得更高质量的结果
  • 通过更短的提示节省成本
  • 用更小的模型达到同等精度
  • 推理时降低延迟
  • 能够展示超出单个上下文窗口的更多示例

数据准备

微调数据需要是包含promptresponse两列的CSV文件。以下是创建示例数据集的代码:

import csv # 定义数据 data = [ ("What is your policy on carry-on luggage?", "Our policy allows each passenger to bring one piece of carry-on luggage and one personal item such as a purse or briefcase."), # 更多数据... ] # 写入CSV文件 with open('customer_service_data.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(["prompt", "response"]) writer.writerows(data)

数据上传

使用File API将数据上传到LLM Engine的私有文件服务器:

from llmengine import File response = File.upload(open("customer_service_data.csv", "r")) print(response.json())

启动微调任务

使用FineTune.create方法启动微调任务:

from llmengine import FineTune response = FineTune.create( model="llama-2-7b", training_file="file-AbCDeLdN2Ty4M2m", # 上传文件后获得的文件ID validation_file="file-ezSRpgtKQyItI26", # 可选的验证文件 hyperparameters={ "lr": 2e-3, # 学习率 "warmup_ratio": 0.03, # 预热步骤比例 "epochs": 5, # 训练轮数 "weight_decay": 0.001 # 权重衰减 }, suffix="airlines" # 自定义后缀,用于标识微调模型 ) print(response.json())

监控微调进度

可以通过FineTune.get和FineTune.get_events方法监控微调进度:

from llmengine import FineTune fine_tune_id = "ft-cabcdefghi1234567890" fine_tune = FineTune.get(fine_tune_id) print(fine_tune.status) # 输出当前状态,如:BatchJobStatus.RUNNING print(fine_tune.fine_tuned_model) # 输出微调后的模型名称 # 获取微调事件 fine_tune_events = FineTune.get_events(fine_tune_id) for event in fine_tune_events.events: print(event)

使用微调后的模型

微调完成后,可以在Completion接口中使用微调后的模型:

from llmengine import Completion response = Completion.create( model="llama-2-7b.airlines.2023-07-17-08-30-45", # 微调后的模型名称 prompt="Do you offer in-flight Wi-fi?", max_new_tokens=100, temperature=0.2, ) print(response.json())

总结

LLM Engine的Completion和FineTune接口为开发者提供了强大而灵活的工具,使你能够轻松实现文本生成和模型定制。通过本文的介绍,你应该已经掌握了这两个接口的基本使用方法和高级功能。

要开始使用LLM Engine,只需克隆仓库:

git clone https://gitcode.com/gh_mirrors/ll/llm-engine

更多详细信息,请参考官方文档:

  • Completion API参考
  • FineTune API参考
  • 模型动物园

【免费下载链接】llm-engineScale LLM Engine public repository项目地址: https://gitcode.com/gh_mirrors/ll/llm-engine

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询