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
相关推荐
言简行敏,心静致远。2 小时前
llama-bench使用说明及其应用
llama
AI_Gump11 小时前
模型篇|llama.cpp 推理引擎深度解读:从一人练手到 LLM 端侧的事实标准
llama·llama.cpp
星野云联AIoT技术洞察3 天前
设备手册离线 RAG:先管住版本、权限和引文,再谈本地大模型
llama·rag·设备告警·rag知识库·设备运维·设备离线·设备手册
晚风吹北巷5 天前
基于LLama-Factory 进行Lora微调
llama
你都会上树?5 天前
本地部署模型 llama + opencode + Qwen3.8-32b
llama
夜瞬6 天前
从零微调 Qwen3-8B:基于 LoRA 与 LLaMA-Factory 打造专属 AI 编程助教
人工智能·python·llama
Victor Zhong9 天前
llama.cpp 部署大模型到 Ubuntu 台式机 SOP
linux·ubuntu·llama
空空dream10 天前
07 | FastAPI + llama.cpp 接入 Langfuse监控Prompt请求
prompt·fastapi·llama
goodlook012310 天前
opencode调用本地模型三
人工智能·llama
Imagination官方博客11 天前
PowerVR 上的 LLM 性能与加速
人工智能·架构·llama·gpu算力