Merge branch 'THUDM:main' into main
This commit is contained in:
commit
3fb52912a9
29
README.md
29
README.md
@ -165,6 +165,13 @@ cd ChatGLM2-6B
|
|||||||
git clone https://huggingface.co/THUDM/chatglm2-6b
|
git clone https://huggingface.co/THUDM/chatglm2-6b
|
||||||
```
|
```
|
||||||
|
|
||||||
|
如果你从 Hugging Face Hub 上下载 checkpoint 的速度较慢,可以只下载模型实现
|
||||||
|
```Shell
|
||||||
|
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/THUDM/chatglm2-6b
|
||||||
|
```
|
||||||
|
然后从[这里](https://cloud.tsinghua.edu.cn/d/674208019e314311ab5c/)手动下载模型参数文件,并将下载的文件替换到本地的 `chatglm2-6b` 目录下。
|
||||||
|
|
||||||
|
|
||||||
将模型下载到本地之后,将以上代码中的 `THUDM/chatglm2-6b` 替换为你本地的 `chatglm2-6b` 文件夹的路径,即可从本地加载模型。
|
将模型下载到本地之后,将以上代码中的 `THUDM/chatglm2-6b` 替换为你本地的 `chatglm2-6b` 文件夹的路径,即可从本地加载模型。
|
||||||
|
|
||||||
模型的实现仍然处在变动中。如果希望固定使用的模型实现以保证兼容性,可以在 `from_pretrained` 的调用中增加 `revision="v1.0"` 参数。`v1.0` 是当前最新的版本号,完整的版本列表参见 [Change Log](https://huggingface.co/THUDM/chatglm2-6b#change-log)。
|
模型的实现仍然处在变动中。如果希望固定使用的模型实现以保证兼容性,可以在 `from_pretrained` 的调用中增加 `revision="v1.0"` 参数。`v1.0` 是当前最新的版本号,完整的版本列表参见 [Change Log](https://huggingface.co/THUDM/chatglm2-6b#change-log)。
|
||||||
@ -225,6 +232,28 @@ curl -X POST "http://127.0.0.1:8000" \
|
|||||||
"time":"2023-03-23 21:38:40"
|
"time":"2023-03-23 21:38:40"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
感谢 [@hiyouga]() 实现了 OpenAI 格式的流式 API 部署,可以作为任意基于 ChatGPT 的应用的后端,比如 [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web)。可以通过运行仓库中的[openai_api.py](openai_api.py) 进行部署:
|
||||||
|
```shell
|
||||||
|
python openai_api.py
|
||||||
|
```
|
||||||
|
进行 API 调用的示例代码为
|
||||||
|
```python
|
||||||
|
import openai
|
||||||
|
if __name__ == "__main__":
|
||||||
|
openai.api_base = "http://localhost:8000/v1"
|
||||||
|
openai.api_key = "none"
|
||||||
|
for chunk in openai.ChatCompletion.create(
|
||||||
|
model="chatglm2-6b",
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": "你好"}
|
||||||
|
],
|
||||||
|
stream=True
|
||||||
|
):
|
||||||
|
if hasattr(chunk.choices[0].delta, "content"):
|
||||||
|
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## 低成本部署
|
## 低成本部署
|
||||||
|
|
||||||
### 模型量化
|
### 模型量化
|
||||||
|
13
cli_demo.py
13
cli_demo.py
@ -44,7 +44,8 @@ def main():
|
|||||||
os.system(clear_command)
|
os.system(clear_command)
|
||||||
print("欢迎使用 ChatGLM2-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
|
print("欢迎使用 ChatGLM2-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
|
||||||
continue
|
continue
|
||||||
count = 0
|
print("\nChatGLM:", end="")
|
||||||
|
current_length = 0
|
||||||
for response, history, past_key_values in model.stream_chat(tokenizer, query, history=history,
|
for response, history, past_key_values in model.stream_chat(tokenizer, query, history=history,
|
||||||
past_key_values=past_key_values,
|
past_key_values=past_key_values,
|
||||||
return_past_key_values=True):
|
return_past_key_values=True):
|
||||||
@ -52,13 +53,9 @@ def main():
|
|||||||
stop_stream = False
|
stop_stream = False
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
count += 1
|
print(response[current_length:], end="", flush=True)
|
||||||
if count % 8 == 0:
|
current_length = len(response)
|
||||||
os.system(clear_command)
|
print("")
|
||||||
print(build_prompt(history), flush=True)
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
|
||||||
os.system(clear_command)
|
|
||||||
print(build_prompt(history), flush=True)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
163
openai_api.py
Normal file
163
openai_api.py
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Implements API for ChatGLM2-6B in OpenAI's format. (https://platform.openai.com/docs/api-reference/chat)
|
||||||
|
# Usage: python openai_api.py
|
||||||
|
# Visit http://localhost:8000/docs for documents.
|
||||||
|
|
||||||
|
|
||||||
|
import time
|
||||||
|
import torch
|
||||||
|
import uvicorn
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from starlette.responses import StreamingResponse
|
||||||
|
from typing import Any, Dict, List, Literal, Optional, Union
|
||||||
|
from transformers import AutoTokenizer, AutoModel
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI): # collects GPU memory
|
||||||
|
yield
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
torch.cuda.empty_cache()
|
||||||
|
torch.cuda.ipc_collect()
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
|
class ModelCard(BaseModel):
|
||||||
|
id: str
|
||||||
|
object: str = "model"
|
||||||
|
created: int = Field(default_factory=lambda: int(time.time()))
|
||||||
|
owned_by: str = "owner"
|
||||||
|
root: Optional[str] = None
|
||||||
|
parent: Optional[str] = None
|
||||||
|
permission: Optional[list] = None
|
||||||
|
|
||||||
|
|
||||||
|
class ModelList(BaseModel):
|
||||||
|
object: str = "list"
|
||||||
|
data: List[ModelCard] = []
|
||||||
|
|
||||||
|
|
||||||
|
class ChatMessage(BaseModel):
|
||||||
|
role: Literal["user", "assistant", "system"]
|
||||||
|
content: str
|
||||||
|
|
||||||
|
|
||||||
|
class DeltaMessage(BaseModel):
|
||||||
|
role: Optional[Literal["user", "assistant", "system"]] = None
|
||||||
|
content: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class ChatCompletionRequest(BaseModel):
|
||||||
|
model: str
|
||||||
|
messages: List[ChatMessage]
|
||||||
|
temperature: Optional[float] = None
|
||||||
|
top_p: Optional[float] = None
|
||||||
|
max_length: Optional[int] = None
|
||||||
|
stream: Optional[bool] = False
|
||||||
|
|
||||||
|
|
||||||
|
class ChatCompletionResponseChoice(BaseModel):
|
||||||
|
index: int
|
||||||
|
message: ChatMessage
|
||||||
|
finish_reason: Literal["stop", "length"]
|
||||||
|
|
||||||
|
|
||||||
|
class ChatCompletionResponseStreamChoice(BaseModel):
|
||||||
|
index: int
|
||||||
|
delta: DeltaMessage
|
||||||
|
finish_reason: Optional[Literal["stop", "length"]]
|
||||||
|
|
||||||
|
|
||||||
|
class ChatCompletionResponse(BaseModel):
|
||||||
|
model: str
|
||||||
|
object: Literal["chat.completion", "chat.completion.chunk"]
|
||||||
|
choices: List[Union[ChatCompletionResponseChoice, ChatCompletionResponseStreamChoice]]
|
||||||
|
created: Optional[int] = Field(default_factory=lambda: int(time.time()))
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/v1/models", response_model=ModelList)
|
||||||
|
async def list_models():
|
||||||
|
global model_args
|
||||||
|
model_card = ModelCard(id="gpt-3.5-turbo")
|
||||||
|
return ModelList(data=[model_card])
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
|
||||||
|
async def create_chat_completion(request: ChatCompletionRequest):
|
||||||
|
global model, tokenizer
|
||||||
|
|
||||||
|
if request.messages[-1].role != "user":
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid request")
|
||||||
|
query = request.messages[-1].content
|
||||||
|
|
||||||
|
prev_messages = request.messages[:-1]
|
||||||
|
if len(prev_messages) > 0 and prev_messages[0].role == "system":
|
||||||
|
query = prev_messages.pop(0).content + query
|
||||||
|
|
||||||
|
history = []
|
||||||
|
if len(prev_messages) % 2 == 0:
|
||||||
|
for i in range(0, len(prev_messages), 2):
|
||||||
|
if prev_messages[i].role == "user" and prev_messages[i+1].role == "assistant":
|
||||||
|
history.append([prev_messages[i].content, prev_messages[i+1].content])
|
||||||
|
|
||||||
|
if request.stream:
|
||||||
|
generate = predict(query, history, request.model)
|
||||||
|
return StreamingResponse(generate, media_type="text/event-stream")
|
||||||
|
|
||||||
|
response, _ = model.chat(tokenizer, query, history=history)
|
||||||
|
choice_data = ChatCompletionResponseChoice(
|
||||||
|
index=0,
|
||||||
|
message=ChatMessage(role="assistant", content=response),
|
||||||
|
finish_reason="stop"
|
||||||
|
)
|
||||||
|
|
||||||
|
return ChatCompletionResponse(model=request.model, choices=[choice_data], object="chat.completion")
|
||||||
|
|
||||||
|
|
||||||
|
async def predict(query: str, history: List[List[str]], model_id: str):
|
||||||
|
global model, tokenizer
|
||||||
|
|
||||||
|
choice_data = ChatCompletionResponseStreamChoice(
|
||||||
|
index=0,
|
||||||
|
delta=DeltaMessage(role="assistant"),
|
||||||
|
finish_reason=None
|
||||||
|
)
|
||||||
|
chunk = ChatCompletionResponse(model=model_id, choices=[choice_data], object="chat.completion.chunk")
|
||||||
|
yield "data: {}\n\n".format(chunk.json(exclude_unset=True, ensure_ascii=False))
|
||||||
|
|
||||||
|
current_length = 0
|
||||||
|
|
||||||
|
for new_response, _ in model.stream_chat(tokenizer, query, history):
|
||||||
|
if len(new_response) == current_length:
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_text = new_response[current_length:]
|
||||||
|
current_length = len(new_response)
|
||||||
|
|
||||||
|
choice_data = ChatCompletionResponseStreamChoice(
|
||||||
|
index=0,
|
||||||
|
delta=DeltaMessage(content=new_text),
|
||||||
|
finish_reason=None
|
||||||
|
)
|
||||||
|
chunk = ChatCompletionResponse(model=model_id, choices=[choice_data], object="chat.completion.chunk")
|
||||||
|
yield "data: {}\n\n".format(chunk.json(exclude_unset=True, ensure_ascii=False))
|
||||||
|
|
||||||
|
choice_data = ChatCompletionResponseStreamChoice(
|
||||||
|
index=0,
|
||||||
|
delta=DeltaMessage(),
|
||||||
|
finish_reason="stop"
|
||||||
|
)
|
||||||
|
chunk = ChatCompletionResponse(model=model_id, choices=[choice_data], object="chat.completion.chunk")
|
||||||
|
yield "data: {}\n\n".format(chunk.json(exclude_unset=True, ensure_ascii=False))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
|
||||||
|
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).cuda()
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)
|
Loading…
Reference in New Issue
Block a user