how to chain the lenght of context if i wanted use gpt-4 model because of increase token i can't use gpt 4 so how to chain the context 
Labels: name: auto:question, name: area: models

### Issue with current documentation:

(import pandas as pd
import json
from IPython.display import Markdown, display
from langchain.agents import create_csv_agent
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
import os

os.environ["OPENAI_API_KEY"] = ""


# Load the dataset
df = pd.read_csv('Loan Collections - Sheet1.csv')


def convert_columns_to_float_by_keywords(df):
    # Convert date and time columns to float
    for column in df.select_dtypes(include=['object']).columns:
        # Check if the column name contains 'date' or 'time'
        if 'date' in column.lower() or 'time' in column.lower() or 'dob' in column.lower() or 'date of birth' in column.lower():
            try:
                # Convert the column to datetime
                df[column] = pd.to_datetime(df[column], errors='coerce')
                
                # Convert datetime to numerical representation (e.g., days since a reference date)
                reference_date = pd.to_datetime('1900-01-01')
                df[column] = (df[column] - reference_date).dt.total_seconds() / (24 * 60 * 60)

            except ValueError:
                # Handle errors during conversion
                print(f"Error converting column '{column}' to float.")

    # Convert columns with specific keywords to float
    keywords_to_convert = ["unique id", "reference id", "account id"]
    for column in df.columns:
        # Check if the column name contains any of the specified keywords
        if any(keyword in column.lower() for keyword in keywords_to_convert):
            try:
                # Convert the column to float
                df[column] = pd.to_numeric(df[column], errors='coerce')

            except ValueError:
                # Handle errors during conversion
                print(f"Error converting column '{column}' to float.")


# Convert 'date' and 'time' columns to float
convert_columns_to_float_by_keywords(df)


# Extract unique values for each column
unique_values_per_column = {}
for column in df.select_dtypes(include=['object']).columns:
    unique_values_per_column[column] = df[column].unique().tolist()

# Convert the dictionary to JSON
json_data_train = json.dumps(unique_values_per_column, indent=4)


testData_fname = "Mutual Funds 2023 - Mutual Funds Data Final File (1).csv"

# Load the dataset
df2 = pd.read_csv(testData_fname)


convert_columns_to_float_by_keywords(df2)



# Extract unique values for each column
unique_values_per_column = {}
for column in df2.select_dtypes(include=['object']).columns:
    unique_values_per_column[column] = df2[column].unique().tolist()

# Convert the dictionary to JSON
json_data_test = json.dumps(unique_values_per_column, indent=4)

# Define user's question
user_question = "monthly growth analysis of Broker Commission ?"

# Define the prompt template
prompt_template = f'''If the dataset has the following columns: {json_data_train}'''+''' Understand user questions with different column names and convert them to a JSON format.

Question might not even mentioned column name at all, it would probably mention value of the column. so it has to figure it out columnn name based on that value.

Example1:
User Question1:  top zone in the year 2019 with Loan Amt between 10k and 20k and tenure > 12 excluding Texas region?


{
    "start_date": "01-01-2019",
    "end_date": "31-12-2019",
    "time_stamp_col": "Due Date",
    "agg_columns": [],
    "trend": "null",
    "to_start_date": "null",
    "to_end_date": "null",
    "growth": "null",
    "variables_grpby": ["Zone"],
    "filters": {},
    "not_in": {"Region": ["Texas"]},
    "num_filter": {
        "gt": [
            ["Loan Tenure", 12],
            ["Loan Amount", 10000]
        ],
        "lt": [
            ["Loan Amount", 20000]
        ]
    },
    "percent": "false",
    "top": "1",
    "bottom": "null"
}

Note the following in the above example
- The word "top" in the User Question made the "top" key have the value as "1". If "highest" is mentioned in the User Question, even then "top" would have the value as "1". If "top" is not mentioned or not implied in the User Question, then it takes on the value "null". Similarly for "bottom" key in the System Response.
- The word "zone" in the User Question refers to a column "Zone" in the dataset and since it is a non-numeric column and we have to group by that column, the system response has it as one of the values of the list of the key "variables_grpby"
- The key "start_date" and "end_date" Since it is mentioned 2019 in the User Question as the timeframe, the "start_date" assumes the beginning of the year 2019 and "end_date" assumes the end of the year 2019. If no date related words are mentioned in the question, "start_date" would be "null" and "end_date" would be "null".
- The key "time_stamp_col" in the System Response should mention the relevant time related column name from the dataset according to the question if the question mentions a time related word.
- The key "agg_columns" in the System Response is a list of columns to be aggregated which should mention the numeric column names on which the question wants us to aggregate on.
- The key "trend" in the System Response, "trend" is set to "null" since the user question doesn't imply any trend analysis . If the question were about trends over time, this key would contain information about the trend, such as "upward," "downward," or "null" if no trend is specified.
- The key "filters" An empty dictionary in this case, as there are no explicit filters mentioned in the user question. If the user asked to filter data based on certain conditions (e.g. excluding a specific region), this key would contain the relevant filters.
- The key "to_start_date" and "to_end_date" Both set to "null" in this example because the user question specifies a single timeframe (2019). If the question mentioned a range (e.g. "from January 2019 to March 2019"), these keys would capture the specified range.
- The key "growth" Set to "null" in this example as there is no mention of growth in the user question. If the user inquired about growth or change over time, this key would provide information about the type of growth (e.g."monthly","yearly"," "absolute") or be set to "null" if not applicable.
- The key "not_in" Contains information about exclusion criteria based on the user's question. In this example, it excludes the "Texas" region. If the user question doesn't involve exclusions, this key would be an empty dictionary.
- The key "num_filter" Specifies numerical filters based on conditions in the user question. In this example, it filters loans with a tenure greater than 12 and loan amounts between 10k and 20k. If the user question doesn't involve numerical filters, this key would be an empty dictionary.
- The key "percent" Set to "false" in this example as there is no mention of percentage in the user question. If the user inquired about percentages, this key would contain information about the use of percentages in the response.

Similarly, below are more examples of user questions and their corresponding expected System Responses.

Example 2:
User Question: What is the Highest Loan Amount and Loan Outstanding by RM Name James in January 2020

{
    "start_date": "01-01-2020",
    "end_date": "31-01-2020",
    "time_stamp_col": "Due Date",
    "agg_columns": ["Loan Amount", "Loan Outstanding"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "null",
    "variables_grpby": [],
    "filters": {"RM Name": ["James"]},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "1",
    "bottom": "null"
}

Example 3:
User Question: Which RM Name with respect to Region has the Highest Interest Outstanding and Principal Outstanding in the year 2019

{
    "start_date": "01-01-2019",
    "end_date": "31-12-2019",
    "time_stamp_col": "Due Date",
    "agg_columns": ["Interest Outstanding", "Principal Outstanding"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "null",
    "variables_grpby": ["RM Name", "Region"],
    "filters": {},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "1",
    "bottom":  "null"
}

Example 4:
User Question: Which Branch in North Carolina with respect to Cibil Score Bucket has the Highest Cibil Score in 2019

{
    "start_date": "01-01-2019",
    "end_date": "31-12-2019",
    "time_stamp_col": "Due Date",
    "agg_columns": ["Cibil Score", "DPD Bucket"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "null",
    "variables_grpby": ["Branch"],
    "filters": {"Region": ["North Carolina"]},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "1",
    "bottom":  "null"
}
''
Example 5:
User Question: With respect to Zone, Region, Branch, RM Name what is the Highest Loan Amount, Loan Tenure, Loan Outstanding, EMI Pending, Principal Outstanding

{
    "start_date":  "null",
    "end_date":  "null",
    "time_stamp_col":  "null",
    "agg_columns": ["Loan Amount", "Loan Tenure", "Loan Outstanding", "EMI Pending", "Principal Outstanding"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "null",
    "variables_grpby": ["Zone", "Region", "Branch", "RM Name"],
    "filters": {},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "1",
    "bottom":  "null"
}

Example 6:
User Question: Top 2 zones by Housing Loan in the year 2019

{
    "start_date": "01-01-2019",
    "end_date": "31-12-2019",
    "time_stamp_col": "Due Date",
    "agg_columns": ["Housing Loan"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "null",
    "variables_grpby": ["Zone"],
    "filters": {"Product": ["Home Loan"]},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "2",
    "bottom":  "null"
}

Example 7:
User Question: yearly growth analysis by Due Date of Loan Amount?

{
    "start_date": "null",
    "end_date": "null",
    "time_stamp_col": "Due Date",
    "agg_columns": ["Loan Amount"],
    "trend":  "null",
    "to_start_date":  "null",
    "to_end_date":  "null",
    "growth":  "yearly",
    "variables_grpby": ["Due Date"],
    "filters": {},
    "not_in": {},
    "num_filter": {},
    "percent": "false",
    "top": "null",
    "bottom":  "null"
}

'''+ f'''Our test dataset has the following columns: {json_data_test}

User Question (to be converted): {user_question}'''





# Set the context length
context_length = 3  # Set your desired context length here

# Load the agent with GPT-4 and the specified context length
gpt4_agent = create_csv_agent(ChatOpenAI(temperature=0, model_name="gpt-4"), testData_fname, context_length=context_length)

# Use the formatted question as the input to your agent
response = gpt4_agent.run(prompt_template)

# Print the response
print(user_question)
print(response)) 

and the error is ( RateLimitError: Rate limit reached for gpt-4 in organization org-bJurmVX4HBor6BJfUF9Q6miB on tokens per min (TPM): Limit 10000, Used 1269, Requested 9015. Please try again in 1.704s. Visit https://platform.openai.com/account/rate-limits to learn more.) so try to run gpt-4 model but chaining the lenght contex

### Idea or request for content:

_No response_