构建文本生成应用:OpenAI SDK、Prompt 与参数调优实战指南(Generative AI for Beginners 第 6 课)
2026/9/8 21:20:18 网站建设 项目流程

构建文本生成应用:OpenAI SDK、Prompt 与参数调优实战指南(Generative AI for Beginners 第 6 课)

【免费下载链接】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》(源文档见 translations/en/06-text-generation-apps/README.md),面向希望把大语言模型能力集成进自有应用的开发者。读完本文,你将掌握文本生成应用与传统命令行/UI 应用的本质区别,学会基于openai库完成安装、密钥配置与首次文本生成,理解 Prompt、Completions、max_tokens 与 temperature 等核心参数的作用,并基于本仓库提供的源码一步步构建一个可交互、可过滤、能生成购物清单的"食谱生成器",同时了解学习伴侣(Study Buddy)与历史人物对话机器人(History Bot)等扩展方向。

一、什么是文本生成应用:为什么它与传统应用不同

我们平时构建的大多数应用都有一类"固定"的交互界面:

  • 命令式(Command-based):在控制台输入命令,程序执行对应任务。git就是典型的命令式应用。
  • 图形界面(UI):通过点击按钮、输入文本、勾选选项来操作,例如各类 GUI 程序。

控制台应用与 UI 应用的天花板

把上面的形态与"输入一句话"的方式对比,能看到两类明显局限:

  • 能力受限:你只能输入应用预先支持的命令,超出支持范围就无能为力。
  • 语言绑定:虽然部分应用支持多语言,但默认通常只为某种特定语言设计,需要额外工作才能扩展语言支持。

文本生成应用的优势

文本生成应用恰好打破这两点:

  1. 更灵活的自然语言交互:用户不受固定命令集或特定输入语言约束,可以用自然语言自由表达意图。
  2. 海量知识底座:应用对接的是经过海量语料训练的大语言模型(LLM),而不是被应用自身数据库内容所局限。

你能用它构建什么

  • 聊天机器人(Chatbot):围绕企业、产品等主题回答用户问题的问答机器人。
  • 内容助手(Helper):LLM 擅长文本摘要、要点抽取、简历等文案生成类任务。
  • 代码助手(Code Assistant):借助合适的语言模型,构建帮助开发者写代码的助手(如 GitHub Copilot、ChatGPT 的编码能力)。

二、如何接入 LLM:API 与库/SDK 的选择

通常有两种集成路径:

  • 直接调用 API:自己构造携带 Prompt 的 Web 请求,并从响应中解析生成的文本。
  • 使用库/SDK:库把 API 调用封装起来,使用更简单、代码更少。

课程重点介绍的是openai库——它负责连接模型并发起 Prompt 请求。除此之外,也存在更高层的编排型框架:

  • LangChain:广受欢迎的 Python 生态编排库。
  • Semantic Kernel:微软出品的 SDK,支持 C#、Python、Java。

三、准备工作:安装依赖、创建资源与获取密钥

1. 安装 openai 库

Python 环境下的安装命令如下:

pip install openai

本仓库的示例锁定了一套可复现的依赖版本,见 06-text-generation-apps/python/requirements.txt:

openai==1.55.1 python-dotenv==1.2.2

也就是说,仓库实际运行环境使用openai SDK v1.x(1.55.1),并依赖python-dotenv从本地.env文件加载环境变量。下面所有现代写法都以 v1.x 客户端 API 为准。

2. 创建模型资源并获取密钥与端点

  • 若使用Azure OpenAI:先创建 Azure 账号并申请开通 Azure OpenAI 服务,随后在 Azure 门户中新建 "Azure OpenAI Service" 资源。完成部署后,进入资源的"Keys and Endpoint"(密钥和终结点)页面,复制Key 1作为 API 密钥,同时记下相邻的 Endpoint(API Base)地址。
  • 若使用OpenAI官方服务:在对应平台创建 API Key,通常是sk-...形式。

[!NOTE] 课程撰写时 Azure OpenAI 仍需先提交访问申请,实际环境以官方最新流程为准。

3. 密钥不要硬编码:使用环境变量

最佳实践是把 API 密钥与代码分离,例如通过环境变量注入。bash 下这样设置:

export OPENAI_API_KEY='sk-...'

而仓库示例代码在 Azure OpenAI 场景下约定读取一组以AZURE_OPENAI_为前缀的环境变量(见 aoai-app.py 的用法):

  • AZURE_OPENAI_API_KEY:Azure OpenAI 的 API 密钥;
  • AZURE_OPENAI_ENDPOINT:Azure OpenAI 的 Endpoint(不带尾部/,代码内部会rstrip('/'));
  • AZURE_OPENAI_DEPLOYMENT:你在 Azure 上创建的模型部署名称。

四、配置 openai:旧式模块级配置与新版 Client 对象

旧版(openai < 1.0)的模块级配置方式

课程文档给出了面向 Azure OpenAI 的经典配置写法:

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 而不是 OpenAI 官方服务;
  • api_key:来自 Azure Portal 的 API 密钥;
  • api_version:指定 API 版本,文档写作时最新为2023-05-15
  • api_base:与密钥相邻的 Azure Portal 端点地址。

[!NOTE]os.getenv用于读取环境变量,可读取OPENAI_API_KEYAPI_BASE等。可在终端手动 export,或用dotenv类库从文件加载。

新版(openai v1.x)的 Client 配置方式(本仓库实测写法)

重要演进提示:仓库锁定的openai==1.55.1属于 v1.x。该版本已不推荐上述"修改模块全局属性"的写法,而是改为实例化OpenAI客户端对象,并且针对 Azure OpenAI 只需传入api_keybase_url。仓库示例的典型写法如下:

from openai import OpenAI import os from dotenv import load_dotenv load_dotenv() # 从 .env 加载环境变量 # 面向 Azure OpenAI v1 端点的客户端 client = OpenAI( api_key=os.environ['AZURE_OPENAI_API_KEY'], base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT'].rstrip('/')}/openai/v1/", ) deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']

若直接使用 OpenAI 官方服务,则更简单——客户端默认从OPENAI_API_KEY读取密钥:

from openai import OpenAI client = OpenAI() # 内部读取 OPENAI_API_KEY deployment = "gpt-4o-mini"

五、第一次文本生成:Completion、ChatCompletion 与 Responses API

旧式补全(Legacy Completion)

课程文档最早版本的文本生成基于Completion类:

prompt = "Complete the following: Once upon a time there was a" completion = openai.Completion.create(model="davinci-002", prompt=prompt) print(completion.choices[0].text)

即创建 completion 对象,指定模型与 prompt,然后打印生成的文本。

旧式 Chat Completion(适合聊天场景)

面向聊天机器人,课程引入更合适的ChatCompletion,消息以role/content结构组织:

import openai openai.api_key = "sk-..." completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}]) print(completion.choices[0].message.content)

这种多轮消息结构正是后续"构建聊天应用"(第 7 课)的基础。

本仓库实测:Responses API 与 Chat Completions 的现代写法

由于仓库使用 openai v1.x,上述openai.Completion/openai.ChatCompletion的模块级类在 1.55.1 中已不可用。仓库代码统一改用client.responses.create(Responses API)client.chat.completions.create(Chat Completions API)。见 oai-app.py:

from openai import OpenAI import os from dotenv import load_dotenv load_dotenv() # 配置 OpenAI 官方客户端 client = OpenAI() deployment = "gpt-4o-mini" # 补全任务 prompt = "Complete the following: Once upon a time there was a" response = client.responses.create(model=deployment, input=prompt, store=False) print(response.output_text)

输出效果示例(模型为非确定性的,每次可能略有差异):

very unhappy _____. Once upon a time there was a very unhappy mermaid.

两个容易混淆的关键差异值得记牢:

  • 在 Responses API 中,请求参数是input(而非prompt/messages),返回内容取response.output_text
  • 在 Chat Completions API 中,请求参数仍是messages,返回内容取completion.choices[0].message.content

