大语言模型文本生成的标准实践与技巧
立即解锁
发布时间: 2025-09-01 00:59:10 阅读量: 44 订阅数: 16 AIGC 

# 大语言模型文本生成的标准实践与技巧
## 1. 大语言模型的发展与应用前景
随着技术的不断进步,未来或许有一天我们能够在手机或其他设备上运行大语言模型(LLMs)。目前,了解不同模型擅长领域的最佳方式是在多个模型上运行相同的提示,并比较它们的响应结果。
例如,在为一款适合任何脚型的鞋子命名的任务中,不同模型的表现如下:
| 模型 | 产品描述 | 产品名称 |
| --- | --- | --- |
| GPT - 4 | 适合任何脚型的鞋子 | iFit, iShoe, iFlexible |
| Claude 3 | 适合任何脚型的鞋子 | iFit, iComfort, iSole |
| Llama 3 70b | 适合任何脚型的鞋子 | iFit, OneSize, iWalkFree |
从这个例子可以看出,GPT - 4在遵循指令方面表现得更好,它是唯一按照正确格式并以符合示例(以字母 i 开头)的名称进行响应的模型。
大语言模型从诞生到如今的先进状态,是一部不断创新、合作与激烈竞争的历史。随着这些模型的不断发展,它们很可能会在我们的日常生活中变得更加不可或缺,改变我们与技术以及彼此之间的互动方式。不过,虽然这些模型提供了广阔的可能性,但数据隐私仍然是一个至关重要的问题。如果这些模型使用你的数据进行再训练或微调,请务必谨慎,避免输入敏感信息。
## 2. 文本生成的简单提示技巧
### 2.1 生成列表
自动生成列表是一项非常强大的功能,它能让我们专注于更高级的任务,而让GPT自动为我们生成、优化、重新排序和去重列表。
例如,输入“Generate a list of Disney characters.”,GPT - 4 输出了包含 30 个迪士尼角色的列表:
1. Mickey Mouse
2. Minnie Mouse
...
30. Bagheera (The Jungle Book)
然而,这种方法存在一些问题:
- **格式不匹配**:如果下游的 Python 代码期望按项目符号分割列表,但 GPT 输出的是编号列表,可能会导致不理想的结果或运行时错误。
- **存在注释**:GPT 提供了前置注释,去除这些注释会使输出的解析更加容易。
- **列表大小不受控**:列表大小由语言模型决定,没有进行控制。
- **名称提取困难**:部分角色名称包含了对应的电影名(如 Bagheera (The Jungle Book)),增加了名称提取的难度。
- **缺乏筛选**:没有根据我们的期望结果对 LLM 生成的内容进行筛选或选择。
为了解决这些问题,可以使用优化后的提示:
```plaintext
Generate a bullet - point list of 5 male Disney characters.
Only include the name of the character for each line.
Never include the film for each Disney character.
Only return the Disney characters, never include any commentary.
Below is an example list:
* Aladdin
* Simba
* Beast
* Hercules
* Tarzan
```
输出结果为:
```plaintext
* Woody
* Buzz Lightyear
* Stitch
* Jack Sparrow
* Prince Charming
```
通过优化提示,我们实现了以下目标:
- 限制列表大小为 5 个。
- 仅生成男性角色。
- 使用项目符号正确格式化列表。
- 去除了前置注释。
### 2.2 生成层次列表
对于需要嵌套输出的任务,层次列表非常有用,例如生成详细的文章结构。
输入:
```plaintext
Generate a hierarchical and incredibly detailed article outline on:
What are the benefits of data engineering.
See an example of the hierarchical structure below:
Article Title: What are the benefits of digital marketing?
* Introduction
a. Explanation of digital marketing
b. Importance of digital marketing in today's business world
* Increased Brand Awareness
a. Definition of brand awareness
b. How digital marketing helps in increasing brand awareness
```
输出:
```plaintext
Article Title: What are the benefits of data engineering?
* Introduction
a. Explanation of data engineering
b. Importance of data engineering in today’s data - driven world
...(10 sections later)...
* Conclusion
a. Importance of data engineering in the modern business world
b. Future of data engineering and its impact on the data ecosystem
```
为了生成有效的文章大纲,我们在提示中包含了两个关键短语:
- **Hierarchical**:表明文章大纲需要生成嵌套结构。
- **Incredibly detailed**:引导语言模型生成更详细的输出。也可以使用“very long”或指定大量子标题(如至少包含 10 个顶级标题)来达到相同的效果。
需要注意的是,要求语言模型生成固定数量的项目并不能保证它会生成相同数量的内容。因此,代码应该验证是否存在所需数量的标题,或者能够灵活处理 LLM 生成的不同长度的内容。
下面是使用 Python 解析层次列表的示例代码:
```python
import re
# openai_result = generate_article_outline(prompt)
# Commented out to focus on a fake LLM response, see below:
openai_result = '''
* Introduction
a. Explanation of data engineering
b. Importance of data engineering in today’s data - driven world
* Efficient Data Management
a. Definition of data management
b. How data engineering helps in efficient data management
* Conclusion
a. Importance of data engineering in the modern business world
b. Future of data engineering and its impact on the data ecosystem
'''
# Regular expression patterns
heading_pattern = r'\* (.+)'
subheading_pattern = r'\s+[a - z]\. (.+)'
# Extract headings and subheadings
headings = re.findall(heading_pattern, openai_result)
subheadings = re.findall(subheading_pattern, openai_result)
# Print results
print("Headings:\n")
for heading in headings:
print(f"* {heading}")
print("\nSubheadings:\n")
for subheading in subheadings:
print(f"* {subheading}")
```
这段代码的输出为:
```plaintext
Headings:
- Introduction
- Efficient Data Management
- Conclusion
Subheadings:
- Explanation of data engineering
- Importance of data engineering in today’s data - driven world
- Definition of data management
- How data engineering helps in efficient data management
- Importance of data engineering in the modern business world
- Future of data engineering and it
```
0
0
复制全文
相关推荐









