我如何构建了一个将混乱文本转换为干净 JSON 的 API(且可免费使用)

发布: (2026年3月8日 GMT+8 13:28)
3 分钟阅读
原文: Dev.to

Source: Dev.to

概览

每位开发者都遇到过凌乱、非结构化的文本——收据、电子邮件、简历等等。有价值的数据隐藏在纯文本中,传统的正则表达式解决方案脆弱,手动解析又繁琐。StructureAI 通过一次 API 调用解决此问题:输入任意文本,返回干净、结构化的 JSON。

示例:收据解析

输入文本

Receipt from Whole Foods 03/15/2024
Apples $3.99
Milk $5.49
Bread $4.29
Tax $1.18
Total: $14.95
Card ending 4242

期望的 JSON 输出

{
  "merchant": "Whole Foods",
  "date": "2024-03-15",
  "items": [
    { "name": "Apples", "price": 3.99 },
    { "name": "Milk", "price": 5.49 },
    { "name": "Bread", "price": 4.29 }
  ],
  "tax": 1.18,
  "total": 14.95,
  "payment_method": "Card ending 4242"
}

API 使用

curl -X POST https://api-service-wine.vercel.app/api/extract \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_KEY" \
  -d '{
    "text": "Receipt from Whole Foods 03/15/2024...",
    "schema": "receipt"
  }'

API 返回结构化的 JSON 以及置信度分数。它适用于收据、发票、电子邮件、简历、联系人,或任何你自定义的模式。

支持的模式

模式提取字段
receipt商品、总计、日期、商家
invoice行项目、金额、到期日期
email发件人、主题、正文、日期
resume姓名、工作经历、技能
contact姓名、电子邮件、电话、地址
custom用户自定义字段

自定义模式示例:产品评论

curl -X POST https://api-service-wine.vercel.app/api/extract \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_KEY" \
  -d '{
    "text": "Love this laptop! Battery lasts 12 hours. Screen is gorgeous. Only downside is the keyboard feels mushy. 4/5 stars.",
    "schema": "custom",
    "custom_fields": ["product_type", "pros", "cons", "rating", "sentiment"]
  }'

响应

{
  "product_type": "laptop",
  "pros": ["12 hour battery life", "gorgeous screen"],
  "cons": ["mushy keyboard"],
  "rating": "4/5",
  "sentiment": "positive"
}

MCP 服务器集成(Claude Desktop / Cursor)

如果你使用 Claude Desktop 或 Cursor,可以安装 MCP 服务器以免费请求。

git clone https://github.com/avatrix1/structureai-mcp.git
cd structureai-mcp && npm install && npm run build

将服务器添加到 Claude Desktop 配置中:

{
  "mcpServers": {
    "structureai": {
      "command": "node",
      "args": ["/path/to/structureai-mcp/dist/index.js"]
    }
  }
}

然后直接向 Claude 提问,例如:“从这段文本中提取收据数据:…”。

定价

  • 免费层(MCP):每月 10 次请求。
  • API 密钥:每 100 次请求 $2。

https://api-service-wine.vercel.app 获取你的 API 密钥。

联系方式

Avatrix LLC 构建。如有疑问,请发送邮件至:support@avatrix.co

0 浏览
Back to Blog

相关文章

阅读更多 »