How Baidu’s Wenxin Yiyan Powers AI Text Creation in Mobile Apps
This article explains the technical workflow behind Baidu App’s AI‑assisted text generation, covering large‑language‑model fundamentals, three‑layer system architecture, prompt design, risk‑control measures, SSE streaming, and a custom Android TextView implementation for smooth, gradient‑styled output.
Large language models (LLMs) such as Baidu’s Wenxin Yiyan contain hundreds of billions of parameters and are trained on massive datasets to achieve strong performance and generalization. Baidu App integrates this capability to offer intelligent, personalized content creation that reduces author effort and cost.
System Architecture
The solution follows a three‑layer architecture:
Business layer : implements AI‑assisted creation features across web, H5, and mini‑programs (e.g., AI‑assisted posts, AI notes).
Strategy layer : provides prompt‑template configuration, input/output policy control, and configuration management.
Foundation layer : consists of the Wenxin Yiyan service and risk‑control services.
End‑to‑End Workflow
The business layer calls the Baidu creation‑brain service to obtain account and feature permissions, then presents appropriate functions (e.g., daily post, AI poetry, travel notes). User input is sent to the creation‑brain API, which validates permissions and prompts, runs the content through a risk‑control word list, and finally forwards the prompt and user query to Wenxin Yiyan for AI‑enhanced output.
Prompt Engineering
A prompt is a textual instruction that guides the LLM. Its basic formula is
Task + Generation Subject + Details (optional) + Format (optional). High‑quality prompts should be clear, generic enough to reuse across similar tasks, and produce stable outputs.
Typical prompt components:
Task : the type of work the model should perform (e.g., write a poem).
Subject : the specific object of generation (e.g., a summer poem).
Details : optional specifications such as emojis.
Format : styling or layout requirements.
Prompt Configurations for the App
Full continuation – ask the model to continue the given text concisely.
Full rewrite – request grammatical correction and polishing.
Daily post – generate a short status based on user input.
AI poetry – create a brief poem on a user‑provided theme.
Product recommendation – write a lively recommendation copy.
Travel notes – produce a short travel diary.
Risk Control Measures
Input content passes through a risk‑control filter using a prohibited‑word list.
If Wenxin Yiyan returns a response that hits the safety list, the user input is cleared.
User behavior is logged; high‑risk accounts may be banned based on content analysis.
Periodic audits of historical records are performed.
SSE Streaming Protocol
Wenxin Yiyan uses Server‑Sent Events (SSE) to push generated text to the client. Required response headers are:
Content-Type: text/event-stream; charset=utf-8
Cache-Control: no-cache, no-transform
Connection: keep-alive
X-Accel-Buffering: noThe client opens an HTTP GET connection, receives incremental events, and displays them until the stream ends or the connection is closed.
Gradient Streaming UI Component (Android)
The front‑end displays streamed text in three states: initial waiting (cursor blinking), displaying (character‑by‑character with gradient), and display finished (full text, cursor hidden). A custom TextView subclass is used instead of EditText to avoid editing complexities.
Key implementation points:
Use a Handler timer to slice the text and update the view for per‑character display.
Apply a LinearGradient via ForegroundColorSpan to achieve color transition.
override fun updateDrawState(tp: TextPaint?) {
tp ?: return
val leadingWidth = tp.measureText(containingText, 0, gradientStart)
val gradientWidth = tp.measureText(containingText, gradientStart, gradientEnd)
val lineGradient = LinearGradient(
leadingWidth, 0f, gradientWidth, 0f,
intArrayOf(startColorInt, endColorInt),
floatArrayOf(0f, 1f), Shader.TileMode.CLAMP)
tp.shader = lineGradient
}Implement a custom ReplacementSpan for the cursor. Override getSize() to reserve space and draw() to render a rounded‑rectangle cursor.
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
return paint.measureText(" ").toInt()
}
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
canvas.drawRoundRect(x, top.toFloat(), x + width, bottom.toFloat(), rx, ry, cursorPaint)
}Animate cursor opacity with a ValueAnimator that updates the paint’s alpha.
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
cursorPaint.alpha = (alpha * 255).toInt().coerceAtMost(255)
canvas.drawRoundRect(x, top.toFloat(), x + width, bottom.toFloat(), rx, ry, cursorPaint)
}Conclusion
The article outlines a practical end‑to‑end integration of Wenxin Yiyan for AI‑driven text creation, detailing system architecture, prompt design, risk mitigation, SSE streaming, and a custom Android UI component that together enable smooth, gradient‑styled, real‑time content generation.
Baidu Tech Salon
Baidu Tech Salon, organized by Baidu's Technology Management Department, is a monthly offline event that shares cutting‑edge tech trends from Baidu and the industry, providing a free platform for mid‑to‑senior engineers to exchange ideas.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
