Closed bibhas2 closed 17 hours ago
Let me see
any workaround available?
A workaround is using HuggingFacePipeline
via ChatHuggingFace
. This is actually somewhat documented:
Instantiate the ChatHuggingFace to apply chat templates https://python.langchain.com/v0.2/docs/integrations/chat/huggingface/#2-instantiate-the-chathuggingface-to-apply-chat-templates
Note that until a release of langchain-huggingface
beyond 0.0.3 is made, you need to apply the following commit manually because of #22804:
https://github.com/langchain-ai/langchain/commit/4796b7eb15b4c3a352c950f56a30659f0379f6e2
A workaround is using
HuggingFacePipeline
viaChatHuggingFace
. This is actually somewhat documented:Instantiate the ChatHuggingFace to apply chat templates https://python.langchain.com/v0.2/docs/integrations/chat/huggingface/#2-instantiate-the-chathuggingface-to-apply-chat-templates
Note that until a release of
langchain-huggingface
beyond 0.0.3 is made, you need to apply the following commit manually because of #22804: 4796b7e
Hello, do you have any examples of code that uses both HuggingFacePipeline
and ChatHuggingFace
? My attempts to use both keeps resulting in errors.
I am facing the same problem with quantized variants of Meta Llama 3 8B-Instruct
A workaround is using
HuggingFacePipeline
viaChatHuggingFace
. This is actually somewhat documented:Instantiate the ChatHuggingFace to apply chat templates https://python.langchain.com/v0.2/docs/integrations/chat/huggingface/#2-instantiate-the-chathuggingface-to-apply-chat-templates
Note that until a release of
langchain-huggingface
beyond 0.0.3 is made, you need to apply the following commit manually because of #22804: 4796b7e
Can you please specify?
I am learning Langchain and I have had to just use pipeline directory instead of the HuggingFacePipeLine
or ChatHuggingFace
abstractions. Still trying to figure out to intregrate this with the rest on Langchain.
I believe I fixed the issue! I detailed pull request will be submitted tomorrow! Everything should work through ChatHuggingFace in my fork
Hi, @bibhas2. I'm helping the LangChain team manage their backlog and am marking this issue as stale.
Your issue regarding the HuggingFacePipeline
class not utilizing the chat template feature has been noted, and users have suggested using ChatHuggingFace
as a workaround. Additionally, there seems to be progress on a pull request to resolve this issue, as indicated by Soumil32.
Could you please let us know if this issue is still relevant to the latest version of the LangChain repository? If it is, feel free to comment here to keep it open. Otherwise, you can close it yourself, or it will be automatically closed in 7 days. Thank you!
my solution
#!/usr/bin/python3
from typing import Any, Union
from collections.abc import Sequence
from transformers import AutoTokenizer
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_core.prompt_values import ChatPromptValue, PromptValue
# NOTE: langchain-core >= 0.3.15
class HFChatPromptValue(ChatPromptValue):
tokenizer: Any = None
def to_string(self) -> str:
hf_messages = []
for m in self.messages:
if isinstance(m, HumanMessage):
role = 'user'
elif isinstance(m, AIMessage):
role = 'assistant'
elif isinstance(m, SystemMessage):
role = 'system'
else:
raise Exception(f'Got unsupported message type: {m}')
hf_messages.append({'role': role, 'content': m.content})
return self.tokenizer.apply_chat_template(hf_messages, tokenize = False, add_generation_prompt = True)
class HFChatPromptTemplate(ChatPromptTemplate):
tokenizer: Any = None
def format_prompt(self, **kwargs: Any) -> PromptValue:
messages = self.format_messages(**kwargs)
return HFChatPromptValue(messages = messages, tokenizer = self.tokenizer)
async def aformat_prompt(self, **kwargs: Any) -> PromptValue:
messages = await self.format_messages(**kwargs)
return HFChatPromptValue(messages = messages, tokenizer = self.tokenizer)
if __name__ == "__main__":
messages = [
('system', "you are a helpful AI bot. Your name is {name}"),
MessagesPlaceholder('chat_history'),
('human', '{user_input}')
]
tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen2.5-7B-Instruct')
prompt = HFChatPromptTemplate(messages, tokenizer = tokenizer)
txt = prompt.format(**{'user_input': 'what is your name', 'name': 'robot test', 'chat_history': [HumanMessage(content = 'your name is awesome!'), AIMessage(content = 'Thanks!')]})
print(txt)
Thank you for your detailed response and for sharing your solution! We appreciate your contribution to the discussion. We'll go ahead and close this issue now.
Checked other resources
Example Code
Hugging Face pipeline now has support for chat templates. This calls the
apply_chat_template()
of the tokenizer. This is a super useful feature which formats the input correctly according to the model. To apply the template one needs to pass a messages list to the pipeline as input (and not a prompt text).Langchain's
HuggingFacePipeline
class is written in a way that only prompt text is passed to the pipeline. We can see this in theHuggingFacePipeline._generate
method. As a result the prompt is constructed using Langchain's default template which is not the same as what the model works best with.Let's build an example.
This sends the following prompt.
The correct prompt, if chat template was applied, would be:
Error Message and Stack Trace (if applicable)
No response
Description
The
HuggingFacePipeline
class should what is necessary to convert theChatPromptTemplate
to a messages list and then pass it to the pipeline. This will cause the pipeline to useapply_chat_template()
of the tokenizer to correctly format the prompt.System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found: