QwenLM / Qwen-Agent

Agent framework and applications built upon Qwen>=2.0, featuring Function Calling, Code Interpreter, RAG, and Chrome extension.
https://pypi.org/project/qwen-agent/
Other
3.38k stars 335 forks source link

BadRequestError #334

Closed tomoyuki28jp closed 1 month ago

tomoyuki28jp commented 1 month ago

Code from this file

from qwen_agent.llm import get_chat_model

def test():
    # Config for the model
    llm_cfg_oai = {
        'model_type': 'qwenvl_oai',
        'model': 'Qwen2-VL-7B-Instruct',
        'model_server': 'http://localhost:8000/v1',  # api_base
        'api_key': 'EMPTY',
    }
    llm = get_chat_model(llm_cfg_oai)

    messages = [{
        'role': 'user',
        'content': [{
            'image': 'https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg'

        }, {
            'text': 'What is in the picture?'
        }]
    }]

    responses = []
    for responses in llm.chat(
            messages=messages,
            stream=False,
            extra_generate_cfg=dict(
                temperature=0.0,
            )
    ):
        print(responses)
    messages.extend(responses)

if __name__ == '__main__':
    test()

Client Side Error

Traceback (most recent call last):
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/oai.py", line 103, in _chat_no_stream
    response = self._chat_complete_create(model=self.model, messages=messages, stream=False, **generate_cfg)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/oai.py", line 66, in _chat_complete_create
    return client.chat.completions.create(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/openai/_utils/_utils.py", line 274, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/openai/resources/chat/completions.py", line 668, in create
    return self._post(
           ^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/openai/_base_client.py", line 1260, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/openai/_base_client.py", line 937, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/openai/_base_client.py", line 1041, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'object': 'error', 'message': "1 validation error for ValidatorIterator\n0.type\n  Field required [type=missing, input_value={'text': 'You are a helpful assistant.'}, input_type=dict]\n    For further information visit https://errors.pydantic.dev/2.9/v/missing", 'type': 'BadRequestError', 'param': None, 'code': 400}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tomo/Downloads/src/fnc5.py", line 35, in <module>
    test()
  File "/Users/tomo/Downloads/src/fnc5.py", line 24, in test
    for responses in llm.chat(
                     ^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 169, in chat
    output = retry_model_service(_call_model_service, max_retries=self.max_retries)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 413, in retry_model_service
    num_retries, delay = _raise_or_delay(e, num_retries, delay, max_retries)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 444, in _raise_or_delay
    raise e
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 410, in retry_model_service
    return fn()
           ^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 156, in _call_model_service
    return self._chat(
           ^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/base.py", line 199, in _chat
    return self._chat_no_stream(messages, generate_cfg=generate_cfg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/qwenvl_oai.py", line 49, in _chat_no_stream
    return super()._chat_no_stream(messages=messages, generate_cfg=generate_cfg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomo/.pyenv/versions/3.12.4/lib/python3.12/site-packages/qwen_agent/llm/oai.py", line 106, in _chat_no_stream
    raise ModelServiceError(exception=ex)
qwen_agent.llm.base.ModelServiceError: Error code: 400 - {'object': 'error', 'message': "1 validation error for ValidatorIterator\n0.type\n  Field required [type=missing, input_value={'text': 'You are a helpful assistant.'}, input_type=dict]\n    For further information visit https://errors.pydantic.dev/2.9/v/missing", 'type': 'BadRequestError', 'param': None, 'code': 400}

vLLM Error Log

python -m vllm.entrypoints.openai.api_server --served-model-name Qwen2-VL-7B-Instruct --model Qwen/Qwen2-VL-7B-Instruct --max-model-len 2048

ERROR 09-07 08:39:03 serving_chat.py:134] Error in applying chat template from request: 1 validation error for ValidatorIterator
ERROR 09-07 08:39:03 serving_chat.py:134] 0.type
ERROR 09-07 08:39:03 serving_chat.py:134]   Field required [type=missing, input_value={'text': 'You are a helpful assistant.'}, input_type=dict]
ERROR 09-07 08:39:03 serving_chat.py:134]     For further information visit https://errors.pydantic.dev/2.9/v/missing
INFO:     103.5.140.152:62514 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request

Ref

https://github.com/QwenLM/Qwen2-VL/issues/142

tomoyuki28jp commented 1 month ago

If I comment out the 'model_type': 'qwenvl_oai', line, the error disappears. However, it seems that the image is not loaded correctly.

When image: https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg

output: The picture shows a dog and a girl. The dog is standing on its hind legs, and the girl is holding the dog's leash.

When I upload the same image with the name 0.jpeg and use the URL

output: I'm sorry, but as an AI, I don't have the ability to view or interpret images. I can only provide information and answer questions to the best of my knowledge based on the text you provide. Is there anything else I can help you with?

JianxinMa commented 1 month ago

Please upgrade the qwen-agent package to version 0.0.9 or later, and check if it resolves the issue.

pip install "qwen-agent>=0.0.9"

My colleague will investigate further on Monday if the issue still exists.

tomoyuki28jp commented 1 month ago

@JianxinMa Thank you for your reply. I upgraded the qwen-agent package and the problem was solved.