Extract Structured JSON from Messy Text with Telnyx AI Inference

Extract Structured JSON from Messy Text with Telnyx AI Inference

使用 Telnyx AI Inference 从杂乱文本中提取结构化 JSON

Messy text is everywhere: support tickets, lead forms, emails, contracts, incident reports, call notes, Slack messages. The annoying part is that the useful data is usually in there somewhere, but not in a shape your app can trust. I built a small Python example that uses Telnyx AI Inference to turn unstructured text into structured JSON. Repo: https://github.com/team-telnyx/telnyx-code-examples/tree/main/extract-structured-json-with-ai-python

杂乱的文本无处不在:支持工单、潜在客户表单、电子邮件、合同、事故报告、通话记录、Slack 消息等。令人头疼的是,有用的数据通常隐藏在其中,但其格式往往无法被应用程序直接信任。我构建了一个小型 Python 示例,利用 Telnyx AI Inference 将非结构化文本转换为结构化 JSON。代码仓库:https://github.com/team-telnyx/telnyx-code-examples/tree/main/extract-structured-json-with-ai-python

What it does

功能概述

The app exposes a simple Flask endpoint where you send messy text plus the fields you want back. For example, you can send something like a customer support note and ask for: customer name, issue type, urgency, product, next action. The model returns a structured JSON object that your app can validate, store, route, or pass into another workflow.

该应用程序提供了一个简单的 Flask 端点,你可以将杂乱的文本以及你希望提取的字段发送给它。例如,你可以发送一条客户支持记录,并要求提取:客户姓名、问题类型、紧急程度、产品、后续行动。模型会返回一个结构化的 JSON 对象,你的应用程序可以对其进行验证、存储、路由或传递到其他工作流中。

Why this pattern is useful

为什么这种模式很有用

A lot of AI demos stop at “summarize this text.” That is useful, but many real apps need something stricter: route a ticket, classify an incident, extract lead details, normalize intake forms, prepare data for a CRM, trigger automations based on extracted fields. Structured JSON makes the LLM output easier to use in actual software.

许多 AI 演示仅停留在“总结文本”这一步。这固然有用,但许多实际应用需要更严格的处理:路由工单、分类事故、提取潜在客户详情、规范化录入表单、为 CRM 准备数据、根据提取的字段触发自动化流程。结构化 JSON 使得大语言模型(LLM)的输出在实际软件中更易于使用。

How it works

工作原理

The example uses Telnyx AI Inference through an OpenAI-compatible client pattern. At a high level: Define the schema you want back, send messy text to the model, ask the model to return JSON, validate the result before using it. That last part matters. Even when you ask an LLM for JSON, your app should still treat model output like external input and validate it.

该示例通过兼容 OpenAI 的客户端模式使用 Telnyx AI Inference。简而言之:定义你想要的返回模式(Schema),将杂乱文本发送给模型,要求模型返回 JSON,并在使用前验证结果。最后一点至关重要:即使你要求 LLM 返回 JSON,你的应用程序仍应将模型输出视为外部输入并进行验证。

Try it

尝试运行

Clone the repo: 克隆仓库: git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/extract-structured-json-with-ai-python

Install dependencies and run the app: 安装依赖并运行应用: pip install -r requirements.txt cp .env.example .env python app.py

Then call the endpoint with your own text. 然后使用你自己的文本调用该端点。

Why I like this example

为什么我喜欢这个示例

It is small enough to understand quickly, but it maps to a very real app pattern: taking messy human language and turning it into data your system can actually act on. Also worth noting: the Telnyx code examples repo is structured to be agent-readable, so coding agents can inspect the examples, understand the API patterns, and help you extend them into fuller apps.

它足够简洁,易于快速理解,但它对应了一个非常实际的应用模式:将杂乱的人类语言转化为你的系统可以实际操作的数据。此外值得一提的是:Telnyx 代码示例仓库的结构设计为可供 AI Agent 读取,因此编码 Agent 可以检查这些示例、理解 API 模式,并帮助你将其扩展为更完整的应用程序。

Resources

资源

Feedback welcome, especially from folks building AI apps that need reliable structured output from messy user input. 欢迎提供反馈,特别是对于那些正在构建 AI 应用、需要从杂乱的用户输入中获取可靠结构化输出的开发者。