ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

llm-universe 实战:基于 LLM API 的文本概括(Summarizing)Prompt 工程全指南

llm-universe 实战:基于 LLM API 的文本概括(Summarizing)Prompt 工程全指南 llm-universe 实战基于 LLM API 的文本概括SummarizingPrompt 工程全指南【免费下载链接】llm-universe本项目是一个面向小白开发者的大模型应用开发教程在线阅读地址https://datawhalechina.github.io/llm-universe/项目地址: https://gitcode.com/GitHub_Trending/ll/llm-universe导读本文以 llm-universe 教程中「第四章 文本概括」为骨架系统讲解如何通过编程调用大模型 API 与精心构造的 Prompt将海量、冗长的文本如电商商品评论提炼为简洁摘要。文章覆盖单一文本概括、长度约束、关键角度侧重、关键信息提取Extract、批量多文本概括五大场景并结合仓库源码 notebook/C2 使用 LLM API 开发应用/C2.ipynb 中get_completion的真实实现讲清每个 Prompt 背后模型调用的底层机制。读完后你将能独立搭建一套基于 LLM 的文本摘要与信息提取应用。背景为什么需要 LLM 文本概括在信息爆炸的时代无论是开发者调研文献、运营人员分析用户反馈还是电商平台处理海量商品评论都面临同一个难题文本太多时间太少。大型语言模型LLM的文本摘要功能恰好解决了这个痛点——它能够将复杂的文本信息简化、提炼出关键观点让使用者无需通读全文即可快速获取所需信息。以电商场景为例购物网站上往往积累着海量商品评论这些评论真实反映了所有客户的想法。如果拥有一个工具能够概括这些冗长评论运营团队就能快速浏览更多评论、洞悉客户偏好从而指导平台与商家提供更优质的服务。这正是 LLM 文本概括能力的典型应用其核心价值可以概括为三点节省时间、提高效率、精准获取信息。在 llm-universe 项目中文本概括能力建立在第二章封装好的get_completion函数之上本文所有示例均直接复用该函数只需传入 Prompt 即可获得摘要结果。一、环境准备get_completion 函数从哪来本文所有代码示例中的get_completion(prompt)来自教程第二章的封装它屏蔽了不同厂商 API 的调用细节统一接收一个prompt字符串并返回模型生成的文本。以 OpenAI 兼容接口的实现为例见 docs/C2/C2.mddef gen_gpt_messages(prompt): 构造 GPT 模型请求参数 messages 请求参数 prompt: 对应的用户提示词 messages [{role: user, content: prompt}] return messages def get_completion(prompt, modelgpt-4o, temperature 0): 获取 GPT 模型调用结果 请求参数 prompt: 对应的提示词 model: 调用的模型默认为 gpt-4o也可以按需选择 gpt-o1 等其他模型 temperature: 模型输出的温度系数控制输出的随机程度取值范围是 0~2。 温度系数越低输出内容越一致。 response client.chat.completions.create( modelmodel, messagesgen_gpt_messages(prompt), temperaturetemperature, ) if len(response.choices) 0: return response.choices[0].message.content return generate answer error从源码可以看出几个关键设计点messages 结构函数内部将用户 Prompt 包装为[{role: user, content: prompt}]格式这是大多数 Chat 类模型 API 的标准请求体temperature 参数控制输出的随机程度。本文的文本概括任务要求稳定、一致的输出因此使用默认值temperature0确定性最强取值范围为 0~2值越低输出越一致越高越随机结果提取通过response.choices[0].message.content取回模型生成的文本无结果时返回错误提示字符串。值得注意的是仓库在 notebook/C2 使用 LLM API 开发应用/C2.ipynb 中为不同厂商模型提供了多套同构封装百度文心版modelERNIE-Bottemperature 范围为 0-1.0 且不能为 0、讯飞星火版、智谱 GLM 版modelglm-4-plustemperature 默认 0.95、取值范围 0.0-1.0。如果你使用的是其他模型 API只需参考 C2 内容修改get_completion函数本文的 Prompt 编写技巧完全通用。二、单一文本概括2.1 准备示例评论数据我们以一段熊猫公仔的商品评价作为贯穿全文的示例该评价来自一个在线购物平台如亚马逊、淘宝、京东等内容涉及商品的质量、大小、价格、物流速度以及购买者女儿的喜爱程度prod_review 这个熊猫公仔是我给女儿的生日礼物她很喜欢去哪都带着。 公仔很软超级可爱面部表情也很和善。但是相比于价钱来说 它有点小我感觉在别的地方用同样的价钱能买到更大的。 快递比预期提前了一天到货所以在送给女儿之前我自己玩了会。 2.2 限制输出文本长度第一步先尝试让模型生成一个简短摘要并显式约束输出长度from tool import get_completion prompt f 您的任务是从电子商务网站上生成一个产品评论的简短摘要。 请对三个反引号之间的评论文本进行概括最多30个字。 评论: {prod_review} response get_completion(prompt) print(response)模型输出熊猫公仔软可爱女儿喜欢但有点小。快递提前一天到货。可以看到模型给出了一段符合字数要求的摘要内容完整覆盖了评论的四个维度质量、喜爱度、大小、物流。技术要点在 Prompt 中限定「最多 30 个字」是一种软性约束。正如本教程前文强调的语言模型在计算和判断文本长度时依赖分词器Tokenizer而分词器在字符统计方面不具备完美精度因此输出可能偶尔略超字数。实际开发中建议一是给出明确的字数/词数上限并在下游做好截断兜底二是将长度约束描述得尽量具体如「最多 30 个字」「不超过 20 个词汇」。2.3 设置关键角度侧重Focus on Specific Aspects不同业务角色对同一文本的关注点截然不同物流部门更关注运输时效商家更关注价格与商品质量平台更看重整体用户体验。我们可以在 Prompt 中显式强调某一视角让摘要向该角度倾斜。2.3.1 侧重于快递服务prompt f 您的任务是从电子商务网站上生成一个产品评论的简短摘要。 请对三个反引号之间的评论文本进行概括最多30个字并且侧重在快递服务上。 评论: {prod_review} response get_completion(prompt) print(response)输出快递提前到货公仔可爱但有点小。通过输出可以看到摘要以「快递提前到货」开头明确体现了对快递效率的侧重其他信息价格、购买对象等被弱化。2.3.2 侧重于价格与质量prompt f 您的任务是从电子商务网站上生成一个产品评论的简短摘要。 请对三个反引号之间的评论文本进行概括最多30个词汇并且侧重在产品价格和质量上。 评论: {prod_review} response get_completion(prompt) print(response)输出可爱的熊猫公仔质量好但有点小价格稍高。快递提前到货。这次摘要以「可爱的熊猫公仔质量好但有点小价格稍高」开头价格与质量信息被前置突出。但仔细观察会发现快递信息仍然被保留——这是「侧重Focus」与「提取Extract」的本质区别侧重只是调整信息的优先级并不会过滤掉其他维度的信息。实现原理从 docs/C2/C2.md 中的get_completion实现可以看到函数把整个 Prompt包括任务描述与评论原文作为一个user消息传给模型。模型的注意力机制会在生成时根据 Prompt 中「侧重在……上」的指令调整各信息点的权重从而改变摘要的编排顺序与取舍。这种「角色化 指令化」写法也是第二章提示原则见 data_base/knowledge_db/prompt_engineering/2. 提示原则 Guidelines.md中「给模型足够清晰的任务指令」原则的直接应用。2.4 关键信息提取Extract而非概括如果业务场景只需要某一角度的信息、并且过滤掉其他所有信息那么应该要求 LLM 做文本提取Extract而不是概括Summarize。二者的区别在于概括会对全篇信息做压缩与重排提取则只挑出目标维度相关的信息其余一律丢弃。prompt f 您的任务是从电子商务网站上的产品评论中提取相关信息。 请从以下三个反引号之间的评论文本中提取产品运输相关的信息最多30个词汇。 评论: {prod_review} response get_completion(prompt) print(response)输出产品运输相关的信息快递提前一天到货。对比 2.3.1 的概括结果「快递提前到货公仔可爱但有点小」提取结果干净利落只保留运输相关信息价格、质量、大小等无关内容被完全过滤。这在实际业务中非常实用——例如物流部门搭建舆情监控面板时只需要从海量评论中抽取「时效」类信息而非完整摘要。三、同时概括多条文本批量处理3.1 构造评论列表实际工作流中我们面对的是大量评论而非单条。下面将四条不同商品的评价放进一个列表覆盖熊猫公仔、落地灯、电动牙刷、搅拌机四种商品review_1 prod_review # 一盏落地灯的评论 review_2 我需要一盏漂亮的卧室灯这款灯不仅具备额外的储物功能价格也并不算太高。 收货速度非常快仅用了两天的时间就送到了。 不过在运输过程中灯的拉线出了问题幸好公司很乐意寄送了一根全新的灯线。 新的灯线也很快就送到手了只用了几天的时间。 装配非常容易。然而之后我发现有一个零件丢失了于是我联系了客服他们迅速地给我寄来了缺失的零件 对我来说这是一家非常关心客户和产品的优秀公司。 # 一把电动牙刷的评论 review_3 我的牙科卫生员推荐了电动牙刷所以我就买了这款。 到目前为止电池续航表现相当不错。 初次充电后我在第一周一直将充电器插着为的是对电池进行条件养护。 过去的3周里我每天早晚都使用它刷牙但电池依然维持着原来的充电状态。 不过牙刷头太小了。我见过比这个牙刷头还大的婴儿牙刷。 我希望牙刷头更大一些带有不同长度的刷毛 这样可以更好地清洁牙齿间的空隙但这款牙刷做不到。 总的来说如果你能以50美元左右的价格购买到这款牙刷那是一个不错的交易。 制造商的替换刷头相当昂贵但你可以购买价格更为合理的通用刷头。 这款牙刷让我感觉就像每天都去了一次牙医我的牙齿感觉非常干净 # 一台搅拌机的评论 review_4 在11月份期间这个17件套装还在季节性促销中售价约为49美元打了五折左右。 可是由于某种原因我们可以称之为价格上涨到了12月的第二周所有的价格都上涨了 同样的套装价格涨到了70-89美元不等。而11件套装的价格也从之前的29美元上涨了约10美元。 看起来还算不错但是如果你仔细看底座刀片锁定的部分看起来没有前几年版本的那么漂亮。 然而我打算非常小心地使用它 例如我会先在搅拌机中研磨豆类、冰块、大米等坚硬的食物然后再将它们研磨成所需的粒度 接着切换到打蛋器刀片以获得更细的面粉如果我需要制作更细腻/少果肉的食物。 在制作冰沙时我会将要使用的水果和蔬菜切成细小块并冷冻 如果使用菠菜我会先轻微煮熟菠菜然后冷冻直到使用时准备食用。 如果要制作冰糕我会使用一个小到中号的食物加工器这样你就可以避免添加过多的冰块。 大约一年后电机开始发出奇怪的声音。我打电话给客户服务但保修期已经过期了 所以我只好购买了另一台。值得注意的是这类产品的整体质量在过去几年里有所下降 所以他们在一定程度上依靠品牌认知和消费者忠诚来维持销售。在大约两天内我收到了新的搅拌机。 reviews [review_1, review_2, review_3, review_4]3.2 用 for 循环批量概括利用 Python 的for循环与文本概括 Prompt将每条评论概括至 20 个词汇以内并按顺序打印for i in range(len(reviews)): prompt f 你的任务是从电子商务网站上的产品评论中提取相关信息。 请对三个反引号之间的评论文本进行概括最多20个词汇。 评论文本: {reviews[i]} response get_completion(prompt) print(f评论{i1}: , response, \n)输出评论1: 熊猫公仔是生日礼物女儿喜欢软可爱面部表情和善。价钱有点小快递提前一天到货。 评论2: 漂亮卧室灯储物功能快速送达灯线问题快速解决容易装配关心客户和产品。 评论3: 这款电动牙刷电池续航好但牙刷头太小价格合理清洁效果好。 评论4: 该评论提到了一个17件套装的产品在11月份有折扣销售但在12月份价格上涨。评论者提到了产品的外观和使用方法并提到了产品质量下降的问题。最后评论者提到他们购买了另一台搅拌机。3.3 批量处理的工程化思考从这段示例可以延伸出批量文本概括在实际生产中的工程化要点循环调用是起点而非终点for循环适用于中小规模文本如几十到几百条。当评论规模进一步扩大时需要考虑整合评论如按商品聚合、分布式/并发调用如asyncio异步并发请求 API等方式提升运算效率展示层设计可以搭建主控面板Dashboard来汇总大量用户评论摘要供快速浏览、原文支持点击查看让运营人员高效掌握顾客的所有想法失败重试与限流真实 API 调用可能因限流、超时失败建议在生产代码中为get_completion增加重试与错误兜底逻辑仓库 docs/C2/C2.md 中封装的函数已包含「无结果返回错误提示」的兜底可在此基础上扩展。四、英文版 Prompt 模板中英对照英文评论在跨境电商场景十分常见本教程同样提供了完整的英文示例与中文版一一对应可直接套用。4.1 英文单一文本概括prod_review Got this panda plush toy for my daughters birthday, \ who loves it and takes it everywhere. Its soft and \ super cute, and its face has a friendly look. Its \ a bit small for what I paid though. I think there \ might be other options that are bigger for the \ same price. It arrived a day earlier than expected, \ so I got to play with it myself before I gave it \ to her. prompt f Your task is to generate a short summary of a product \ review from an ecommerce site. Summarize the review below, delimited by triple backticks, in at most 30 words. Review: {prod_review} response get_completion(prompt) print(response)输出This panda plush toy is loved by the reviewers daughter, but they feel it is a bit small for the price.4.2 英文侧重快递服务prompt f Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ Shipping deparmtment. Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that mention shipping and delivery of the product. Review: {prod_review} response get_completion(prompt) print(response)输出The customer is happy with the product but suggests offering larger options for the same price. They were pleased with the early delivery.4.3 英文侧重价格与质量prompt f Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ pricing deparmtment, responsible for determining the \ price of the product. Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that are relevant to the price and perceived value. Review: {prod_review} response get_completion(prompt) print(response)输出The customer loves the panda plush toy for its softness and cuteness, but feels it is overpriced compared to other options available.4.4 英文关键信息提取prompt f Your task is to extract relevant information from \ a product review from an ecommerce site to give \ feedback to the Shipping department. From the review below, delimited by triple quotes \ extract the information relevant to shipping and \ delivery. Limit to 30 words. Review: {prod_review} response get_completion(prompt) print(response)输出The shipping department should take note that the product arrived a day earlier than expected.4.5 英文批量概括review_1 prod_review # review for a standing lamp review_2 Needed a nice lamp for my bedroom, and this one \ had additional storage and not too high of a price \ point. Got it fast - arrived in 2 days. The string \ to the lamp broke during the transit and the company \ happily sent over a new one. Came within a few days \ as well. It was easy to put together. Then I had a \ missing part, so I contacted their support and they \ very quickly got me the missing piece! Seems to me \ to be a great company that cares about their customers \ and products. # review for an electric toothbrush review_3 My dental hygienist recommended an electric toothbrush, \ which is why I got this. The battery life seems to be \ pretty impressive so far. After initial charging and \ leaving the charger plugged in for the first week to \ condition the battery, Ive unplugged the charger and \ been using it for twice daily brushing for the last \ 3 weeks all on the same charge. But the toothbrush head \ is too small. Ive seen baby toothbrushes bigger than \ this one. I wish the head was bigger with different \ length bristles to get between teeth better because \ this one doesnt. Overall if you can get this one \ around the $50 mark, its a good deal. The manufactuers \ replacements heads are pretty expensive, but you can \ get generic ones thatre more reasonably priced. This \ toothbrush makes me feel like Ive been to the dentist \ every day. My teeth feel sparkly clean! # review for a blender review_4 So, they still had the 17 piece system on seasonal \ sale for around $49 in the month of November, about \ half off, but for some reason (call it price gouging) \ around the second week of December the prices all went \ up to about anywhere from between $70-$89 for the same \ system. And the 11 piece system went up around $10 or \ so in price also from the earlier sale price of $29. \ So it looks okay, but if you look at the base, the part \ where the blade locks into place doesnt look as good \ as in previous editions from a few years ago, but I \ plan to be very gentle with it (example, I crush \ very hard items like beans, ice, rice, etc. in the \ blender first then pulverize them in the serving size \ I want in the blender then switch to the whipping \ blade for a finer flour, and use the cross cutting blade \ first when making smoothies, then use the flat blade \ if I need them finer/less pulpy). Special tip when making \ smoothies, finely cut and freeze the fruits and \ vegetables (if using spinach-lightly stew soften the \ spinach then freeze until ready for use-and if making \ sorbet, use a small to medium sized food processor) \ that you plan to use that way you can avoid adding so \ much ice if at all-when making your smoothie. \ After about a year, the motor was making a funny noise. \ I called customer service but the warranty expired \ already, so I had to buy another one. FYI: The overall \ quality has gone done in these types of products, so \ they are kind of counting on brand recognition and \ consumer loyalty to maintain sales. Got it in about \ two days. reviews [review_1, review_2, review_3, review_4]for i in range(len(reviews)): prompt f Your task is to generate a short summary of a product \ review from an ecommerce site. Summarize the review below, delimited by triple \ backticks in at most 20 words. Review: {reviews[i]} response get_completion(prompt) print(i, response, \n)输出0 Soft and cute panda plush toy loved by daughter, but small for the price. Arrived early. 1 Great lamp with storage, fast delivery, excellent customer service, and easy assembly. Highly recommended. 2 Impressive battery life, but toothbrush head is too small. Good deal if bought around $50. 3 The reviewer found the price increase after the sale disappointing and noticed a decrease in quality over time.五、从示例到实战文本概括的 Prompt 模式总结回顾全文示例可以提炼出四条可复用的 Prompt 设计模式模式Prompt 关键指令适用场景效果长度约束「最多 30 个字 / at most 30 words」简报、面板展示控制输出篇幅便于阅读角度侧重「侧重在快递服务上 / focusing on shipping」部门分工分析重排信息优先级但保留其他信息信息提取「提取……相关的信息 / extract the information relevant to」舆情监控、结构化抽取只保留目标维度过滤其余信息批量概括循环 「最多 20 个词汇」海量评论处理单条聚合为可扫描的摘要列表此外还有三个贯穿始终的工程要点分隔符规范使用三个反引号包裹待处理的评论文本是第二章提示原则中「使用分隔符清晰界定输入的不同部分」的实践能有效避免 Prompt 指令与评论内容混淆长度约束的软性模型按 Token 而非字符计数字数约束为软约束关键场景需在下游做截断校验温度参数概括类任务建议使用较低temperature如 GPT 版默认 0保证输出稳定、可复现get_completion源码与完整 API 参数说明见 docs/C2/C2.md 与 notebook/C2 使用 LLM API 开发应用/C2.ipynb。这套能力在 llm-universe 的后续章节中会持续复用例如构建 RAG 应用时需要为知识库文档生成摘要以加速检索定位构建个人知识库助手时需要对用户问题与检索结果做信息浓缩。掌握本文的概括与提取技巧就掌握了这类应用的信息处理核心环节。更多相关练习可参考同一系列文档 data_base/knowledge_db/prompt_engineering/3. 迭代优化 Iterative.mdPrompt 优化方法与 data_base/knowledge_db/prompt_engineering/5. 推断 Inferring.md从文本中推断结论的进阶技巧。【免费下载链接】llm-universe本项目是一个面向小白开发者的大模型应用开发教程在线阅读地址https://datawhalechina.github.io/llm-universe/项目地址: https://gitcode.com/GitHub_Trending/ll/llm-universe创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表