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
相关推荐
开发者导航3 天前
【开发者导航】轻量可微调且开源的大语言模型家族:LLaMA
语言模型·开源·llama
缘友一世4 天前
借助LLama_Factory工具对大模型进行lora微调
llama
illuspas5 天前
MI50运算卡使用llama.cpp的ROCm后端运行Qwen3-Coder-30B-A3B的速度测试
人工智能·llama
herogus丶5 天前
【LLM】LLaMA-Factory 训练模型入门指南
python·ai编程·llama
illuspas5 天前
MI50运算卡使用llama.cpp的ROCm后端运行gpt-oss-20b的速度测试
人工智能·gpt·llama
谏书稀5 天前
LLaMA Factory微调大模型
python·transformer·llama
菠菠萝宝6 天前
【AI应用探索】-7- LLaMA-Factory微调模型
人工智能·深度学习·大模型·llm·nlp·attention·llama
wuningw7 天前
Windows环境下LLaMA-Factory微调模型时“未检测到CUDA环境”
llama
喜欢吃豆8 天前
llama.cpp 全方位技术指南:从底层原理到实战部署
人工智能·语言模型·大模型·llama·量化·llama.cpp
skywalk81639 天前
在星河社区部署大模型unsloth/Llama-3.3-70B-Instruct-GGUF
llama·aistudio