找回密码
 立即注册
首页 业界区 业界 AI开发-python-langchain框架(1-12 返回json-格式解析 ...

AI开发-python-langchain框架(1-12 返回json-格式解析器)

窟聿湎 2026-2-6 13:55:01
关键点来了,现在json格式是开发中是最为普遍的数据格式,尤其在前后端交互中应用十分广泛,如何让大模型返回的数据是标准的json格式?
看如下代码:
  1. from langchain.prompts import PromptTemplate
  2. from langchain_openai import ChatOpenAI
  3. from langchain_core.output_parsers import JsonOutputParser
  4. from langchain_core.pydantic_v1 import BaseModel, Field
  5. import os
  6. # 定义您想要的数据结构。
  7. class Book(BaseModel):
  8.     title: str = Field(description="书名")
  9.     author: str = Field(description="作者")
  10.     description: str = Field(description="书的简介")
  11. # Set up a parser + inject instructions into the prompt template.
  12. output_parser = JsonOutputParser(pydantic_object=Book)
  13. format_instructions = output_parser.get_format_instructions()
  14. print('原版提示词')
  15. print(format_instructions)
  16. print('#############')
  17. #改成中文提示词
  18. format_instructions = '''输出应格式化为符合以下 JSON 结构的 JSON 实例。
  19. JSON结构
  20. ```
  21. {
  22. 'title': '书的标题',
  23. 'author': '作者',
  24. 'description': '书的简介'
  25. }
  26. ```
  27. '''
  28. prompt = PromptTemplate(
  29.     template="{format_instructions}\n{query}\n",
  30.     input_variables=["query"],
  31.     partial_variables={"format_instructions": format_instructions},
  32. )
  33. # 初始化聊天模型(使用DeepSeek API)
  34. llm = ChatOpenAI(
  35.     api_key=os.getenv("DEEPSEEK_API_KEY"),            # 从环境变量读取API密钥
  36.     base_url=os.getenv("BASE_URL"),                   # 从环境变量读取API基础URL(如 https://api.deepseek.com)
  37.     model="deepseek-v3:671b",                         # 指定使用的模型版本
  38.     temperature=0.7,                                  # 生成随机性控制:0.7 适中创造性
  39.     max_tokens=1024                                   # 单次响应最大token数
  40. )
  41. chain = prompt | llm | output_parser
  42. print('--------------')
  43. # 以及旨在提示语言模型填充数据结构的查询。
  44. query = "请给我介绍2本学习中国历史的经典书籍"
  45. result = chain.invoke({"query": query})
  46. print(result)
  47. #流式输出
  48. # for s in chain.stream({"query": query}):
  49. #     print(s)
复制代码
 输出:
  1. 原版提示词
  2. The output should be formatted as a JSON instance that conforms to the JSON schema below.
  3. As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}
  4. the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.
  5. Here is the output schema:
  6. ```
  7. {"properties": {"title": {"title": "Title", "description": "\u4e66\u540d", "type": "string"}, "author": {"title": "Author", "description": "\u4f5c\u8005", "type": "string"}, "description": {"title": "Description", "description": "\u4e66\u7684\u7b80\u4ecb", "type": "string"}}, "required": ["title", "author", "description"]}
  8. ```
  9. #############
  10. --------------
  11. [{'title': '中国通史', 'author': '吕思勉', 'description': '《中国通史》是吕思勉先生的代表作之一,系统全面地介绍了中国从远古时代到近代的历史发展脉络。该书内容详实,分析深入,是学习中国历史的经典入门书籍。'}, {'title': '万历十五年', 'author': '黄仁宇', 'description': '《万历十五年》是黄仁宇先生的经典著作,以明朝万历十五年为切入点,通过细致入微的历史分析,展现了当时社会的政治、经济和文化状况。该书视角独特,文笔流畅,深受读者喜爱。'}]
复制代码
 看这个返回数据是不是就是需要的标准json格式
上面这段代码的核心是通过定义数据结构、构建提示词、调用大模型、解析输出的完整流程,精准控制大模型返回指定格式的 JSON 数据。
首先通过 Pydantic 的 BaseModel 定义 Book 类,明确要求输出包含 title、author、description 三个字段及对应含义,为 JSON 输出提供规则蓝本;
接着利用 JsonOutputParser 绑定该数据结构(关键点),既自动生成格式提示词,又能后续校验并解析模型输出,同时自定义中文格式提示词强化大模型对 JSON 结构的理解,确保字段与定义完全匹配;
再通过 PromptTemplate 将格式要求与用户查询拼接为标准化提示词,明确告知大模型需返回符合结构的 JSON 实例;
初始化兼容第三方模型的 ChatOpenAI 时,将 temperature 设为 0.7 平衡创造性与格式合规性,降低输出偏离 JSON 结构的概率;
最后通过 LangChain 的链式调用(prompt | llm | output_parser)自动化完成 “提示词拼接→模型生成→JSON 解析” 全流程,
最终输出可直接操作的 Python 字典,全程通过结构约束、提示词引导、解析器校验三重保障,实现大模型稳定返回合规 JSON 数据的核心目标。
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

2026-2-9 23:11:24

举报

2026-2-10 05:37:02

举报

2026-2-10 13:48:23

举报

2026-2-10 17:32:54

举报

喜欢鼓捣这些软件,现在用得少,谢谢分享!
2026-2-11 13:13:00

举报

您需要登录后才可以回帖 登录 | 立即注册