Views
No views yet
vllm with pre-given passages. Make sure to install dependencies listed at self-rag/requirements.txt.
To run our full inference pipeline with a retrieval system and fine-grained tree decoding, please use our code.1from transformers import AutoTokenizer, AutoModelForCausalLM
2from vllm import LLM, SamplingParams
3model = LLM("selfrag/selfrag_llama2_13b", download_dir="/gscratch/h2lab/akari/model_cache", dtype="half")
4sampling_params = SamplingParams(temperature=0.0, top_p=1.0, max_tokens=100, skip_special_tokens=False)
5def format_prompt(input, paragraph=None):
6 prompt = "### Instruction:\n{0}\n\n### Response:\n".format(input)
7 if paragraph is not None:
8 prompt += "[Retrieval]<paragraph>{0}</paragraph>".format(paragraph)
9 return prompt
10query_1 = "Leave odd one out: twitter, instagram, whatsapp."
11query_2 = "Can you tell me the difference between llamas and alpacas?"
12queries = [query_1, query_2]
13preds = model.generate([format_prompt(query) for query in queries], sampling_params)
14for pred in preds:
15 print("Model prediction: {0}".format(pred.outputs[0].text))
16# Model prediction: Twitter, Instagram, and WhatsApp are all social media platforms.[No Retrieval]WhatsApp is the odd one out because it is a messaging app, while Twitter and # Instagram are primarily used for sharing photos and videos.[Utility:5]</s> (this query doesn't require factual grounding; just skip retrieval and do normal instruction-following generation)
17# Model prediction: Sure![Retrieval]<paragraph> ... (this query requires factual grounding, call a retriever)
18# generate with the retrieved passage
19prompt = format_prompt("Can you tell me the difference between llamas and alpacas?", paragraph="The alpaca (Lama pacos) is a species of South American camelid mammal. It is similar to, and often confused with, the llama. Alpacas are considerably smaller than llamas, and unlike llamas, they were not bred to be working animals, but were bred specifically for their fiber.")
20preds = model.generate([prompt], sampling_params)
21print([pred.outputs[0].text for pred in preds])
22# ['[Relevant]Alpacas are considerably smaller than llamas, and unlike llamas, they were not bred to be working animals, but were bred specifically for their fiber.[Fully supported][Utility:5]</s>']format_prompt function, your input should be formed as### Instruction:\n{instruction}\n\n### Response:\n".format(instruction)### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n"### Response:\n", but make sure to mark paragraphs as paragraph tokens (i.e., <paragraph>{0}</paragraph>).@article{asai2023selfrag,
author = {Asai, Akari and Wu, Zeqiu and Wang, Yizhong and Sil, Avirup and Hajishirzi, Hannaneh},
title = {{Self-RAG}: Learning to Retrieve, Generate, and Critique through Self-Reflection},
year = {2023},
journal = { arXiv preprint arXiv:2310.11511 },
URL = {https://arxiv.org/abs/2310.11511}
}