How to combine spoken audio into LlamaIndex.TS utilizing AssemblyAI

How to combine spoken audio into LlamaIndex.TS utilizing AssemblyAI

How to integrate spoken audio into LlamaIndex.TS using AssemblyAI

In this tutorial, you may discover ways to create an utility that may reply your questions on an audio file, utilizing LlamaIndex.TS and AssemblyAI’s new integration with LlamaIndex.

LlamaIndex is a versatile knowledge framework for connecting customized knowledge sources to Large Language Models (LLMs).
With LlamaIndex, you possibly can simply retailer and index your knowledge, after which use them with LLMs to construct functions.
However, LLMs solely function on textual knowledge and don’t perceive what is alleged in audio information. That’s why we contributed an integration with AssemblyAI to LlamaIndex.TS so builders can transcribe audio information to textual content in LlamaIndex.

Prerequisites for working with audio knowledge in LlamaIndex

To comply with alongside, you may want the next:
Node.js (model 18 or above)
An OpenAI account and an OpenAI API key
An AssemblyAI account and API key

Sign up for an AssemblyAI account to get an API key free of charge, or signal into your AssemblyAI account, then seize your API key from the dashboard.

Set up your TypeScript Node.js mission

Next, for those who do not have already got a Node.js utility, create a brand new listing and run npm init.

mkdir transcript-llamaindex
cd transcript-llamaindex
npm init -y

Install typescript and initialize your TypeScript mission:

npm set up --save-dev typescript
npx tsc --init

Next, add tsx, a instrument to simply execute TypeScript:

npm set up --save-dev tsx

Create a file named index.ts and add the next code:

console.log("Hello World!")

Then, run npx tsx index.ts to execute your TypeScript:

npx tsx index.ts

The output must be Hello World!.

Configure setting variables

The utility you are constructing wants your OpenAI API key and AssemblyAI API key. A standard approach to do that is by storing them right into a .env file and loading the .env file firstly of your utility.

First, create a brand new .env file with the next contents:

OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
ASSEMBLYAI_API_KEY=<YOUR_ASSEMBLYAI_API_KEY>

Then change <YOUR_OPENAI_API_KEY> together with your OpenAI API key, and <YOUR_ASSEMBLYAI_API_KEY> together with your AssemblyAI API key.

Then, set up the dotenv module and the @sorts/node module for some TypeScript sorts that you’re going to use:

npm set up dotenv --save
npm i --save-dev @sorts/node

Update the index.ts code to load the setting variables and print them to the console:

import 'dotenv/config';
console.log(course of.env);

Finally, run the code to see the outcome:

npx tsx index.ts

You’ll see all of your setting variables printed out, together with the OPENAI_API_KEY and ASSEMBLYAI_API_KEY.

Note

Make positive to by no means test your API keys and different secrets and techniques into supply management, by hard-coding or unintentionally including the .env file. Keep these secrets and techniques protected and safe!

Add LlamaIndex.TS and create a Q&A sequence

First, add LlamaIndex.TS utilizing NPM or your most popular bundle supervisor:

npm set up llamaindex --save

Next, replace the index.ts code with the next query and solutions (Q&A) pattern:

import "dotenv/config";
import { Document, VectorStoreIndex } from "llamaindex";

async perform predominant() {
  // Transcribe audio and retailer transcript in paperwork
  const docs = [new Document({ text: "I'm using LlamaIndex to work with LLMs"})]

  // Split textual content and create embeddings. Store them in a VectorStoreIndex
  const index = await VectorStoreIndex.fromDocuments(docs);

  // Query the index
  const queryEngine = index.asQueryEngine();
  const response = await queryEngine.question("What LLM framework am I utilizing?");

  // Output response
  console.log(response.toString());
}

predominant();

The code above hardcodes a doc to supply context to the LLM. The VectorStoreIndex creates embedding and indexes the hardcoded doc. Then the appliance queries the index with the query "What LLM framework am I utilizing?". The question engine will pull the related context from the index, and ship the doc and query to OpenAI’s LLM. The response from the LLM is then printed to the console.

If you run the code utilizing npx tsx index.ts, the output shall be one thing like "You are utilizing the LlamaIndex framework.".

In the upcoming part, you may use AssemblyAI’s transcript reader to transcribe an audio file and put the transcript in a doc as a substitute of the hard-coded doc above.

Transcribe audio with the LlamaIndex AudioTranscriptReader

The llamaindex module comes with the brand new readers from AssemblyAI. To use the readers, import them from the module. Update the index.ts file with the code under:

import "dotenv/config";
import { VectorStoreIndex, AudioTranscriptReader } from "llamaindex";

async perform predominant() {
  const reader = new AudioTranscriptReader();
  // Transcribe audio and retailer transcript in paperwork
  const docs = await reader.loadData({
    // You may use an area path to an audio file, like ./sports_injuries.mp3
    audio: "https://storage.googleapis.com/aai-docs-samples/sports_injuries.mp3",
    language_code: "en_us",
  });

  // Split textual content and create embeddings. Store them in a VectorStoreIndex
  const index = await VectorStoreIndex.fromDocuments(docs);

  // Query the index
  const queryEngine = index.asQueryEngine();
  const response = await queryEngine.question("What is a runner's knee?");

  // Output response
  console.log(response.toString());
}

predominant();

The AudioTranscriptReader makes use of AssemblyAI’s API to transcribe the audio file handed to audio. The AudioTranscriptReader.loadData() perform creates an array with a single doc containing the transcript textual content.

Note

You may move in an area file path to the audio property and the loader will add the file so that you can AssemblyAI’s CDN.

Now that the AudioTranscriptReader transcribed the sports activities accidents audio file, the LLM can provide a well-informed reply concerning the audio.

Run the appliance once more utilizing npx tsx index.ts. The output ought to seem like this:

Runner's knee is a situation characterised by ache behind or across the kneecap. It is brought on by overuse muscle imbalance and insufficient stretching. Symptoms embody ache beneath or across the kneecap, ache when strolling.

Info

Check out the LlamaIndex.TS integration docs to discover ways to use the extra readers supplied by AssemblyAI.

Conclusion

In this tutorial, you realized concerning the new AssemblyAI integration that was added to LlamaIndex.TS.
You created a Q&A utility that may reply questions on an audio file, by leveraging AssemblyAI’s speech-to-text APIs, OpenAI, and LlamaIndex.TS.

Try LeMUR now

You can be taught extra about LeMUR or strive it out in our Playground utilizing the hyperlink under

Check it out

HI-FI News

through News, Tutorials, AI Research https://ift.tt/aWyvQ50

December 6, 2023 at 05:58PM