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
相关推荐
wei_shuo16 小时前
Llama-2-7b 昇腾 NPU 测评总结:核心性能数据、场景适配建议与硬件选型参考
大模型·llama·昇腾
凯子坚持 c16 小时前
Llama-2-7b在昇腾NPU上的六大核心场景性能基准报告
java·开发语言·llama
落798.16 小时前
【在昇腾NPU上部署Llama-2-7B:从环境配置到性能测试的完整实战】
经验分享·llama·1024程序员节
缘友一世17 小时前
LLama 3分组查询注意力与KV缓存机制
人工智能·深度学习·缓存·transformer·llama·gqa·kv缓存
skywalk81632 天前
在Ubuntu Linux安装brew 使用brew安装llama.cpp 运行文心Ernie大模型
人工智能·ubuntu·llama·ernie·brew·1024程序员节
七宝大爷3 天前
大模型是什么?从 GPT 到 LLaMA 的核心概念解析
gpt·llama
倔强的石头1064 天前
昇腾NPU运行Llama模型全攻略:环境搭建、性能测试、问题解决一网打尽
大模型·llama·昇腾
码农阿豪5 天前
在昇腾NPU上跑Llama 2模型:一次完整的性能测试与实战通关指南
llama
Qiuner5 天前
快速入门LangChain4j Ollama本地部署与阿里百炼请求大模型
语言模型·langchain·nlp·llama·ollama
辣大辣条5 天前
LLAMA-Factory Qwen3-1.7b模型微调
llama