-
Hi, Thank you for showcasing the use of Llama2 for labeling topics. I use the same prompt with my dataset, which is unrelated to the example of "Environmental impacts of eating meat."
Thank you for your time and consideration! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Could you share your full code? That might help me understand what exactly is happening.
Few-shot learning is already applied with the Llama 2 example. That is what the [DOCUMENTS] and [KEYWORDS] tags are for. You can find more about that here. |
Beta Was this translation helpful? Give feedback.
-
Alright, it seems there are snippets of code missing here and there but I think I get the general gist of what you are trying to achieve. This might simply be a result of the prompt itself. With the one-shot approach here, it might be that the LLM "overfits" on that single example. When it does not know a good response or suffers from "lost in the middle syndrome", it might default to what it has seen first, which is the example. Instead, I can recommend the following approach with Zephyr which will be in the documentation soon. Here, the prompt might be of use to you but if you want to use it for Llama 2, make sure to use the chat template for Llama 2 instead. Zephyr (Mistral 7B)We can go a step further with open-source Large Language Models (LLMs) that have shown to match the performance of closed-source LLMs like ChatGPT. In this example, we will show you how to use Zephyr, a fine-tuning version of Mistral 7B. Mistral 7B outperforms other open-source LLMs at a much smaller scale and is a worthwhile solution for use cases such as topic modeling. We want to keep inference as fast as possible and a relatively small model helps with that. Zephyr is a fine-tuned version of Mistral 7B that was trained on a mix of publicly available and synthetic datasets using Direct Preference Optimization (DPO). To use Zephyr in BERTopic, we will first need to install and update a couple of packages that can handle quantized versions of Zephyr: pip install ctransformers[cuda]
pip install --upgrade git+https://github.com/huggingface/transformers Instead of loading in the full model, we can instead load a quantized model which is a compressed version of the original model: from ctransformers import AutoModelForCausalLM
from transformers import AutoTokenizer, pipeline
# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/zephyr-7B-alpha-GGUF",
model_file="zephyr-7b-alpha.Q4_K_M.gguf",
model_type="mistral",
gpu_layers=50,
hf=True
)
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
# Pipeline
generator = pipeline(
model=model, tokenizer=tokenizer,
task='text-generation',
max_new_tokens=50,
repetition_penalty=1.1
) This Zephyr model requires a specific prompt template in order to work: prompt = """<|system|>You are a helpful, respectful and honest assistant for labeling topics..</s>
<|user|>
I have a topic that contains the following documents:
[DOCUMENTS]
The topic is described by the following keywords: '[KEYWORDS]'.
Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.</s>
<|assistant|>""" After creating this prompt template, we can create our representation model to be used in BERTopic: from bertopic.representation import TextGeneration
# Text generation with Zephyr
zephyr = TextGeneration(generator, prompt=prompt)
representation_model = {"Zephyr": zephyr}
# Topic Modeling
topic_model = BERTopic(representation_model=representation_model, verbose=True) |
Beta Was this translation helpful? Give feedback.
Alright, it seems there are snippets of code missing here and there but I think I get the general gist of what you are trying to achieve. This might simply be a result of the prompt itself. With the one-shot approach here, it might be that the LLM "overfits" on that single example. When it does not know a good response or suffers from "lost in the middle syndrome", it might default to what it has seen first, which is the example.
Instead, I can recommend the following approach with Zephyr which will be in the documentation soon. Here, the prompt might be of use to you but if you want to use it for Llama 2, make sure to use the chat template for Llama 2 instead.
Zephyr (Mistral 7B)
We can …