admin管理员组文章数量:1026463
def standardize_column_names(df, target_columns):
"""
Uses a language model to standardize column names dynamically based on semantic similarity.
Args:
df (pd.DataFrame): The DataFrame whose columns need standardization.
target_columns (list): A list of target column names to map the DataFrame's columns to.
Returns:
pd.DataFrame: DataFrame with standardized column names.
"""
raw_columns = list(df.columns) # Extract the raw column names
raw_columns_str = ", ".join(raw_columns) # Convert to a comma-separated string
target_columns_str = ", ".join(target_columns) # Convert target columns to a string
# Define the LLM prompt
prompt = PromptTemplate(
input_variables=["raw_columns", "target_columns"], # Match keys exactly with dictionary passed to `invoke`
template=(
"You are tasked with standardizing column names. Here are the raw column names:\n"
"{raw_columns}\n"
"And here is the list of target column names to map to:\n"
"{target_columns}\n"
"Provide a mapping of raw column names to target column names as a dictionary in this format:\n"
"{'raw_column': 'target_column', ...}"
),
)
# Initialize LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
try:
# Use `invoke` with correctly matched keys
response = chain.invoke({"raw_columns": raw_columns_str, "target_columns": target_columns_str})
mapping_result = response["text"] # Extract the LLM's generated text
column_mapping = eval(mapping_result) # Convert the string response into a Python dictionary
except Exception as e:
raise ValueError(f"Error in LLM-based column mapping: {e}")
# Apply the generated mapping to rename columns
df.rename(columns=column_mapping, inplace=True)
return df
The above code is projecting an error:
Exception has occurred: ValueError
Error in LLM-based column mapping: Missing some input keys: {"'raw_column'"}
File "/Users/pro/Desktop/Technology/Bicycle AI/Data_analysis_AI.py", line 57, in standardize_column_names
response = chain.invoke({"raw_columns": raw_columns_str, "target_columns": target_columns_str})
I don't know why it is mapping like that one.
def standardize_column_names(df, target_columns):
"""
Uses a language model to standardize column names dynamically based on semantic similarity.
Args:
df (pd.DataFrame): The DataFrame whose columns need standardization.
target_columns (list): A list of target column names to map the DataFrame's columns to.
Returns:
pd.DataFrame: DataFrame with standardized column names.
"""
raw_columns = list(df.columns) # Extract the raw column names
raw_columns_str = ", ".join(raw_columns) # Convert to a comma-separated string
target_columns_str = ", ".join(target_columns) # Convert target columns to a string
# Define the LLM prompt
prompt = PromptTemplate(
input_variables=["raw_columns", "target_columns"], # Match keys exactly with dictionary passed to `invoke`
template=(
"You are tasked with standardizing column names. Here are the raw column names:\n"
"{raw_columns}\n"
"And here is the list of target column names to map to:\n"
"{target_columns}\n"
"Provide a mapping of raw column names to target column names as a dictionary in this format:\n"
"{'raw_column': 'target_column', ...}"
),
)
# Initialize LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
try:
# Use `invoke` with correctly matched keys
response = chain.invoke({"raw_columns": raw_columns_str, "target_columns": target_columns_str})
mapping_result = response["text"] # Extract the LLM's generated text
column_mapping = eval(mapping_result) # Convert the string response into a Python dictionary
except Exception as e:
raise ValueError(f"Error in LLM-based column mapping: {e}")
# Apply the generated mapping to rename columns
df.rename(columns=column_mapping, inplace=True)
return df
The above code is projecting an error:
Exception has occurred: ValueError
Error in LLM-based column mapping: Missing some input keys: {"'raw_column'"}
File "/Users/pro/Desktop/Technology/Bicycle AI/Data_analysis_AI.py", line 57, in standardize_column_names
response = chain.invoke({"raw_columns": raw_columns_str, "target_columns": target_columns_str})
I don't know why it is mapping like that one.
本文标签: pythonException has occurred ValueError in langchainStack Overflow
版权声明:本文标题:python - Exception has occurred: ValueError in langchain - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745637695a2160551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论