Enhancing Transaction Processing with Langchain and ReAct Techniques
Written on
Generative AI Series
Prompt Engineering: Chain of Thought and ReAct — SQL Agent
This article explores the Chain of Thought and ReAct Prompt Engineering methods, which guide language models (LLMs) to think logically and perform actions. We will also create a Langchain agent for executing traditional transactions via SQL.
Prompt Engineering
Prompt engineering is the art of designing specific inputs for language models to generate the desired outputs. This intricate process necessitates a comprehensive understanding of the model’s strengths and weaknesses. By refining prompts, engineers can direct models to yield more precise, relevant, and innovative responses. Effective prompt engineering transcends simple keyword adjustments; it requires providing context, establishing the tone, and defining the expected result.
While zero-shot prompting works well for straightforward tasks, few-shot prompting enables the model to utilize its pre-existing knowledge for improved accuracy. Nonetheless, few-shot prompting can struggle with tasks requiring arithmetic, common sense, and symbolic reasoning.
To overcome the shortcomings of few-shot prompting, we employ techniques such as "Chain of Thought" prompting. This method leads the language model through a logical reasoning sequence by offering several examples that illustrate the logic. This significantly enhances the model’s performance on intricate tasks by encouraging it to articulate its reasoning along with its conclusions.
Chain of Thought
The Chain of Thought technique is a highly effective prompt engineering strategy that elevates the process by utilizing the natural flow of ideas. Instead of a static prompt, this method involves creating a series of interconnected prompts that build on one another. The goal is to guide the model through a series of related concepts, prompting it to explore and elaborate on ideas in a coherent manner.
This technique aids in enhancing reasoning capabilities as it guides the model step by step, similar to human thought processes. However, despite its effectiveness, naive greedy decoding can lead to issues where the model generates responses sentence by sentence without maintaining overall coherence.
Self-consistency prompting addresses this issue by querying the language model multiple times with the same prompt and selecting the most coherent response. This technique is effective for improving arithmetic and common sense reasoning. It generates multiple reasoning pathways for a problem, aggregating them to find the most consistent answer.
LangChain simplifies the management of various prompt modifiers, making the process completely unsupervised. Self-consistency enhances response accuracy without needing additional human intervention or models.
For further reading on Chain of Thought prompting, check out more information here.
Reasoning and Action Prompting Technique
ReAct, short for "synergizing reasoning and acting," builds on Chain of Thought prompting techniques and serves as a powerful tool for assisting Foundation models in completing user-requested tasks.
ReAct is a reasoning framework designed to structure problems and guide Foundation models through a sequence of steps to reach a solution. This process involves three essential components: Thought, Action, and Observation.
- Thought: This reasoning step guides the Foundation model, illustrating how to approach a problem. It involves formulating a series of questions that lead the model toward the desired solution.
- Action: After establishing the thought process, the next step is to define an action for the Foundation model to perform. This action typically entails calling an API from a predefined list, enabling the model to interact with external resources.
- Observation: After executing the action, the model observes and evaluates the results. These observations become vital inputs for further reasoning and decision-making.
To demonstrate ReAct in practice, consider the following example of its implementation with chatGPT.
> This may not be the perfect prompts :-D Please feel free to correct me if I am not using the right prompt.
> But I think, you get the idea of how the ReAct technique: > > 1. Maintains overall coherence to avoid illogical responses. > 2. Promotes diverse reasoning paths, ensuring thorough exploration of alternative perspectives. > 3. Utilizes self-consistency prompting, querying the model multiple times for a consistent response.
For more insights on ReAct, you can read further here.
Langchain enables the integration of various tools to create agents. These tools can fetch additional information that may enhance the context.
Implementing a Langchain Agent to See ReAct in Action
Let’s implement this using Langchain and create an SQL agent to demonstrate how reasoning is applied to retrieve the correct response for a query. This will showcase another approach to Text2SQL. (You can read my blog on how we implemented the RAG-based approach).
Let's begin by configuring our local Postgres database. I have installed Postgres on my MacBook and set up two tables:
- product_master: This table stores the product_id and the name of the product.
- inventory: This table uses the product_id to maintain the inventory count for each product.
The following screenshots illustrate the schema and the data I inserted.
Now, let’s create a requirements.txt file that includes all the necessary libraries for this project.
langchain openai streamlit python-dotenv psycopg2 langchain_openai
Among the essential libraries listed, sqlalchemy serves as the Python SQL toolkit, and psycopg2 is the adapter used to connect to Postgres.
Next, we'll establish the environment variables in the .env file. In this file, we specify the OPENAI_API_KEY and database variables, as demonstrated below:
OPENAI_API_KEY=<<YourOPENAI key>> DB_USER=orderadmin DB_PASSWORD=orderadmin DB_HOST=localhost DB_NAME=postgres
Now, let’s review the application code.
In the code above, we initiate a connection to the database and configure the necessary database parameters. We then pass this information to the SQL agent alongside the Language Model (LLM). The agent utilizes these details to perform reasoning and interact with the database as needed. A demonstration of this functionality is available in the video below.
In this code snippet, we define a method to invoke the agent using a specific prompt. Following this, we are creating a Streamlit chatbot application that employs the callAgent() method to process all user-submitted prompts.
In the above code, we establish a Streamlit application with a chat-like interface. Messages are stored in st.session_state and displayed in the main window as a chat. We capture the prompt entered in st.chat_input() and call the chat_engine() we created. LlamaIndex provides a convenient function to set up a chat engine with the created index, managing all complexities of RAG and invoking the appropriate LLM to generate responses.
The output is demonstrated in the following video.
As shown in the video and screenshots, the agent can observe results, apply reasoning, and act accordingly. This powerful technique allows agents to function as intelligent bots to achieve their tasks. I plan to delve deeper into Agents and Agent orchestration in future posts.
Conclusion
ReAct is a valuable technique for guiding Foundation models in domain-specific tasks. By leading the model through structured problem-solving, ReAct equips it to reason and act effectively, becoming even more extensible through integration with external resources.
I hope you found this blog helpful; please share your feedback and comments. Take care, and I look forward to presenting more insights in future posts.
You can find the code on my GitHub here.