Integrating ERNIE Bot API for Automated Python Data Cleaning Pipelines
The biggest hurdle with LLM-based cleaning is the "hallucination" risk and the token cost of sending thousands of rows. To solve this, I don't feed the whole dataset into the prompt. Instead, I extract a set of unique "dirty" values, use ERNIE to map them to a "canonical" version, and then apply a local mapping dictionary back to the original dataframe.
Here is the core logic for the mapping function. I use the erniebot SDK for simplicity:
import erniebot
import pandas as pd
erniebot.api_key = 'your_api_key_here'
def get_canonical_value(dirty_value, category_context):
prompt = f"Context: This is a list of company names. Normalize the following value to its official corporate name. Return ONLY the name, no explanation. Value: {dirty_value}"
response = erniebot.ChatCompletion.create(
model='ernie-3.5',
messages=[{'role': 'user', 'content': prompt}]
)
return response.results[0]['content'].strip()
# Example workflow
df = pd.read_csv('raw_data.csv')
unique_dirty = df['company_name'].unique()
mapping_dict = {}
for val in unique_dirty:
# Simple cache to avoid redundant API calls
mapping_dict[val] = get_canonical_value(val, "company_name")
df['company_name_clean'] = df['company_name'].map(mapping_dict)To make this production-ready, I had to implement a few specific tweaks:
Prompt Engineering for Strict Output
ERNIE can be chatty. If you don't explicitly tell it "Return ONLY the name," you'll get responses like "The official name is Apple Inc." which breaks your dataframe. I found that adding a one-shot example inside the prompt significantly increases the reliability of the output format.
Batching and Rate Limiting
Hitting the API in a tight loop will get you throttled. I wrapped the API call in a retry decorator from the tenacity library. Also, using df['col'].unique() is non-negotiable; otherwise, you're paying for the same "Apple Inc" correction 500 times.
The "Human-in-the-Loop" Validation
I never trust the AI blindly for financial or critical data. I added a step that flags low-confidence changes. If the original string and the cleaned string differ by more than a certain Levenshtein distance (using fuzzywuzzy), I push those specific rows into a review_needed.csv for manual verification.
Performance Gains
Compared to writing 50+ complex regex patterns for every possible misspelling of a vendor name, this setup reduced my preprocessing time from two days of manual coding to about 20 minutes of API runtime. The productivity gain isn't just in the time saved, but in the mental energy of not having to account for every edge case in a regex string.
The main gotcha is the latency. If you're processing millions of rows, even with a mapping dictionary, the initial unique-value scan can be slow. For those cases, I've started using a local SQLite cache to store the dirty_value -> clean_value pairs, so subsequent runs of the pipeline are nearly instantaneous for previously seen errors.
All Replies (0)
No replies yet — be the first!
