Windows CPU部署llama2量化模型并实现API接口

目录

模型部署

从huggingface下载模型

https://huggingface.co/

放在本地文件夹,如下

本地运行llama2

python 复制代码
from ctransformers import AutoModelForCausalLM

llm = AutoModelForCausalLM.from_pretrained("D:\llm\llama2\models\llama2-7b-chat-ggml", model_file = 'llama-2-7b-chat.ggmlv3.q3_K_S.bin')

print(llm('<s>Human: 介绍一下中国\n</s><s>Assistant: '))

使用fastapi实现API接口

服务端

python 复制代码
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from ctransformers import AutoModelForCausalLM
# 参考 https://blog.csdn.net/qq_36187610/article/details/131835752

app = FastAPI()

class Query(BaseModel):
    text: str

@app.post("/chat/")
async def chat(query: Query):
    input = query.text 
    llm = AutoModelForCausalLM.from_pretrained("D:\llm\llama2\models\llama2-7b-chat-ggml", model_file = 'llama-2-7b-chat.ggmlv3.q3_K_S.bin')
    output = llm('<s>Human: ' + input + '\n</s><s>Assistant: ')
    print(output)   
    return {"result": output}
    
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=6667)

客户端

python 复制代码
import requests

url = "http://192.168.3.16:6667/chat/"  # 注意这里ip地址不能使用0.0.0.0,而是使用实际IP地址,通过ipconfig可以查看
query = {"text": "你好,请做一段自我介绍,使用中文回答,不能超过100个字。"}

response = requests.post(url, json=query)

if response.status_code == 200:
    result = response.json()
    print("BOT:", result["result"])
else:
    print("Error:", response.status_code, response.text)

常用git仓库

bash 复制代码
https://github.com/marella/ctransformers
https://github.com/FlagAlpha/Llama2-Chinese
https://github.com/tiangolo/fastapi
相关推荐
try2find3 天前
安装llama-cpp-python踩坑记
开发语言·python·llama
西西弗Sisyphus3 天前
LLaMA-Factory 单卡后训练微调Qwen3完整脚本
微调·llama·llama-factory·后训练
顾道长生'3 天前
(Arxiv-2024)自回归模型优于扩散:Llama用于可扩展的图像生成
计算机视觉·数据挖掘·llama·自回归模型·多模态生成与理解
Zhijun.li@Studio13 天前
【LLaMA-Factory 实战系列】二、WebUI 篇 - Qwen2.5-VL 多模态模型 LoRA 微调保姆级教程
人工智能·自然语言处理·llama·多模态大模型
1213413 天前
LLM:重构数字世界的“智能操作系统”
gpt·aigc·ai编程·llama·gpu算力
冷雨夜中漫步21 天前
Java中如何使用lambda表达式分类groupby
java·开发语言·windows·llama
扫地的小何尚23 天前
全新NVIDIA Llama Nemotron Nano视觉语言模型在OCR基准测试中准确率夺冠
c++·人工智能·语言模型·机器人·ocr·llama·gpu
CFAteam23 天前
DeepSeek AI功能演示:如何生成Verilog脚本
人工智能·ai·fpga开发·llama
Tadas-Gao25 天前
从碳基羊驼到硅基LLaMA:开源大模型家族的生物隐喻与技术进化全景
人工智能·机器学习·大模型·llm·llama
Run_Clover25 天前
llama-factory微调大模型环境配置避坑总结
llama