为了把这样的体验带到应用程序中,您需要掌握提示、补全等概念,并选择适合您的库。这就是本章要教您的内容。
什么是文本生成应用通常,当您构建应用程序时,它会有一种界面,比如:
基于命令。控制台应用程序是一种典型的应用程序,您可以在里面输入命令并执行任务。比如,“git”就是一个基于命令的应用程序。 用户界面(UI)。一些应用程序有图形用户界面(GUI),您可以在里面点击按钮、输入文本、选择选项等。传统的控制台和 UI 应用程序的限制

和传统的输入命令的应用程序相比:
存在局限性。您不能输入任何命令,只能输入应用程序支持的命令。 基于特定的语言。一些应用程序支持多种语言,但默认情况下,即使可以增加更多的语言支持,应用程序也是针对有限的语言构建的。文本生成应用程序的优势
文本生成应用程序有什么不同呢?
在文本生成应用程序中,您有更大的灵活性,不再受限于一组特定的命令或特定的输入语言。相反,您可以用自然语言和应用程序交互。另一个好处是,因为您已经在和一个经过大量信息库训练的数据源交互,而传统的应用程序可能只限于在数据库中存储的有限内容。
您可以用文本生成应用程序构建什么?
您可以构建很多东西。比如:
聊天机器人。一个可以回答关于您的公司和产品等主题的问题的聊天机器人可能是一个好选择。 协作助手。LLMs 擅长总结文本、从文本中提取见解、生成简历等文本等。 代码助手。根据您使用的编程语言模型,您可以构建一个代码助手来帮助您编写代码。比如,您可以使用 GitHub Copilot 和 ChatGPT 等产品来帮助您编写代码。如何开始?您需要找到一种和 LLMs 结合的方法,通常有以下两种方法:
API。在这里,您会根据提示构建 Web 请求并返回生成的文本。 Libraries。库可以帮助您封装 API 调用并使其更容易使用。 Libraries/SDK有一些通用的和 LLMs 集成的库,比如:
openai,这个库可以让您轻松地连接到您的模型并发送提示。 还有一些运行在更高层次的框架的库,比如:Langchain。Langchain 是一个支持 Python 的通用库。 Semantic Kernel。Semantic Kernel 是 Microsoft 提供的一个库,支持 C#、Python 和 Java 语言。创建第一个 openai 应用让我们看看如何构建您的第一个 openai 应用,您需要什么库,需要什么技能等等。
安装 openai
有很多库可以用来和 OpenAI 或 Azure OpenAI 交互。您也可以使用不同的编程语言,如 C#、Python、JavaScript、Java 等。我们选择使用 openai Python 库,通过 pip 来安装它。
pip install openai
添加 API 密钥和端点
您需要告诉“openai”库要使用什么 API 密钥。要找到 API 密钥,请转到您创建的 Azure Open AI 服务中的“密钥和端点”部分,并复制“密钥 1”值。
复制密钥后,让我们用libraries来调用它。
文本生成生成文本的方法是使用“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 对象,并传入了我们要使用的模型和提示。然后我们输出了生成的文本。
聊天补全
到目前为止,您已经学习了我们如何使用“Completion”来生成文本。但还有另一个更适合聊天机器人的类,叫做“ChatCompletion”。这是一个例子:
import openaiopenai.api_key = "sk-..."completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])print(completion.choices[0].message.content)
下一章会详细介绍这个功能。
提示的不同类型,用于不同的事情您已经学会了如何使用提示来生成文本。您甚至可以启动并运行一个程序,可以修改它来生成不同类型的文本。
提示可以用于各种任务。例如:
生成一种类型的文本。例如,生成一首诗、一个测验题目等。查找信息。您可以使用提示来查找信息,例如下面的例子’What does CORS mean in web development?'。生成代码。您可以使用提示来生成代码,例如开发一个用于验证电子邮件的正则表达式,或者为什么不生成一个完整的程序,例如一个 web 应用?进阶学习:菜谱生成器想象一下,你家里有一些食材,你想做点东西吃。为此,你需要一个菜谱。找菜谱的一种方法是使用搜索引擎,或者您也可以使用 LLMs 来做这件事。
你可以写一个这样的提示:
"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
1. 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
1. 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
1. 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
1. 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
1. 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
1. 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
1. 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
1. 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
这是根据你的购物清单生成的菜谱(不含大蒜)
改进你的设置到目前为止,我们拥有的是一个可以运行的代码,但是我们应该做一些调整来进一步改进它。我们应该做的一些事情有:
把密钥和代码分开,比如 API 密钥。密钥不应该放在代码里,应该存储在安全的地方。为了把密钥和代码分开,我们可以使用环境变量和像 python-dotenv 这样的库来从文件中加载它们。代码如下:import osfrom dotenv import load_dotenvload_dotenv()openai.api_key = os.getenv("OPENAI_API_KEY")
关于 token 长度。 我们应该考虑需要多少 token 来生成我们想要的文本。 token 需要花钱,因此在可能的情况下,我们应该尽量节约使用 token 的数量。 例如,我们可以对提示进行调整,以便我们可以使用更少的 token
要更改使用的 token,您可以使用 max_tokens 参数。 例如,如果您想使用 100 个 token,您可以这样做:
completion = openai.Completion.create(model="davinci-002", prompt=prompt, max_tokens=100)
进行 temperature 调整试验。 temperature 是我们到目前为止还没有提到的东西,但它是我们的程序如何执行的重要元素。 temperature 值越高,输出就越随机。 相反, temperature 值越低,输出就越可预测。