English
主导航

旧版 API

会议纪要

使用 Whisper 和 GPT-4 创建自动会议纪要生成器。

在本教程中,我们将利用 OpenAI 的 Whisper 和 GPT-4 模型的强大功能,开发一个自动会议纪要生成器。该应用程序转录会议音频,提供讨论摘要,提取关键点和行动项,并进行情感分析。

入门

本教程假设您对 Python 和 OpenAI API 密钥。你可以使用本教程提供的音频文件,或者你自己的音频文件。

此外,您还需要安装 python-docx and OpenAI 库。您可以使用以下命令创建一个新的 Python 环境并安装所需的包:

1
2
3
4
5
6
python -m venv env

source env/bin/activate

pip install openai
pip install python-docx

使用 Whisper 转录音频

Audio Waveform created by DALL·E

转录会议音频的第一步是将会议的音频文件传递给我们的 /v1/audio API。为音频 API 提供支持的 Whisper 模型能够将口语转换为书面文本。首先,我们将避免传递

提示词

or

temperature

(可选参数用于控制模型的输出)并保持默认值。

下载示例音频


接下来,我们导入所需的包,并定义一个使用 Whisper 模型接收音频文件并将其转录的函数:

1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI

client = OpenAI(
    # defaults to os.environ.get("OPENAI_API_KEY")
    # api_key="My API Key",
)
from docx import Document

def transcribe_audio(audio_file_path):
    with open(audio_file_path, 'rb') as audio_file:
        transcription = client.audio.transcriptions.create("whisper-1", audio_file)
    return transcription['text']

In this function, audio_file_path 是您要转录的音频文件的路径。该函数打开此文件并将其传递给 Whisper ASR 模型(whisper-1)进行转录。结果作为原始文本返回。需要注意的是, openai.Audio.transcribe 函数需要传递实际的音频文件,而不仅仅是本地或远程服务器上文件的路径。这意味着,如果您在服务器上运行此代码,而该服务器上可能并未存储您的音频文件,那么您需要一个预处理步骤,首先将音频文件下载到该设备上。

使用 GPT-4 总结和分析文本

获得转录文本后,我们现在通过 Chat Completions API。GPT-4 是 OpenAI 最先进的大型语言模型,我们将使用它来生成摘要、提取要点和待办事项,并进行情感分析。

将其传递给 GPT-4。本教程为我们希望 GPT-4 执行的每个任务使用了不同的函数。这不是完成此任务最高效的方法——您可以将这些指令放入一个函数中,但是,将它们分开通常可以获得更高质量的摘要。

为了将任务分开,我们定义了 meeting_minutes 函数,该函数将作为此应用程序的主函数:

1
2
3
4
5
6
7
8
9
10
11
def meeting_minutes(transcription):
    abstract_summary = abstract_summary_extraction(transcription)
    key_points = key_points_extraction(transcription)
    action_items = action_item_extraction(transcription)
    sentiment = sentiment_analysis(transcription)
    return {
        'abstract_summary': abstract_summary,
        'key_points': key_points,
        'action_items': action_items,
        'sentiment': sentiment
    }

In this function, transcription 是我们从 Whisper 获得的文本。转录文本可以传递给其他四个函数,每个函数旨在执行特定的任务: abstract_summary_extraction 生成会议摘要, key_points_extraction 提取要点, action_item_extraction 确定行动项,以及 sentiment_analysis performs 情感分析。如果您还需要其他功能,也可以使用上面展示的相同框架将它们添加进来。

以下是每个函数的工作方式:

摘要提取

