STAR形式分享AI编程-1
开发环境
编码工具: Cursor
AI模型: claude-3.5-sonnet
实践描述
Situation & Task
在开发某个统计功能时,需要根据后端返回的数据结构计算不同管理者类型的分布情况。传统的纯文字prompt方式往往需要多轮对话才能获得准确的代码实现。
为了提升效率,我们采用了以下方案:
- 设计一个清晰的伪代码prompt,包含:
- 完整的数据结构示例
- 详细的计算逻辑规则
- 预期的返回值格式
- 利用该prompt生成TypeScript代码,并封装为独立方法
Action
具体实施步骤如下:
编写伪代码构建清晰化的prompt:
- 管理者类型的完整定义
- 后端返回的JSON数据结构示例
- 使用if-else形式描述的三层判断逻辑
- 清晰的返回值数据结构
生成
CalculationManagerType.ts
文件,包含:- 类型定义和函数
- 辅助函数的模块化设计
- 主计算函数
calculateManagerDistribution
- 完整的TypeScript类型注解和注释
Result
技术成果
- 生成的代码无需过多修改即可投入使用
- 代码结构清晰,包含完整的类型定义和注释
- 实现了业务逻辑要求
效率提升
- 相比纯文字prompt,减少了多轮对话的需求
- 显著提高了代码生成的准确性
- 缩短了从需求到实现的时间:1h → 10min
最佳实践
- 证实了结构化伪代码prompt的优势
- 为类似场景总结了可复用的prompt写法
以下是完成此任务的详细prompt和AI生成的代码
整理的伪代码的prompt如下:
以下是管理者类型分布的逻辑:
产品制定的管理者类型:探索方向型,驱动增长型,强化执行型,塑造环境型,待分化型
后端返回的数据结构如下:
“totalPeople”:4,
"assessmentGroupReportCpaTalentDistributionVO": [
{
"typeName": "探索方向型",
"definition": "一段文本一段文本一段文本",
"exerciseOpportunity": "一段文本一段文本一段文本",
"cpaTalentDistributionTypeCode": "exploratoryDirection",
"peopleList": []
},
{
"typeName": "驱动增长型",
"definition": "作为业务战将,帮助组织实现业务增长",
"exerciseOpportunity": "一段文本一段文本一段文本",
"cpaTalentDistributionTypeCode": "growthDriven",
"peopleList": [
"用户26"
]
},
{
"typeName": "强化执行型",
"definition": "一段文本一段文本一段文本",
"exerciseOpportunity": "一段文本一段文本一段文本一段文本",
"cpaTalentDistributionTypeCode": "executionEnhancer",
"peopleList": [
"用户24",
"用户27"
]
},
{
"typeName": "塑造环境型",
"definition": "一段文本一段文本一段文本",
"exerciseOpportunity": "一段文本一段文本一段文本一段文本",
"cpaTalentDistributionTypeCode": "environmentShaper",
"peopleList": [
"用户25"
]
},
{
"typeName": "待分化型",
"definition": "一段文本一段文本一段文本",
"exerciseOpportunity": "一段文本一段文本一段文本一段文本",
"cpaTalentDistributionTypeCode": "pendingDifferentiation",
"peopleList": []
}
]
人数占比计算逻辑:
if (条件 1:待分化型的人数最高):
{
return { 待分化型人数最高:true,highList:[], lowList:[] }
}
else if( 条件2 :当非待分化型的四种类型的人数相同 )
{
return { 待分化型占比最高:false,highList:[], lowList:[] }
}
else
{
highList = [找出非待分化型人数最多的那个管理者类型对象,并列的也一起放到这个数组里]
lowList: = [找出非待分化型人数最少的那个管理者类型对象,并列的也一起放到这个数组里]
return { 待分化型人数最高:false,highList:highList,lowList:lowList }
}
根据上面的逻辑和数据返回结构生成一个方法来计算出返回值
生成的代码CalculationManagerType.ts:
可以看到ai直接会把需要定义的数据返回类型和计算逻辑完全按照伪代码prompt里的要求去生成,并且下面这段calculateManagerDistribution 的函数代码是完全没有二次修改的直接可以使用的:
/**
* @fileoverview 管理者类型分布计算工具
*/
import { ManagerTypeVO, DistributionResult } from '../data'
/**
* @function getPendingDifferentiationType
* @description 从列表中获取待分化类型
*/
const getPendingDifferentiationType = (types: ManagerTypeVO[]): ManagerTypeVO => {
return types.find((type) => type.cpaTalentDistributionTypeCode === 'pendingDifferentiation')!
}
/**
* @function getNonPendingTypes
* @description 获取除待分化类型外的所有类型
*/
const getNonPendingTypes = (types: ManagerTypeVO[]): ManagerTypeVO[] => {
return types.filter((type) => type.cpaTalentDistributionTypeCode !== 'pendingDifferentiation')
}
/**
* @function findExtremeTypes
* @description 找出人数最多和最少的类型
*/
const findExtremeTypes = (types: ManagerTypeVO[]): { highList: ManagerTypeVO[]; lowList: ManagerTypeVO[] } => {
const counts = types.map((type) => type.peopleList.length)
const maxCount = Math.max(...counts)
const minCount = Math.min(...counts)
return {
highList: types.filter((type) => type.peopleList.length === maxCount),
lowList: types.filter((type) => type.peopleList.length === minCount)
}
}
/**
* @function calculateManagerDistribution
* @description 计算管理者类型的分布情况
* @param types 管理者类型数据数组
* @returns 分布计算结果
*/
export const calculateManagerDistribution = (types: ManagerTypeVO[]): DistributionResult => {
const pendingType = getPendingDifferentiationType(types)
const nonPendingTypes = getNonPendingTypes(types)
const pendingCount = pendingType.peopleList.length
// 条件1:待分化类型人数最多
const nonPendingCounts = nonPendingTypes.map((type) => type.peopleList.length)
const maxNonPendingCount = Math.max(...nonPendingCounts)
if (pendingCount > maxNonPendingCount) {
return {
isPendingDifferentiationHighest: true,
highList: [],
lowList: []
}
}
// 条件2:所有非待分化类型人数相等
const allNonPendingEqual = nonPendingCounts.every((count) => count === nonPendingCounts[0])
if (allNonPendingEqual) {
return {
isPendingDifferentiationHighest: false,
highList: [],
lowList: []
}
}
// 条件3:找出非待分化类型中人数最多和最少的组
const { highList, lowList } = findExtremeTypes(nonPendingTypes)
return {
isPendingDifferentiationHighest: false,
highList,
lowList
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!