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
相关推荐
AI大模型2 天前
基于 Docker 的 LLaMA-Factory 全流程部署指南
docker·llm·llama
m0_603888718 天前
LLaMA-Adapter V2 Parameter-Efficient Visual Instruction Model
人工智能·深度学习·ai·llama·论文速览
三千院本院12 天前
LlaMA_Factory实战微调VL大模型
llama
爱分享的飘哥17 天前
第四十六章:AI的“瞬时记忆”与“高效聚焦”:llama.cpp的KV Cache与Attention机制
llama·llama.cpp·kv cache·attention优化·llm cpu推理·量化attention·gguf推理
psyq18 天前
LLaMA Factory 角色扮演模型微调实践记录
人工智能·llama
liliangcsdn1 个月前
mac测试ollama llamaindex
数据仓库·人工智能·prompt·llama
茫茫人海一粒沙1 个月前
使用 LLaMA 3 8B 微调一个 Reward Model:从入门到实践
llama
liliangcsdn1 个月前
mac llama_index agent算术式子计算示例
人工智能·python·macos·llama
许愿与你永世安宁1 个月前
RAG(检索增强生成)里的文档管理
数据库·人工智能·gpt·oracle·llama·rag
许愿与你永世安宁1 个月前
基于Llama的RAG 3种模型配置方法
人工智能·python·自然语言处理·json·github·llama·faiss