Financial CSV Agent with Native Multi-Step Cohere API

Financial CSV Agent with Native Multi-Step Cohere API

Jason Jung

Jason Jung

Notebook Overview

Motivation

Tabular data reasoning continues to be an exciting application of Agents, particularly in the financial domain, where multiple reasoning steps may be needed to identify the right answer. For example, a financial analyst working with financial statements may be interested in computing various financial ratios with natural language queries.

Some examples may include:

  • ROE (Return on Equity) = Net Income / Shareholder's Equity
  • Net Profit Margin = Net Income / Revenue
  • Asset Turnover = Revenue / Average Total Assets
  • Financial Leverage = Average Total Assets / Shareholder's Equity

Having an Agent which is able to correctly compute these and other ratios would be a great help for any analyst in the field of Finance.

Objective

In this notebook we explore how to setup a Cohere Agent to answer questions over tables in Apple's SEC10K 2020 form. Financial CSV Agent already showed how to use Langchain to ask questions about your data. This notebook will demonstrate how you can build the same agent using Cohere's native API with Langchain Python tool. We will also explore how to make your agent more resilient to errors.

Table of Contents

Setup

import os
from typing import List

import cohere
import langchain
import langchain_core
import langchain_experimental
import pandas as pd
from langchain.agents import Tool
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_experimental.utilities import PythonREPL
# Uncomment if you need to install the following packages
# !pip install --quiet langchain langchain_experimental cohere --upgrade
# versions
print('cohere version:', cohere.__version__)
print('langchain version:', langchain.__version__)
print('langchain_core version:', langchain_core.__version__)
print('langchain_experimental version:', langchain_experimental.__version__)
cohere version: 5.5.1
langchain version: 0.2.0
langchain_core version: 0.2.0
langchain_experimental version: 0.0.59

API Key

COHERE_API_KEY = os.environ["COHERE_API_KEY"]
CHAT_URL= "https://api.cohere.ai/v1/chat"
COHERE_MODEL = 'command-r-plus'
co = cohere.Client(api_key=COHERE_API_KEY)

Data Loading

income_statement = pd.read_csv('income_statement.csv')
balance_sheet = pd.read_csv('balance_sheet.csv')
income_statement.head(2)
Unnamed: 0 index RevenueFromContractWithCustomerExcludingAssessedTax CostOfGoodsAndServicesSold GrossProfit ResearchAndDevelopmentExpense SellingGeneralAndAdministrativeExpense OperatingExpenses OperatingIncomeLoss NonoperatingIncomeExpense IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest IncomeTaxExpenseBenefit NetIncomeLoss EarningsPerShareBasic EarningsPerShareDiluted WeightedAverageNumberOfSharesOutstandingBasic WeightedAverageNumberOfDilutedSharesOutstanding
0 0 2017-10-01-2018-09-29 265595000000 1.637560e+11 101839000000 1.423600e+10 1.670500e+10 3.094100e+10 7.089800e+10 2.005000e+09 7.290300e+10 1.337200e+10 59531000000 3.00 2.98 1.982151e+10 2.000044e+10
1 1 2018-09-30-2018-12-29 84310000000 NaN 32031000000 NaN NaN NaN NaN NaN NaN NaN 19965000000 1.05 1.05 NaN NaN
balance_sheet.head(2)
Unnamed: 0 index CashAndCashEquivalentsAtCarryingValue MarketableSecuritiesCurrent AccountsReceivableNetCurrent InventoryNet NontradeReceivablesCurrent OtherAssetsCurrent AssetsCurrent MarketableSecuritiesNoncurrent ... LongTermDebtNoncurrent OtherLiabilitiesNoncurrent LiabilitiesNoncurrent Liabilities CommitmentsAndContingencies CommonStocksIncludingAdditionalPaidInCapital RetainedEarningsAccumulatedDeficit AccumulatedOtherComprehensiveIncomeLossNetOfTax StockholdersEquity LiabilitiesAndStockholdersEquity
0 0 2017-09-30 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 134047000000 NaN
1 1 2018-09-29 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN 107147000000 NaN

2 rows × 30 columns

Define Python Tool

Here we define the python tool using langchain's PythonREPL. We also define functions_map that will later be used by the Cohere Agent to correctly map function name to the actual function. Lastly, we define the tools that will be passed in the Cohere API.

python_repl = PythonREPL()
python_tool = Tool(
    name="python_repl",
    description="Executes python code and returns the result. The code runs in a static sandbox without interactive mode, so print output or save output to a file.",
    func=python_repl.run,
)
python_tool.name = "python_interpreter"

class ToolInput(BaseModel):
    code: str = Field(description="Python code to execute.")
python_tool.args_schema = ToolInput

def run_python_code(code: str) -> dict:
    """
    Function to run given python code
    """
    input_code = ToolInput(code=code)
    return {'python_answer': python_tool.func(input_code.code)}

functions_map = {
    "run_python_code": run_python_code,
}

tools = [
    {
        "name": "run_python_code",
        "description": "given a python code, runs it",
        "parameter_definitions": {
            "code": {
                "description": "executable python code",
                "type": "str",
                "required": True
            }
        }
    },]

Create Cohere Agent

As Multi-Step Tool Use shows, you have a lot of flexiblity on how you can customize and interact with the cohere agent. Here I am creating a wrapper so that it automatically determines when to stop calling the tools and output final answer. It will run maximum of 15 steps.

def cohere_agent(
    message: str,
    preamble: str,
    tools: List[dict],
    force_single_step=False,
    verbose: bool = False,
) -> str:
    """
    Function to handle multi-step tool use api.

    Args:
        message (str): The message to send to the Cohere AI model.
        preamble (str): The preamble or context for the conversation.
        tools (list of dict): List of tools to use in the conversation.
        verbose (bool, optional): Whether to print verbose output. Defaults to False.

    Returns:
        str: The final response from the call.
    """

    counter = 1

    response = co.chat(
        model=COHERE_MODEL,
        message=message,
        preamble=preamble,
        tools=tools,
        force_single_step=force_single_step,
    )

    if verbose:
        print(f"\nrunning 0th step.")
        print(response.text)

    while response.tool_calls:
        tool_results = []

        if verbose:
            print(f"\nrunning {counter}th step.")

        for tool_call in response.tool_calls:
            output = functions_map[tool_call.name](**tool_call.parameters)
            outputs = [output]
            tool_results.append({"call": tool_call, "outputs": outputs})

            if verbose:
                print(
                    f"= running tool {tool_call.name}, with parameters: {tool_call.parameters}"
                )
                print(f"== tool results: {outputs}")

        response = co.chat(
            model=COHERE_MODEL,
            message="",
            chat_history=response.chat_history,
            preamble=preamble,
            tools=tools,
            force_single_step=force_single_step,
            tool_results=tool_results,
        )

        if verbose:
            print(response.text)

            counter += 1

    return response.text


# test
output = cohere_agent("can you use python to answer 1 + 1", None, tools, verbose=True)
running 0th step.
I will use Python to answer this question.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'print(1 + 1)'}
== tool results: [{'python_answer': '2\n'}]
The answer is **2**.

QnA over Single Table

In the example below, we show how the Python tool can be used to load a dataframe and extract information from it. To do this successfully we need to:

  • Pass the file name to the preamble so the model knows how to load the dataframe
  • Pass a preview of the dataframe in the preamble so the model knows which columns/rows to query

We will ask the following questions given income statement data.

  • What is the highest value of cost of goods and service?
  • What is the largest gross profit margin?
  • What is the minimum ratio of operating income loss divided by non operating income expense?
question_dict ={
    'q1': ['what is the highest value of cost of goods and service?',169559000000],
    'q2': ['what is the largest gross profit margin?',0.3836194330595236],
    'q3': ['what is the minimum ratio of operating income loss divided by non operating income expense?',35.360599]
}
preamble = """
You are an expert who answers the user's question. You are working with a pandas dataframe in Python. The name of the dataframe is `income_statement.csv`.
Here is a preview of the dataframe:
{head_df}
""".format(head_df=income_statement.head(3).to_markdown())

