当前位置:   article > 正文

Fastapi部署大语言模型llama2_llama, fastapi

llama, fastapi

Fastapi部署llama

服务端代码

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, LlamaForCausalLM
import torch

app = FastAPI()

class Query(BaseModel):
    text: str

device = torch.device("cuda:0")

model_path = 'llama-2-7b-chat-hf'
model = LlamaForCausalLM.from_pretrained(model_path, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_path)

@app.post("/chat/")
async def generate_response(query: Query):
    inputs = f"[INST] {query.text.strip()} [/INST]"

    input_ids = tokenizer(inputs, return_tensors="pt").input_ids.to(device)
    generate_ids = model.generate(
        input_ids,
        max_new_tokens=500,
        do_sample=True,
        top_p=0.85,
        temperature=1.0,
        repetition_penalty=1.,
        eos_token_id=2,
        bos_token_id=1,
        pad_token_id=0)

    output = tokenizer.batch_decode(generate_ids)[0]
    return {"result": output}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=6006)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

客户端代码

import requests

url = "https://xxxxxxxxxxxx/chat/"
# 使用新的输入格式,包裹用户输入
query = {"text": "[INST] introduce china[/INST]"}  # 修改为使用[INST]标签

response = requests.post(url, json=query)

if response.status_code == 200:
  result = response.json()
  print("chat:", result["result"])
else:
  print("Error:", response.status_code, response.text)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/787367
推荐阅读
相关标签
  

闽ICP备14008679号