发布时间:2026/8/25 13:51:40
笔记:面向开发者的提示工程课程(ChatGPT Prompt Engineering for Developers) 提示工程关键原则提示词关键1.写清楚具体的说明2.给模型充足的思考时间3.清楚模型的限制以下样例均基于deepseek需要配置deepseek api key1.写清楚具体的说明1使用分隔符清楚地指示输入的不同部分import os import sys from pathlib import Path from dotenv import load_dotenv from openai import OpenAI if sys.platform win32: sys.stdout.reconfigure(encodingutf-8) # 从本文件所在目录读取 .env load_dotenv(Path(__file__).resolve().parent / .env) api_key os.getenv(DEEPSEEK_API_KEY) if not api_key: raise SystemExit( 未检测到 DEEPSEEK_API_KEY。请在同目录的 .env 里填入\n DEEPSEEK_API_KEY你的DeepSeek密钥 ) # DeepSeek 兼容 OpenAI SDK只需改 base_url 和模型名 client OpenAI( api_keyapi_key, base_urlhttps://api.deepseek.com, ) def get_completion(prompt, modeldeepseek-chat): messages [{role: user, content: prompt}] response client.chat.completions.create( modelmodel, messagesmessages, temperature0, # this is the degree of randomness of the models output ) return response.choices[0].message.content text f You should express what you want a model to do by \ providing instructions that are as clear and \ specific as you can possibly make them. \ This will guide the model towards the desired output, \ and reduce the chances of receiving irrelevant \ or incorrect responses. Dont confuse writing a \ clear prompt with writing a short prompt. \ In many cases, longer prompts provide more clarity \ and context for the model, which can lead to \ more detailed and relevant outputs. prompt f Summarize the text delimited by triple backticks \ into a single sentence. {text} response get_completion(prompt) print(response)得到输出Clear and specific instructions, even if longer, are essential for guiding a model to produce relevant and accurate outputs, as they provide the necessary context and reduce errors.2要求结构化的输出text f prompt f 输出三本书的名称和它的作者以及类型\ 用JSON格式四个关键词书籍号书名作者类型。 {text} response get_completion(prompt) print(response)得到输出json [ { 书籍号: 978-7-5334-1234-5, 书名: 百年孤独, 作者: 加西亚·马尔克斯, 类型: 魔幻现实主义文学 }, { 书籍号: 978-7-5447-5678-9, 书名: 三体, 作者: 刘慈欣, 类型: 科幻小说 }, { 书籍号: 978-7-5063-9012-3, 书名: 活着, 作者: 余华, 类型: 长篇小说 } ] 3要求模型先对条件进行检查当模型做出了假设要求模型先对假设做出校验。你还可以通过考虑边缘的潜在情况要求模型做出特殊化处理。4在要求模型完成任务之前提供已经完成的任务实例。2.给模型充足的思考时间1规定模型完成任务的步骤。text f In a charming village, siblings Jack and Jill set out on a quest to fetch water from a hilltop \ well. As they climbed, singing joyfully, misfortune struck-Jack tripped on a stone and tumbled \ down the hill, with Jill following suit. \ Though slightly battered, the pair returned home to \ comforting embraces. Despite the mishap, their adventurous spirits remained undimmed, and they continued exploring with delight. #example 1 prompt_1 f Perform the following actions: 1 - Summarize the following text delimited by triple \ backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. Output a json object that contains the following keys: french_summary, num_names. Separate your answers with line breaks. Text: {text} response get_completion(prompt_1) print(Completion for prompt 1:) print(response)输出Completion for prompt 1: 1 - In a charming village, siblings Jack and Jill set out to fetch water from a hilltop well, but after Jack tripped and tumbled down the hill with Jill following, they returned home battered yet undimmed in their adventurous spirits. 2 - Dans un charmant village, les frère et sœur Jack et Jill sont partis chercher de l’eau à un puits au sommet d’une colline, mais après que Jack a trébuché et dévalé la colline suivi de Jill, ils sont rentrés chez eux meurtris mais avec un esprit aventureux intact. 3 - Jack, Jill. json { french_summary: Dans un charmant village, les frère et sœur Jack et Jill sont partis chercher de l’eau à un puits au sommet d’une colline, mais après que Jack a trébuché et dévalé la colline suivi de Jill, ils sont rentrés chez eux meurtris mais avec un esprit aventureux intact., num_names: 2 } 2要求模型在得出结论前先得出自己的解决方案。3.了解模型限制1模型并不了解自己知识的边界因此它可能会捏造一些信息来进行回答这种现象被称之为“幻觉”。较为有效的解决方案1.要求模型用输入文本中的相关内容进行回答2.要求模型追溯到自己回答的源文件提示工程需要迭代迭代过程需要找出为什么指令不够清晰或者为什么它没有给模型足够的时间去思考让你改进想法改进提示。并且多次循环最后得到完美的结果。迭代过程1尝试一些方法2分析结果未提供你想要的内容的地方3澄清指令给予更多思考时间4使用一批示例优化提示词总结类应用#example 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) #resultThe panda plush is soft, cute, and loved \ #by the daughter, but its small for the price. \ #It arrived early, which was a bonus.1.可以使用模型生成简洁明了的总结2.可以针对特定对象生成业务中更适用于某个群体的摘要3.还可以摘出重要信息而不是仅仅进行总结推理类应用类似于提取标签提取名字理解文本感情诸如此类的事情。大语言模型很擅长提取特定的文本减轻了传统机器学习中“提炼数据集→进行机器学习→训练出特殊模型”的负担。只需要对模型进行特定的提示词处理。转换类应用1.翻译2.转换格式3.纠正翻译错误校准原文本和模型生成文本的差异扩展类应用1.让模型扮演助理并生成ai文本时让用户知道对话是由ai生成的非常重要2.使用temperature模型的探索程度或随机性变量temperat0时模型的可靠性越高构建一个自定义聊天机器人import os import sys from pathlib import Path from dotenv import load_dotenv from openai import OpenAI if sys.platform win32: sys.stdout.reconfigure(encodingutf-8) # 与 prompt_test 一致先读本目录 .env再读上级目录 .env _script_dir Path(__file__).resolve().parent load_dotenv(_script_dir / .env) load_dotenv(_script_dir.parent / .env) api_key os.getenv(DEEPSEEK_API_KEY) if not api_key: raise SystemExit( 未检测到 DEEPSEEK_API_KEY。请在 .env 里填入\n DEEPSEEK_API_KEY你的DeepSeek密钥 ) # DeepSeek 兼容 OpenAI SDK client OpenAI( api_keyapi_key, base_urlhttps://api.deepseek.com, ) DEFAULT_MODEL deepseek-chat def get_completion(prompt, modelDEFAULT_MODEL): 单条用户 prompt对应图片里的 get_completion。 messages [{role: user, content: prompt}] response client.chat.completions.create( modelmodel, messagesmessages, temperature0, # this is the degree of randomness of the models output ) return response.choices[0].message.content def get_completion_from_messages(messages, modelDEFAULT_MODEL, temperature0): response client.chat.completions.create( modelmodel, messagesmessages, temperaturetemperature, ) return response.choices[0].message.content def chat_loop( system_prompt: str You are a helpful assistant., model: str DEFAULT_MODEL, temperature: float 0.7, ): 交互式聊天维护 messages 历史循环读取用户输入。 messages [{role: system, content: system_prompt}] print(DeepSeek 聊天机器人已启动。输入 quit / exit / q 退出。) print(- * 40) while True: user_input input(你: ).strip() if not user_input: continue if user_input.lower() in {quit, exit, q}: print(再见) break messages.append({role: user, content: user_input}) response get_completion_from_messages( messages, modelmodel, temperaturetemperature ) messages.append({role: assistant, content: response}) print(fAI: {response}\n) if __name__ __main__: chat_loop()

