deepset-ai / haystack

:mag: AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.
https://haystack.deepset.ai
Apache License 2.0
17.35k stars 1.89k forks source link

Changing the returned context #950

Closed angelopf closed 3 years ago

angelopf commented 3 years ago

Question Is it possible to change the returned context to not only include the surrounding characters of the answer but also specific information over each document?

Additional context I am using Haystack on a Portuguese-case project that is intended to help users understand Portuguese law by asking the model questions about it. However, not only do I want an answer to the question (which the model seems to find), I also need to retrieve additional details (like the article link from where the information was retrieved). I am willing, of course, to change the way the context function works for me, but I cannot find where it is defined. The only parameter I found was context_window_size.

Thanks a lot!

RicardoLuisAraujo commented 3 years ago

Yup, I have the same problem. Is it possible to change the way the answers or context are structured?

For example, right now the model & context is like this:

Context:

" https://www.amazon.es/

Amazon.com, Inc.[8] (/ˈæməzɒn/ AM-ə-zon) is an American multinational technology company based in Seattle, Washington, which focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. It is one of the Big Five companies in the U.S. information technology industry, along with Google, Apple, Microsoft, and Facebook.[9][10][11][12] The company has been referred to as "one of the most influential economic and cultural forces in the world", as well as the world's most valuable brand.[13][14]... You can buy toothbrushes at Amazon "

Q: Where can I buy toothbrushes? A: Amazon. Context: You can buy toothbrushes at Amazon.

But I want the answer (or context) to return something like :

A: You can buy toothbrushes at Amazon. See more at: https://www.amazon.es/

oryx1729 commented 3 years ago

Hi @angelopf and @RicardoLuisAraujo,

If the "context" for the answers is directly associated with the indexed documents, then you could consider adding meta fields to the documents.

meta fields allow adding custom key-value pairs to documents indexed in Haystack. For instance, the link of an article, author, or the purchase link for a product can be added as metadata.

Would that work for your use cases?

angelopf commented 3 years ago

Thanks a lot, that would most likely work! Is there any predefined way of adding meta fields to the documents through Haystack?

oryx1729 commented 3 years ago

Thanks a lot, that would most likely work! Is there any predefined way of adding meta fields to the documents through Haystack?

You can add meta fields in document dicts before writing them to a document store. For instace,

document = {"text": "The quick brown fox ....", "meta": {"author": "Tom", "source": "wiki"}
document_store.write_documents([document])
tholor commented 3 years ago

Seems resolved. Feel free to re-open if not :)