AI

How to Implement the ReAct Logic for Working With Document Store?

LangChain is the framework that contains all the dependencies and libraries for building language models and chatbots. These chatbots need to be trained on massive data in order to understand the complexities of the language efficiently. The developers can use the ReAct logic with these models that can learn and understand the language accurately. The ReAct logic is the combination of Reasoning (training) and Acting (Testing) phases to get the optimized results from the model.

Quick Outline

This post will demonstrate:

How to Implement the ReAct Logic With Document Store in LangChain

Conclusion

How to Implement the ReAct Logic With Document Store in LangChain?

The language models are trained on a huge pool of data written in natural languages like English, etc. The data is managed and stored in the document stores and the user can simply load the data from the store and train the model. The model training can take multiple iterations as each iteration makes the model more effective and improved.

To learn the process of implementing ReAct logic for working with the document store in LangChain, simply follow this simple guide:

Step 1: Installing Frameworks

First, get started with the process of implementing the ReAct logic for working with the document store by installing the LangChain framework. Installing the LangChain framework will get all the required dependencies to get or import the libraries for completing the process:

pip install langchain

Install the Wikipedia dependencies for this guide as it can be used to get the document stores to work with the ReAct logic:

pip install wikipedia

Install the OpenAI modules using the pip command to get its libraries and build Large Language Models or LLMs:

pip install openai

Step 2: Providing OpenAI API Key

After installing all the required modules, simply set up the environment using the API key from the OpenAI account using the following code:

import os

import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

Step 3: Importing Libraries

Once the environment is set up, import the libraries from the LangChain that are required to configure the ReAct logic for working with the document stores. Using LangChain agents to get the DocstoreExplaorer and agents with its types to configure the language model:

from langchain.llms import OpenAI

from langchain.docstore import Wikipedia

from langchain.agents import initialize_agent, Tool

from langchain.agents import AgentType

from langchain.agents.react.base import DocstoreExplorer

Step 4: Using Wikipedia Explorer

Configure the “docstore” variable with the DocstoreExplorer() method and call Wikipedia() method in its argument. Build the Large Language Model using the OpenAI method with the “text-davinci-002” model after setting the tools for the agent:

docstore = DocstoreExplorer(Wikipedia())
tools = [
    Tool(
        name="Search",
        func=docstore.search,
        description="It is used for asking queries/prompts with the search",
    ),
    Tool(
        name="Lookup",
        func=docstore.lookup,
        description="It is used for asking queries/prompts with lookup",
    ),
]

llm = OpenAI(temperature=0, model_name="text-davinci-002")
#defining the variable by configuring the model with the agent
react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)

Step 5: Testing the Model

Once the model is built and configured, set the question string and run the method with the question variable in its argument:

question = "Which U.S. Navy admiral collaborated with Author David Chanoff"

react.run(question)

Once the question variable is executed, the model has understood the question without any external prompt template or training. The model is being trained automatically using the model uploaded in the previous step and generating text accordingly. The ReAct logic is working with the document stores to extract information based on the question:

Ask another question from the data provided to the model from the document stores and the model will extract the answer from the store:

question = "Author David Chanoff has collaborated with William J Crowe who served under which President?"

react.run(question)

That’s all about implementing the ReAct logic for working with the document store in LangChain.

Conclusion

To implement the ReAct logic for work with the document store in LangChain, install the modules or frameworks for building the language model. After that, set up the environment for OpenAI to configure the LLM and load the model from the document store to implement the ReAct logic. This guide has elaborated on implementing the ReAct logic for working with the document store.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.