Closed BlueKiji77 closed 2 months ago
Hey @BlueKiji77! Good to see you back in the labyrinth of code. Ready for another adventure? 🌀
Based on the error message you're encountering, it seems like the issue is related to the format of the input you're passing to the chat engine. The error message suggests that the chat engine expects an alternating pattern of user and assistant roles in the conversation history.
From the steps you've followed, it appears that you're passing a string directly to the chat
method. However, the chat
method expects a string representing the user's message and an optional list of ChatMessage
objects representing the chat history. Each ChatMessage
object should have a content
attribute representing the text content of the message and a role
attribute representing the role of the message, which can be either "user" or "assistant".
Here's an example of how you can structure your input:
from llama_index.core.chat_engine import ChatMessage
chat_history = [
ChatMessage(content="Hi, I'm a virtual assistant. How can I help you today?", role="assistant"),
ChatMessage(content="Can you tell me a joke?", role="user"),
]
user_message = "What are the first programs Paul Graham tried writing?"
response = chat_engine.chat(message=user_message, chat_history=chat_history)
In this example, the chat_history
list contains ChatMessage
objects that alternate between "assistant" and "user" roles, and the user_message
string represents the user's current message.
If you're not providing a chat history and are only passing a user message to the chat
method, the chat engine will treat it as a new conversation. In this case, the user message should be the first message in the conversation.
If you're still encountering issues after trying this, please provide more details about your implementation, and I'll be happy to help further. You can refer to the ContextChatEngine class in the LlamaIndex repository for more information about how the chat engine works.
I had tried that before and using your recommendation below unsuccessfully results with the same error: TemplateError: Conversation roles must alternate user/assistant/user/assistant/..
from llama_index.core.base.llms.types import ChatMessage
chat_history = [
ChatMessage(content="Hi, I'm a virtual assistant. How can I help you today?", role="assistant"),
ChatMessage(content="Can you tell me a joke?", role="user"),
]
user_message = "What are the first programs Paul Graham tried writing?"
response = chat_engine.chat(message=user_message, chat_history=chat_history)
This is coming from the LLM you are using. It's not supporting system messages, but this chat engine uses system messages.
I would provide a messages_to_prompt callable to the LLM constructor, to handle the messages that are system messages and formatting the prompt properly. This largely depends on your LLM
Bug Description
I am trying the Chat Engine - Context Mode tutorial from the docs and passing a string to the chat engine result in an error seemingly requiring a user/assistant/user/assistant format as input. I dug through the code but can' figure out what is causing the issue.
Version
llama_index-0.10.25
Steps to Reproduce
`
Setting and Loading Up Index
index_store_path= "/kaggle/working/storytelling-gamedesign-qdrantindexstore" client = qdrant_client.QdrantClient(path=index_store_path) qdrant_vector_store = QdrantVectorStore(client=client, collection_name="game_design_qdrant_store") qdrant_vector_store._collection_exists("game_design_qdrant_store")
output_dir = "/kaggle/input/" index_persist_dir = os.path.join(output_dir) persist_dir = "/kaggle/working/storytelling-gamedesign-qdrantindexstore/storage" storage_context = StorageContext.from_defaults( docstore=SimpleDocumentStore.from_persist_dir(persist_dir=persist_dir), vector_store=qdrant_vector_store, index_store=SimpleIndexStore.from_persist_dir(persist_dir=persist_dir), )
from llama_index.core import load_index_from_storage index_name = "game_design_qdrant_index" index = load_index_from_storage(storage_context)
from llama_index.core.memory import ChatMemoryBuffer
memory = ChatMemoryBuffer.from_defaults(token_limit=1500)
chat_engine = index.as_chat_engine( chat_mode="context", memory=memory, system_prompt=( "You are a chatbot, able to have normal interactions, as well as talk" "design in video games." ), )
response = chat_engine.chat( "What are the first programs Paul Graham tried writing?" ) print(response)
`
Relevant Logs/Tracbacks