THUDM / ChatGLM3

ChatGLM3 series: Open Bilingual Chat LLMs | 开源双语对话语言模型
Apache License 2.0
13.15k stars 1.52k forks source link

openai_api.py 运行报错 AttributeError: type object 'UsageInfo' has no attribute 'model_validate' #398

Closed txy6666yr closed 8 months ago

txy6666yr commented 8 months ago

System Info / 系統信息

(base) [root@nlu-tianxingyuan-sn46fg-0 openai_api_demo]$pip list
Package                  Version
------------------------ -----------
absl-py                  1.4.0
accelerate               0.19.0
aiofiles                 22.1.0
aiohttp                  3.8.4
aiosignal                1.3.1
aiosqlite                0.19.0
altair                   5.0.0
anyio                    3.6.2
appdirs                  1.4.4
argon2-cffi              21.3.0
argon2-cffi-bindings     21.2.0
arrow                    1.2.3
asttokens                2.2.1
async-timeout            4.0.2
attrs                    23.1.0
Babel                    2.12.1
backcall                 0.2.0
beautifulsoup4           4.12.2
bleach                   6.0.0
blinker                  1.7.0
brotlipy                 0.7.0
cachetools               5.3.0
certifi                  2023.5.7
cffi                     1.15.1
charset-normalizer       3.1.0
click                    8.1.3
cmake                    3.26.3
comm                     0.1.3
conda                    23.1.0
conda-content-trust      0.1.3
conda-package-handling   2.0.2
conda_package_streaming  0.7.0
contourpy                1.0.7
cpm-kernels              1.0.11
cryptography             38.0.4
cycler                   0.11.0
datasets                 2.12.0
debugpy                  1.6.7
decorator                5.1.1
defusedxml               0.7.1
dill                     0.3.6
docker-pycreds           0.4.0
einops                   0.6.1
et-xmlfile               1.1.0
evaluate                 0.4.0
executing                1.2.0
fastapi                  0.95.1
fastjsonschema           2.16.3
ffmpy                    0.3.0
filelock                 3.12.0
fire                     0.5.0
flash-attn               1.0.4
fonttools                4.39.4
fqdn                     1.5.1
frozenlist               1.3.3
fschat                   0.2.7
fsspec                   2023.5.0
gevent                   22.10.2
gitdb                    4.0.10
GitPython                3.1.31
google-auth              2.18.0
google-auth-oauthlib     1.0.0
gradio                   3.23.0
greenlet                 2.0.2
grequests                0.6.0
grpcio                   1.54.0
h11                      0.14.0
httpcore                 0.17.0
httpx                    0.24.0
huggingface-hub          0.19.4
idna                     3.4
importlib-metadata       6.8.0
importlib-resources      5.12.0
ipykernel                6.23.0
ipython                  8.13.2
ipython-genutils         0.2.0
isoduration              20.11.0
jedi                     0.18.2
Jinja2                   3.1.2
joblib                   1.2.0
json5                    0.9.11
jsonpointer              2.3
jsonschema               4.17.3
jupyter_client           8.2.0
jupyter_core             5.3.0
jupyter-events           0.6.3
jupyter_server           2.5.0
jupyter_server_fileid    0.9.0
jupyter_server_terminals 0.4.4
jupyter_server_ydoc      0.8.0
jupyter-ydoc             0.2.4
jupyterlab               3.6.3
jupyterlab-pygments      0.2.2
jupyterlab_server        2.22.1
kiwisolver               1.4.4
latex2mathml             3.76.0
linkify-it-py            2.0.2
lit                      16.0.3
loguru                   0.7.2
Markdown                 3.4.3
markdown-it-py           2.2.0
markdown2                2.4.8
MarkupSafe               2.1.2
matplotlib               3.7.1
matplotlib-inline        0.1.6
mdit-py-plugins          0.3.3
mdtex2html               1.2.0
mdurl                    0.1.2
mistune                  2.0.5
mpmath                   1.3.0
multidict                6.0.4
multiprocess             0.70.14
nbclassic                1.0.0
nbclient                 0.7.4
nbconvert                7.4.0
nbformat                 5.8.0
nest-asyncio             1.5.6
networkx                 3.1
nh3                      0.2.11
nltk                     3.8.1
notebook                 6.5.4
notebook_shim            0.2.3
numpy                    1.24.3
oauthlib                 3.2.2
openai                   0.27.6
openpyxl                 3.1.2
orjson                   3.8.12
packaging                23.1
pandas                   2.0.1
pandocfilters            1.5.0
parso                    0.8.3
pathtools                0.1.2
pexpect                  4.8.0
pickleshare              0.7.5
Pillow                   9.5.0
pip                      23.1.2
pip-requirements.txt     100.10.7
platformdirs             3.5.1
pluggy                   1.0.0
prometheus-client        0.16.0
prompt-toolkit           3.0.38
protobuf                 4.23.0
psutil                   5.9.5
ptyprocess               0.7.0
pure-eval                0.2.2
pyarrow                  12.0.0
pyasn1                   0.5.0
pyasn1-modules           0.3.0
pybind11                 2.10.4
pycosat                  0.6.4
pycparser                2.21
pydantic                 1.10.7
pydeck                   0.8.1b0
pydub                    0.25.1
Pygments                 2.15.1
pylcs                    0.0.8
pyOpenSSL                22.0.0
pyparsing                3.0.9
pyrsistent               0.19.3
PySocks                  1.7.1
python-dateutil          2.8.2
python-json-logger       2.0.7
python-multipart         0.0.6
pytz                     2023.3
PyYAML                   6.0
pyzmq                    25.0.2
regex                    2023.5.5
requests                 2.30.0
requests-oauthlib        1.3.1
responses                0.18.0
rfc3339-validator        0.1.4
rfc3986-validator        0.1.1
rich                     13.3.5
rouge                    1.0.1
rouge-score              0.1.2
rsa                      4.9
ruamel.yaml              0.17.21
ruamel.yaml.clib         0.2.6
semantic-version         2.10.0
Send2Trash               1.8.2
sentencepiece            0.1.99
sentry-sdk               1.22.2
setproctitle             1.3.2
setuptools               65.6.3
shortuuid                1.0.11
six                      1.16.0
smmap                    5.0.0
sniffio                  1.3.0
soupsieve                2.4.1
sse-starlette            1.8.1
stack-data               0.6.2
starlette                0.26.1
streamlit                1.28.2
supervisor               4.2.5
svgwrite                 1.4.3
sympy                    1.12
tenacity                 8.2.3
tensorboard              2.13.0
tensorboard-data-server  0.7.0
termcolor                2.3.0
terminado                0.17.1
tinycss2                 1.2.1
tokenizers               0.13.3
toml                     0.10.2
tomli                    2.0.1
toolz                    0.12.0
torch                    2.0.0+cu118
tornado                  6.3.1
tqdm                     4.65.0
traitlets                5.9.0
transformers             4.29.1
triton                   2.0.0
typing_extensions        4.5.0
tzdata                   2023.3
tzlocal                  5.2
uc-micro-py              1.0.2
uri-template             1.2.0
urllib3                  2.0.2
uvicorn                  0.22.0
validators               0.22.0
wandb                    0.15.2
watchdog                 3.0.0
wavedrom                 2.0.3.post3
wcwidth                  0.2.6
webcolors                1.13
webencodings             0.5.1
websocket-client         1.5.1
websockets               11.0.3
Werkzeug                 2.3.4
wheel                    0.37.1
xxhash                   3.2.0
y-py                     0.5.9
yarl                     1.9.2
ypy-websocket            0.8.2
zipp                     3.15.0
zope.event               4.6
zope.interface           6.0
zstandard                0.18.0

