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
相关推荐
EterNity_TiMe_5 小时前
从 0 到 1:Llama 3-8B 在昇腾 Atlas 800T 上的推理调优与算力榨干指南
数据库·llama·昇腾·atlas 800t·实战部署
不爱学英文的码字机器6 小时前
基于昇腾 NPU 部署 Llama-3-8B 实战教程:从环境搭建到构建昇腾问答智能体
人工智能·pytorch·llama
是店小二呀8 小时前
从 CUDA 到 CANN:昇腾 NPU 环境下 Llama-2 大模型部署
llama
红苕稀饭66613 小时前
Llama-AVSR论文阅读
论文阅读·llama
Dragon水魅1 天前
LLaMA Factory 详解
llama
禁默1 天前
在昇腾 NPU上跑通 Llama 3-8B:从环境部署到 100% 算力满载
llama·昇腾npu·atlas 800t
百***78753 天前
Step-Audio-2 轻量化接入全流程详解
android·java·gpt·php·llama
Robot侠3 天前
RTX 3090单卡微调 Llama-3 / Qwen2.5:基于 Unsloth + ModelScope 的极速实战
llama·modelscope·llama-3·unsloth·llm 微调·rtx 3090
Robot侠3 天前
从 Python 到 Ollama:将微调后的 Llama-3/Qwen 一键导出为 GGUF
开发语言·python·llama·qwen
Robot侠3 天前
给自己做一个 ChatGPT:基于 Gradio 的本地 LLM 网页对话界面
人工智能·chatgpt·llm·llama·qwen·gradio