Skip to content

feat(docagent): hypothetical questions - #615

Closed
dfm88 wants to merge 21 commits into
langroid:mainfrom
dfm88:feat/docagent-hypotetical-questions
Closed

dfm88 wants to merge 21 commits into
langroid:mainfrom
dfm88:feat/docagent-hypotetical-questions

Conversation

@dfm88

@dfm88 dfm88 commented Nov 18, 2024

Copy link
Copy Markdown
Contributor

Hypothetical Question Index

Despite there being no papers supporting this technique, Hypothetical Question Indexing is mentioned in several blogs as an advanced RAG technique. It somewhat contrasts with Hypothetical Answer (HyDE) and intervenes during the embedding saving phase, specifically trying to solve the problem where the question posed on an embedding contains words distant from it. Therefore, it associates different questions to increase the probability of retrieval.

Some link:

}
questions = self.llm_response_forget(prompt).content
return Document(
content=questions,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content is just the questions, which is fine since it helps match the query, but we lose the original content. I know you are keeping it in the metadata, but original_content is not used in the normal flow of this agent anywhere.

I think it's better to augment the content field value with the questions, and also demarcate those questions in some special way, so that during passage retrieval, the questions are ignored. I.e. only use the questions for matching/ranking, but ignore in retrieval.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other advantage of augmenting (i.e. combining chunk content + questions) is that now we can match with both the question and content, rather than having to deal with de-duplication, e.g. when we find a match for questions_5 that links to chunk_3 and also find a match to chunk_3.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

I've implemented the suggested improvements like this:

  1. Combined content+questions in a single field instead of separating them, using a consistent marker (GENERATED_QUESTIONS_MARKER) to demarcate them.

  2. Added clean_generated_content() method in the so that questions are used in ranking but not showed in retrieval

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @dfm88 ! Added to my queue of things to look at this week.

@pchalasani

Copy link
Copy Markdown
Contributor

Thanks for this @dfm88
Please also add suitable tests in test_doc_chat_agent.py

@pchalasani pchalasani changed the title feat(docagent): hypotetical questions feat(docagent): hypothetical questions Nov 19, 2024
@dfm88
dfm88 force-pushed the feat/docagent-hypotetical-questions branch 2 times, most recently from 73b2978 to 9c385f0 Compare November 20, 2024 23:29
@dfm88
dfm88 force-pushed the feat/docagent-hypotetical-questions branch 2 times, most recently from b1d7f78 to 7282d26 Compare November 20, 2024 23:42
@dfm88
dfm88 force-pushed the feat/docagent-hypotetical-questions branch from 7282d26 to 350175b Compare November 20, 2024 23:45

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I had to change the signature of
llm_response_forget and llm_response_forget_async (which might also have sense) because I had MyPy problem in passing the llm_response_forget_async method to run_batch_agent_method


return query, extracts

def clean_generated_content(self, passages: List[Document]) -> List[Document]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest rename - remove_hypothetical_questions to be more clear

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thoughy it might need for other cleaning stuff so I used a generic name, but ok I'll make it specific for this task

Comment thread tests/main/test_doc_chat_agent.py Outdated
@pytest.mark.parametrize(
"hypothetical_questions, expect_cleaned", [(True, True), (False, False)]
)
def test_clean_generated_content(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==> test_remove_generated_questions

Comment thread tests/main/test_doc_chat_agent.py Resolved

@pchalasani pchalasani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dfm88 I just reviewed it.

@dfm88

dfm88 commented Dec 15, 2024 •

Copy link
Copy Markdown
Contributor Author

Hi @pchalasani , sorry for the delay in making changes on this. Here an overview of the changes, since it was not a straightforward task to handle batch number:

I saw you added a function in batch.py file to handle batches, that actually was handling only sync code, I slightly changed it to also handle aysnc code and tried to reuse it in the run_batch_agent_method

The most challenging part was ensuring correct behavior when stop_on_first_result=True, so I maintained strict task ordering by preserving original indices.
Force batch_size=1 when stop_on_first_result=True to prevent later tasks from completing first.
Used an async lock to ensure thread-safe access when storing the first completed result. Reconstructed the result list to preserve the original task positions

For normal operation (when stop_on_first_result=False), the code fully leverages the batching capabilities.

I have also added an example of when leveraging Hypothetical Questions might be beneficial. Here another interesting article Advanced RAG Optimization: Aligning Question and Document Embedding Spaces with Hypothetical Questions

I know there are a lot of changes, but tests using batch module are passing. Let me know if you'd like me to clarify any part of these changes or have suggestions for improvement.

@pchalasani

Copy link
Copy Markdown
Contributor

Thank you @dfm88 will check in a few days.

Comment thread langroid/agent/batch.py Outdated

async def _do_task(input: str | ChatDocument, i: int) -> Any:
agent_cfg.name = f"{agent_cfg.name}-{i}"
first_result = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @dfm88.

Is there a reason the implementation of stop_on_first_result here is different from the one in run_batch_task_gen -- is this somehow better? This will help me better understand + review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pchalasani,

thank you for pointing this out. I actually didn't notice the run_batch_task_gen implementation when I was working on this PR. Looking at it now, I can see it already provides a good handling of batch async tasks, so I basically took inspiration from that to implement batch_size also in run_batch_agent_method in my last commit.

So I could revert also the changes I did to langroid/agent/batch.py::run_batch_function.

Finally I fixed the tests where I still was using the previous naming for the doc agent method relative to hypothetical questions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will review soon thanks !

Comment thread langroid/agent/batch.py Outdated
f"{agent_name}{complete_str}..."
)

if stop_on_first_result and any(results):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should discard None results here, just like we do in the run_batch_task_gen fn.

Also more generally, since this _do_all functions in the two places are so similar, I think we should avoid code duplication and have a common fn and use it in both places. What do you think @dfm88 ?

@dfm88
dfm88 force-pushed the feat/docagent-hypotetical-questions branch from fc2931f to 4150509 Compare January 18, 2025 16:19
@pchalasani

pchalasani commented Jan 19, 2025 •

Copy link
Copy Markdown
Contributor

@dfm88 this is going to be very useful, as you say in cases with very technical/domain-specific terms that are not captured well by generic embeddings. I changed your example script to the medical domain to illustrate the benefit more clearly and explicitly, see the docagent-hypoq branch:
https://github.com/langroid/langroid/blob/docagent-hypoq/examples/docqa/hypothetical_questions.py

I added explanatory notes at the top of the script.

EDIT - Please merge the changes from branch docagent-hypoq into your branch.
The docagent-hypoq branch also includes some fixes to doc_chat_agent.py and a new method Document.from_string in mytypes.py

@dfm88

dfm88 commented Jan 19, 2025

Copy link
Copy Markdown
Contributor Author

@dfm88 this is going to be very useful, as you say in cases with very technical/domain-specific terms that are not captured well by generic embeddings. I changed your example script to the medical domain to illustrate the benefit more clearly and explicitly, see the docagent-hypoq branch: https://github.com/langroid/langroid/blob/docagent-hypoq/examples/docqa/hypothetical_questions.py

I added explanatory notes at the top of the script.

EDIT - Please merge the changes from branch docagent-hypoq into your branch. The docagent-hypoq branch also includes some fixes to doc_chat_agent.py and a new method Document.from_string in mytypes.py

Great thanks! I did the merge

@pchalasani

Copy link
Copy Markdown
Contributor

@dfm88 I am making some changes on top of this.

@pchalasani pchalasani mentioned this pull request Jan 19, 2025
@pchalasani

Copy link
Copy Markdown
Contributor

@dfm88 I've made a new PR with my changes on top, #683
I made the design more flexible, to support any type of chunk enrichment, and also allow a different LLM to be used for the chunk enrichment. Essentially, we can can pass a config for a "chunk enrichment agent".

@dfm88

dfm88 commented Jan 19, 2025

Copy link
Copy Markdown
Contributor Author

@dfm88 I've made a new PR with my changes on top, #683 I made the design more flexible, to support any type of chunk enrichment, and also allow a different LLM to be used for the chunk enrichment. Essentially, we can can pass a config for a "chunk enrichment agent".

Yeah I see, it makes a lot of sense to me, thanks!

@pchalasani

Copy link
Copy Markdown
Contributor

Yeah I see, it makes a lot of sense to me, thanks!

Thank you for leading this PR !

@pchalasani

Copy link
Copy Markdown
Contributor

finished in #683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants