恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

generative-ai-for-beginners 课程实践:使用 openai 库构建文本生成应用

  • 首页
  • 资讯中心
  • /
  • generative-ai-for-beginners 课程实践:使用 openai 库构建文本生成应用

相关资讯

Chrome DevTools MCP:让AI真正动手调试前端代码 2026/9/7 19:00:12
XMind电脑版深度使用指南:从核心功能到效率进阶 2026/9/7 18:55:11
ATE-3146静态参数测试机与常规功率半导体测试设备的全面对比 2026/9/7 18:55:11

最新资讯

微信小程序日语词汇学习系统:从全栈设计到Spring Boot后端实现
四合一模型实战:ARIMA、Prophet、LSTM、GRU时间序列预测对比
Bitcoin Core 降低 P2P 流量实战:-maxuploadtarget、-listen、-maxconnections 与 -blocksonly 详解
非对称加密算法(二):DSA
拓推客免费提供打印纸,助力支付宝小蓝环动销
权限检查为什么越来越慢?Casbin匹配器缓存把表达式编译变成一次性开销

今日推荐

基于YOLOv8和PyQt5的麦穗稻穗检测识别系统设计与实现
UL 1642锂电池安全标准全解析:测试项目、认证流程与避坑指南
BS EN 13814-1-2019游乐设施安全标准:设计与制造核心要点解析

本周热门

超人会飞不算本事:系统稳定依赖清晰规则与边界设计
超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论
基于CNN的调制信号识别:MATLAB实现时频图分类实战

本月精选

自研推理加速器Redwood:两周内实现PyTorch模型高效部署的实战教程
V4L2摄像头采集实战:从camera_client.rar到出图全流程解析
从“谁发明了钢琴键”到知识问答智能体:RAG与记忆工程实践

generative-ai-for-beginners 课程实践:使用 openai 库构建文本生成应用