print(preamble)
You are an expert who answers the user's question. You are working with a pandas dataframe in Python. The name of the dataframe is `income_statement.csv`.
Here is a preview of the dataframe:
|    |   Unnamed: 0 | index                 |   RevenueFromContractWithCustomerExcludingAssessedTax |   CostOfGoodsAndServicesSold |   GrossProfit |   ResearchAndDevelopmentExpense |   SellingGeneralAndAdministrativeExpense |   OperatingExpenses |   OperatingIncomeLoss |   NonoperatingIncomeExpense |   IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest |   IncomeTaxExpenseBenefit |   NetIncomeLoss |   EarningsPerShareBasic |   EarningsPerShareDiluted |   WeightedAverageNumberOfSharesOutstandingBasic |   WeightedAverageNumberOfDilutedSharesOutstanding |
|---:|-------------:|:----------------------|------------------------------------------------------:|-----------------------------:|--------------:|--------------------------------:|-----------------------------------------:|--------------------:|----------------------:|----------------------------:|----------------------------------------------------------------------------------------------:|--------------------------:|----------------:|------------------------:|--------------------------:|------------------------------------------------:|--------------------------------------------------:|
|  0 |            0 | 2017-10-01-2018-09-29 |                                          265595000000 |                  1.63756e+11 |  101839000000 |                      1.4236e+10 |                               1.6705e+10 |          3.0941e+10 |            7.0898e+10 |                   2.005e+09 |                                                                                    7.2903e+10 |                1.3372e+10 |     59531000000 |                    3    |                      2.98 |                                     1.98215e+10 |                                       2.00004e+10 |
|  1 |            1 | 2018-09-30-2018-12-29 |                                           84310000000 |                nan           |   32031000000 |                    nan          |                             nan          |        nan          |          nan          |                 nan         |                                                                                  nan          |              nan          |     19965000000 |                    1.05 |                      1.05 |                                   nan           |                                     nan           |
|  2 |            2 | 2018-09-30-2019-09-28 |                                          260174000000 |                  1.61782e+11 |   98392000000 |                      1.6217e+10 |                               1.8245e+10 |          3.4462e+10 |            6.393e+10  |                   1.807e+09 |                                                                                    6.5737e+10 |                1.0481e+10 |     55256000000 |                    2.99 |                      2.97 |                                     1.84713e+10 |                                       1.85957e+10 |
for qsn,val in question_dict.items():
    print(f'question:{qsn}')
    question = val[0]
    answer = val[1]
    output = cohere_agent(question, preamble, tools, verbose=True)
    print(f'GT Answer:{val[1]}')
    print('-'*50)
question:q1

running 0th step.
I will use Python to find the highest value of 'CostOfGoodsAndServicesSold' in the 'income_statement.csv' dataframe.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv(\'income_statement.csv\')\n\n# Find the highest value of \'CostOfGoodsAndServicesSold\'\nhighest_cost = df[\'CostOfGoodsAndServicesSold\'].max()\n\nprint(f"The highest value of \'CostOfGoodsAndServicesSold\' is {highest_cost}")'}
== tool results: [{'python_answer': "The highest value of 'CostOfGoodsAndServicesSold' is 169559000000.0\n"}]
The highest value of 'CostOfGoodsAndServicesSold' is 169559000000.0.
GT Answer:169559000000
--------------------------------------------------
question:q2

running 0th step.
I will write and execute Python code to find the largest gross profit margin.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv(\'income_statement.csv\')\n\n# Calculate gross profit margin\ndf[\'GrossProfitMargin\'] = df[\'GrossProfit\'] / df[\'RevenueFromContractWithCustomerExcludingAssessedTax\'] * 100\n\n# Find the largest gross profit margin\nlargest_gross_profit_margin = df[\'GrossProfitMargin\'].max()\n\nprint(f"The largest gross profit margin is {largest_gross_profit_margin:.2f}%")'}
== tool results: [{'python_answer': 'The largest gross profit margin is 38.36%\n'}]
The largest gross profit margin is 38.36%.
GT Answer:0.3836194330595236
--------------------------------------------------
question:q3

running 0th step.
I will use Python to find the minimum ratio of operating income loss divided by non-operating income expense.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv("income_statement.csv")\n\n# Calculate the ratio of operating income loss to non-operating income expense\ndf["OperatingIncomeLossRatio"] = df["OperatingIncomeLoss"] / df["NonoperatingIncomeExpense"]\n\n# Find the minimum ratio\nmin_ratio = df["OperatingIncomeLossRatio"].min()\n\nprint(f"The minimum ratio of operating income loss to non-operating income expense is: {min_ratio:.2f}")'}
== tool results: [{'python_answer': 'The minimum ratio of operating income loss to non-operating income expense is: 35.36\n'}]
The minimum ratio of operating income loss to non-operating income expense is 35.36.
GT Answer:35.360599
--------------------------------------------------

QnA over Multiple Tables

