Integrating ERNIE Bot API into a Python Flask app for automated documentation
I’ve been using a pattern where I scan my source files for docstrings and function signatures, then send those specifically to ERNIE to expand into a full technical guide. If you just dump the whole file, you hit token limits or get "hallucinated" logic.
Here is the core implementation for the API wrapper. I suggest using a dedicated Service class to keep the Flask routes clean.
import requests
import json
from flask import Flask, request, jsonify
class ErnieDocService:
def __init__(self, app_id, secret_key):
self.app_id = app_id
self.secret_key = secret_key
self.url = "https://ernie-bot-api.baidu.com/v1/api"
def get_access_token(self):
# Simplified token fetch logic
resp = requests.get(f"https://aip.baidu.com/ OAuth/2.0/token?grant_type=client_credentials&client_id={self.app_id}&client_secret={self.secret_key}")
return resp.json().get("access_token")
def generate_docs(self, code_snippet):
token = self.get_access_token()
prompt = f"Act as a technical writer. Convert the following Python code into professional Markdown documentation. Include a 'Usage' section and 'Parameters' table:\n\n{code_snippet}"
payload = {
"access_token": token,
"query": prompt,
"bot_name": "ernie-bot"
}
response = requests.post(self.url, json=payload)
return response.json().get("result")
app = Flask(__name__)
doc_service = ErnieDocService(app_id="YOUR_ID", secret_key="YOUR_KEY")
@app.route('/generate-doc', methods=['POST'])
def handle_doc():
data = request.json
code = data.get("code")
if not code:
return jsonify({"error": "No code provided"}), 400
result = doc_service.generate_docs(code)
return jsonify({"markdown": result})A few hard-won productivity tips from this setup:
Prompt Engineering for Docs
Don't just say "document this." ERNIE can be too wordy. I found that adding "Use a concise, engineering-focused tone" and "Format as GitHub-flavored Markdown" drastically reduces the amount of manual editing I have to do after the API returns the text.
Handling Large Files
If you try to send a 500-line file, the output often gets truncated or loses focus. I wrote a small pre-processor using the ast module to split the file into individual functions. I then loop through the functions and send them one by one.
The "Gotcha" with Token Refresh
Don't call get_access_token() on every single request if you're processing a whole project. You'll hit rate limits on the auth endpoint. Cache the token in a global variable or Redis and only refresh it when it expires.
Configuration Wins
Use a .env file for your APP_ID and SECRET_KEY. Never hardcode them in the Flask app, especially if you're pushing to a shared repo.
Set a timeout on the requests.post call. The API can occasionally hang, and you don't want your Flask worker to stay occupied forever.
For the actual workflow, I've integrated this into a Git hook. Every time I commit a file with a #TODO: docs tag, the hook hits my Flask local endpoint, generates the markdown, and appends it to a docs/ folder automatically. It’s a massive win for keeping the readme up to date without manual overhead.
All Replies (0)
No replies yet — be the first!
