Are you sure it's the same context?
context_list = self.get_context(response)
just grabs the text from each source node
context = []
for context_info in response.source_nodes:
context.append(Document(text=context_info.node.get_content()))
Not totally sure how it can repeat context π€
An easier implementation might look like this though. Asyncio is concurrancy, not multithreading, so this should have about the same speed running on every response no?
async def aevaluate_response(self, query: str, response: Response) -> Evaluation:
"""Evaluate the response from an index.
Args:
query: Query for which response is generated from index.
response: Response object from an index based on the query.
Returns:
Evaluation object with passing boolean and feedback "YES" or "NO".
"""
answer = str(response)
context = self.get_context(response)
index = SummaryIndex.from_documents(
context, service_context=self.service_context
)
QUERY_RESPONSE_EVAL_PROMPT_TMPL = PromptTemplate(QUERY_RESPONSE_EVAL_PROMPT)
QUERY_RESPONSE_REFINE_PROMPT_TMPL = PromptTemplate(QUERY_RESPONSE_REFINE_PROMPT)
query_response = f"Question: {query}\nResponse: {answer}"
query_engine = index.as_query_engine(
text_qa_template=QUERY_RESPONSE_EVAL_PROMPT_TMPL,
refine_template=QUERY_RESPONSE_REFINE_PROMPT_TMPL,
)
# JUST ADDED AWAIT AQUERY HERE
response_obj = await query_engine.aquery(query_response)
raw_response_txt = str(response_obj)
if "yes" in raw_response_txt.lower():
return Evaluation(query, response, True, "YES")
else:
if self.raise_error:
raise ValueError("The response is invalid")
return Evaluation(query, response, False, "NO")