ChatHuggingFace cutting total tokens #24125
-
Checked other resources
Commit to Help
Example Codefrom langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_huggingface import HuggingFaceEndPoint, ChatHuggingFace
# Using LLM
llama_llm = HuggingFaceEndpoint(
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
max_new_tokens=1024,
temperature=0.1,
huggingfacehub_api_token=HUGGINGFACE_API_KEY
)
messages = [
"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You're a helpful assistant that answer general questions.
question: {question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
]
prompt = ChatPromptTemplate.from_messages(messages)
parser = StrOutputParser()
chain_llm = prompt | llama_llm | parser
generate_with_llm = chain_llm.invoke({"question": "What is a blackhole?"})
print(f'LLM answer:\n {generate_with_llm}\n')
print(f'Total tokens using LLM: {len(generate_with_llm)}')
# Using ChatHuggingFace
llama_chat_model = ChatHuggingFace(llm=llama_llm)
chain_chat = prompt | llama_chat_model | parser
generate_with_chat = chain_chat.invoke({"question": "What is a blackhole?"})
print(f'Chat answer:\n {generate_with_chat}\n')
print(f'Total tokens using Chat: {len(generate_with_chat)}') DescriptionWhen I use ChatHuggingFace, the number of tokens is reduced. LLM answer: A fascinating topic! A black hole is a region in space where the gravitational pull is so strong that nothing, including light, can escape. It's formed when a massive star collapses in on itself and its gravity becomes so strong that it warps the fabric of spacetime around it. Here's a simplified explanation:
Black holes come in various sizes, ranging from small, stellar-mass black holes formed from the collapse of individual stars, to supermassive black holes found at the centers of galaxies, with masses millions or even billions of times that of the sun. Some interesting facts about black holes:
While black holes are still a topic of ongoing research, they continue to fascinate scientists and the general public alike, offering a glimpse into the mysterious and awe-inspiring universe we inhabit. Total tokens using LLM: 1671 Chat answer: A black hole is a region in space where the gravitational pull is so strong that nothing, including light, can escape. It's formed when a massive star collapses in on itself and its gravity becomes so strong that it warps the fabric of spacetime around it. Here's a simplified explanation:
Total tokens using Chat: 504 System InfoSystem Information
Package Information
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello, @luizguilhermedev! I'm here to help you with any bugs, questions, or contributions you may have. Let's work on this together while we wait for a human maintainer. The issue you're experiencing with To address this, you can adjust the from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
# Using LLM
llama_llm = HuggingFaceEndpoint(
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
max_new_tokens=2048, # Increase this value to increase the token limit
temperature=0.1,
huggingfacehub_api_token=HUGGINGFACE_API_KEY
)
messages = [
"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You're a helpful assistant that answer general questions.
question: {question}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
]
prompt = ChatPromptTemplate.from_messages(messages)
parser = StrOutputParser()
chain_llm = prompt | llama_llm | parser
generate_with_llm = chain_llm.invoke({"question": "What is a blackhole?"})
print(f'LLM answer:\n {generate_with_llm}\n')
print(f'Total tokens using LLM: {len(generate_with_llm)}')
# Using ChatHuggingFace
llama_chat_model = ChatHuggingFace(llm=llama_llm)
chain_chat = prompt | llama_chat_model | parser
generate_with_chat = chain_chat.invoke({"question": "What is a blackhole?"})
print(f'Chat answer:\n {generate_with_chat}\n')
print(f'Total tokens using Chat: {len(generate_with_chat)}') By increasing the |
Beta Was this translation helpful? Give feedback.
-
Try: |
Beta Was this translation helpful? Give feedback.
Try:
llama_chat_model = ChatHuggingFace(llm=llama_llm).bind(max_tokens=8192, temperature=0.0)