!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   Audio/Video Structured Extraction

On this page

Audio/Video Structured Extraction
=================================

Google's Gemini API offers support for audio and video input, along with function calling. Together, we can pair these API features to extract structured data given audio or video input.

In the following examples, we'll demonstrate how to read and send MP3 and MP4 files to the Gemini API, and receive structured output as a response.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

These examples use the Gemini API, so you'll need a Google VertexAI credentials file (or stringified credentials file if using a web environment):

    GOOGLE_APPLICATION_CREDENTIALS="credentials.json"

Next, install the `@langchain/google-vertexai` and `@langchain/community` packages:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/google-vertexai @langchain/core

    yarn add @langchain/google-vertexai @langchain/core

    pnpm add @langchain/google-vertexai @langchain/core

Video[​](#video "Direct link to Video")
---------------------------------------

This example uses a [LangChain YouTube video on datasets and testing in LangSmith](https://www.youtube.com/watch?v=N9hjO-Uy1Vo) sped up to 1.5x speed. It's then converted to `base64`, and sent to Gemini with a prompt asking for structured output of tasks I can do to improve my knowledge of datasets and testing in LangSmith.

We create a new tool for this using Zod, and pass it to the model via the `withStructuredOutput` method.

    import {  ChatPromptTemplate,  MessagesPlaceholder,} from "@langchain/core/prompts";import { ChatVertexAI } from "@langchain/google-vertexai";import { HumanMessage } from "@langchain/core/messages";import fs from "fs";import { z } from "zod";function fileToBase64(filePath: string): string {  return fs.readFileSync(filePath, "base64");}const lanceLsEvalsVideo = "lance_ls_eval_video.mp4";const lanceInBase64 = fileToBase64(lanceLsEvalsVideo);const tool = z.object({  tasks: z.array(z.string()).describe("A list of tasks."),});const model = new ChatVertexAI({  model: "gemini-1.5-pro-preview-0409",  temperature: 0,}).withStructuredOutput(tool, {  name: "tasks_list_tool",});const prompt = ChatPromptTemplate.fromMessages([  new MessagesPlaceholder("video"),]);const chain = prompt.pipe(model);const response = await chain.invoke({  video: new HumanMessage({    content: [      {        type: "media",        mimeType: "video/mp4",        data: lanceInBase64,      },      {        type: "text",        text: `The following video is an overview of how to build datasets in LangSmith.Given the following video, come up with three tasks I should do to further improve my knowledge around using datasets in LangSmith.Only reference features that were outlined or described in the video.Rules:Use the "tasks_list_tool" to return a list of tasks.Your tasks should be tailored for an engineer who is looking to improve their knowledge around using datasets and evaluations, specifically with LangSmith.`,      },    ],  }),});console.log("response", response);/*response {  tasks: [    'Explore the LangSmith SDK documentation for in-depth understanding of dataset creation, manipulation, and versioning functionalities.',    'Experiment with different dataset types like Key-Value, Chat, and LLM to understand their structures and use cases.',    'Try uploading a CSV file containing question-answer pairs to LangSmith and create a new dataset from it.'  ]}*/

#### API Reference:

*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [MessagesPlaceholder](https://api.js.langchain.com/classes/langchain_core_prompts.MessagesPlaceholder.html) from `@langchain/core/prompts`
*   [ChatVertexAI](https://api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

Audio[​](#audio "Direct link to Audio")
---------------------------------------

The next example loads an audio (MP3) file containing Mozart's Requiem in D Minor and prompts Gemini to return a single array of strings, with each string being an instrument from the song.

Here, we'll also use the `withStructuredOutput` method to get structured output from the model.

    import {  ChatPromptTemplate,  MessagesPlaceholder,} from "@langchain/core/prompts";import { ChatVertexAI } from "@langchain/google-vertexai";import { HumanMessage } from "@langchain/core/messages";import fs from "fs";import { z } from "zod";function fileToBase64(filePath: string): string {  return fs.readFileSync(filePath, "base64");}const mozartMp3File = "Mozart_Requiem_D_minor.mp3";const mozartInBase64 = fileToBase64(mozartMp3File);const tool = z.object({  instruments: z    .array(z.string())    .describe("A list of instruments found in the audio."),});const model = new ChatVertexAI({  model: "gemini-1.5-pro-preview-0409",  temperature: 0,}).withStructuredOutput(tool, {  name: "instruments_list_tool",});const prompt = ChatPromptTemplate.fromMessages([  new MessagesPlaceholder("audio"),]);const chain = prompt.pipe(model);const response = await chain.invoke({  audio: new HumanMessage({    content: [      {        type: "media",        mimeType: "audio/mp3",        data: mozartInBase64,      },      {        type: "text",        text: `The following audio is a song by Mozart. Respond with a list of instruments you hear in the song.Rules:Use the "instruments_list_tool" to return a list of tasks.`,      },    ],  }),});console.log("response", response);/*response {  instruments: [    'violin',   'viola',    'cello',    'double bass',    'flute',    'oboe',    'clarinet', 'bassoon',    'horn',     'trumpet',    'timpani'  ]}*/

#### API Reference:

*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [MessagesPlaceholder](https://api.js.langchain.com/classes/langchain_core_prompts.MessagesPlaceholder.html) from `@langchain/core/prompts`
*   [ChatVertexAI](https://api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

From a quick Google search, we see the song was composed using the following instruments:

    The Requiem is scored for 2 basset horns in F, 2 bassoons, 2 trumpets in D, 3 trombones (alto, tenor, and bass),timpani (2 drums), violins, viola, and basso continuo (cello, double bass, and organ).

Gemini did pretty well here! For music not being its primary focus, it was able to identify a few of the instruments used in the song, and didn't hallucinate any!

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Code Understanding

](/v0.1/docs/use_cases/code_understanding/)

*   [Setup](#setup)
*   [Video](#video)
*   [Audio](#audio)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.