六、Prompt 能做什么:不同任务对应不同写法

一旦理解"用一句话驱动模型",就会发现 Prompt 的用途非常广泛:

  • 生成特定类型文本:诗歌、测验题等;
  • 检索信息:如 "What does CORS mean in web development?";
  • 生成代码:例如让模型写一个邮箱校验正则,甚至完整的小程序。

七、实战场景:用一次 Prompt 生成"食谱建议"

想象一下:家里有鸡肉、土豆、胡萝卜,不知道该做什么菜。与其在搜索引擎翻来翻去,不如把食材交给 LLM:

"Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used."

模型可能会返回类似下面的完整回答:

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: Ingredients: - 2 tablespoons olive oil - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 onion, diced - 2 cloves garlic, minced - 1 teaspoon dried oregano - 1 teaspoon dried thyme - 1 cup chicken broth - Salt and pepper, to taste 4. Chicken and Potato Soup: 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 - 4 cups chicken broth - Salt and pepper, to taste 5. Chicken and Potato Hash: Ingredients: - 2 tablespoons olive oil - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 onion, diced - 2 cloves garlic, minced - 1 teaspoon dried oregano

这份结果已经可以指导你下厨。要让它更"贴心",还可以继续约束模型,比如:

  • 过滤掉你讨厌或过敏的食材;
  • 结合家中已有食材,生成一份缺什么买什么的购物清单。

例如追加一轮约束:

"Please remove recipes with garlic as I'm 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."

模型会返回"去掉大蒜"后的菜谱,以及基于"家里已有鸡肉、土豆、胡萝卜"推断出的购物清单:

1. Roasted Chicken and Vegetables: Ingredients: - 4 chicken thighs - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 2 tablespoons olive oil - 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 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: Ingredients: - 2 tablespoons olive oil - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 onion, diced - 1 teaspoon dried oregano - 1 teaspoon dried thyme - 1 cup chicken broth - Salt and pepper, to taste 4. Chicken and Potato Soup: Ingredients: - 2 tablespoons olive oil - 1 onion, diced - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 teaspoon dried oregano - 1 teaspoon dried thyme - 4 cups chicken broth - Salt and pepper, to taste 5. Chicken and Potato Hash: Ingredients: - 2 tablespoons olive oil - 2 chicken breasts, cut into cubes - 2 potatoes, cut into cubes - 2 carrots, cut into cubes - 1 onion, diced - 1 teaspoon dried oregano Shopping List: - Olive oil - Onion - Thyme - Oregano - Salt - Pepper

八、从概念到代码:构建可交互的食谱生成器

第一步:把固定食材变成程序中的 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"

一次运行的可能输出为(注意 LLM 是非确定性的,每次结果可能不同):

-Chicken Stew with Potatoes and Carrots: 3 tablespoons oil, 1 onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 bay leaf, 1 thyme sprig, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 1 1/2 cups chicken broth, 1/2 cup dry white wine, 2 tablespoons chopped fresh parsley, 2 tablespoons unsalted butter, 1 1/2 pounds boneless, skinless chicken thighs, cut into 1-inch pieces -Oven-Roasted Chicken with Potatoes and Carrots: 3 tablespoons extra-virgin olive oil, 1 tablespoon Dijon mustard, 1 tablespoon chopped fresh rosemary, 1 tablespoon chopped fresh thyme, 4 cloves garlic, minced, 1 1/2 pounds small red potatoes, quartered, 1 1/2 pounds carrots, quartered lengthwise, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 1 (4-pound) whole chicken -Chicken, Potato, and Carrot Casserole: cooking spray, 1 large onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and shredded, 1 potato, peeled and shredded, 1/2 teaspoon dried thyme leaves, 1/4 teaspoon salt, 1/4 teaspoon black pepper, 2 cups fat-free, low-sodium chicken broth, 1 cup frozen peas, 1/4 cup all-purpose flour, 1 cup 2% reduced-fat milk, 1/4 cup grated Parmesan cheese -One Pot Chicken and Potato Dinner: 2 tablespoons olive oil, 1 pound boneless, skinless chicken thighs, cut into 1-inch pieces, 1 large onion, chopped, 3 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 bay leaf, 1 thyme sprig, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 2 cups chicken broth, 1/2 cup dry white wine -Chicken, Potato, and Carrot Curry: 1 tablespoon vegetable oil, 1 large onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 teaspoon ground coriander, 1 teaspoon ground cumin, 1/2 teaspoon ground turmeric, 1/2 teaspoon ground ginger, 1/4 teaspoon cayenne pepper, 2 cups chicken broth, 1/2 cup dry white wine, 1 (15-ounce) can chickpeas, drained and rinsed, 1/2 cup raisins, 1/2 cup chopped fresh cilantro

