场景与选型
课堂录音往往长达数十分钟,人工整理耗时。采用本地部署的 OpenAI Whisper 做语音识别,可离线运行、避免音频上传隐私风险;识别结果再交给大语言模型做结构化摘要。本文以 LangChain 的摘要链为例,演示如何把转写文本压缩为知识点清单。
步骤一:本地转写录音
先安装 Whisper 与 ffmpeg,再对音频文件转写。中文场景建议指定 language='zh':
pip install openai-whisper
# 命令行直接转写
whisper lesson.mp3 --model medium --language zh --output_format txt
# 或在 Python 中调用
import whisper
model = whisper.load_model('medium')
result = model.transcribe('lesson.mp3', language='zh')
text = result['text']
步骤二:用 LangChain 抽取知识点
将长文本切分后交给摘要链,输出结构化要点。以下示例把整堂课压缩为“核心知识点 + 课后待办”:
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import Ollama
llm = Ollama(model='qwen2.5')
splitter = CharacterTextSplitter(chunk_size=2000, chunk_overlap=200)
docs = splitter.create_documents([text])
map_prompt = PromptTemplate.from_template(
'请提取以下课堂片段的知识点,逐条列出:\n{text}'
)
reduce_prompt = PromptTemplate.from_template(
'合并下列知识点,去重并归类:\n{text}'
)
map_chain = LLMChain(llm=llm, prompt=map_prompt)
reduce_chain = ReduceDocumentsChain(
combine_documents_chain=LLMChain(llm=llm, prompt=reduce_prompt),
collapse_documents_chain=LLMChain(llm=llm, prompt=reduce_prompt)
)
chain = MapReduceDocumentsChain(
llm_chain=map_chain, reduce_documents_chain=reduce_chain
)
print(chain.run(docs))
落地注意
说明:录音涉及师生声音与隐私,须取得授权并在隔离环境处理;模型输出仅为辅助,知识点准确性由教师最终确认。中等规模模型在消费级 GPU 即可运行,长音频建议分段转写。
参考来源
Whisper: Robust Speech Recognition via Large-Scale Weak Supervision - OpenAI(GitHub);LangChain 官方文档 - LangChain。