| """ |
| tools/transcribe_audio.py —— 工具⑤:把录音转成文字(语音识别) |
| |
| 有的题目附带一段录音(比如"语音备忘录""课堂录音"),问的内容藏在话里。 |
| 这个工具把音频文件交给专门的语音识别模型(whisper-1),让它"听写"成文字, |
| 然后大模型就能从文字里找答案了。 |
| """ |
|
|
| import os |
|
|
| from langchain_core.tools import tool |
|
|
| |
| from config import LLM_BASE_URL, LLM_API_KEY, ASR_MODEL_ID |
|
|
|
|
| @tool |
| def transcribe_audio(file_path: str) -> str: |
| """Transcribe a local audio file (.mp3/.wav/.m4a/...) to text. Use it whenever a |
| question references a voice memo or recording. The transcript is returned as plain |
| text.""" |
| |
| if not os.path.exists(file_path): |
| return f"File not found: {file_path}" |
| try: |
| from openai import OpenAI |
|
|
| |
| client = OpenAI(base_url=LLM_BASE_URL, api_key=LLM_API_KEY) |
| |
| with open(file_path, "rb") as f: |
| result = client.audio.transcriptions.create(model=ASR_MODEL_ID, file=f) |
| |
| return getattr(result, "text", None) or str(result) |
| except Exception as e: |
| return f"Error transcribing audio '{file_path}': {e}" |
|
|