We now make the task for the Agent more complicated by asking a question that can be only answered by retrieving relevant information from multiple tables:

  • Q: What is the ratio of the largest stockholders equity to the smallest revenue?

As you will see below, this question can be obtained only by accessing both the balance sheet and the income statement.

question_dict ={
    'q1': ['what is the ratio of the largest stockholders equity to the smallest revenue'],
}
# get the largest stockholders equity
x = balance_sheet['StockholdersEquity'].astype(float).max()
print(f"The largest stockholders equity value is: {x}")

# get the smallest revenue
y = income_statement['RevenueFromContractWithCustomerExcludingAssessedTax'].astype(float).min()
print(f"The smallest revenue value is: {y}")

# compute the ratio
ratio = x/y
print(f"Their ratio is: {ratio}")
The largest stockholders equity value is: 134047000000.0
The smallest revenue value is: 53809000000.0
Their ratio is: 2.4911631883142227
preamble = """
You are an expert who answers the user's question in complete sentences. You are working with two pandas dataframe in Python. Ensure your output is a string.

Here is a preview of the `income_statement.csv` dataframe:
{table_1}

Here is a preview of the `balance_sheet.csv` dataframe:
{table_2}
""".format(table_1=income_statement.head(3).to_markdown(),table_2=balance_sheet.head(3).to_markdown())


print(preamble)
You are an expert who answers the user's question in complete sentences. You are working with two pandas dataframe in Python. Ensure your output is a string.

Here is a preview of the `income_statement.csv` dataframe:
|    |   Unnamed: 0 | index                 |   RevenueFromContractWithCustomerExcludingAssessedTax |   CostOfGoodsAndServicesSold |   GrossProfit |   ResearchAndDevelopmentExpense |   SellingGeneralAndAdministrativeExpense |   OperatingExpenses |   OperatingIncomeLoss |   NonoperatingIncomeExpense |   IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest |   IncomeTaxExpenseBenefit |   NetIncomeLoss |   EarningsPerShareBasic |   EarningsPerShareDiluted |   WeightedAverageNumberOfSharesOutstandingBasic |   WeightedAverageNumberOfDilutedSharesOutstanding |
|---:|-------------:|:----------------------|------------------------------------------------------:|-----------------------------:|--------------:|--------------------------------:|-----------------------------------------:|--------------------:|----------------------:|----------------------------:|----------------------------------------------------------------------------------------------:|--------------------------:|----------------:|------------------------:|--------------------------:|------------------------------------------------:|--------------------------------------------------:|
|  0 |            0 | 2017-10-01-2018-09-29 |                                          265595000000 |                  1.63756e+11 |  101839000000 |                      1.4236e+10 |                               1.6705e+10 |          3.0941e+10 |            7.0898e+10 |                   2.005e+09 |                                                                                    7.2903e+10 |                1.3372e+10 |     59531000000 |                    3    |                      2.98 |                                     1.98215e+10 |                                       2.00004e+10 |
|  1 |            1 | 2018-09-30-2018-12-29 |                                           84310000000 |                nan           |   32031000000 |                    nan          |                             nan          |        nan          |          nan          |                 nan         |                                                                                  nan          |              nan          |     19965000000 |                    1.05 |                      1.05 |                                   nan           |                                     nan           |
|  2 |            2 | 2018-09-30-2019-09-28 |                                          260174000000 |                  1.61782e+11 |   98392000000 |                      1.6217e+10 |                               1.8245e+10 |          3.4462e+10 |            6.393e+10  |                   1.807e+09 |                                                                                    6.5737e+10 |                1.0481e+10 |     55256000000 |                    2.99 |                      2.97 |                                     1.84713e+10 |                                       1.85957e+10 |

