Shamba-MedCare 提示工程

发布: (2025年12月2日 GMT+8 05:30)
6 min read
原文: Dev.to

Source: Dev.to

介绍

我正在使用 AI 构建一个简易的植物病害诊断解决方案,灵感来源于我的农业背景以及智能技术工具的进步。

你可以在 Shamba‑MedCare 应用这里 查看。抱歉目前只能自行提供 API 密钥,直到公开发布为止。密钥存储在浏览器的本地存储中,因此是私密的。

Shamba‑MedCare 截图

为了说明,当我提到 LLM(大语言模型)时,我主要使用 Claude。我喜欢使用 LLM,因为它们通用,这个解决方案可以适配任何 LLM。

我尝试了多个提示词,以获得最佳结果。这就是我在 Shamba‑MedCare 中的提示工程之路:

提示演进

我最初给 LLM Vision 的提示非常天真:

“这株植物有什么病?”

得到的回复是一篇 2,000 字的关于植物病理学的概述——对教材有帮助,却对一株快枯死的番茄植物的农民毫无用处。让 AI 返回 结构化、可操作、考虑预算的诊断 需要多次迭代。以下是我的收获。

架构

架构图

两个提示词至关重要:

  • 系统提示 – 定义 LLM 要扮演的角色。
  • 分析提示 – 告诉 LLM 对特定图像要做什么。

系统提示:创建 “Shamba”

给提示加上角色会更好。我创建了 Shamba 角色,一个农业病理学家,职责如下:

You are Shamba, an expert agricultural pathologist. You analyze
plant images to identify diseases, pests, and nutrient deficiencies.

Your expertise includes:
- 50+ crop types worldwide
- Fungal, bacterial, viral, and physiological disorders
- Traditional and modern treatment methods
- Practical advice for resource‑limited farmers

Guidelines:
1. Always include at least one FREE/traditional treatment
2. Describe WHERE symptoms appear (for visual mapping)
3. Be honest about uncertainty—use confidence scores
4. Recommend professional help for severe cases

关键句是:“Always include at least one FREE/traditional treatment.”
如果没有这条明确指示,LLM 会默认推荐商业产品——对郊区园丁有帮助,却对买不起 15 美元杀菌剂的农民毫无意义。

失败案例 #1 – JSON 噩梦

我第一次尝试让 LLM 返回 JSON,结果它确实返回了 JSON,但被包在 markdown 代码块里,还带有说明文字:

Here's my analysis:

    ```json
    { "disease": "Early Blight" }
    ```

This is a common fungal disease...

我的解析器因此崩溃。解决办法是把请求写得更明确:

Return ONLY a valid JSON object. No markdown, no commentary,
no text before or after. Start with { and end with }

即便如此,它仍有约 10 % 的概率出错,于是我在后端加入了逻辑:

  • 如有代码块则去除 markdown 包裹
  • 从周围文本中提取 JSON
  • 按预期 schema 验证

失败案例 #2 – 位置信息描述

为了实现可视化热图功能,我需要 LLM 描述 病害出现的部位。最初的提示要求“受影响的区域”,LLM 的回答却模糊如“受影响的面积相当大”。

改进后的提示:

Describe affected regions with:
- Location (helpful for heatmaps): top‑left, center, lower‑right, edges, margins
- Coverage: percentage of area affected (e.g., "35%")
- Spread direction: "Moving from lower leaves upward."

得到的输出:

{
  "affected_regions": [
    {
      "location": "lower-left",
      "severity": "severe",
      "description": "Dark brown lesions with concentric rings",
      "coverage": 15
    },
    {
      "location": "center",
      "severity": "moderate",
      "coverage": 20
    }
  ]
}

这已经足以生成热图叠加层。

热图示例

失败案例 #3 – 治疗费用盲点

早期,治疗方案的顺序随意,有时先列出 50 美元的系统性杀菌剂,再列出免费木灰疗法。LLM 本身并不具备预算感知,于是我强制规定了排序结构:

Provide treatments in EXACTLY this order:
1. FREE TIER: Traditional/home remedies ($0)
2. LOW COST: Basic solutions ($1‑5)
3. MEDIUM COST: Commercial organic ($5‑20)
4. HIGH COST: Synthetic/professional ($20+)

Each tier must have at least one option if applicable.

强制的响应 schema:

{
  "treatments": [
    {
      "method": "Wood ash paste",
      "cost_tier": "free",
      "estimated_cost": "$0",
      "ingredients": ["Wood ash", "Water"],
      "application": "Apply directly to affected areas",
      "availability": "Common from cooking fires"
    },
    {
      "method": "Neem oil spray",
      "cost_tier": "low",
      "estimated_cost": "$1-3"
    }
  ]
}

针对不同植物部位的提示策略

不同部位会呈现不同问题,因此我为每个部位定制了提示词。

叶子

Examine: color patterns, spot shapes, curling, holes, coating
Common issues: fungal spots, viral mosaic, nutrient chlorosis, pest damage

根系

Examine: color (white=healthy, brown/black=rot), texture, galls, structure
Common issues: root rot, nematode damage, waterlogging

将 LLM 的注意力聚焦在对应器官上,诊断准确率会显著提升。

Back to Blog

相关文章

阅读更多 »

切换账户

@blink_c5eb0afe3975https://dev.to/blink_c5eb0afe3975 正如大家所知,我正重新开始记录我的进展,我认为最好在一个不同的…

Strands 代理 + Agent Core AWS

入门指南:Amazon Bedrock AgentCore 目录 - 前置要求(requisitos‑previos) - 工具包安装(instalación‑del‑toolkit) - 创建…