相关新闻

2026/8/25 13:46:40

【python】条件语句

if语法单分支语法if 条件:条件成立执行的代码1条件成立执行的代码2......要点说明a)if后面跟条件表达式,末尾必须加冒号 : ,冒号代表代码块开始。b)冒号下方的代码,必须缩进(一般 4 个空格)&…

2026/8/25 16:12:09

SpringBoot集成测试的最佳实践分享

你盯着那条红色失败用例,昨晚它明明还是绿的。没有改任何业务代码,只是CI环境的数据库连接池比本地小。这是SpringBoot集成测试最常见的噩梦——测试本身成了比被测代码更脆弱的存在。当一套测试需要依赖环境、顺序、甚至运气才能通过时,它就…

2026/8/25 16:12:09

FreeRTOS-之中断管理(Interrupt Management)详解与实战

在嵌入式实时操作系统(RTOS)中,硬件中断(ISR)拥有最高优先级,用于对外部突发事件(如串口接收、按键按下、ADC 采样完成等)做出即时响应。 然而,如果在中断服务函数(ISR)中执行耗时操作,会导致系统中断延迟过长,甚至影响其他高优先级中断的响应。FreeRTOS 提供了完…

2026/8/25 16:12:09

主键索引为什么要有序

数据库索引是什么? B树B树是如何工作的?按排序顺序存储数据如果无序排序/随机uuid会怎么样? 页分裂,碎片化,写入性能下降,空间浪费具体好处:快速查找(二分查找),范围查询(between 、…

2026/8/25 16:12:09

【AI智能体】Codex 插件从使用到项目开发实战操作详解

目录 一、前言 二、Codex 介绍 2.1 Codex 是什么 2.2 Codex能做什么? 2.3 Codex 插件介绍 2.3.1 插件是什么? 2.3.2 插件有什么作用? 2.3.3 Codex 有哪些官方插件? 2.3.4 Codex 插件如何使用和管理 三、Codex 内置插件使…

2026/8/25 16:12:09

如何构建自己的“ Harness ”

干货长文预警。很多人"让 AI 写代码"已经驾轻就熟,但"让 AI 稳定地产出能上生产的代码"是另一回事。这篇文章我拿一个真实例子——RBAC 权限系统(用户-角色-权限)——把完整工作流拆开:每一步为什么这么做、具…

2026/8/25 16:07:08

装修还没开工,AI已经替你把坑踩了一遍

装修这件事,大概是成年人世界里最让人头疼的“必修课”之一。 你刚拿到新房钥匙,满心欢喜地开始看效果图、选风格、谈装修公司。设计师给你看的效果图美得让你心动——无主灯设计、满墙收纳柜、开放式厨房、艺术吊顶……每一个细节都像是理想中家的模样…

2026/8/25 1:04:19

[光学原理与应用-521]:对光的错误理解与纠偏

首先光是一种能量的载体和形态,宏观上观察到的光是由无数个微观的光量子组成的,每个光子在产生的瞬间,其在真空的空间中以确定不变的速度沿着一个初始的方向一直向前,在微观层面,每个光量子的运动轨迹是以波函数所展现…

2026/8/25 11:48:27

SIP通话转接原理与REFER方法实战解析

1. 通话转接不是“挂断再拨号”,而是SIP会话的动态重定向你有没有遇到过这样的场景:客服坐席A正在和客户通电话,突然需要把这通对话无缝转给专家坐席B,客户完全感知不到中间的断连——既没听到忙音,也没被要求重新拨号…

2026/8/24 8:17:29

Kolla-ansible单节点OpenStack部署实战:从环境准备到排坑指南

1. 为什么选择Kolla-ansible来部署单节点OpenStack?如果你正在寻找一种能把OpenStack从“概念”快速变成“可用的实验环境”的方法,那么Kolla-ansible几乎是当前最主流、最省心的选择。我见过太多人卡在手动编译依赖、配置服务、处理版本冲突的泥潭里&am…

2026/8/25 0:04:14

三步把QQ空间历史说说导出到本地:GetQzonehistory 极简指南

三步把QQ空间历史说说导出到本地:GetQzonehistory 极简指南 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory Meta Description:GetQzonehistory 是一个QQ空间历史说…

2026/8/25 0:04:14

洛谷 P7912:[CSP-J 2021 T4] 小熊的果篮 ← 双向链表

【题目来源】 https://www.luogu.com.cn/problem/P7912 【题目描述】 小熊的水果店里摆放着一排 n 个水果。每个水果只可能是苹果或桔子,从左到右依次用正整数 1,2,…,n 编号。连续排在一起的同一种水果称为一个“块”。小熊要把这一排水果挑到若干个果篮里&#x…

2026/8/24 13:42:17

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/24 18:13:48

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/25 1:08:14

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…