Build an Earnings Call ReAct Agent - Custom Tool Use

Build an Earnings Call ReAct Agent - Custom Tool Use

by EarningsCall Editor

6/15/2024

Large Language Models (LLMs) like ChatGPT excel at answering general questions but often struggle to incorporate domain-specific knowledge or data beyond their training cutoff date. One effective solution to this limitation is “Retrieval Augmented Generation” (RAG). RAG enhances in-context learning by allowing users to include information retrieved from documents within a prompt. This enables the language model to make inferences based on previously unseen information. However, traditional RAG operates under the assumption that every query requires additional context, even when it may not be necessary. A more refined approach involves enabling the LLM to determine when additional context is needed.
 

Enter ReAct.

ReAct is a method that integrates both thinking and doing into a single process for LLMs. It aids the model in not only devising a plan by thinking through the problem but also taking specific actions to gather more information or interact with external systems. This dual capability enhances the model’s effectiveness in solving tasks, such as answering questions or making decisions, by improving its ability to plan, adjust, and understand its actions. Additionally, a ReAct agent can discern when it is appropriate to request additional context and when it is not.

Researchers at Google pioneered this method in 2022, yielding notable results. Here’s an example of how using ReAct produces accurate answers in two different queries.

 

 

from earningscall import get_company
from langchain.pydantic_v1 import BaseModel
from langchain_core.callbacks import (
    CallbackManagerForToolRun,
)
from langchain_core.tools import BaseTool
from pydantic.v1 import Field

class CompanyMetaDataToolInput(BaseModel):
    ticker_symbol: str = Field(description="A ticker symbol is a unique series of letters assigned to a security or "
                                           "stock for trading purposes on a particular stock exchange. It acts as an"
                                           " abbreviation or code that represents the publicly traded company. For e"
                                           "xample, Apple’s ticker symbol is AAPL, and Microsoft’s is MSFT. Ticker s"
                                           "ymbols are used to identify the stock in trading and other financial tra"
                                           "nsactions. They can consist of letters, numbers, or a combination of bo"
                                           "th, depending on the exchange and the type of security.")


class CompanyMetaDataTool(BaseTool):
    name = "CompanyMetaDataTool"
    description = ("The Company Metadata Retriever is a tool designed to fetch comprehensive company "
                   "metadata using a stock ticker symbol. This tool is essential for investors, analysts, and "
                   "businesses seeking detailed information about a specific company in a streamlined and "
                   "efficient manner.")
    args_schema: Type[BaseModel] = CompanyMetaDataToolInput
    return_direct: bool = True

    def _run(
        self, ticker_symbol: str, run_manager: Optional[CallbackManagerForToolRun] = None
    ) -> str:
        """Use the tool."""
        company = get_company(ticker_symbol)
        return company.company_info.to_json()