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
相关推荐
墨家巨子@俏如来13 小时前
LLaMA-Factory训练DeepSeek大模型+本地部署
ai·大模型·llama·ai人工智能
月光技术杂谈1 天前
llama.cpp 利用intel集成显卡xpu加速推理
人工智能·python·llama·intel·llama.cpp·xpu·集成显卡
1nv1s1ble2 天前
llama.cpp编译
llama
啊晚2 天前
模型微调-基于LLaMA-Factory进行微调的一个简单案例
llama
timer_0172 天前
Meta 计划在 Llama 4 中引入改进的语音功能,接近双向自然对话
llama
后端小肥肠3 天前
企业抢着要的AI方案:DeepSeek-R1微调实战,3天构建行业内容生成器
人工智能·llama·deepseek
anda01093 天前
01-简单几步!在Windows上用llama.cpp运行DeepSeek-R1模型
llama
扫地僧9853 天前
MuMu-LLaMA:通过大型语言模型进行多模态音乐理解和生成(Python代码实现+论文)
人工智能·语言模型·llama
watersink4 天前
Llama-Factory框架下的Meta-Llama-3-8B-Instruct模型微调
llama
凉拌三丝5 天前
Llama Index实战(二):Agent与简单工具的调用
人工智能·llama