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
相关推荐
下班走回家8 小时前
本地部署大模型的三种方式:Ollama vs vLLM vs llama.cpp
人工智能·llama·vllm
zhy295632 天前
【DNN】基于llama.cpp的Qwen3-0.6B量化部署微调
人工智能·lora·dnn·llama·qwen3
kuokay3 天前
MLOps 与 AIOps 的核心概
人工智能·分布式·大模型·agent·llama
Trouville014 天前
windows系统使用llama.cpp进行本地大模型部署
llama
棒棒的唐4 天前
windows 直接安装llama.cpp的方法
llama
troubles maker4 天前
LLaMA-Adapter V2: Parameter-Efficient Visual Instruction Model
llm·nlp·llama·多模态
xyz_CDragon4 天前
把旧电脑变成AI算力:llama.cpp RPC 局域网分布式推理验证与实战
人工智能·分布式·python·rpc·llama
wengad5 天前
llama.cpp进行模型格式转换和量化
llama
小七-七牛开发者6 天前
本地模型为什么能跑起来?从 llama.cpp 量化说起
agent·llama·模型部署·ollama·本地模型