The abstract_summary_extraction 函数接收转录文本并将其总结为一个简洁的摘要段落,旨在保留最重要的要点,同时避免不必要的细节或偏题的论点。实现此过程的主要机制是如下所示的系统消息。有许多不同的可能方法可以通过通常称为提示工程的过程获得类似的结果。您可以阅读我们的 提示词工程指南 ,其中提供了有关如何最有效地执行此操作的深入建议。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def abstract_summary_extraction(transcription):
    response = client.chat.completions.create(
        model="gpt-4",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "You are a highly skilled AI trained in language comprehension and summarization. I would like you to read the following text and summarize it into a concise abstract paragraph. Aim to retain the most important points, providing a coherent and readable summary that could help a person understand the main points of the discussion without needing to read the entire text. Please avoid unnecessary details or tangential points."
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return completion.choices[0].message.content

要点提取

The key_points_extraction 函数识别并列出会议中讨论的要点。这些要点应代表对讨论本质至关重要的最重要想法、发现或主题。同样,控制这些要点识别方式的主要机制是系统消息。您可能希望在此处围绕您的项目或公司的运行方式提供一些额外的上下文,例如“我们是一家向消费者销售赛车的公司。我们做 XYZ 的目标是 XYZ”。这种额外的上下文可以显著提高模型提取相关信息的能力。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def key_points_extraction(transcription):
    response = client.chat.completions.create(
        model="gpt-4",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "You are a proficient AI with a specialty in distilling information into key points. Based on the following text, identify and list the main points that were discussed or brought up. These should be the most important ideas, findings, or topics that are crucial to the essence of the discussion. Your goal is to provide a list that someone could read to quickly understand what was talked about."
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return completion.choices[0].message.content

行动项提取

The action_item_extraction 函数识别会议期间商定或提及的任务、分配或行动。这些可以是分配给特定个人的任务,也可以是团队决定采取的一般行动。虽然本教程未涵盖,但 Chat Completions API 提供了 函数调用能力 ,这将允许您构建自动在您的任务管理软件中创建任务并将其分配给相关人员的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def action_item_extraction(transcription):
    response = client.chat.completions.create(
        model="gpt-4",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "You are an AI expert in analyzing conversations and extracting action items. Please review the text and identify any tasks, assignments, or actions that were agreed upon or mentioned as needing to be done. These could be tasks assigned to specific individuals, or general actions that the group has decided to take. Please list these action items clearly and concisely."
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return completion.choices[0].message.content

情感分析

The sentiment_analysis 函数分析讨论的整体情感。它会考虑语气、所用语言传达的情感以及使用单词和短语的上下文。对于不太复杂的任务,可能也值得尝试 gpt-3.5-turbo in addition to gpt-4 以了解您是否可以获得类似水平的性能。尝试获取 sentiment_analysis 函数的结果并将其传递给其他函数,看看对话的情感如何影响其他属性可能也会很有用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def sentiment_analysis(transcription):
    response = client.chat.completions.create(
        model="gpt-4",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "As an AI with expertise in language and emotion analysis, your task is to analyze the sentiment of the following text. Please consider the overall tone of the discussion, the emotion conveyed by the language used, and the context in which words and phrases are used. Indicate whether the sentiment is generally positive, negative, or neutral, and provide brief explanations for your analysis where possible."
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return completion.choices[0].message.content

导出会议纪要

Audio Waveform created by DALL·E

一旦我们生成了会议纪要,将其保存为易于分发的易读格式是有益的。此类报告的一种常见格式是 Microsoft Word。Python docx 库是用于创建 Word 文档的流行开源库。如果您想构建一个端到端的会议纪要应用程序,您可以考虑取消此导出步骤,转而将摘要直接作为电子邮件跟进发送。


要处理导出过程,请定义一个函数 save_as_docx 将原始文本转换为 Word 文档:

1
2
3
4
5
6
7
8
9
10
def save_as_docx(minutes, filename):
    doc = Document()
    for key, value in minutes.items():
        # Replace underscores with spaces and capitalize each word for the heading
        heading = ' '.join(word.capitalize() for word in key.split('_'))
        doc.add_heading(heading, level=1)
        doc.add_paragraph(value)
        # Add a line break between sections
        doc.add_paragraph()
    doc.save(filename)

在此函数中,minutes 是一个包含会议摘要、要点、行动项和情感分析的字典。Filename 是要创建的 Word 文档文件的名称。该函数创建一个新的 Word 文档,为纪要的每个部分添加标题和内容,然后将文档保存到当前工作目录。

最后,您可以将所有内容整合在一起,并从音频文件生成会议纪要:

1
2
3
4
5
6
audio_file_path = "Earningscall.wav"
transcription = transcribe_audio(audio_file_path)
minutes = meeting_minutes(transcription)
print(minutes)

save_as_docx(minutes, 'meeting_minutes.docx')

此代码将转录音频文件 Earningscall.wav,生成会议纪要,将其打印出来,然后保存到名为以下名称的 Word 文档中: meeting_minutes.docx.

现在您已经有了基本的会议纪要处理设置,可以考虑尝试通过 提示词工程 优化性能,或者使用原生 函数调用.