Here is a preview of the `balance_sheet.csv` dataframe:
|    |   Unnamed: 0 | index      |   CashAndCashEquivalentsAtCarryingValue |   MarketableSecuritiesCurrent |   AccountsReceivableNetCurrent |   InventoryNet |   NontradeReceivablesCurrent |   OtherAssetsCurrent |   AssetsCurrent |   MarketableSecuritiesNoncurrent |   PropertyPlantAndEquipmentNet |   OtherAssetsNoncurrent |   AssetsNoncurrent |        Assets |   AccountsPayableCurrent |   OtherLiabilitiesCurrent |   ContractWithCustomerLiabilityCurrent |   CommercialPaper |   LongTermDebtCurrent |   LiabilitiesCurrent |   LongTermDebtNoncurrent |   OtherLiabilitiesNoncurrent |   LiabilitiesNoncurrent |   Liabilities |   CommitmentsAndContingencies |   CommonStocksIncludingAdditionalPaidInCapital |   RetainedEarningsAccumulatedDeficit |   AccumulatedOtherComprehensiveIncomeLossNetOfTax |   StockholdersEquity |   LiabilitiesAndStockholdersEquity |
|---:|-------------:|:-----------|----------------------------------------:|------------------------------:|-------------------------------:|---------------:|-----------------------------:|---------------------:|----------------:|---------------------------------:|-------------------------------:|------------------------:|-------------------:|--------------:|-------------------------:|--------------------------:|---------------------------------------:|------------------:|----------------------:|---------------------:|-------------------------:|-----------------------------:|------------------------:|--------------:|------------------------------:|-----------------------------------------------:|-------------------------------------:|--------------------------------------------------:|---------------------:|-----------------------------------:|
|  0 |            0 | 2017-09-30 |                            nan          |                  nan          |                   nan          |    nan         |                 nan          |         nan          |   nan           |                    nan           |                   nan          |            nan          |      nan           | nan           |             nan          |               nan         |                            nan         |        nan        |           nan         |        nan           |             nan          |                 nan          |            nan          | nan           |                           nan |                                   nan          |                         nan          |                                        nan        |         134047000000 |                      nan           |
|  1 |            1 | 2018-09-29 |                            nan          |                  nan          |                   nan          |    nan         |                 nan          |         nan          |   nan           |                    nan           |                   nan          |            nan          |      nan           | nan           |             nan          |               nan         |                            nan         |        nan        |           nan         |        nan           |             nan          |                 nan          |            nan          | nan           |                           nan |                                   nan          |                         nan          |                                        nan        |         107147000000 |                      nan           |
|  2 |            2 | 2019-09-28 |                              4.8844e+10 |                    5.1713e+10 |                     2.2926e+10 |      4.106e+09 |                   2.2878e+10 |           1.2352e+10 |     1.62819e+11 |                      1.05341e+11 |                     3.7378e+10 |              3.2978e+10 |        1.75697e+11 |   3.38516e+11 |               4.6236e+10 |                 3.772e+10 |                              5.522e+09 |          5.98e+09 |             1.026e+10 |          1.05718e+11 |               9.1807e+10 |                   5.0503e+10 |              1.4231e+11 |   2.48028e+11 |                             0 |                                     4.5174e+10 |                           4.5898e+10 |                                         -5.84e+08 |          90488000000 |                        3.38516e+11 |
for qsn,val in question_dict.items():
    print(f'question:{qsn}')
    question = val[0]
    output = cohere_agent(question, preamble, tools, verbose=True)
question:q1

running 0th step.
I will use the provided code to find the ratio of the largest stockholders equity to the smallest revenue.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\n# Read the CSV files into dataframes\nincome_statement = pd.read_csv(\'income_statement.csv\')\nbalance_sheet = pd.read_csv(\'balance_sheet.csv\')\n\n# Find the smallest revenue\nsmallest_revenue = income_statement[\'RevenueFromContractWithCustomerExcludingAssessedTax\'].min()\n\n# Find the largest stockholders equity\nlargest_stockholders_equity = balance_sheet[\'StockholdersEquity\'].max()\n\n# Calculate the ratio\nratio = largest_stockholders_equity / smallest_revenue\nprint(f"The ratio of the largest stockholders equity to the smallest revenue is {ratio:.2f}")'}
== tool results: [{'python_answer': 'The ratio of the largest stockholders equity to the smallest revenue is 2.49\n'}]
The ratio of the largest stockholders equity to the smallest revenue is 2.49.

Error Resilience

In the previous example over single table, the model successfully answered your questions. However, the model may not always have access to the preview of the data. You will see that when we remove the preview from the preamble, the model runs into an error and is not produce the answer. We will solve this problem with two different ways:

  • Asking the model to keep trying until it fixes the issue.
  • Giving the model another tool to view the data and telling it to preview the data before writing code.

You will see that the second method is able to come to the answer with fewer steps.

preamble = """
You are an expert who answers the user's question. You are working with a pandas dataframe in Python. The name of the dataframe is `income_statement.csv`.
"""

