CSV agent dose not Actually writing down the code the changing. #23796
-
Checked other resources
Commit to Help
Example Codeimport config
import pandas as pd
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers.openai_tools import JsonOutputKeyToolsParser
from langchain_experimental.agents import create_pandas_dataframe_agent
import sys
import os
from tenacity import retry, stop_after_attempt, wait_fixed
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def df_agent(csv_input,team):
if team == "song":
team = "song"
else:
team = "jing"
df = pd.read_csv(f'{BASE_DIR}\\{team}_record.csv', encoding='utf-8')
print(df.shape)
print(df.columns.tolist())
llm = ChatOpenAI(api_key=config.OpenAI_API_KEY,
base_url=config.OpenAI_API_BASE_URL, model="gpt-4o",
temperature=0.7) # 确认模型名称是否正确
agent = create_pandas_dataframe_agent(llm, df, agent_type="openai-tools", verbose=True)
res = agent.invoke(
{
"input": csv_input
}
)
output = res.get('output')
if output is not None:
print(output)
else:
for step in res['steps']:
output = step.observation['output']
print(output)
return output
if __name__ == '__main__':
res = df_agent("increase 2 point in 国力 ", "song") DescriptionWhen I writing down the requirement of manipulate CSV file, The CSV agents recognize that as correctly. However, it does not really change the csv data actually. The original CSV data remain the same. System Info(1, 8)
Invoking: 国力 腐败 内政 军事 战争倾向 外交 部队总数 总战斗力
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To ensure that the changes made by the CSV agent are saved back to the CSV file, you need to write the modified DataFrame back to the CSV file after processing it. Here is an example of how you can modify your script to save the changes back to the CSV file: import config
import pandas as pd
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers.openai_tools import JsonOutputKeyToolsParser
from langchain_experimental.agents import create_pandas_dataframe_agent
import sys
import os
from tenacity import retry, stop_after_attempt, wait_fixed
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def df_agent(csv_input, team):
if team == "song":
team = "song"
else:
team = "jing"
file_path = f'{BASE_DIR}\\{team}_record.csv'
df = pd.read_csv(file_path, encoding='utf-8')
print(df.shape)
print(df.columns.tolist())
llm = ChatOpenAI(api_key=config.OpenAI_API_KEY,
base_url=config.OpenAI_API_BASE_URL, model="gpt-4o",
temperature=0.7) # 确认模型名称是否正确
agent = create_pandas_dataframe_agent(llm, df, agent_type="openai-tools", verbose=True)
res = agent.invoke(
{
"input": csv_input
}
)
output = res.get('output')
if output is not None:
print(output)
else:
for step in res['steps']:
output = step.observation['output']
print(output)
# Save the modified DataFrame back to the CSV file
df.to_csv(file_path, index=False)
return output
if __name__ == '__main__':
res = df_agent("increase 2 point in 国力 ", "song") In this example, after invoking the agent and processing the DataFrame, the modified DataFrame is saved back to the original CSV file using |
Beta Was this translation helpful? Give feedback.
To ensure that the changes made by the CSV agent are saved back to the CSV file, you need to write the modified DataFrame back to the CSV file after processing it. Here is an example of how you can modify your script to save the changes back to the CSV file: