> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON 模式

<Card title="写在最前：问题咨询请直接点击此处提交 ISSUE" icon="star" href="https://github.com/pig-mesh/deepseek4j/issues">
  * ⭐️ **请先 Star 本项目后再提问，作者精力有限，未 Star 直接 Close**
  * 🐞 [GitHub ISSUE](https://github.com/pig-mesh/deepseek4j/issues) 需包含：问题截图 + SDK版本 + 配置代码
</Card>

## 基础使用示例

deepseek4j 支持通过 JSON Schema 来格式化模型输出。这使得我们可以精确控制 AI 模型的输出格式，确保输出符合预期的 JSON 结构。

<Warning>此功能只支持 deepseek-chat 模型</Warning>

### 简单示例

以下是一个简单的示例，展示如何使用 JSON 格式化输出解析考试文本：

```java theme={"system"}
String systemPrompt = "   The user will provide some exam text. Please parse the \"question\" and \"answer\" and output them in JSON format.\n" +
        "            \n" +
        "            EXAMPLE INPUT:\n" +
        "            Which is the highest mountain in the world? Mount Everest.\n" +
        "            \n" +
        "            EXAMPLE JSON OUTPUT:\n" +
        "            {\n" +
        "                \"question\": \"Which is the highest mountain in the world?\",\n" +
        "                \"answer\": \"Mount Everest\"\n" +
        "            }";



String userPrompt = "Which is the longest river in the world? The Nile River.";


ChatCompletionRequest request = ChatCompletionRequest.builder()
        .model(ChatCompletionModel.DEEPSEEK_CHAT)
        .addSystemMessage(systemPrompt)
        .addUserMessage(userPrompt)
        .responseFormat(ResponseFormatType.JSON_OBJECT)
        .build();

ChatCompletionResponse completionResponse = deepSeekClient.chatCompletion(request).execute();
```

预期输出：

```json theme={"system"}
{
    "question": "Which is the longest river in the world?",
    "answer": "The Nile River"
}
```

<Card title="PIG AI应用开发平台 | 适合中大型企业构建自主可控的AI中台" icon="link" href="https://ai.pig4cloud.com">
  **为Java开发者提供全栈式AI工程化解决方案**，强类型/高可维护性架构，内置30+主流大模型支持。

  * **🔍 知识引擎体系**：RAG 知识引擎全自动化多模态解决方案
  * **📝 AI-OCR 中枢**：复杂非标场景高精度识别
  * **⚙️ 业务智能融合**：函数编排 + Chat2SQL，无缝对接现有业务系统
  * **🛡️ N维风控体系**：敏感词/IP/Token/User 规则控制引擎
</Card>

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/deepseek/json.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
