简介 在前面的课程中,你已经学习了如何设计提示、连接到 AI API,以及构建代理工作流的结构。本课将把一切联系起来:你将从用户界面一路到评估,在真实的网页应用中构建一个完整的 AI 驱动功能。我们在整个课程中使用的示例功能是一个 AI 驱动的代码审阅工具,它会获取用户粘贴的代码片段并返回反馈以及质量分数。
目标不是编写完美的生产代码。目标是理解堆栈的每一层——前端表单、后端端点、提示、API 调用、响应解析和测试——以便你可以对任何功能重复这种模式。
项目结构 一个小型的 Node.js/Express 应用就足以演示完整流程。后端暴露一个路由,接收代码文本,将其发送给 AI 模型,并返回结构化的反馈。前端是一个简单的 HTML 表单,会提交代码片段并渲染结果。
javascript 复制
import express from "express" ;
import { reviewCode } from "./agent.js" ;
const app = express ( ) ;
app . use ( express . json ( ) ) ;
app . use ( express . static ( "public" ) ) ;
app . post ( "/api/review" , async ( req , res ) => {
const { code } = req . body ;
if ( ! code || code . length < 5 ) {
return res . status ( 400 ) . json ( { error : "Code snippet required" } ) ;
}
const result = await reviewCode ( code ) ;
res . json ( result ) ;
} ) ;
app . listen ( 3000 , ( ) => console . log ( "Server on http://localhost:3000" ) ) ;
代理层 代理是一个简单的模块,拥有提示模板和 API 调用。将其与路由处理分离,便于独立测试。提示要求模型返回带分数和问题清单的 JSON,从而避免后续脆弱的字符串解析。
javascript 复制
import OpenAI from "openai" ;
const client = new OpenAI ( ) ;
export async function reviewCode ( code ) {
const prompt = ` You are a senior code reviewer. Analyze the following code and respond ONLY with valid JSON in this exact shape:
{"score": <number 0-100>, "issues": [{"line": <number or null>, "severity": "low|medium|high", "message": "<text>"}], "summary": "<one sentence>"}
Code:
${ code } ` ;
const response = await client . chat . completions . create ( {
model : "gpt-4o-mini" ,
messages : [ { role : "user" , content : prompt } ] ,
temperature : 0.2 ,
response_format : { type : "json_object" }
} ) ;
return JSON . parse ( response . choices [ 0 ] . message . content ) ;
}
前端 界面设计故意保持极简。一个用于代码的文本区域、一个提交按钮、以及一个结果面板。发送 JSON 能让代码与后端保持一致,避免表单编码带来的意外。
html 复制
< form id = " review-form " >
< textarea id = " code " rows = " 10 " placeholder = " Paste your code here " > </ textarea >
< button type = " submit " > Review </ button >
</ form >
< pre id = " result " > </ pre >
< script >
document . getElementById ( "review-form" ) . onsubmit = async ( e ) => {
e . preventDefault ( ) ;
const code = document . getElementById ( "code" ) . value ;
const res = await fetch ( "/api/review" , {
method : "POST" ,
headers : { "Content-Type" : "application/json" } ,
body : JSON . stringify ( { code } )
} ) ;
const data = await res . json ( ) ;
document . getElementById ( "result" ) . textContent = JSON . stringify ( data , null , 2 ) ;
} ;
</ script >
测试该功能 直接对代理进行单元测试是捕捉提示回归的最快方式。构建一小组包含预期分数范围的代码片段并循环运行它们。如果对提示进行微调使某个用例变差,你会立即看到。
javascript 复制
import { reviewCode } from "./agent.js" ;
const cases = [
{ code : "def add(a,b): return a+b" , expectMin : 70 } ,
{ code : "eval(input())" , expectMax : 30 } ,
{ code : "function f(){ while(true){} }" , expectMax : 40 }
] ;
for ( const c of cases ) {
const r = await reviewCode ( c . code ) ;
const ok = ( c . expectMin ? r . score >= c . expectMin : true ) &&
( c . expectMax ? r . score <= c . expectMax : true ) ;
console . log ( ` ${ ok ? "PASS" : "FAIL" } score= ${ r . score } code= ${ c . code . slice ( 0 , 30 ) } ` ) ;
}
常见陷阱 同样的问题经常出现三次。第一,在需要结构化输出时未将 response_format 设置为 json_object,导致静默的解析失败。第二,忘记在服务器端验证输入长度,使用户可以发送超长提示并增加成本。第三,只用完美代码来测试,永远看不出模型对错误的处理。测试集里至少包含一个明显错误的片段。
总结 现在你已经有了发布 AI 功能的工作模式:一个简洁的前端、一个 Express 路由、一个独立的 agent 模块负责 prompt 与 API 调用,以及一个小型测试套件,能够防止回归。将此布局用于摘要器、分类器、聊天助手,或你在网页应用中添加的任何其他 AI 驱动工具。