question1 = "what is the highest value of cost of goods and service?"

output = cohere_agent(question1, preamble, tools, verbose=True)
running 0th step.
I will use Python to find the highest value of 'Cost of Goods and Service' in the 'income_statement.csv' file.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv(\'income_statement.csv\')\n\n# Find the highest value of \'Cost of Goods and Service\'\nhighest_cost = df[\'Cost of Goods and Service\'].max()\n\nprint(f"The highest value of \'Cost of Goods and Service\' is: {highest_cost}")'}
== tool results: [{'python_answer': "KeyError('Cost of Goods and Service')"}]
Sorry, there is no column named 'Cost of Goods and Service' in the 'income_statement.csv' file.

As you see above, the model failed to execute because it assumed certain column names but they turned out to be wrong. One simple fix is to tell the model to continue to solve the problem in the face of error.

preamble = """
You are an expert who answers the user's question. You are working with a pandas dataframe in Python. The name of the dataframe is `income_statement.csv`.
If you run into error, keep trying until you fix it. You may need to view the data to understand the error.
"""

question1 = "what is the highest value of cost of goods and service?"

output = cohere_agent(question1, preamble, tools, verbose=True)
running 0th step.
I will use Python to find the highest value of cost of goods and service.

running 1th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv(\'income_statement.csv\')\n\n# Find the highest value of \'Cost of Goods and Services\'\nhighest_cost = df[\'Cost of Goods and Services\'].max()\n\nprint(f"The highest value of \'Cost of Goods and Services\' is: {highest_cost}")'}
== tool results: [{'python_answer': "KeyError('Cost of Goods and Services')"}]
I encountered an error. I will now view the data to understand the error.

running 2th step.
= running tool run_python_code, with parameters: {'code': "import pandas as pd\n\ndf = pd.read_csv('income_statement.csv')\n\nprint(df.columns)"}
== tool results: [{'python_answer': "Index(['Unnamed: 0', 'index',\n       'RevenueFromContractWithCustomerExcludingAssessedTax',\n       'CostOfGoodsAndServicesSold', 'GrossProfit',\n       'ResearchAndDevelopmentExpense',\n       'SellingGeneralAndAdministrativeExpense', 'OperatingExpenses',\n       'OperatingIncomeLoss', 'NonoperatingIncomeExpense',\n       'IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest',\n       'IncomeTaxExpenseBenefit', 'NetIncomeLoss', 'EarningsPerShareBasic',\n       'EarningsPerShareDiluted',\n       'WeightedAverageNumberOfSharesOutstandingBasic',\n       'WeightedAverageNumberOfDilutedSharesOutstanding'],\n      dtype='object')\n"}]
I found that the column name is 'CostOfGoodsAndServicesSold'. I will now use this to find the highest value.

running 3th step.
= running tool run_python_code, with parameters: {'code': 'import pandas as pd\n\ndf = pd.read_csv(\'income_statement.csv\')\n\n# Find the highest value of \'Cost of Goods and Services\'\nhighest_cost = df[\'CostOfGoodsAndServicesSold\'].max()\n\nprint(f"The highest value of \'Cost of Goods and Services\' is: {highest_cost}")'}
== tool results: [{'python_answer': "The highest value of 'Cost of Goods and Services' is: 169559000000.0\n"}]
The highest value of 'Cost of Goods and Services' is 169559000000.0.

Add Viewing Tool

What if we directly give the model the ability to view the data as a tool so that it can explicitly use it instead of indirectly figuring it out?

def view_csv_data(path: str) -> dict:
    """
    Function to view the head, tail and shape of a given csv file.
    """
    df = pd.read_csv(path)

    return {
        "head": df.head().to_string(),
        "tail": df.tail().to_string(),
        "shape": str(df.shape),
    }

functions_map = {
    "run_python_code": run_python_code,
    "view_csv_data": view_csv_data
}

