当前位置: 首页 > news >正文

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

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),仅供参考

http://www.rkmt.cn/news/1499753.html

相关文章:

  • MobileOne模型性能对比:S0-S4五个版本速度与精度全面评测
  • 界面控件DevExpress WPF中文教程:Data Grid - 绑定数据
  • 2026年6月最新版黄冈第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • PR计算题——2025
  • wgs-84高精度空间直角坐标转为CGCS2000坐标程序开发
  • 腾讯云Redis与自建方案技术经济性对比 - 领先技术探路人
  • 188数码管新版本,简单易懂
  • 2026北京公司注册代办机构实测排行:合规性+效率双维度对比(附避坑指南) - 互联网科技品牌测评
  • 重力场模型计算的布格重力异常值用于一、二等水准重力异常改正计算
  • 题解:学而思编程 降雨统计
  • 2026年6月最新版贺州第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • Triton Inference Server自动扩缩容与负载均衡:生产环境最佳实践
  • 题解:学而思编程 优秀的排列
  • Sideloader跨平台支持对比:Linux、Windows、macOS三大平台安装与配置指南
  • 2026济南车灯实测|后浪灯改灯光升级,澳兹姆透镜夜间实景效果,后浪灯改实惠,靠谱 - Ayu8888
  • 礼品定制避坑与选型:五大实战服务商深度横评 - 品牌报告
  • Orz与其他压缩库对比:何时选择Orz最合适?
  • Apache 虚拟主机配置指南:从单站点到多站点
  • BRFlabbyTable与FlabbyListView对比:iOS与Android弹性列表实现差异终极指南
  • OpenAI最强编程助手Codex:下载安装、使用指南(含使用方式、提示技巧、趋势)
  • RollToolsApi架构深度解析:构建稳定聚合API接口源的技术实践
  • 2026年6月最新版东营第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • Polyglot-Ko-1.3B应用场景探索:客服机器人、内容创作与教育辅助
  • CAD如何修改快捷键?CAD如何自定义快捷键。
  • 2026年6月最新版大庆第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • 从3D Tiles到I3S:使用loaders.gl实现不同瓦片格式的转换
  • Progenitor客户端高级配置:自定义请求头、超时和认证的实用技巧
  • 5个Claudian插件使用技巧:快速提升AI交互效率的完整指南
  • PVC 橡胶阻燃剂应用分类解析 优质生产厂家甄选指南 - 变量人生001
  • 批量改图片DPI的Python脚本