Search code examples
textnlplangchain

Langchain sql agent with context


I am working on a langchain based SQL chat application and wanted my agent to understand context w.r.t the user session. For e.g. User - What is highest order placed in last placed? Bot - Order id : XYZ User - When was this placed?

Here, bot should be able to deduce that 'this' refers to 'order id XYZ' from previous question. How can I incorporate this in my code?

I am tried using ChatHistory but getting context from session history is where I am stuck.


Solution

  • I had a similar issue and I solved by “Contextualizing” the questions. Here is an example (not for SQL) : https://python.langchain.com/docs/use_cases/question_answering/chat_history/

    Read the part on Contextualizing the question to reformulate your question based on the history.

    In the context of a RAG system that uses a SQL agent, contextualizing questions involves modifying or framing these questions based on the specific data structure and contents of the SQL database. Here, the questions are crafted to match the structure of the database to efficiently retrieve relevant information. This means incorporating the correct table names, field names, and specific terms used within the database into the query. By doing so, the RAG system leverages the SQL agent to execute precise database queries that fetch data relevant to the ongoing conversation or task, thus enhancing the response accuracy and relevance.

    Hope this helps!