Find answers from the community

Updated last month

Injecting existing history into chat engine request fails due to missing index and id

I'm trying to inject existing history into a Chat Engine request, but when I follow the tutorial here: https://docs.llamaindex.ai/en/stable/module_guides/deploying/chat_engines/usage_pattern/#low-level-composition-api

it says that I need index and id, but I can't find examples of those and everything I try gets a pydantic error
O
L
7 comments
Here's the pydantic error when I give a list of ChatMessage with an id (my message guids) and index (of the message sort order).

Plain Text
message_templates.1
ray-indexer  |   Input should be a valid dictionary or instance of ChatMessage [type=model_type, input_value=ChatMessage(id='4a93f34d-...s=None, class_name=None), input_type=ChatMessage]
What does your code look like in this case?
For creating the chat message list?
helper functions:
Plain Text
def get_model_role(created_by):
    if created_by == "Ray":
        return MessageRole.ASSISTANT
    else:
        return MessageRole.USER

def convert_chat_to_chat_message(index, message):
    print(index, message)
    return ChatMessage(
        id=message["id"],
        index=index,
        content=message["message"], 
        role=get_model_role(message["created_by"])
    )


Creating the chat_history object:
Plain Text
# Convert all but the last message to a List[ChatMessage]
    chat_history = [convert_chat_to_chat_message(index, message) for index, message in enumerate(chat.messages[:-1])]

    # Send message plus history to chat engine
    result = chat_engine.chat(
        message = chat.messages[-1]["message"],
        chat_history = chat_history
    )
Where do you import chat message?

(Actually, pretty confused, index and id are not part of the chat message class in llama-index)
ohhh...my ide imported from llama_cloud
If anyone else runs into this, you should be using:
Plain Text
from llama_index.core.llms import ChatMessage, MessageRole

and not this:
Plain Text
from llama_cloud import ChatMessage, MessageRole
Add a reply
Sign up and join the conversation on Discord