第二步:让菜谱数量和食材可输入(f-string 插值)

硬编码 Prompt 不够灵活。课程接下来用input()读取用户输入,再用 f-string 插值拼装 Prompt:

no_recipes = input("No of recipes (for example, 5): ") ingredients = input("List of ingredients (for example, chicken, potatoes, and carrots): ") # 将菜谱数量与食材插值进 Prompt prompt = f"Show 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 extract

第三步:加入"排除条件"过滤器

为了让应用真正可用,需要让用户能剔除不喜欢/过敏的食材,例如素食(vegetarian)、纯素(vegan)、无麸质(gluten-free)。实现方式很简单——在 Prompt 末尾追加约束条件:

filter = input("Filter (for example, vegetarian, vegan, or gluten-free): ") prompt = f"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used, no {filter}"

{filter}被插值到 Prompt 尾部,而值来自用户输入。一次带过滤的交互如下(这里要求 "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 -1 cup milk -6 slices french bread -1/4 cup shredded Parmesan cheese -1 tablespoon butter -1 teaspoon dried thyme -1/4 teaspoon salt -1/4 teaspoon black pepper Instructions: 1. In a large pot, sauté onions in butter until golden brown. 2. Add beef broth, milk, thyme, salt, and pepper. Bring to a boil. 3. Reduce heat and simmer for 10 minutes. 4. Place french bread slices on soup bowls. 5. Ladle soup over bread. 6. Sprinkle with Parmesan cheese. 2. Onion and Potato Soup Ingredients: -1 large onion, chopped -2 cups potatoes, diced -3 cups vegetable broth -1 cup milk -1/4 teaspoon black pepper Instructions: 1. In a large pot, sauté onions in butter until golden brown. 2. Add potatoes, vegetable broth, milk, and pepper. Bring to a boil. 3. Reduce heat and simmer for 10 minutes. 4. Serve hot. 3. Creamy Onion Soup Ingredients: -1 large onion, chopped -3 cups vegetable broth -1 cup milk -1/4 teaspoon black pepper -1/4 cup all-purpose flour -1/2 cup shredded Parmesan cheese Instructions: 1. In a large pot, sauté onions in butter until golden brown. 2. Add vegetable broth, milk, and pepper. Bring to a boil. 3. Reduce heat and simmer for 10 minutes. 4. In a small bowl, whisk together flour and Parmesan cheese until smooth. 5. Add to soup and simmer for an additional 5 minutes, or until soup has thickened.

提示:示例中"过滤牛奶"后仍出现了含牛奶的菜谱,说明排除式指令需要尽量精确(例如乳糖不耐受者还应补充 "no cheese",并描述得更具体),这也反映了 Prompt 工程中"表达越明确、结果越可控"的原则。

第四步:两段式 Prompt 生成购物清单

方案思路:可以在一个 Prompt 里做完所有事,也可以拆成两个 Prompt,并把第一轮的输出作为第二轮的上下文——课程采用后者。

在第一轮结果打印之后追加以下代码:

old_prompt_result = completion.choices[0].message.content prompt = "Produce a shopping list for the generated recipes and please don't include ingredients that I already have." new_prompt = f"{old_prompt_result} {prompt}" messages = [{"role": "user", "content": new_prompt}] completion = openai.Completion.create(engine=deployment_name, messages=messages, max_tokens=1200) # 打印响应 print("Shopping list:") print(completion.choices[0].message.content)

两个要点:

  1. 拼接上下文new_prompt = f"{old_prompt_result} {prompt}"把第一轮的菜谱结果作为背景信息,喂给第二轮请求;
  2. 注意 token 预算:第二轮要把第一轮消耗的 token 也算进去,因此把max_tokens放宽到 1200。

一轮完整的运行效果:

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, 1/2 tsp baking soda, 1/4 tsp salt, 1 tbsp sugar, 1 egg, 1 cup buttermilk or sour milk, 1/4 cup melted butter, 1 Granny Smith apple, peeled and grated -Apple fritters: 1-1/2 cups flour, 1 tsp baking powder, 1/4 tsp salt, 1/4 tsp baking soda, 1/4 tsp nutmeg, 1/4 tsp cinnamon, 1/4 tsp allspice, 1/4 cup sugar, 1/4 cup vegetable shortening, 1/4 cup milk, 1 egg, 2 cups shredded, peeled apples Shopping list: -Flour, baking powder, baking soda, salt, sugar, egg, buttermilk, butter, apple, nutmeg, cinnamon, allspice

仓库现成实现:aoai-app-recipe.py(带输入校验的完整版)

课程仓库已经把上述逻辑落成可直接运行的脚本,推荐直接对照阅读:

  • oai-app-recipe.py:面向 OpenAI 官方服务;
  • aoai-app-recipe.py:面向 Azure OpenAI。

其中 aoai-app-recipe.py 在课程思路之上还做了工程加固,值得逐行分析:

  1. 密钥缺失快速报错get_required_env()在环境变量缺失时抛出带指引的错误信息;
  2. 数字输入校验validate_number_input()将菜谱数量限制在 1~20 之间,防御异常输入;
  3. 文本输入净化validate_text_input()限制长度并剥离 `< > { } [ ] | `` 等危险字符,既防止注入也保证 Prompt 可预测;
  4. 调用 Responses API 并控制成本:第一轮max_output_tokens=600, temperature=0.1,第二轮temperature=0,并分别打印 Recipes 与 Shopping list。

其核心调用段为:

prompt = f"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used, no {filter_value}: " response = client.responses.create(model=deployment, input=prompt, max_output_tokens=600, temperature=0.1, store=False) print("Recipes:") old_prompt_result = response.output_text ... prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" response = client.responses.create(model=deployment, input=new_prompt, max_output_tokens=600, temperature=0, store=False)

注意它与课程文档中的旧写法openai.Completion.create(...)的对应关系:在现代 SDK 中统一为client.responses.create(model=deployment, input=..., max_output_tokens=..., temperature=..., store=False)

九、工程化增强:密钥分离、token 预算与 temperature

1. 用 .env + python-dotenv 分离密钥

.env文件内容(请勿提交到版本库):

OPENAI_API_KEY=sk-...

若走 Azure OpenAI,则需设置以下变量(课程旧式命名):

OPENAI_API_TYPE=azure OPENAI_API_VERSION=2023-05-15 OPENAI_API_BASE=<replace>

代码中加载:

from dotenv import load_dotenv load_dotenv() openai.api_key = os.environ["OPENAI_API_KEY"]

仓库示例实际约定的是AZURE_OPENAI_API_KEY / AZURE_OPENAI_ENDPOINT / AZURE_OPENAI_DEPLOYMENT(Azure 场景)或OPENAI_API_KEY(OpenAI 官方场景),并以load_dotenv()开头,运行前把对应变量写入.env即可。这也是 oai-app.py、aoai-app.py、aoai-study-buddy.py、aoai-history-bot.py 等所有脚本的统一模式。

2. 关注 token 成本:用 max_tokens 控制长度

token 直接对应费用,Prompt 表达应尽量精炼。通过max_tokens限制模型最多生成的 token 数量:

completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=100)

在多轮/拼接上下文的场景(如食谱 + 购物清单),还要把前序文本占用的 token 一并算入预算。

3. 用 temperature 调节随机性

temperature决定输出的"冒险程度":

  • 数值越高(越接近 1.0),输出越随机、变化越大;
  • 数值越低(越接近 0),输出越保守、稳定、可预测;
  • 取 0 时变化最小,取 1 时变化最大。课程知识自测的答案是:temperature 控制的是输出的随机程度(而非响应长度或 token 数量)。

按需设置:

completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0.5)

十、更多仓库源码对照:多语言与多服务形态

同一份"食谱生成器"逻辑,仓库提供了多种语言/服务的平行实现,可交叉印证:

  • Azure OpenAI(Python):aoai-app-recipe.py、aoai-app.py;
  • OpenAI(Python):oai-app-recipe.py、oai-app.py;
  • GitHub Models / Azure AI Inference(Python):githubmodels-app.py——使用azure.ai.inferenceChatCompletionsClient,读取AZURE_INFERENCE_CREDENTIALAZURE_INFERENCE_ENDPOINT,通过/chat/completions发送system+user消息,并显式给出temperature=1.0max_tokens=1000top_p=1.0参数;
  • GitHub Models(JavaScript/TypeScript):js-githubmodels/app.js——基于@azure-rest/ai-inference,实现相同的"菜谱生成 + 购物清单"两轮对话流程;
  • TypeScript 版本:见 typescript/recipe-app(Node 工程,含 package.json 等依赖描述);
  • .NET 笔记本:dotnet/notebook-azure-openai.dib(Azure OpenAI 场景的 Polyglot Notebook)。

观察上述实现可以发现一个共同的模式:系统消息负责定调("You are a helpful assistant."),用户消息携带拼接好的 Prompt——这与本课两段式 Prompt 的设计一脉相承。

十一、课后实战作业与参考解法

完成基础食谱生成器后,可从以下方向任选其一深入:

  1. 继续打磨食谱生成器:尝试不同的temperature与 Prompt 措辞,观察输出风格变化;
  2. 开发"学习伴侣"(Study Buddy):让它回答特定主题(如 Python)的问题;
  3. 开发"历史人物机器人"(History Bot):让模型扮演某位历史人物,用符合时代的语法与你对话。

仓库中已提供两份完整参考实现:

  • 学习伴侣:aoai-study-buddy.py 的起始 Prompt 模板为——要求模型以 Python 专家身份,按"概念 → 示例代码 → 通俗解释"三段式格式输出课程与练习:

    - "You're 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"

    仓库实现将其结构化为 system 式约束 Prompt,并插值用户问题{question}

  • 历史机器人:aoai-history-bot.py 的核心约束是"只依据史实回答、不编造、不知道就明说不知道",可运行的角色扮演 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"

    仓库版本把persona(历史人物)与question(提问)做成运行时输入,进一步泛化了场景。

十二、知识自测

temperature 到底控制什么?

  1. 它决定输出的随机程度(✅ 正确答案)
  2. 它决定响应内容的长短
  3. 它决定使用的 token 数量

十三、挑战:动手试出"你的最佳温度"

做作业时建议系统地对比temperature = 00.51三档效果:0 变化最小、1 变化最大。请判断——对你的应用而言,究竟是需要"每次输出稳定一致"(偏低温),还是"每次都有新鲜感"(偏高温)?结合 max_tokens 的预算控制,为你的应用找到最合适的参数组合。

延伸学习

本课是文本补全应用的入门,下一课将引入多轮消息结构与对话记忆,学习构建真正的聊天应用,详见 第 7 课:Building Chat Applications。同一课程的其余章节位于 README.md 索引中;配套的提示词、API 密钥等基础环境准备参见 00-course-setup。

【免费下载链接】generative-ai-for-beginners21 Lessons, Get Started Building with Generative AI项目地址: https://gitcode.com/GitHub_Trending/ge/generative-ai-for-beginners

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询