Find answers from the community

Updated 9 months ago

Chat

Hello guys! I have a problem with ChatPromptTemplate when combined with QueryPipeline.

For some reason, it raises an error: Input is not stringable when processing system message.

The code snippet is here:

Plain Text
system_prompt = "You are a helpful asistant."

user_prompt = "Give me five reasons to try {query}."

prompt = ChatPromptTemplate(
  ChatMessage(role="system", content=system_prompt),
  ChatMessage(role="user", content=user_prompt),

pipeline = QueryPipeline(chain=[prompt, llm])

output = pipeline.run(query="diving")


Seems like QueryPipeine does not expect ChatMessage to be passed? It is not included in a set of stringable types.

I checked the docs and only found mentions of PromptTemplate but no examples for ChatPromptTemplate. Does this mean that it cannot be used with ChatPromptTemplates? Or i have a mistake somewhere?\

I tried regular prompt and it seems to work. But i need to define custom system prompt in order to use my model properly (Hermes 2 Pro uses custom system prompt for JSON mode)
L
s
32 comments
I think your definition of the template is not correct.. lemme check
yep, i got small typo, sorry for that, missed closing )

But it still doenst work, since in my code i tested it was without typo
Plain Text
from llama_index.core import ChatPromptTemplate
from llama_index.core.llms import ChatMessage, MessageRole

message_templates = [
    ChatMessage(content="You are an expert system.", role=MessageRole.SYSTEM),
    ChatMessage(
        content="Generate a short story about {topic}",
        role=MessageRole.USER,
    ),
]
chat_template = ChatPromptTemplate(message_templates=message_templates)
I think it should be like that
Ok, thanks, lemme check
Doesnt, help. The problem persists
Do you have the full traceback?
Plain Text
Traceback (most recent call last):
  File "/home/magnesium/Projects/LLM/local-rag/test.py", line 34, in <module>
    output = pipeline.run(query="diving")
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/query_pipeline/query.py", line 319, in run
    return self._run(
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/query_pipeline/query.py", line 442, in _run
    result_outputs = self._run_multi({root_key: kwargs})
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/query_pipeline/query.py", line 544, in _run_multi
    output_dict = module.run_component(**module_input)
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/base/query_pipeline/query.py", line 198, in run_component
    kwargs = self.validate_component_inputs(kwargs)
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/base/query_pipeline/query.py", line 187, in validate_component_inputs
    return self._validate_component_inputs(input)
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/llms/llm.py", line 702, in _validate_component_inputs
    input["prompt"] = validate_and_convert_stringable(input["prompt"])
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/base/query_pipeline/query.py", line 57, in validate_and_convert_stringable
    new_input_list.append(validate_and_convert_stringable(elem))
  File "/home/magnesium/.pyenv/versions/lrag/lib/python3.10/site-packages/llama_index/core/base/query_pipeline/query.py", line 64, in validate_and_convert_stringable
    raise ValueError(f"Input {input} is not stringable.")
ValueError: Input system: You are a helpful asistant. is not stringable.
Code:

Plain Text
from llama_index.core.llms import ChatMessage
from llama_index.core import ChatPromptTemplate
from llama_index.core.query_pipeline import QueryPipeline

from llama_index.llms.llama_cpp import LlamaCPP

from dotenv import load_dotenv

import os

load_dotenv()


llm = LlamaCPP(
    model_path=os.getenv("LLAMACPP_MODEL"),
    temperature=0.0,
    max_new_tokens=1024,
    context_window=8192,
    model_kwargs={"n_gpu_layers": -1, "n_batch": 512},
)

system_prompt = "You are a helpful asistant."

user_prompt = "Give me five reasons to try {query}."

prompt = ChatPromptTemplate(
    [ChatMessage(role="system", content=system_prompt),
    ChatMessage(role="user", content=user_prompt)],
)


pipeline = QueryPipeline(chain=[prompt, llm], verbose=True)

output = pipeline.run(query="diving")
Hmmm, what version of llama-index do you have? I wasn't able to reproduce
It ran fine for me πŸ˜…
Plain Text
(venv) python ./test_pipeline.py
> Running module 75f80bac-9ba4-4b3b-9606-a6e767990d6e with input: 
query: diving

> Running module 265b3d3f-28ae-41ad-9c0e-ca25bd5898d6 with input: 
messages: [ChatMessage(role=<MessageRole.SYSTEM: 'system'>, content='You are a helpful asistant.', additional_kwargs={}), ChatMessage(role=<MessageRole.USER: 'user'>, content='Give me five reasons to try diving...

assistant: 1. Explore a whole new world: Diving allows you to discover the beauty and wonder of the underwater world, with vibrant marine life, colorful coral reefs, and fascinating underwater landscapes waiting to be explored.

2. Relaxation and stress relief: Diving can be a therapeutic and calming experience, as you immerse yourself in the peaceful underwater environment and focus on your breathing and movements.

3. Adventure and excitement: Diving offers the thrill of exploring unknown depths and encountering marine creatures up close, providing an adrenaline rush and a sense of adventure unlike any other activity.

4. Physical fitness: Diving is a great way to stay active and improve your overall fitness, as it engages multiple muscle groups and requires strength, flexibility, and endurance to navigate the water and currents.

5. Conservation and environmental awareness: Diving can also raise awareness about the importance of protecting our oceans and marine ecosystems, as you witness firsthand the beauty and fragility of underwater life, inspiring you to become a more responsible steward of the environment.
Although I did swap the LLM for openai, but the traceback doesn't seem related to the LLM
llama-index==0.10.23
πŸ€” I tried with ollama, it also worked
maybe try a fresh venv, just to be sure? Or try swapping the LLM class if you have access to openai or ollama?
(I refuse to setup llama-cpp, its such a pain, ollama is much better πŸ˜…)
Works with ollama. Damn. That's unfortunate. I might be unable to use ollama on Databricks instance at work

However llamacpp works with with PromptTempate
@Logan M + it seems like it might work normally.

Plain Text
prompt = ChatPromptTemplate(
    [ChatMessage(role="system", content=system_prompt),
    ChatMessage(role="user", content=user_prompt)],
)

input = prompt.format_messages(query="diving")

output = llm.chat(input)


This snippet works with llamacpp
Yea that would work with llamacpp. Really weird that it doesn't work in the query pipeline with llamacpp πŸ˜… I might have to install it to debug properly what the issue is
Super strange that it would be LLM dependant, I wonder what's going on there
With family this weekend, so can't fully dive in. Will have to try later tonight
I understand. Appreciate your help!
Any updates on this?
Ok, I think I figured this out. LLMs have this metadata attribute llm.metadata -- part of that is declaring whether the LLM is a chat model or not

Since LlamaCPP has this hardcoded to not a chat model, it gets turned into an LLMCompletionComponent instead of an LLMChatComponent

LLMCompletionComponents cannot accept chat messages as input
The solution here would be creating the component yourself I think
Plain Text
from llama_index.core.llms.llm import LLMChatComponent

pipeline = QueryPipeline(chain=[prompt, LLMChatComponent(llm=llm)], verbose=True)
That worked for me
A lot of this required just digging through the source code -- this is still a pretty new component, I wouldn't expect a user to know to do this πŸ˜…
Thank you so much for your help! Seems like it solves the issue.

However, in my opinion it is kind of tricky to assume that LLamaCPP is not a chat model by default. Since they provide a unified interface and resolve many templating stuff under the hood based on metadata from GGUF file.

Seems like langchain also has some weird behaviors related to chat llamacpp models.
Yea that's fair. The LLM class itself hasn't been updated in ages, it's still relying on the user to provide templating functions, and it does not use and message dict features.

I'd highly welcome a PR to the class to improve it
Add a reply
Sign up and join the conversation on Discord