教育领域“知之”大模型--山东大学软件学院2024年项目实训(九)

流输出

如果使用模型默认的输出方式是直接等回答全部完成才会输出,这样会大大增加等待时间,在swift中有设置流输出的方式。

以deepseek_math_7b_instruct为例:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

from swift.llm import (
    get_model_tokenizer, get_template, inference, ModelType, get_default_template_type,inference_stream
)
from swift.utils import seed_everything

model_type = ModelType.deepseek_math_7b_instruct
template_type = get_default_template_type(model_type)
print(f'template_type: {template_type}')  # template_type: qwen


kwargs = {}
# kwargs['use_flash_attn'] = True  # 使用flash_attn

model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'}, **kwargs)
# 修改max_new_tokens
model.generation_config.max_new_tokens = 128

template = get_template(template_type, tokenizer)
seed_everything(42)
query = 'what is the integral of x^2 from 0 to 2?\nPlease reason step by step, and put your final answer within \boxed{}.'
# gen=inference_stream(model,template,query)

gen = inference_stream(model, template, query)
print(f'query: {query}')
for response, history in gen:
    pass
    print(f'response: {response}')

运行结果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值