{"id":15014461,"url":"https://github.com/windvalley/chatgpt-prompt-practice","last_synced_at":"2026-02-09T10:31:13.562Z","repository":{"id":163354634,"uuid":"638734371","full_name":"windvalley/chatgpt-prompt-practice","owner":"windvalley","description":"Learn prompt engineering skills to improve work efficiency.","archived":false,"fork":false,"pushed_at":"2023-05-11T02:23:33.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-19T17:09:48.328Z","etag":null,"topics":["chatgpt","ipython","jupyter-notebook","prompt"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/windvalley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-10T01:55:35.000Z","updated_at":"2023-05-10T09:19:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"22a9afc4-c707-445d-8d55-597d7256fb04","html_url":"https://github.com/windvalley/chatgpt-prompt-practice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Fchatgpt-prompt-practice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Fchatgpt-prompt-practice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Fchatgpt-prompt-practice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Fchatgpt-prompt-practice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/windvalley","download_url":"https://codeload.github.com/windvalley/chatgpt-prompt-practice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243313410,"owners_count":20271178,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","ipython","jupyter-notebook","prompt"],"created_at":"2024-09-24T19:45:39.757Z","updated_at":"2026-02-09T10:31:12.310Z","avatar_url":"https://github.com/windvalley.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chatgpt-prompt-practice\n\n学习 prompt 工程技能, 提高工作效率.\n\n## 快速开始\n\n### 配置 API KEY\n\n\u003e 前提:\n\u003e\n\u003e - 你可以魔法上网\n\u003e - 有 chatgpt api key\n\u003e - 系统上有 python3 环境\n\n```sh\n$ git clone --depth 1 https://github.com/windvalley/chatgpt-prompt-practice.git\n\n# NOTE: 本文档之后的所有操作均在此目录下进行\n$ cd chatgpt-prompt-practice\n\n# 这里的sk-开头的字符串即是你的api key, NOTE: 这里是假的key, 需要你自己去申请.\n$ echo 'OPENAI_API_KEY=\"sk-mywzQ7Pc1ijBWVQTMpacT3BlbkF8YaLJdn8SaXcrT9alfTk6\"' \u003e .env\n```\n\n### 使用 ipython 或 jupyter notebook\n\n可以按你的习惯在终端使用`ipython`或在浏览器中使用 `jupyter notebook` 来运行代码.\n\n二者选其一即可.\n\n#### 使用 ipython\n\n```sh\n# 安装\n$ pip3 install ipython\n$ pip3 install openai\n$ pip3 install -U python-dotenv\n\n# 运行\n$ ipython\n\n# 在ipython终端运行env.py文件, 加载openai相关环境和配置,\n# 为之后使用chatgpt提供相关配置和函数.\nIn [1]: %run env.py\n```\n\nTips:\n\n1. 通过在代码行尾, 使用 ` \\` + `enter`键来换行, 实现输入多行代码,\n   类似于`jupyter notebook`直接通过`enter`键来换行.\n   注意换行后还要再按`enter`键输入一个空行才可以继续输入代码行,\n   否则运行代码将报错.\n\n#### 使用 jupyter notebook\n\n```sh\n# 安装\n$ pip3 install jupyter\n$ pip3 install openai\n$ pip3 install -U python-dotenv\n\n# 运行如下命令行, 会弹出默认浏览器运行 http://localhost:8888/tree,\n# 新建notebook, 后续操作类似ipython.\n$ jupyter notebook\n\n# 在notebook界面, 运行env.py文件, 为之后使用chatgpt提供相关配置和函数.\nIn [  ]: %run env.py\n```\n\nTips:\n\n1. 相比 `ipython` 的优势:\n\n- 历史输入输出有保存到 notebook 文件, 方便回顾历史;\n- 可以渲染 html、markdown 等源码\n\n  ```python\n  # 表格是以 HTML 格式呈现的，加载出来\n  from IPython.display import display, HTML\n\n  display(HTML(response))\n  ```\n\n### 提问示例\n\n运行`ipython`或`jupyter notebook`, 执行完 `%run env.py` 后, 再按以下模版输入内容并运行, 将得到模型的回答.\n\n```python\ntext = f\"\"\"\n我希望你充当 IT 专家。我会向您提供有关我的技术问题所需的所有信息，而您的职责是解决我的问题。你应该使用你的项目管理知识，敏捷开发知识来解决我的问题。在您的回答中使用适合所有级别的人的智能、简单和易于理解的语言将很有帮助。用要点逐步解释您的解决方案很有帮助。我希望您回复解决方案，而不是写任何解释。我的第一个问题是:\n\"\"\"\n\nprompt = f\"\"\"\n'''{text}'''\nnvim报错:\nError executing lua callback: ...m/HEAD-2ef9d2a_1/share/nvim/runtime/lua/vim/lsp/sync.lua:66: i\nndex out of range\nstack traceback:\n        [C]: in function 'str_utfindex'\n\"\"\"\n\nresponse = get_completion(prompt)\nprint(response)\n```\n\n## 提示工程\n\n### 模型局限性\n\nLLM 偶尔会给出虚假知识(幻觉), 会生成一些看似真实实则编造的知识, 这是模型已知的一个弱点.\n\n暂时的解决策略是先要求模型找到文本中的任何相关引用，然后要求它使用这些引用来回答问题，这种追溯源文档的方法通常对减少幻觉非常有帮助。\n\n### 提示原则\n\n#### 原则 1. 编写清晰、具体的指令\n\n更长的提示实际上更清晰且提供了更多上下文，这实际上可能导致更详细更相关的输出。\n\n策略:\n\n- 使用分隔符清晰地表示输入的不同部分，分隔符可以是：\\`\\`\\`，\"\"，\u003c\u003e，\u003ctag\u003e，\u003c\\tag\u003e等\n\n  ```python\n  text = f\"\"\"\n  some detail text\n  \"\"\"\n  prompt = f\"\"\"\n  具体让chatgpt如何做的话术.\n  '''{text}'''\n  \"\"\"\n  ```\n\n- 要求一个结构化的输出，可以是 Json、HTML 等格式\n\n- 要求模型检查是否满足条件, 不满足该如何处理\n\n- 提供少量示例, 让模型模仿你的示例\n\n#### 原则 2. 给模型时间去思考\n\n您可以指示模型花更多时间思考问题，这意味着它在任务上花费了更多的计算资源。\n\n策略:\n\n- 指定完成任务所需的步骤\n\n- 指导模型在下结论之前找出一个自己的解法. 意思是先让模型推导出一个自己的解法, 再和你给出的解法进行比较, 最后再给出结论.\n\n### 迭代式提示\n\n只要您有一个好的迭代过程来不断改进您的 Prompt，那么你就能够得到一个适合任务的 Prompt。\n\n在编写 Prompt 以使用 LLM 开发应用程序时:\n\n- 首先, 您有一个关于要完成的任务的想法，可以尝试编写第一个 Prompt，满足上一章说过的两个原则：清晰明确，并且给模型足够的时间思考。\n\n- 然后您可以运行它并查看结果。\n\n- 如果第一次效果不好，那么迭代的过程就是找出为什么指令不够清晰或为什么没有给算法足够的时间思考，以便改进想法、改进提示等等.\n\n- 循环多次，直到找到适合您的应用程序的 Prompt。\n\n### 文本概括\n\n目前 LLM 在文本概括任务上展现了强大的水准.\n\n举例:\n\n```python\nprompt = f\"\"\"\n你的任务是从电子商务网站上生成一个产品评论的简短摘要。\n\n请对三个单引号之间的评论文本进行概括，最多30个词汇，并且聚焦在产品价格和质量上。\n\n评论: '''{prod_review_zh}'''\n\"\"\"\n```\n\n策略:\n\n- 限制输出文本长度\n- 关键角度侧重\n- 关键信息提取, 如果想过滤掉不想要的信息, 则用关键字`提取`代替`概括`\n\n### 推断\n\n可以看作是模型接收文本作为输入并执行某种分析的过程。\n\n```python\nprompt = f\"\"\"\n以下用三个单引号分隔的产品评论的情感是什么？\n\n评论文本: '''{lamp_review_zh}'''\n\"\"\"\n```\n\n```python\nprompt = f\"\"\"\n以下用三个反引号分隔的产品评论的情感是什么？\n\n用一个单词回答：「正面」或「负面」。\n\n评论文本: '''{lamp_review_zh}'''\n\"\"\"\n```\n\n### 文本转换\n\nLLM 非常擅长将输入转换成不同的格式，例如多语种文本翻译、拼写及语法纠正、语气调整、格式转换等。\n\n1. 翻译+输出语气\n\n   ````python\n   text = f\"\"\"\n   Would you like to order a pillow?\n   \"\"\"\n   prompt = f\"\"\"\n   请将以下文本翻译成中文，分别展示成正式与非正式两种语气:\n   ```{text}```\n   \"\"\"\n   response = get_completion(prompt)\n   print(response)\n   ````\n\n2. 语气/风格调整\n\n   ````python\n   text = f\"\"\"\n   小老弟，我小羊，上回你说咱部门要采购的显示器是多少寸来着？\n   \"\"\"\n   prompt = f\"\"\"\n   将以下文本翻译成商务信函的格式:\n   ```{text}```\n   \"\"\"\n   response = get_completion(prompt)\n   print(response)\n   ````\n\n3. ChatGPT 非常擅长不同格式之间的转换，例如 JSON 到 HTML、XML、Markdown 等\n\n   ```python\n   data_json = { \"resturant employees\" :[\n       {\"name\":\"Shyam\", \"email\":\"shyamjaiswal@gmail.com\"},\n       {\"name\":\"Bob\", \"email\":\"bob32@gmail.com\"},\n       {\"name\":\"Jai\", \"email\":\"jai87@gmail.com\"}\n   ]}\n   prompt = f\"\"\"\n   将以下Python字典从JSON转换为HTML表格，保留表格标题和列名：{data_json}\n   \"\"\"\n   response = get_completion(prompt)\n   print(response)\n\n   # 渲染(不适用于ipython, 适用于jupyter notebook)\n   from IPython.display import display, Markdown, Latex, HTML, JSON\n   display(HTML(response))\n   ```\n\n4. 拼写及语法纠正\n\n   输入原始文本，输出纠正后的文本，并基于 Redlines 输出纠错过程。\n\n   ````python\n   # 如未安装redlines，需先安装\n   !pip3 install redlines\n\n   text = f\"\"\"\n   Got this for my daughter for her birthday cuz she keeps taking \\\n   mine from my room.  Yes, adults also like pandas too.  She takes \\\n   it everywhere with her, and it's super soft and cute.  One of the \\\n   ears is a bit lower than the other, and I don't think that was \\\n   designed to be asymmetrical. It's a bit small for what I paid for it \\\n   though. I think there might be other options that are bigger for \\\n   the same price.  It arrived a day earlier than expected, so I got \\\n   to play with it myself before I gave it to my daughter.\n   \"\"\"\n   prompt = f\"校对并更正以下商品评论：```{text}```\"\n   response = get_completion(prompt)\n   print(response)\n\n   from redlines import Redlines\n   from IPython.display import display, Markdown\n\n   diff = Redlines(text,response)\n   display(Markdown(diff.output_markdown))\n   ````\n\n5. 综合样例：文本翻译+拼写纠正+风格调整+格式转换\n\n   ````python\n   text = f\"\"\"\n   Got this for my daughter for her birthday cuz she keeps taking \\\n   mine from my room.  Yes, adults also like pandas too.  She takes \\\n   it everywhere with her, and it's super soft and cute.  One of the \\\n   ears is a bit lower than the other, and I don't think that was \\\n   designed to be asymmetrical. It's a bit small for what I paid for it \\\n   though. I think there might be other options that are bigger for \\\n   the same price.  It arrived a day earlier than expected, so I got \\\n   to play with it myself before I gave it to my daughter.\n   \"\"\"\n   prompt = f\"\"\"\n   针对以下三个反引号之间的英文评论文本，\n   首先进行拼写及语法纠错，\n   然后将其转化成中文，\n   再将其转化成优质淘宝评论的风格，从各种角度出发，分别说明产品的优点与缺点，并进行总结。\n   润色一下描述，使评论更具有吸引力。\n   输出结果格式为：\n   【优点】xxx\n   【缺点】xxx\n   【总结】xxx\n   注意，只需填写xxx部分，并分段输出。\n   将结果输出成Markdown格式。\n   ```{text}```\n   \"\"\"\n   response = get_completion(prompt)\n\n   from IPython.display import display, Markdown\n   display(Markdown(response))\n   ````\n\n### 文本扩展\n\n扩展是将短文本，例如一组说明或主题列表，输入到大型语言模型中，让模型生成更长的文本，例如基于某个主题的电子邮件或论文。\n\n温度系数如何使用:\n\n- 可以将温度视为模型探索或随机性的程度。在构建需要可预测响应的应用程序时，我建议使用温度为零。\n- 如果您尝试以更具创意的方式使用模型，可能需要更广泛地输出不同的结果，那么您可能需要使用更高的温度。您几乎可以将其视为在更高的温度下，助手更易分心，但也许更有创造力。\n\n  ```python\n  # 将温度参数从0调整为0.7\n  response = get_completion(prompt, t=0.7)\n  print(response)\n  ```\n\n### 聊天机器人\n\n可以使用 LLM 来构建一个定制的聊天机器人，只需要很少的工作量。\n\n由于涉及到图型界面, 所以必须用 `jupyter notebook` 来演示:\n\n```python\n# 安装GUI\nIn [  ]: !pip install panel\n\n# 加载聊天机器人\nIn [  ]: %run chat_robot.py\n\n# 运行对话框, 并进行对话查看效果\nIn [  ]: dashboard\n```\n\n具体实现方式请参考 [chat_robot.py](chat_robot.py) 脚本, 可以通过修改 `context` 变量改变系统消息内容, 来确定你要实现什么样的聊天机器人.\n\n## 参考文档\n\n- https://github.com/datawhalechina/prompt-engineering-for-developers\n- https://github.com/datawhalechina/hugging-llm\n- https://github.com/rockbenben/ChatGPT-Shortcut\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindvalley%2Fchatgpt-prompt-practice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwindvalley%2Fchatgpt-prompt-practice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindvalley%2Fchatgpt-prompt-practice/lists"}