RWKV / rwkv.cpp

INT4/INT5/INT8 and FP16 inference on CPU for RWKV language model
MIT License
1.37k stars 90 forks source link

Add API server #114

Open lloydzhou opened 1 year ago

lloydzhou commented 1 year ago

Add API server

start

python rwkv/api.py RWKV-4-World-3B-v1-20230619-ctx4096_Q5_1.bin world 0.0.0.0 8000

/completions

curl localhost:8000/v1/completions -X POST -H 'Content-Type: application/json' -d '{"prompt": "你是一个历史老师,请告诉我周文王是谁?", "stream": false}'

--> 

{
  "object":"text_completion",
  "response":"周文王是中国春秋时期的一位著名君主,他统一了周围的诸侯国,并且在其统治时期内,周朝成为了中国历史上最为繁荣和强大的王朝之一。",
  "model":"rwkv",
  "choices":[{
    "text":"周文王是中国春秋时期的一位著名君主,他统一了周围的诸侯国,并且在其统治时期内,周朝成为了中国历史上最为繁荣和强大的王朝之一。",
    "index":0,
    "finish_reason":"stop"
  }],
  "usage":{
    "prompt_tokens":26,
    "completion_tokens":63
  }
}

/chat/completions

curl localhost:8000/v1/chat/completions -X POST -H 'Content-Type: application/json' -d '{
  "messages": [{
    "role": "system",
    "content": "你是一个历史老师"
  }, {
    "role": "user",
    "content": "请告诉我周文王是谁?"
  }],
  "stream": false
}'

--> 
{
  "object":"chat.completion",
  "response":"周文王是中国春秋时期的一位著名君主,他统一了周围的诸侯国,建立了周朝,并且推行了许多改革和改革措施,如推行新的制度、推广农业生产、实行法律和秩序等等。他的统治时期被称为“周文王时代”\n\n",
  "model":"rwkv",
  "choices":[{
    "delta": {
      "content": "周文王是中国春秋时期的一位著名君主,他统一了周围的诸侯国,建立了周朝,并且推行了许多改革和改革措施,如推行新的制度、推广农业生产、实行法律和秩序等等。他的统治时期被称为“周文王时代”\n\n"
    },
    "index":0,
    "finish_reason":"stop"
  }],
  "usage":{
    "prompt_tokens":55,
    "completion_tokens":94
  }
}
l1006986533 commented 1 year ago

少导入了一个 from fastapi import HTTPException,status 而且也没有处理跨域问题

from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

注:我纯路人,不是rwkv.cpp的开发者

lloydzhou commented 1 year ago

Add dockerfile

docker build -t lloydzhou/rwkv-cpp-api -f Dockerfile .

# run docker
docker run --rm -it -v `pwd`/RWKV-4-World-3B-v1-20230619-ctx4096_Q4_0.bin:/RWKV-4-World-3B-v1-20230619-ctx4096_Q4_0.bin -e MODEL_PATH=/RWKV-4-World-3B-v1-20230619-ctx4096_Q4_0.bin -p 8000:8000 lloydzhou/rwkv-cpp-api 

# test 
curl localhost:8000/v1/chat/completions -X POST -H 'Content-Type: application/json' -d '{"messages": [{"role": "system", "content": "你是一个历史老师"}, {"role": "user", "content": "请告诉我周文王是谁?"}], "stream": false}'
az13js commented 7 months ago

这个是兼容OpenAI的接口吗?

lloydzhou commented 7 months ago

@az13js 格式是和openai的api一样的,也支持stream模式