tools = [
    {
        "name": "run_python_code",
        "description": "given a python code, runs it",
        "parameter_definitions": {
            "code": {
                "description": "executable python code",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "view_csv_data",
        "description": "give path to csv data and get head, tail and shape of the data",
        "parameter_definitions": {
            "path": {
                "description": "path to csv",
                "type": "str",
                "required": True
            }
        }
    },
]
preamble = """
You are an expert who answers the user's question. You are working with a pandas dataframe in Python. The name of the dataframe is `income_statement.csv`.
Always view the data first to write flawless code.
"""

question1 = "what is the highest value of cost of goods and service?"

output = cohere_agent(question1, preamble, tools, verbose=True)
running 0th step.
I will first view the data and then write and execute Python code to find the highest value of cost of goods and service.

running 1th step.
= running tool view_csv_data, with parameters: {'path': 'income_statement.csv'}
== tool results: [{'head': '   Unnamed: 0                  index  RevenueFromContractWithCustomerExcludingAssessedTax  CostOfGoodsAndServicesSold   GrossProfit  ResearchAndDevelopmentExpense  SellingGeneralAndAdministrativeExpense  OperatingExpenses  OperatingIncomeLoss  NonoperatingIncomeExpense  IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest  IncomeTaxExpenseBenefit  NetIncomeLoss  EarningsPerShareBasic  EarningsPerShareDiluted  WeightedAverageNumberOfSharesOutstandingBasic  WeightedAverageNumberOfDilutedSharesOutstanding\n0           0  2017-10-01-2018-09-29                                         265595000000                1.637560e+11  101839000000                   1.423600e+10                            1.670500e+10       3.094100e+10         7.089800e+10               2.005000e+09                                                                                 7.290300e+10             1.337200e+10    59531000000                   3.00                     2.98                                   1.982151e+10                                     2.000044e+10\n1           1  2018-09-30-2018-12-29                                          84310000000                         NaN   32031000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    19965000000                   1.05                     1.05                                            NaN                                              NaN\n2           2  2018-09-30-2019-09-28                                         260174000000                1.617820e+11   98392000000                   1.621700e+10                            1.824500e+10       3.446200e+10         6.393000e+10               1.807000e+09                                                                                 6.573700e+10             1.048100e+10    55256000000                   2.99                     2.97                                   1.847134e+10                                     1.859565e+10\n3           3  2018-12-30-2019-03-30                                          58015000000                         NaN   21821000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    11561000000                   0.62                     0.61                                            NaN                                              NaN\n4           4  2019-03-31-2019-06-29                                          53809000000                         NaN   20227000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    10044000000                   0.55                     0.55                                            NaN                                              NaN', 'tail': '    Unnamed: 0                  index  RevenueFromContractWithCustomerExcludingAssessedTax  CostOfGoodsAndServicesSold   GrossProfit  ResearchAndDevelopmentExpense  SellingGeneralAndAdministrativeExpense  OperatingExpenses  OperatingIncomeLoss  NonoperatingIncomeExpense  IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest  IncomeTaxExpenseBenefit  NetIncomeLoss  EarningsPerShareBasic  EarningsPerShareDiluted  WeightedAverageNumberOfSharesOutstandingBasic  WeightedAverageNumberOfDilutedSharesOutstanding\n6            6  2019-09-29-2019-12-28                                          91819000000                         NaN   35217000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    22236000000                   1.26                     1.25                                            NaN                                              NaN\n7            7  2019-09-29-2020-09-26                                         274515000000                1.695590e+11  104956000000                   1.875200e+10                            1.991600e+10       3.866800e+10         6.628800e+10                803000000.0                                                                                 6.709100e+10             9.680000e+09    57411000000                   3.31                     3.28                                   1.735212e+10                                     1.752821e+10\n8            8  2019-12-29-2020-03-28                                          58313000000                         NaN   22370000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    11249000000                   0.64                     0.64                                            NaN                                              NaN\n9            9  2020-03-29-2020-06-27                                          59685000000                         NaN   22680000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    11253000000                   0.65                     0.65                                            NaN                                              NaN\n10          10  2020-06-28-2020-09-26                                          64698000000                         NaN   24689000000                            NaN                                     NaN                NaN                  NaN                        NaN                                                                                          NaN                      NaN    12673000000                   0.74                     0.73                                            NaN                                              NaN', 'shape': '(11, 17)'}]
The column name is 'CostOfGoodsAndServicesSold'. I will now write and execute Python code to find the highest value in this column.

running 2th step.
= running tool run_python_code, with parameters: {'code': "import pandas as pd\n\ndf = pd.read_csv('income_statement.csv')\n\nprint(df['CostOfGoodsAndServicesSold'].max())"}
== tool results: [{'python_answer': '169559000000.0\n'}]
The highest value of cost of goods and services is 169559000000.0.

By being prescriptive, we were able to cut down a step and get to the answer faster.