-
I am generating queries from my query_str like show below. My query engine like to replace query_str to queries
quries are generate like below:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, you can integrate the generated queries into your query engine instead of using
Here is a complete example: from llama_index.core.tools import ToolMetadata
from llama_index.core.selectors import (
PydanticMultiSelector,
PydanticSingleSelector,
)
from llama_index.core import PromptTemplate
from llama_index.llms.openai import OpenAI
from llama_index.core.retrievers import BaseRetriever
from llama_index.core.llms import LLM
from dataclasses import dataclass
from typing import Optional, List
# Define the query generation prompt
query_gen_str = """\
You are a helpful assistant that generates multiple search queries based on a \
single input query. Generate {num_queries} search queries, one on each line, \
related to the following input query:
Query: {query}
Queries:
"""
query_gen_prompt = PromptTemplate(query_gen_str)
# Function to generate queries
def generate_queries(query: str, llm, num_queries: int = 4):
response = llm.predict(
query_gen_prompt, num_queries=num_queries, query=query
)
# assume LLM properly put each query on a newline
queries = response.split("\n")
queries_str = "\n".join(queries)
print(f"Generated queries:\n{queries_str}")
return queries
# Example LLM initialization
llm = OpenAI(api_key="your_openai_api_key")
# Generate queries
query_str = "What did Paul Graham do after going to RISD?"
queries = generate_queries(query_str, llm)
# Define a custom query engine to handle multiple queries
@dataclass
class Response:
response: str
source_nodes: Optional[List] = None
def __str__(self):
return self.response
class MyQueryEngine:
def __init__(
self,
retriever: BaseRetriever,
qa_prompt: PromptTemplate,
llm: LLM,
num_children=10,
) -> None:
self._retriever = retriever
self._qa_prompt = qa_prompt
self._llm = llm
self._num_children = num_children
def query(self, queries: List[str]):
all_responses = []
for query in queries:
retrieved_nodes = self._retriever.retrieve(query)
response_txt, _ = generate_response_hs(
retrieved_nodes,
query,
self._qa_prompt,
self._llm,
num_children=self._num_children,
)
all_responses.append(response_txt)
combined_response = "\n".join(all_responses)
response = Response(combined_response)
return response
async def aquery(self, queries: List[str]):
all_responses = []
for query in queries:
retrieved_nodes = await self._retriever.aretrieve(query)
response_txt, _ = await agenerate_response_hs(
retrieved_nodes,
query,
self._qa_prompt,
self._llm,
num_children=self._num_children,
)
all_responses.append(response_txt)
combined_response = "\n".join(all_responses)
response = Response(combined_response)
return response
# Initialize and use the custom query engine
retriever = ... # Initialize your retriever
qa_prompt = ... # Define your QA prompt
query_engine = MyQueryEngine(retriever, qa_prompt, llm, num_children=10)
response = query_engine.query(queries)
print(str(response))
# For async usage
response = await query_engine.aquery(queries)
print(str(response)) This approach allows you to generate multiple queries from a single input query and use them in your query engine to retrieve and synthesize responses [1][2]. |
Beta Was this translation helpful? Give feedback.
Yes, you can integrate the generated queries automatically by directing the output of the queries into your query engine. Here's an example of how to achieve this:
Generate the Queries: Use the provided code to generate multiple queries from the original
query_str
.Modify the Query Engine: Update your query engine to handle multiple queries.
Here is a complete example: