[Python] how to get the target generated query on a self-query retriever(langchain)

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm implementing a self-query retriever using langchain with OpenSearch as the target vectore store, so far everything is good but we need to capture the generated query in DSL, for debugging and auditing purposes, after some testing I cannot find how to do it, I found how to return thye StructuredQuery, and how to use the StructuredQuery and OpenSearchTranslator to get a step closer to the final query, however it is not the final query sent to OpenSearch. Question is, how to get the query? This is my current code(that returns something close to it but not the final version):

    opensearch_translator = OpenSearchTranslator()
    def show_translated_query(query):
    chain_structured_query = retriever.llm_chain.invoke(query)
    print("langchain structured query:")
    print(chain_structured_query)
    os_structured_query = opensearch_translator.visit_structured_query(chain_structured_query)
    print("OS query(semantic, filter):")
    print(os_structured_query)

    show_translated_query("a fire ocurring before 2023")
    >>langchain structured query:
    >>query='fire' filter=Comparison(comparator=<Comparator.LT: 'lt'>, attribute='year', value=2023) limit=None
    >>OS query(semantic, filter):
    >>('fire', {'filter': {'range': {'metadata.year': {'lt': 2023}}}})

    Continue reading...

Compartilhe esta Página