langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.89k stars 14k forks source link

Errors about ErnieBotChat #10309

Closed liye199033 closed 10 months ago

liye199033 commented 10 months ago

Issue you'd like to raise.

my test code

import unittest
from langchain.chat_models import ErnieBotChat
from langchain.schema import (
    HumanMessage
)

class TestErnieBotCase(unittest.TestCase):
    def test_ernie_bot(self):
        chat_llm = ErnieBotChat(
            ernie_client_id="xxx",
            ernie_client_secret="xxx"
        )
        result = chat_llm.generate(messages=[HumanMessage(content="请列出清朝的所有皇帝的姓名和年号")])
        print(result)

if __name__ == '__main__':
    unittest.main()

it's return error:

Error
Traceback (most recent call last):
  File "/Users/keigo/Workspace/study/langchain/tests/test_erniebot.py", line 14, in test_ernie_bot
    result = chat_llm.generate(messages=[HumanMessage(content="请列出清朝的所有皇帝的姓名和年号")])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/base.py", line 309, in generate
    raise e
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/base.py", line 299, in generate
    self._generate_with_cache(
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/base.py", line 446, in _generate_with_cache
    return self._generate(
           ^^^^^^^^^^^^^^^
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/ernie.py", line 157, in _generate
    "messages": [_convert_message_to_dict(m) for m in messages],
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/ernie.py", line 157, in <listcomp>
    "messages": [_convert_message_to_dict(m) for m in messages],
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keigo/Workspace/study/langchain/venv/lib/python3.11/site-packages/langchain/chat_models/ernie.py", line 31, in _convert_message_to_dict
    raise ValueError(f"Got unknown type {message}")
ValueError: Got unknown type ('content', '请列出清朝的所有皇帝的姓名和年号')

in ernie.py file function _convert_message_to_dict,the variable message is of type tuple。

Suggestion:

No response

dosubot[bot] commented 10 months ago

🤖

Hello,

Thank you for reaching out. The error you're encountering is due to the _convert_message_to_dict function in the ErnieBotChat class. This function is designed to convert a message object into a dictionary, but it only knows how to handle instances of ChatMessage, HumanMessage, and AIMessage. If it receives an instance of any other type, it raises a ValueError.

The error message you're seeing indicates that the function received a tuple with two elements: 'content' and '请列出清朝的所有皇帝的姓名和年号'. This is not an instance of any of the three types the function knows how to handle, so it raises a ValueError.

To fix this issue, you need to ensure that the _convert_message_to_dict function is only ever called with instances of ChatMessage, HumanMessage, or AIMessage. Check the part of your code where you're calling this function, and make sure you're not accidentally passing in a tuple instead of a message object.

Here is the relevant part of the code:

def _convert_message_to_dict(message: BaseMessage) -> dict:
    if isinstance(message, ChatMessage):
        message_dict = {"role": message.role, "content": message.content}
    elif isinstance(message, HumanMessage):
        message_dict = {"role": "user", "content": message.content}
    elif isinstance(message, AIMessage):
        message_dict = {"role": "assistant", "content": message.content}
    else:
        raise ValueError(f"Got unknown type {message}")
    return message_dict

And here is where it's being used:

payload = {
    "messages": [_convert_message_to_dict(m) for m in messages],
    "top_p": self.top_p,
    "temperature": self.temperature,
    "penalty_score": self.penalty_score,
    **kwargs,
}

The expected type of the messages parameter in the generate method of the ErnieBotChat class in the LangChain codebase is List[BaseMessage]. This is evident from the method signature:

def _generate(
    self,
    messages: List[BaseMessage],
    stop: Optional[List[str]] = None,
    run_manager: Optional[CallbackManagerForLLMRun] = None,
    **kwargs: Any,
) -> ChatResult:

Here, messages: List[BaseMessage] indicates that messages should be a list of instances of the BaseMessage class.

I hope this helps! If you have any other questions, feel free to ask.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

liye199033 commented 10 months ago

Thank you. See what the problem is