本文章主要介绍自己学习如何使用调用别人提供的大模型,目前没计划学习训练微调,后面的文章可能会涉及。
下载资源
下载一个小一点的模型练手https://hf-mirror.com/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/tree/main
拷贝下载的模型到工作目录的models/tinyllama文件夹下,没有文件夹就创建
示例下载模型:tinyllama-1.1b-chat-v1.0.Q2_K.gguf,还有该模型的config.json文件
搭建运行环境
# 创建一个学习的python环境,并进入
conda create -n learnspace
conda activate learnspace
# 进入工作目录,workspace根据自己的项目位置确认
cd $workspace
# 安装pt,onnx,gguf等格式所需要的库文件,onnx有几种加速方案
pip install llama-cpp-python
# 需要代理则加--proxy: pip install torch transformers onnxruntime llama-cpp-python --proxy=http://127.0.0.1:1080
运行gguf模型
下载的模型为Llama模型,需要导入Llama库,其他兼容模型就要导入不同的库,相关api要自行查询
Llama api 查询:https://llama-cpp-python.readthedocs.io/en/stable/api-reference/
from llama_cpp import Llama
# 加载GGUF模型
llm = Llama(
model_path="models/tinyllama/tinyllama-1.1b-chat-v1.0.Q2_K.gguf",
n_ctx=512, # 上下文长度
n_threads=4 # CPU线程数
)
# 生成文本
output = llm.create_completion(
prompt="你是一个中文AI助手,请以助手的身份回复",
max_tokens=50,
temperature=0.7
)
print(output["choices"][0]["text"])
评论区