Who can help? / 谁可以帮助到您?

No response

Information / 问题信息

Reproduction / 复现过程

魔塔下载官方的chatglm3-6b模型 运行 openai_api.py 然后运行 openai_api_request.py 报错

2023-11-22 10:52:19.109 | DEBUG    | __main__:create_chat_completion:145 - ==== request ====
{'messages': [ChatMessage(role='system', content="You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.", name=None, function_call=None), ChatMessage(role='user', content='你好,给我讲一个故事,大概100字', name=None, function_call=None)], 'temperature': 0.8, 'top_p': 0.8, 'max_tokens': 100, 'echo': False, 'stream': False, 'repetition_penalty': 1.1, 'functions': None}
INFO:     127.0.0.1:52440 - "POST /v1/chat/completions HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 428, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/opt/conda/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/fastapi/applications.py", line 276, in __call__
    await super().__call__(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await dependant.call(**values)
  File "/mnt/pfs-guan-ssai/nlu/data/tianxy/ChatGLM3/openai_api_demo/openai_api.py", line 178, in create_chat_completion
    task_usage = UsageInfo.model_validate(response["usage"])
AttributeError: type object 'UsageInfo' has no attribute 'model_validate'

Expected behavior / 期待表现

请问是什么原因?

txy6666yr commented 8 months ago

已解决是 pydantic 版本问题,大家遇到相同问题可以查看 pydantic 版本

rainsoft commented 8 months ago

和pydantic版本无关啊

hawcat commented 8 months ago

和pydantic版本无关啊

pydantic == 2.5.1 再往上就不行了

tangsipeng commented 8 months ago

我按照之前的版本修改了这两句 把 task_usage = UsageInfo.model_validate(response["usage"]) for usage_key, usage_value in task_usage.model_dump().items(): 替换成 task_usage = UsageInfo.parse_obj(response["usage"]) for usage_key, usage_value in task_usage.dict().items():

jingzl commented 7 months ago

调整为 pydantic == 2.5.1 ,可以。

如果替换原来的代码,openai_api.py 运行时会出现新的错误