-
Assume that an LLM is generating insights from a DataFrame. How can LLMs use 'field descriptions' to provide a narrative based on these descriptions rather than just the field names? For example, instead of saying '500 EMPID are present in the data,' I want the LLM to say '500 Employees are present in the data.' I have a separate file with field descriptions, such as EMPID = Employee. My objective is for the LLM to recognize that EMPID corresponds to Employee and thus generate the final response as '500 Employees' instead of '500 EMPID.' Thank you for your suggestions, and could you please provide sample code? Sample Code:
Python Tool used:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
To achieve your goal of using 'field descriptions' to provide a narrative based on these descriptions rather than just the field names, you can follow these steps:
Here is a sample code to demonstrate this: import pandas as pd
# Sample DataFrame for employees
employee_data = {
'EMPID': [101, 102, 103, 104, 105],
'EMPName': ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
}
employee_df = pd.DataFrame(employee_data)
# Sample DataFrame for field descriptions
insights_data = {
'Field_Name': ['EMPID', 'EMPName'],
'Description': [
'Employee', # Description for EMPID
'Employee Name' # Description for EMPName
]
}
insights_df = pd.DataFrame(insights_data)
# Create a dictionary mapping field names to descriptions
field_desc_dict = dict(zip(insights_df['Field_Name'], insights_df['Description']))
# Function to generate narrative based on field descriptions
def generate_narrative(query_result, field_desc_dict):
narrative = []
for field, value in query_result.items():
description = field_desc_dict.get(field, field)
narrative.append(f"{description}: {value}")
return "\n".join(narrative)
# Example query
query_result = employee_df.iloc[0].to_dict()
# Generate narrative
narrative = generate_narrative(query_result, field_desc_dict)
print(narrative) In this example:
This approach ensures that the generated insights are more descriptive and user-friendly. For integrating this with the from langchain.experimental.tools.python.tool import PythonAstREPLTool
from langchain.experimental.tools.python.inputs import PythonInputs
# Initialize the REPL tool with the DataFrame and field descriptions
repl = PythonAstREPLTool(
locals={"employee_df": employee_df, "field_desc_dict": field_desc_dict, "generate_narrative": generate_narrative},
name="python_repl",
description="Runs code and returns the output of the final line",
args_schema=PythonInputs,
)
# Example usage of the REPL tool
query = """
query_result = employee_df.iloc[0].to_dict()
narrative = generate_narrative(query_result, field_desc_dict)
narrative
"""
output = repl._run(query)
print(output) This setup allows you to use the |
Beta Was this translation helpful? Give feedback.
-
@dosu...perfect! I will play around with more advanced examples and let you know. |
Beta Was this translation helpful? Give feedback.
-
@dosu, I am using a few-shot template as shown below. My request is: Is it possible to integrate this into my 'agent_executor'? By doing so, I can run everything together before invoking. Please help. https://python.langchain.com/docs/how_to/few_shot_examples/ (reference)
)
|
Beta Was this translation helpful? Give feedback.
-
@dosu My goal is ingest few-shot prompt in the original agent_executor. Not to create separate agent_executor. Please help to converge in below agent executor.
|
Beta Was this translation helpful? Give feedback.
To modify your script to accommodate both DataFrames and tune it for use with the
PythonAstREPLTool
in LangChain, you need to update your imports to use thelangchain_experimental
package as thePythonAstREPLTool
has been moved there. Here is an example of how you can modify your script:locals
dictionary.Here is the updated script: