Gemini JSON 文本摘要实战把长故事拆成结构化数据【免费下载链接】cookbookExamples and guides for using the Gemini API项目地址: https://gitcode.com/GitHub_Trending/coo/cookbookGitHub_Trending/coo/cookbook里有个Text_Summarization示例演示用 Gemini API 做 JSON 文本摘要把一篇长故事拆成剧情梗概、角色、地点和题材直接落成 Python 字典。适合给内容管道补一个文本变数据的环节。写内容管道时手里常是一大堆非结构化文本小说、访谈、长报道。人读没问题程序拿去做筛选、检索、入库就麻烦了。JSON 文本摘要解决的就是这一步你定好字段结构模型只按这个结构输出 JSON后续代码直接拿字典用不用写正则去抠字符串。先看效果一段真实输出示例的故事是制图师 Elara 沿一张古地图寻找失落之城 Eldoria路上用一支木笛感化守关的石怪最后选择把这座城留给传说。运行pprint(response.parsed)后终端直接打印{characters: [{alignment: Good, description: A cartographer and adventurer seeking the lost city of Eldoria., name: Elara}, {alignment: Neutral, description: A creature of stone and shadow tasked with protecting the secrets of Eldoria., name: Guardian of the Hidden Passage}], genres: [Fantasy, Adventure], locations: [{description: A treacherous mountain range with jagged peaks, dangerous paths, and a hidden passage to Eldoria., name: Dragons Tooth Mountains}, {description: A legendary, vanished civilization rumored to hold unimaginable riches and powerful magic., name: Eldoria (Lost City)}, {description: A village where tales of Eldoria are whispered in taverns., name: Whisperwind Village}], synopsis: Elara, a cartographer and adventurer, seeks the lost city of Eldoria, following an ancient map through the Dragon\s Tooth mountains. She faces treacherous terrain and a guardian creature, ultimately using music to connect with the guardian and realizing that Eldoria\s true value lies in its wisdom, not material riches. She chooses to leave Eldoria undisturbed, gaining newfound courage and respect for legends.}这个字典就是 JSON 文本摘要的产物字段和后面要定义的结构一一对应模型没有多吐一个字。跑起来最小可运行示例装依赖、配密钥两条命令pip install -U google-genai2.9.0 export GEMINI_API_KEY你的密钥密钥在 Google AI Studio 申请详细配置流程见资源入口里的 Authentication 示例。先用 TypedDict 定义输出结构模型会严格按它输出from typing_extensions import TypedDict class Character(TypedDict): name: str description: str alignment: str # 阵营如 Good / Neutral class Location(TypedDict): name: str description: str class TextSummary(TypedDict): synopsis: str # 剧情梗概 genres: list[str] # 题材列表 locations: list[Location] characters: list[Character]Python 3.12 起可以直接从typing导入TypedDict不用装typing_extensions。示例 Notebook 里故事是模型现编的本地跑把任意文本存成story.txt读进来就行import os from google import genai client genai.Client(api_keyos.environ[GEMINI_API_KEY]) MODEL_ID gemini-3.7-flash story open(story.txt, encodingutf-8).read() response client.models.generate_content( modelMODEL_ID, contentsfSummarize the story. With a list of genres locations and characters:\n\n{story}, config{ response_mime_type: application/json, # 强制返回 JSON而不是散文 response_schema: TextSummary, # 按上面的结构约束输出字段 }, ) from pprint import pprint pprint(response.parsed)三个参数各一句话response_mime_type强制模型用 JSON 格式回复。response_schema传入TextSummary类客户端会把它转成 JSON Schema 约束输出。response.parsedJSON 已解析成字典不用再调json.loads。看懂结果输出逐字段解读synopsis一句话讲完整个故事起点、转折、结局都在里面。genres模型判定的题材分类这里是[Fantasy, Adventure]。locations每项是namedescription示例抓出三处Dragons Tooth Mountains、Eldoria、Whisperwind Village。characters比地点多一个alignment字段表示角色阵营这里是Good和Neutral。这些字段全部来自TextSummary的声明不多一个也不少一个。下游是数据库的话json.dumps一把梭直接入库。换到新闻文本上试试要改的只有 schema调用代码不动新闻稿把TextSummary换成headline标题、date日期、entities实体列表提示词改成 Summarize this news article...。商品评价定义productsentiment: list[dict]结构逐条抽卖点和情绪。长报告把genres换成key_points: list[str]直接得到结构化要点列表。同目录还有实体抽取、情感分析、文本分类的现成示例写 schema 时可以对着参考。资源入口完整示例 Notebookexamples/json_capabilities/Text_Summarization.ipynb更多 JSON 示例实体抽取、情感、分类examples/json_capabilities/密钥配置指南quickstarts/Authentication.ipynb把仓库克隆下来照着跑一遍git clone https://gitcode.com/GitHub_Trending/coo/cookbook跑通这个例子后把story换成你自己的文本。【免费下载链接】cookbookExamples and guides for using the Gemini API项目地址: https://gitcode.com/GitHub_Trending/coo/cookbook创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考