Chat engine with fusion retriever #15242
Unanswered
d3buggerdan
asked this question in
Q&A
Replies: 1 comment 4 replies
-
To use a chat engine instead of a query engine with your
Here's how you can modify your code: Step 1: Initialize the Chat Engine# Assuming `index` is already defined and set up
chat_engine = index.as_chat_engine() Step 2: Modify the
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
@dosu
I made a FusionRetriever with the following code but instead of using a query engine id like to use a chat engine. How can i do it?
Function to generate queries
def generate_queries(llm, query_str: str, num_queries: int = 4):
fmt_prompt = query_gen_prompt.format(
num_queries=num_queries - 1, query=query_str
)
print(f"Formatted prompt for query generation:\n{fmt_prompt}\n") # Debugging
response = llm.complete(fmt_prompt)
print(f"Response from LLM for query generation:\n{response.text}\n") # Debugging
queries = response.text.split("\n")
return queries
Function to run queries synchronously
def run_queries_sync(queries, retrievers):
results_dict = {}
for query in queries:
print(f"Running query: {query}") # Debugging
for i, retriever in enumerate(retrievers):
query_result = retriever.retrieve(query)
results_dict[(query, i)] = query_result
print(f"Results for retriever {i} and query '{query}': {query_result}\n") # Debugging
return results_dict
Function to fuse results
def fuse_results(results_dict, similarity_top_k: int = 2):
k = 60.0 # Parameter used to control the impact of outlier rankings.
fused_scores = {}
text_to_node = {}
Function to determine whether to use the reranked result or fallback to LLM
def should_fallback_to_llm(reranked_nodes, cutoff_score=0.05):
if not reranked_nodes:
return True
top_score = reranked_nodes[0].score
return top_score < cutoff_score
Define FusionRetriever class
class FusionRetriever(BaseRetriever):
def init(
self,
llm,
retrievers: List[BaseRetriever],
similarity_top_k: int = 2,
cutoff_score: float = 0.05, # Added cutoff_score parameter
) -> None:
self._retrievers = retrievers
self._similarity_top_k = similarity_top_k
self._llm = llm
self._cutoff_score = cutoff_score # Store the cutoff score
super().init()
Example usage
vector_retriever = index.as_retriever(similarity_top_k=2)
fusion_retriever = FusionRetriever(llm, [vector_retriever], similarity_top_k=2, cutoff_score=0.05)
query_engine = RetrieverQueryEngine(fusion_retriever)
Beta Was this translation helpful? Give feedback.
All reactions