All systems operationalAI Agent Automation Platform
English
Language简体中文EnglishSelected

Choose your language

qwen-7b-chat-agent

flashyzy/qwen-7b-chat-agent

Publisherflashyzy

Download access requirementsModelScope source

模型中文名:小小Agent,英文名:SmallSmallAgent 该模型“小小Agent”是由西安交通大学”大模型实训队“在2024年1月份于阿里与魔搭社区举行的“大模型进校园”实战训练营·西交大站中微调开发得到的Agent模型。他主要有如下几个功能: 1⃣️调用多种API,包括语⾳⽣成, 图像⽣成, python解析器(数学计算/代码运⾏)等多种API🤒

Text Chat
Model details

模型介绍

中文整理

模型中文名:小小Agent,英文名:SmallSmallAgent

模型简介

该模型“小小Agent”是由西安交通大学”大模型实训队“在2024年1月份于阿里与魔搭社区举行的“大模型进校园”实战训练营·西交大站中微调开发得到的Agent模型。他主要有如下几个功能:

1⃣️调用多种API,包括语⾳⽣成, 图像⽣成, python解析器(数学计算/代码运⾏)等多种API🤒

2⃣️具有自我认知能力😊

3⃣️具有多轮对话能力😤

实验环境

我们在Modelscope提供的单卡A10环境上进行微调

训练方法

1⃣️数据集:采用了damo-agent-mini-zh数据集,并将自我认知能力的数据集采样数设置为500

2⃣️微调的模型:我们基于Qwen-7b-chat模型进行微调

3⃣️ 训练过程

The following bitsandbytes quantization config was used during training:

quant method: bitsandbytes

load in 8bit: False

load in 4bit: True

llm int8 threshold: 6.0

llm int8 skip modules: None

llm int8 enable fp32 cpu offload: False

llm int8 has fp16 weight: False

bnb 4bit quant type: nf4

bnb 4bit use double quant: True

bnb 4bit compute dtype: bfloat16

4⃣️ 微调框架版本

SWIFT 1.5.1

推理效果

自我认知能力

图片生成能力

语音生成能力

python解释器API调用能力+多轮对话能力

示例代码

1⃣️sft.sh

PYTHONPATH=../../.. \

CUDA VISIBLE DEVICES=0 \

python ../../../llm sft.py \

  • -model id or path qwen/Qwen-7B-Chat \
  • -model revision master \
  • -sft type lora \
  • -tuner backend swift \
  • -template type qwen \
  • -system '你是名为小小agent的大语言模型,是由大模型实训队于2024年1月开发的。你是一个helpful assistant。' \
  • -dtype AUTO \
  • -output dir output \
  • -dataset damo-agent-mini-zh \
  • -train dataset sample 20000 \
  • -num train epochs 1 \
  • -max length 4096 \
  • -check dataset strategy warning \
  • -quantization bit 4 \
  • -bnb 4bit comp dtype AUTO \
  • -lora rank 8 \
  • -lora alpha 32 \
  • -lora dropout p 0.05 \
  • -lora target modules ALL \
  • -gradient checkpointing true \
  • -batch size 1 \
  • -weight decay 0.01 \
  • -learning rate 1e-4 \
  • -gradient accumulation steps 8 \
  • -max grad norm 0.5 \
  • -warmup ratio 0.03 \
  • -eval steps 100 \
  • -save steps 100 \
  • -save total limit 2 \
  • -logging steps 10 \
  • -use flash attn true \
  • -push to hub false \
  • -hub model id push test \
  • -hub private repo true \
  • -hub token 'dc2af58d-7c8a-4f45-8bcb-b0dc9be32f2e' \

2⃣️sft.py

'''

import inspect

import os

from functools import partial

from typing import Any, Dict, Union

import json

import numpy as np

import torch

from modelscope import BitsAndBytesConfig, GenerationConfig

from swift.trainers import Seq2SeqTrainer, Seq2SeqTrainingArguments

from swift.utils import (check json format, compute acc metrics,

compute nlg metrics, get dist setting, get logger,

get main, get model info, is ddp plus mp, is dist,

is master, plot images, preprocess logits for metrics,

seed everything, show layers)

from .tuner import prepare model

from .utils import (TEMPLATE MAPPING, LazyLLMDataset, SftArguments, Template,

add self cognition dataset, dataset map,

get additional saved files, get dataset,

get model tokenizer, get template, get time info,

print example, set generation config, sort by max length,

stat dataset)

logger = get logger()

def llm sft(args: SftArguments) -> Dict[str, Union[str, Any]]:

logger.info(f'args: {args}')

print(f'device count: {torch.cuda.device count()}')

rank, local rank, world size, local world size = get dist setting()

print(f'rank: {rank}, local rank: {local rank}, '

f'world size: {world size}, local world size: {local world size}')

seed everything(args.seed)

# Loading Model and Tokenizer

model kwargs = {'low cpu mem usage': True}

if is dist() and not is ddp plus mp():

model kwargs['device map'] = {'': local rank}

else:

model kwargs['device map'] = 'auto'

if args.load in 8bit or args.load in 4bit:

quantization config = BitsAndBytesConfig(

args.load in 8bit,

args.load in 4bit,

bnb 4bit compute dtype=args.bnb 4bit compute dtype,

bnb 4bit quant type=args.bnb 4bit quant type,

bnb 4bit use double quant=args.bnb 4bit use double quant)

logger.info(f'quantization config: {quantization config. dict }')

model kwargs['quantization config'] = quantization config

kwargs = {}

if args.use flash attn is not None:

kwargs['use flash attn'] = args.use flash attn

if args.model cache dir is not None:

kwargs['model dir'] = args.model cache dir

model, tokenizer = get model tokenizer(args.model type, args.torch dtype,

model kwargs, **kwargs)

logger.debug(f'device map: {dict(model.hf device map)}')

logger.info(f'model config: {model.config}')

generation config = GenerationConfig(

max new tokens=args.max new tokens,

temperature=args.temperature,

top k=args.top k,

top p=args.top p,

do sample=args.do sample,

repetition penalty=args.repetition penalty,

num beams=args.num beams,

pad token id=tokenizer.pad token id,

eos token id=tokenizer.eos token id)

logger.info(f'generation config: {generation config}')

set generation config(model, generation config)

# Preparing LoRA

model, callbacks = prepare model(model, args)

show layers(model)

model info = get model info(model)

logger.info(model info)

logger.info(model)

# Loading Dataset

random state = np.random.RandomState(args.dataset seed)

train dataset, val dataset = get dataset(

args.dataset,

args.dataset test ratio,

random state,

check dataset strategy=args.check dataset strategy)

val dataset sample = args.val dataset sample

if train dataset is not None and args.train dataset sample >= 0:

train dataset sample = min(args.train dataset sample,

train dataset.shape[0])

if train dataset.shape[0] > train dataset sample:

logger.info(f'train dataset sample: {train dataset sample}')

train idxs = random state.permutation(train dataset sample)

train dataset = train dataset.select(train idxs)

if val dataset sample is None:

val dataset sample = max(

int(train dataset sample * args.dataset test ratio), 1)

if val dataset is not None and val dataset sample is not None and val dataset sample >= 0:

if val dataset.shape[0] > val dataset sample:

logger.info(f'val dataset sample: {val dataset sample}')

val dataset = val dataset.select(range(val dataset sample))

# add self-cognition dataset

if args.self cognition sample > 0:

train dataset = add self cognition dataset(train dataset,

args.self cognition sample,

args.model name,

args.model author)

logger.info(f'train dataset: {train dataset}')

logger.info(f'val dataset: {val dataset}')

template kwargs = {}

template info = TEMPLATE MAPPING[args.template type]

use model = template info.get('use model', False)

if use model:

template kwargs['model'] = model

template: Template = get template(args.template type, tokenizer,

args.system, args.max length,

args.truncation strategy,

  • *template kwargs)

args.system = template.default system

logger.info(f'system: {args.system}')

logger.info(f'args.lazy tokenize: {args.lazy tokenize}')

if not args.lazy tokenize:

dataset info = {}

logger.info(f'Using num proc: {args.preprocess num proc}')

train dataset = dataset map(train dataset, template.encode,

args.preprocess num proc)

if val dataset is not None:

val dataset = dataset map(val dataset, template.encode,

args.preprocess num proc)

if args.test oom error:

train dataset = sort by max length(train dataset, 20000)

# Data analysis

td0, tkwargs0 = train dataset.data[0]

print example(td0, tokenizer, tkwargs0)

dataset info['train dataset'] = stat dataset(train dataset)

if val dataset is not None:

dataset info['val dataset'] = stat dataset(val dataset)

else:

dataset info = None

td0, tkwargs0 = template.encode(train dataset[0])

print example(td0, tokenizer, tkwargs0)

train dataset = LazyLLMDataset(train dataset, template)

val dataset = LazyLLMDataset(val dataset, template)

padding to = args.max length if args.sft type == 'longlora' else None

data collator = partial(template.data collator, padding to=padding to)

# Setting training args

evaluation strategy = args.evaluation strategy

load best model at end = True

if val dataset is None:

evaluation strategy = 'no'

if evaluation strategy == 'no':

load best model at end = False

additional saved files = []

if args.sft type == 'full':

additional saved files = get additional saved files(args.model type)

kwargs = {}

parameters = inspect.signature(

Seq2SeqTrainingArguments. init ).parameters

for key in ['neftune noise alpha']:

if key in parameters:

kwargs[key] = getattr(args, key)

training args = Seq2SeqTrainingArguments(

output dir=args.output dir,

evaluation strategy=evaluation strategy,

logging dir=args.logging dir,

per device train batch size=args.batch size,

per device eval batch size=args.eval batch size,

gradient accumulation steps=args.gradient accumulation steps,

learning rate=args.learning rate,

weight decay=args.weight decay,

max grad norm=args.max grad norm,

num train epochs=args.num train epochs,

max steps=args.max steps,

lr scheduler type=args.lr scheduler type,

warmup ratio=args.warmup ratio,

logging steps=args.logging steps,

save strategy=args.save strategy,

save steps=args.save steps,

save total limit=args.save total limit,

remove unused columns=False,

bf16=args.bf16,

fp16=args.fp16,

eval steps=args.eval steps,

dataloader num workers=args.dataloader num workers,

load best model at end=load best model at end,

metric for best model='rouge-l'

if args.predict with generate else 'loss',

greater is better=args.predict with generate,

sortish sampler=True,

optim=args.optim,

adam beta1=args.adam beta1,

adam beta2=args.adam beta2,

hub model id=args.hub model id,

hub private repo=args.hub private repo,

push hub strategy=args.push hub strategy,

hub token=args.hub token,

push to hub=args.push to hub,

resume from checkpoint=args.resume from checkpoint,

ddp backend=args.ddp backend,

gradient checkpointing=args.gradient checkpointing,

predict with generate=args.predict with generate,

generation config=generation config,

local rank=local rank,

save only model=args.save only model,

train sampler random=args.train sampler random,

report to=args.report to,

deepspeed=args.deepspeed,

additional saved files=additional saved files,

disable tqdm=args.disable tqdm,

save on each node=args.save on each node,

acc strategy=args.acc strategy,

save safetensors=args.save safetensors,

logging first step=True,

  • *kwargs)

if args.gradient checkpointing:

model.config.use cache = False # fix transformers==4.36

logger.info('Setting model.config.use cache: False')

model.enable input require grads()

if is dist():

# Compatible with https://github.com/huggingface/transformers/pull/25903

training args. frozen = False

if args.gradient checkpointing:

training args.ddp find unused parameters = False

training args.ddp broadcast buffers = False

else:

training args.ddp find unused parameters = True

training args.ddp broadcast buffers = True

logger.info(f'training args: {training args}')

trainer kwargs = {}

if args.predict with generate:

trainer kwargs['compute metrics'] = partial(

compute nlg metrics, tokenizer=tokenizer)

else:

compute metrics = partial(

compute acc metrics, acc strategy=args.acc strategy)

trainer kwargs['compute metrics'] = compute metrics

trainer kwargs[

'preprocess logits for metrics'] = preprocess logits for metrics

if args.check model is latest is False:

trainer kwargs['check model'] = False

trainer = Seq2SeqTrainer(

model=model,

args=training args,

data collator=data collator,

train dataset=train dataset,

eval dataset=val dataset,

tokenizer=tokenizer,

callbacks=callbacks,

  • *trainer kwargs)

trainer.sft args = args

if is master():

for args obj, fname in zip([args, training args],

['sft args.json', 'training args.json']):

fpath = os.path.join(args.output dir, fname)

logger.info(

f'The {args obj. class . name } will be saved in: {fpath}')

with open(fpath, 'w', encoding='utf-8') as f:

json.dump(

check json format(args obj. dict ),

f,

ensure ascii=False,

indent=2)

logging path = os.path.join(args.output dir, 'logging.jsonl')

logger.info(f'The logging file will be saved in: {logging path}')

trainer.train(training args.resume from checkpoint)

last model checkpoint = getattr(trainer.state, 'last model checkpoint',

None)

logger.info(f'last model checkpoint: {last model checkpoint}')

logger.info(

f'best model checkpoint: {trainer.state.best model checkpoint}')

train time = get time info(trainer.state.log history, len(train dataset))

# Visualization

if is master():

images dir = os.path.join(args.output dir, 'images')

logger.info(f'images dir: {images dir}')

tb dir = os.path.join(args.output dir, 'runs')

plot images(images dir, tb dir, ['train/loss'], 0.9)

if args.push to hub:

trainer. add patterns to gitignore(['images/'])

trainer.push to hub()

return {

'memory': trainer.perf['memory'],

'train time': train time,

'last model checkpoint': last model checkpoint,

'best model checkpoint': trainer.state.best model checkpoint,

'best metric': trainer.state.best metric,

'global step': trainer.state.global step,

'log history': trainer.state.log history,

'model info': model info,

'dataset info': dataset info,

}

'''

sft main = get main(SftArguments, llm sft)

BaseModel Class QWenLMHeadModel

优先采用原作者提供的中文模型卡片;仅有英文说明时由闲社AI翻译整理。重要参数、许可证和商用范围请以原文为准。

你将获得什么

完整仓库方式包含该固定版本中的权重、配置、分词器及说明文件;指定版本方式只下载所选文件或整组分片。多模态模型的投影文件、配置等依赖,请按作者说明配齐。

完整仓库可能包含多个权重格式,因此需要的磁盘空间可能大于单一格式的模型大小。下载文件不等于完成模型部署。

使用前须知

运行环境

运行模型需要的设备、工具与依赖,以原作者说明为准。

授权范围

是否允许商用、修改和分发,请查看模型许可证。

闲社提供资料与镜像下载方法,不代表已运行模型或完成安全审计。下载的代码应先检查,再执行。

Back to top