Extract Transcript Quotes with LLM Gateway
This guide will demonstrate how to use AssemblyAI’s LLM Gateway to process an audio file and find the best quotes via the chat completions endpoint.
Quickstart
Python
JavaScript
1 import requests 2 import json 3 import time 4 5 API_KEY = "YOUR_API_KEY" 6 headers = {"authorization": API_KEY} 7 8 # Transcribe the audio file 9 print("Submitting audio for transcription...") 10 transcript_response = requests.post( 11 "https://api.assemblyai.com/v2/transcript", 12 headers=headers, 13 json={ 14 "audio_url": "https://assembly.ai/wildfires.mp3", 15 "speaker_labels": True 16 } 17 ) 18 19 transcript_id = transcript_response.json()["id"] 20 21 # Poll for transcription completion 22 while True: 23 transcript_result = requests.get( 24 f"https://api.assemblyai.com/v2/transcript/{transcript_id}", 25 headers=headers 26 ).json() 27 28 if transcript_result["status"] == "completed": 29 break 30 elif transcript_result["status"] == "error": 31 raise Exception(f"Transcription failed: {transcript_result['error']}") 32 33 time.sleep(3) 34 35 # Extract utterances with timestamps 36 utterances_data = [ 37 {"text": u["text"], "start": u["start"], "end": u["end"], "speaker": u["speaker"]} 38 for u in transcript_result["utterances"] 39 ] 40 41 # Create prompt with timestamped utterances 42 prompt = f"""You are analyzing a transcript with timestamped utterances. Each utterance includes the text content, speaker label, and start/end timestamps in milliseconds. 43 44 Here is the transcript data: 45 {json.dumps(utterances_data, indent=2)} 46 47 Task: Identify the 3-5 most engaging, impactful, or quotable utterances from this transcript. 48 49 Return your response as a JSON array with the following structure: 50 {{ 51 "quotes": [ 52 {{ 53 "text": "exact quote text", 54 "start": start_timestamp_in_milliseconds, 55 "end": end_timestamp_in_milliseconds, 56 "speaker": "speaker_label", 57 "reason": "brief explanation of why this quote is engaging" 58 }} 59 ] 60 }} 61 62 Return ONLY valid JSON, no additional text.""" 63 64 # Use LLM Gateway to extract quotes 65 print("Submitting transcript to LLM Gateway for quote extraction...") 66 gateway_response = requests.post( 67 "https://llm-gateway.assemblyai.com/v1/chat/completions", 68 headers=headers, 69 json={ 70 "model": "gpt-5-nano", 71 "messages": [ 72 {"role": "user", "content": prompt} 73 ] 74 } 75 ) 76 77 result = gateway_response.json() 78 quotes_json = json.loads(result["choices"][0]["message"]["content"]) 79 print(json.dumps(quotes_json, indent=2))
Getting Started
Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for an AssemblyAI account and get your API key from your dashboard.
Step-by-Step Instructions
Step 1: Set up your API key and headers
Python
JavaScript
1 import requests 2 import json 3 import time 4 5 API_KEY = "YOUR_API_KEY" 6 headers = {"authorization": API_KEY}
Step 2: Transcribe the audio file
Next, we’ll use AssemblyAI to transcribe a file and save our transcript for later use. We’ll enable speaker_labels to get utterances grouped by speaker.
Python
JavaScript
1 # Transcribe the audio file 2 print("Submitting audio for transcription...") 3 transcript_response = requests.post( 4 "https://api.assemblyai.com/v2/transcript", 5 headers=headers, 6 json={ 7 "audio_url": "https://assembly.ai/wildfires.mp3", 8 "speaker_labels": True 9 } 10 ) 11 12 transcript_id = transcript_response.json()["id"] 13 14 # Poll for transcription completion 15 while True: 16 transcript_result = requests.get( 17 f"https://api.assemblyai.com/v2/transcript/{transcript_id}", 18 headers=headers 19 ).json() 20 21 if transcript_result["status"] == "completed": 22 break 23 elif transcript_result["status"] == "error": 24 raise Exception(f"Transcription failed: {transcript_result['error']}") 25 26 time.sleep(3)
Step 3: Extract utterances with timestamps
Then we’ll take the timestamped utterances array from our transcript and format it as structured data. Utterances are grouped by speaker and include continuous speech segments.
Python
JavaScript
1 utterances_data = [ 2 {"text": u["text"], "start": u["start"], "end": u["end"], "speaker": u["speaker"]} 3 for u in transcript_result["utterances"] 4 ]
Step 4: Use LLM Gateway to extract engaging quotes
Finally, we’ll provide the timestamped utterances to the LLM Gateway chat completions endpoint to extract the most engaging quotes from this transcript with their associated timestamps in a structured JSON format.
Python
JavaScript
1 # Create prompt with timestamped utterances 2 prompt = f"""You are analyzing a transcript with timestamped utterances. Each utterance includes the text content, speaker label, and start/end timestamps in milliseconds. 3 4 Here is the transcript data: 5 {json.dumps(utterances_data, indent=2)} 6 7 Task: Identify the 3-5 most engaging, impactful, or quotable utterances from this transcript. 8 9 Return your response as a JSON array with the following structure: 10 {{ 11 "quotes": [ 12 {{ 13 "text": "exact quote text", 14 "start": start_timestamp_in_milliseconds, 15 "end": end_timestamp_in_milliseconds, 16 "speaker": "speaker_label", 17 "reason": "brief explanation of why this quote is engaging" 18 }} 19 ] 20 }} 21 22 Return ONLY valid JSON, no additional text.""" 23 24 # Use LLM Gateway to extract quotes 25 print("Submitting transcript to LLM Gateway for quote extraction...") 26 gateway_response = requests.post( 27 "https://llm-gateway.assemblyai.com/v1/chat/completions", 28 headers=headers, 29 json={ 30 "model": "gpt-5-nano", 31 "messages": [ 32 {"role": "user", "content": prompt} 33 ] 34 } 35 ) 36 37 result = gateway_response.json() 38 quotes_json = json.loads(result["choices"][0]["message"]["content"]) 39 print(json.dumps(quotes_json, indent=2))
Example Response
1 { 2 "quotes": [ 3 { 4 "text": "It is, it is. The levels outside right now in Baltimore are considered unhealthy. And most of that is due to what's called particulate matter, which are tiny particles, microscopic, smaller than the width of your hair, that can get into your lungs and impact your respiratory system, your cardiovascular system, and even your neurological, your brain.", 5 "start": 62350, 6 "end": 82590, 7 "speaker": "B", 8 "reason": "Defines particulate matter and explains how it harms health." 9 }, 10 { 11 "text": "Yeah. So the concentration of particulate matter, I was looking at some of the monitors that we have was reaching levels of what are, in science speak, 150 micrograms per meter cubed, which is more than 10 times what the annual average should be in about four times higher than what you're supposed to have on a 24 hour average. And so the concentrations of these particles in the air are just much, much, much higher than we typically see. And exposure to those high levels can lead to a host of health problems.", 12 "start": 93550, 13 "end": 123350, 14 "speaker": "B", 15 "reason": "Gives specific concentration figures and links to health risks." 16 }, 17 { 18 "text": "It's the youngest. So children, obviously, whose bodies are still developing, the elderly who are, you know, their bodies are more in decline and they're more susceptible to the health impacts of breathing, the poor air quality. And then people who have pre existing health conditions, people with respiratory conditions or heart conditions, can be triggered by high levels of air pollution.", 19 "start": 137610, 20 "end": 156650, 21 "speaker": "B", 22 "reason": "Highlights the most vulnerable groups affected by poor air quality." 23 }, 24 { 25 "text": "Well, I think the fires are going to burn for a little bit longer. But the key for us in the US Is the weather system changing. Right now it's the weather systems that are pulling that air into our Mid Atlantic and Northeast region. As those weather systems change and shift, we'll see that smoke going elsewhere and not impact us in this region as much. I think that's going to be the defining factor. I think the next couple days we're going to see a shift in that weather pattern and start to push the smoke away from where we are.", 26 "start": 198280, 27 "end": 227480, 28 "speaker": "B", 29 "reason": "Offers an outlook on how weather patterns may reduce exposure." 30 }, 31 { 32 "text": "I mean, that is one of the predictions for climate change. Looking into the future, the fire season is starting earlier and lasting longer and we're seeing more frequent fires. So yeah, this is probably something that we'll be seeing more, more frequently. This tends to be much more of an issue in the western U.S. so the eastern U.S. getting hit right now is a little bit new. But yeah, I think with climate change moving forward, this is something that is going to happen more frequently.", 33 "start": 241370, 34 "end": 267570, 35 "speaker": "B", 36 "reason": "Connects current event to longer-term climate change trends and future frequency." 37 } 38 ] 39 }