Replies: 1 comment
-
To add few-shot examples in the query string for a retrieval-augmented generation (RAG) task using LlamaIndex, you can follow this example: from llama_index.core.schema import TextNode
import json
# Load few-shot examples
few_shot_nodes = []
for line in open("../llama2_qa_citation_events.jsonl", "r"):
few_shot_nodes.append(TextNode(text=line))
few_shot_index = VectorStoreIndex(few_shot_nodes)
few_shot_retriever = few_shot_index.as_retriever(similarity_top_k=2)
# Define function to retrieve few-shot examples
def few_shot_examples_fn(**kwargs):
query_str = kwargs["query_str"]
retrieved_nodes = few_shot_retriever.retrieve(query_str)
result_strs = []
for n in retrieved_nodes:
raw_dict = json.loads(n.get_content())
query = raw_dict["query"]
response_dict = json.loads(raw_dict["response"])
result_str = f"""\
Query: {query}
Response: {response_dict}"""
result_strs.append(result_str)
return "\n\n".join(result_strs)
# Define prompt template with few-shot examples
qa_prompt_tmpl_str = """\
Context information is below.
---------------------
{context_str}
---------------------
Given the context information and not prior knowledge, \
answer the query asking about citations over different topics.
Please provide your answer in the form of a structured JSON format containing \
a list of authors as the citations. Some examples are given below.
{few_shot_examples}
Query: {query_str}
Answer: \
"""
qa_prompt_tmpl = PromptTemplate(
qa_prompt_tmpl_str,
function_mappings={"few_shot_examples": few_shot_examples_fn},
)
# Update query engine prompts
query_engine.update_prompts(
{"response_synthesizer:text_qa_template": qa_prompt_tmpl}
)
# Example query
citation_query_str = "Which citations are mentioned in the section on Safety RLHF?"
# Run query
response = query_engine.query(citation_query_str)
print(str(response)) This code demonstrates how to dynamically add few-shot examples to the prompt by setting the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am looking this example to add few shot examples in query string. Is there a easier way? can I have some sample code please?
llama_index/docs/docs/examples/prompts/prompts_rag.ipynb
Line 8 in 2377848
Beta Was this translation helpful? Give feedback.
All reactions