发布时间:2026/9/7 19:00:12
generative-ai-for-beginners 课程实践:使用 openai 库构建文本生成应用 generative-ai-for-beginners 课程实践使用 openai 库构建文本生成应用【免费下载链接】generative-ai-for-beginners21 Lessons, Get Started Building with Generative AI项目地址: https://gitcode.com/GitHub_Trending/ge/generative-ai-for-beginners本篇基于 generative-ai-for-beginners 课程第 6 课《Building Text Generation Applications》完整覆盖从概念到落地的全过程理解文本生成应用与传统控制台/UI 应用的区别使用 Pythonopenai库完成客户端配置含 Azure OpenAI 接入方式并逐步实现一个可交互的“食谱生成器”实战项目掌握 prompt 设计、两段式提示、max_tokens与temperature等核心生成参数的调优方法。读完后你可以独立搭建自己的文本生成应用并了解仓库中配套示例代码食谱生成、学习伙伴、历史人物问答的完整实现。1. 什么是文本生成应用传统应用的接口通常有两类命令式应用Command-based控制台应用是典型代表你输入一条命令程序执行对应任务。例如git就是一个命令式应用。图形界面应用UI/GUI点击按钮、输入文本、选择选项等。1.1 控制台与 UI 应用的局限对比一个命令式应用你会发现它有两个天然限制能力受限你只能输入应用预先支持的命令无法随意输入任意指令。语言特定部分应用虽然支持多语言但默认是围绕某一特定语言构建的即使后续可以扩展。1.2 文本生成应用的优势文本生成应用的交互方式完全不同更灵活你不受固定命令集或特定输入语言约束可以直接用自然语言与应用交互。数据面更广你实际上是在与一个在海量语料上训练过的数据源交互而传统应用往往只能访问数据库里已有的数据。1.3 可以构建哪些应用课程给出了三类典型方向聊天机器人Chatbot例如回答关于你所在公司和产品的问答。助手HelperLLM 擅长文本摘要、从文本中提取洞察、生成简历等文本。代码助手Code assistant取决于你使用的语言模型可以构建辅助写代码的助手例如 GitHub Copilot、ChatGPT 等。2. 如何接入 LLMAPI 还是 SDK把 LLM 能力加进应用通常需要以下两种途径之一直接调用 API手工构造带 prompt 的 Web 请求拿回生成文本。使用 SDK/库库把 API 请求封装起来使用起来更简单。课程列举了几类常见库openai底层库方便连接模型并发送 prompt。本课程的所有示例都基于它。Langchain更高层的知名框架支持 Python。Semantic Kernel微软的 SDK支持 C#、Python 和 Java。3. 第一个 openai 应用安装与资源准备3.1 安装 openai与 OpenAI 或 Azure OpenAI 交互的库有很多可选 C#、Python、JavaScript、Java 等多种语言。课程选择 Python 的openai库用pip安装pip install openai仓库中本课配套的依赖清单 requirements.txt 固定了两个依赖版本可以复现课程的运行环境openai1.55.1 python-dotenv1.2.23.2 创建 Azure 资源按课程文档接入 Azure OpenAI 需要依次完成在 Azure 官网创建免费账户申请 Azure OpenAI 的访问权限文档注明撰写时申请访问仍需审批安装 Python可从 Python 官网下载创建 Azure OpenAI Service 资源Azure 官方文档有“创建资源”的门户操作指南。3.3 找到 API 密钥与端点进入 Azure OpenAI 资源的“Keys and Endpoint”页面复制“Key 1”的值即可得到你的 API 密钥同页面还能看到资源端点endpoint。值得把 API 密钥与代码分离标准做法是使用环境变量export OPENAI_API_KEYsk-...3.4 面向 Azure 的客户端配置翻译版文档保留了旧版 SDKopenai 0.x的配置写法用于理解各参数的含义openai.api_type azure openai.api_key os.environ[OPENAI_API_KEY] openai.api_version 2023-05-15 openai.api_base os.getenv(API_BASE)api_type azure告诉库使用 Azure OpenAI 而非 OpenAIapi_keyAzure 门户中的 API 密钥api_version要使用的 API 版本撰写时最新为2023-05-15api_baseAPI 端点与 API 密钥同在门户中可查。os.getenv用于读取环境变量如OPENAI_API_KEY、API_BASE你可以在终端中设置也可以用dotenv之类的库从文件加载。仓库源码中的现行写法本课的配套代码已经迁移到新版 SDK 的OpenAI客户端直接指向 Azure OpenAI 的/openai/v1/稳定端点不再需要管理api_version。见 aoai-app.py# configure the OpenAI client against the Azure OpenAI (Microsoft Foundry) v1 endpoint client OpenAI( api_keyos.environ[AZURE_OPENAI_API_KEY], base_urlf{os.environ[AZURE_OPENAI_ENDPOINT].rstrip(/)}/openai/v1/, ) deployment os.environ[AZURE_OPENAI_DEPLOYMENT]这里涉及三个环境变量AZURE_OPENAI_API_KEY密钥、AZURE_OPENAI_ENDPOINT资源端点、AZURE_OPENAI_DEPLOYMENT模型部署名。而面向原生 OpenAI 的 oai-app.py 则更简洁只需默认客户端加一个模型名client OpenAI() deployment gpt-4o-mini4. 生成文本Completion 与 ChatCompletion文档介绍了两个生成入口。文本补全Completion——文档给出的经典示例prompt Complete the following: Once upon a time there was a completion openai.Completion.create(modeldavinci-002, promptprompt) print(completion.choices[0].text)创建“补全”对象时传入要使用的模型和 prompt随后打印生成的文本。对话补全ChatCompletion——更适合聊天机器人场景import openai openai.api_key sk-... completion openai.ChatCompletion.create(modelgpt-3.5-turbo, messages[{role: user, content: Hello world}]) print(completion.choices[0].message.content)多轮对话的完整机制在课程第 7 课聊天应用中展开。仓库源码中的现行写法配套代码已改用 Responses API 的client.responses.create方法通过response.output_text读取结果并用storeFalse避免服务端留存本次响应。见 aoai-app.pyprompt Complete the following: Once upon a time there was a # make a request using the Responses API response client.responses.create(modeldeployment, inputprompt, storeFalse) # print response print(response.output_text)该示例的典型输出very unhappy _____. Once upon a time there was a very unhappy mermaid.5. 练习你的第一个文本生成应用5.1 创建虚拟环境并安装依赖python -m venv venv source venv/bin/activate pip install openai注意在 Windows 上应使用venv\Scripts\activate代替source venv/bin/activate。在 Azure 门户中搜索Open AI选择你的资源进入Keys and Endpoint复制Key 1的值即可拿到 Azure OpenAI 密钥。5.2 编写 app.py文档给出的起点版本沿用旧 SDK 配置风格便于理解各参数import openai openai.api_key replace this value with your open ai key or Azure OpenAI key openai.api_type azure openai.api_version 2023-05-15 openai.api_base endpoint found in Azure Portal where your API key is deployment_name deployment name # add your completion code prompt Complete the following: Once upon a time there was a messages [{role: user, content: prompt}] # make completion completion openai.chat.completions.create(modeldeployment_name, messagesmessages) # print response print(completion.choices[0].message.content)注意使用 Azure OpenAI 时api_type必须为azureapi_key填 Azure OpenAI 密钥如果直接走 OpenAI 而非 Azure则不需要base_urlmodel传模型名如gpt-4o-mini即可。预期输出与第 4 节一致very unhappy _____. Once upon a time there was a very unhappy mermaid.仓库中对应的完整可运行实现是 aoai-app.py它把密钥、端点、部署名全部改为从.env文件加载的环境变量load_dotenv()避免把秘密写死在代码里。6. 不同任务不同的提示现在你已经拥有了一个可反复修改的程序。Prompt 几乎可以用于任何任务课程列举了三类生成特定类型的文本例如生成一首诗、一套测验题等。信息查询例如 “What does CORS mean in web development?”CORS 在 Web 开发中是什么意思。生成代码例如生成一个校验邮箱的正则表达式甚至生成一个完整的 Web 应用。7. 实战案例食谱生成器假设你家里有若干食材想做饭需要一份食谱。除了用搜索引擎也可以把这件事交给 LLM。第一条 prompt 可以是“Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used” 给我 5 个用鸡肉、土豆、胡萝卜做的菜式食谱每个食谱列出用到的全部食材可能得到的回答节选前两条完整 5 条与原文档一致1. Roasted Chicken and Vegetables: Ingredients: - 4 chicken thighs - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 2 tablespoons olive oil - 2 cloves garlic, minced - 1 teaspoon dried thyme - 1 teaspoon dried oregano - Salt and pepper, to taste 2. Chicken and Potato Stew: Ingredients: - 2 tablespoons olive oil - 1 onion, diced - 2 cloves garlic, minced - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 teaspoon dried oregano - 1 teaspoon dried thyme - 1 cup chicken broth - Salt and pepper, to taste 3. Chicken and Potato Bake / 4. Chicken and Potato Soup / 5. Chicken and Potato Hash 结构类似这个结果不错但还有两个有价值的改进点过滤掉不喜欢或对之过敏的食材生成采购清单Shopping List考虑家里已有的食材。于是追加一条 prompt“Please remove recipes with garlic as Im allergic and replace it with something else. Also, please produce a shopping list for the recipes, considering I already have chicken, potatoes and carrots at home.” 我过敏请去掉含大蒜的食谱并替换成别的同时生成采购清单我已经家里有鸡肉、土豆、胡萝卜。新结果中所有食谱都不再出现大蒜并且末尾附上了采购清单...5 个不含大蒜的食谱结构同上 Shopping List: - Olive oil - Onion - Thyme - Oregano - Salt - Pepper8. 练习用代码构建食谱生成器8.1 固定 Prompt 的第一步以app.py为起点把prompt变量改成prompt Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used运行后输出类似-Chicken Stew with Potatoes and Carrots: 3 tablespoons oil, 1 onion, chopped, 2 cloves garlic, minced, ... -Oven-Roasted Chicken with Potatoes and Carrots: 3 tablespoons extra-virgin olive oil, ... -Chicken, Potato, and Carrot Casserole: cooking spray, 1 large onion, chopped, ... -One Pot Chicken and Potato Dinner: 2 tablespoons olive oil, 1 pound boneless, skinless chicken thighs, ... -Chicken, Potato, and Carrot Curry: 1 tablespoon vegetable oil, 1 large onion, chopped, ...注意LLM 是非确定性的每次运行可能得到不同结果。8.2 让程序灵活动态输入为了让食谱数量和食材可配置用input()接收用户输入并用 f-string 插值no_recipes input(No of recipes (for example, 5): ) ingredients input(List of ingredients (for example, chicken, potatoes, and carrots): ) # interpolate the number of recipes into the prompt an ingredients prompt fShow me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used测试运行示例No of recipes (for example, 5): 3 List of ingredients (for example, chicken, potatoes, and carrots): milk,strawberries -Strawberry milk shake: milk, strawberries, sugar, vanilla extract, ice cubes -Strawberry shortcake: milk, flour, baking powder, sugar, salt, unsalted butter, strawberries, whipped cream -Strawberry milk: milk, strawberries, sugar, vanilla extract8.3 增加过滤条件在 prompt 末尾追加过滤条件并捕获用户输入filter input(Filter (for example, vegetarian, vegan, or gluten-free): ) prompt fShow me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used, no {filter}示例运行输入no milk过滤牛奶No of recipes (for example, 5): 3 List of ingredients (for example, chicken, potatoes, and carrots): onion,milk Filter (for example, vegetarian, vegan, or gluten-free): no milk 1. French Onion Soup Ingredients: 1 large onion, sliced / 3 cups beef broth / ... 2. Onion and Potato Soup Ingredients: 1 large onion, chopped / 2 cups potatoes, diced / ... 3. Creamy Onion Soup Ingredients: 1 large onion, chopped / 3 cups vegetable broth / ... 每条食谱均附 Instructions 步骤课程也提醒如果用户乳糖不耐可能希望连含奶酪的食谱也过滤掉所以过滤条件表述越清晰结果越符合预期。8.4 两段式提示生成采购清单与其在一条 prompt 里塞进所有需求不如拆成两段并把第一段的结果作为第二段 prompt 的上下文old_prompt_result completion.choices[0].message.content prompt Produce a shopping list for the generated recipes and please dont include ingredients that I already have. new_prompt f{old_prompt_result} {prompt} messages [{role: user, content: new_prompt}] completion openai.Completion.create(enginedeployment_name, messagesmessages, max_tokens1200) # print response print(Shopping list:) print(completion.choices[0].message.content)两个关键点拼接上下文new_prompt f{old_prompt_result} {prompt}把第一段的生成结果前置到第二段 prompt 中模型才能“看见”刚生成的食谱调整 token 预算因为新 prompt 更长含上一段结果第二段显式设置max_tokens1200。完整运行示例No of recipes (for example, 5): 2 List of ingredients (for example, chicken, potatoes, and carrots): apple,flour Filter (for example, vegetarian, vegan, or gluten-free): sugar -Apple and flour pancakes: 1 cup flour, 1/2 tsp baking powder, ... -Apple fritters: 1-1/2 cups flour, 1 tsp baking powder, ... Shopping list: -Flour, baking powder, baking soda, salt, sugar, egg, buttermilk, butter, apple, nutmeg, cinnamon, allspice仓库源码中的成品实现aoai-app-recipe.py 是上述练习的完整版且在两点上比练习代码更进一步输入校验防注入validate_number_input将食谱数量限制在 1–20L18-L26validate_text_input限制长度、并剔除非字母数字字符与{}[]等符号以缓解提示注入风险L28-L37分段控制生成参数食谱生成用max_output_tokens600, temperature0.1L65采购清单用max_output_tokens600, temperature0L78——即“越需要稳定的输出温度越低”。response client.responses.create(modeldeployment, inputprompt, max_output_tokens600, temperature0.1, storeFalse) # ... prompt_shopping Produce a shopping list, and please dont include ingredients that I already have at home: new_prompt fGiven ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping} response client.responses.create(modeldeployment, inputnew_prompt, max_output_tokens600, temperature0, storeFalse)9. 改进你的设置密钥、Token 预算与温度代码能跑起来之后还有三件事值得做。9.1 把密钥与代码分离API 密钥属于秘密信息不应写在代码里。用环境变量 python-dotenv从.env文件加载.env直接调用 OpenAI 时OPENAI_API_KEYsk-....envAzure OpenAI 时AZURE_OPENAI_API_KEYreplace AZURE_OPENAI_ENDPOINTreplace AZURE_OPENAI_API_VERSION2024-10-21代码中加载import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() client OpenAI(api_keyos.environ[OPENAI_API_KEY])注意对 Azure OpenAI 而言还需要base_url指向资源端点的/openai/v1/完整示例见 aoai-app.py。9.2 Token 长度Token 意味着成本应尽量精简。控制生成 token 数response client.responses.create(modeldeployment, inputprompt, max_output_tokens100, storeFalse)旧 SDK 中对应参数为max_tokens新版 Responses API 使用max_output_tokens。也可以在 prompt 措辞上想办法减少不必要的 token 消耗。9.3 温度Temperature温度决定输出的随机程度温度越高输出越随机温度越低输出越可预测。要根据场景决定是否需要多样性response client.responses.create(modeldeployment, inputprompt, temperature0.5, storeFalse)数值越接近 1.0输出越多样。10. 作业与参考实现课程给了三个方向继续打磨食谱生成器尝试不同的 temperature 与 prompt构建“学习伙伴Study Buddy”回答某个主题如 Python的问题构建“历史人物机器人History Bot”让模型扮演某位历史人物并回答关于其生平时代的问题。10.1 学习伙伴文档给出的起始 prompt- Youre an expert on the Python language Suggest a beginner lesson for Python in the following format: Format: - concepts: - brief explanation of the lesson: - exercise in code with solutions仓库中的成品 aoai-study-buddy.py 展示了如何把它变成结构化系统提示读取用户问题后要求模型按 “Concept / Example code / explanation” 三段式格式作答question input(Ask your questions on python language to your study buddy: ) prompt f You are an expert on the python language. Whenever certain questions are asked, you need to provide response in below format. - Concept - Example code showing the concept implementation - explanation of the example and how the concept is done for the user to understand better. Provide answer for the question: {question} response client.responses.create(modeldeployment, inputprompt, storeFalse)10.2 历史人物机器人文档给出的示例 prompt- You are Abe Lincoln, tell me about yourself in 3 sentences, and respond using grammar and words like Abe would have used - You are Abe Lincoln, respond using grammar and words like Abe would have used: Tell me about your greatest accomplishments, in 300 words成品 aoai-history-bot.py 在此基础上加入了两处工程化约束人物由用户输入persona并在系统提示中明确禁止模型自行编造——“Dont create content yourself. If you dont know something, tell that you dont remember.”这对角色扮演类应用控制幻觉很关键persona input(Tell me the historical character I want to be: ) question input(Ask your question about the historical character: ) prompt f You are going to play as a historical character {persona}. Whenever certain questions are asked, you need to remember facts about the timelines and incidents and respond the accurate answer only. Dont create content yourself. If you dont know something, tell that you dont remember. Provide answer for the question: {question} 11. 知识检查与挑战知识检查temperature温度这个概念的作用是什么控制输出的随机程度。控制响应的大小。控制使用的 token 数。答案是 1温度控制的是输出的随机性响应大小与 token 数由max_tokens/max_output_tokens一类参数控制。挑战在做作业的过程中尝试把 temperature 分别设为 0、0.5、1观察并思考哪个值最适合你的应用。记住0 是多样性最低的1 是多样性最高的。12. 小结与后续本课建立了一条完整链路从pip install openai、密钥与端点配置到responses.create的最小调用再到“食谱生成器”的动态 prompt、两段式提示上文拼接 token 预算以及温度/token 调优。仓库中本课的配套代码集中在 06-text-generation-apps/python 目录可按oai-*原生 OpenAI与aoai-*Azure OpenAI两套对照阅读。下一步请进入第 7 课学习如何构建聊天应用。【免费下载链接】generative-ai-for-beginners21 Lessons, Get Started Building with Generative AI项目地址: https://gitcode.com/GitHub_Trending/ge/generative-ai-for-beginners创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号