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
相关推荐
究极无敌暴龙战神X2 天前
RAG基本流程
自然语言处理·llama·rag
code_pgf3 天前
Llama 3详解
人工智能·llama
m0_488913013 天前
万字长文带你梳理Llama开源家族:从Llama-1到Llama-3,看这一篇就够了!
人工智能·学习·机器学习·大模型·产品经理·llama·uml
helpme流水3 天前
LLaMA Factory 从入门到精通,一篇讲完
人工智能·ai·语言模型·llama
许彰午4 天前
零成本搭建RAG智能客服:Ollama + Milvus + DeepSeek全程实战
人工智能·语音识别·llama·milvus
d1z8885 天前
(二十一)32天GPU测试从入门到精通-LLaMA 系列模型测试day19
llama·显卡·llm推理·推理引擎
AI精钢5 天前
OpenClaw 本地内存检索与 node-llama-cpp 的依赖关系深度解析
llama·向量数据库·内存检索·openclaw·node-llama-cpp·本地 ai
❀͜͡傀儡师5 天前
ShadowBroker:实时全球情报地图,开源情报(OSINT)的终极聚合平台
docker·容器·llama
❀͜͡傀儡师5 天前
基于Docker的LLaMA-Factory全流程部署指南
docker·容器·llama
d1z8885 天前
(二十)32天GPU测试从入门到精通-llama.cpp CPU/GPU 混合推理day18
人工智能·llama·显卡·llama.cpp