Skip to content

Instantly share code, notes, and snippets.

@patrickloeber
Last active October 8, 2024 13:04
Show Gist options
  • Select an option

  • Save patrickloeber/83677abf1e3b80d8d024a11c271d28a8 to your computer and use it in GitHub Desktop.

Select an option

Save patrickloeber/83677abf1e3b80d8d024a11c271d28a8 to your computer and use it in GitHub Desktop.
import { RealtimeTranscriber, RealtimeTranscript } from 'assemblyai';
import { getAssemblyToken } from './getAssemblyToken';
export async function createTranscriber(
transcriptionProcessed: (transcription: string, isFinal: boolean) => void
): Promise<RealtimeTranscriber | undefined> {
const token = await getAssemblyToken();
if (!token) {
console.error('No token found');
return;
}
const transcriber = new RealtimeTranscriber({
sampleRate: 16_000,
token: token,
wordBoost: ['Llama'],
endUtteranceSilenceThreshold: 1000,
// encoding: 'pcm_mulaw',
});
transcriber.on('open', ({ sessionId }) => {
console.log(`Transcriber opened with session ID: ${sessionId}`);
});
transcriber.on('error', (error: Error) => {
console.error('Transcriber error:', error);
await transcriber.close();
});
transcriber.on('close', (code: number, reason: string) => {
console.log(`Transcriber closed with code ${code} and reason: ${reason}`);
transcriber = null;
});
transcriber.on('transcript', (transcript: RealtimeTranscript) => {
if (!transcript.text) {
return;
}
if (transcript.message_type === 'PartialTranscript') {
transcriptionProcessed(transcript.text, false);
} else {
transcriptionProcessed(transcript.text, true);
}
});
return transcriber;
}
import { RealtimeTranscriber, RealtimeTranscript } from 'assemblyai';
import { getAssemblyToken } from './getAssemblyToken';
import { Dispatch, SetStateAction } from 'react';
export async function createTranscriber(
transcriptionProcessed: (transcription: string, isFinal: boolean) => void,
setLlamaActive: Dispatch<SetStateAction<boolean>>,
processPrompt: (prompt: string) => void
): Promise<RealtimeTranscriber | undefined> {
const token = await getAssemblyToken();
if (!token) {
console.error('No token found');
return;
}
const transcriber = new RealtimeTranscriber({
sampleRate: 16_000,
token: token,
wordBoost: ['Llama'],
endUtteranceSilenceThreshold: 1000,
// encoding: 'pcm_mulaw',
});
transcriber.on('open', ({ sessionId }) => {
console.log(`Transcriber opened with session ID: ${sessionId}`);
});
transcriber.on('error', (error: Error) => {
console.error('Transcriber error:', error);
await transcriber.close();
});
transcriber.on('close', (code: number, reason: string) => {
console.log(`Transcriber closed with code ${code} and reason: ${reason}`);
transcriber = null;
});
transcriber.on('transcript', (transcript: RealtimeTranscript) => {
if (!transcript.text) {
return;
}
// Detect if we're asking something for the LLM
setLlamaActive(transcript.text.toLowerCase().indexOf('llama') > 0);
if (transcript.message_type === 'PartialTranscript') {
transcriptionProcessed(transcript.text, false);
} else {
transcriptionProcessed(transcript.text, true);
if (transcript.text.toLowerCase().indexOf('llama') > 0) {
processPrompt(transcript.text);
}
}
});
return transcriber;
}
import { AssemblyAI } from 'assemblyai';
export async function POST(request: Request) {
const apiKey = process.env.ASSEMBLY_API_KEY;
if (!apiKey) {
return Response.error();
}
const client = new AssemblyAI({ apiKey: apiKey });
const body = await request.json();
const prompt = body?.prompt;
if (!prompt) {
return Response.error();
}
console.log('This is the prompt: ', prompt);
const finalPrompt = `You act as an assistant during a video call. You get a question and I want you to answer it directly without repeating it.
If you do not know the answer, clearly state that.
Here is the user question:
${prompt}`;
const lemurResponse = await client.lemur.task({
prompt: finalPrompt,
});
const response = {
prompt: prompt,
response: lemurResponse.response,
};
return Response.json(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment