Getting Started

An introduction to using Deepgram's text intelligence features to analyze text using Deepgram SDKs.

In this guide, you'll learn how to analyze text using Deepgram's text intelligence features: Summarization, Topic Detection, Intent Recognition, and Sentiment Analysis. The code examples use Deepgram's SDKs.

📘

Before you run the code, you'll need to follow the steps in this guide to create a Deepgram account, get a Deepgram API key, configure your environment, and install the SDK of your choice.

What is Text Intelligence?

Deepgram's Text Intelligence API lets users send a text source to Deepgram, and Deepgram will perform one of four types of analysis on the content of that text. Read about each feature in its individual feature guides:

Make the Request

A request made using one of the text intelligence features will follow the same form for each of the features; therefore, this guide will walk you through how to make one request, and you can use the feature(s) of your choice depending on which feature you want to use (Summarization, Topic Detection, Intent Recognition, or Sentiment Analysis).

Choose a Text

A text source can be sent to Deepgram as text (a text string or local text file) or as a url (hosted text file). These are referred to as a basic text request (string of text such as "This is a string of text.") or a basic url request (a hosted url such as https://YOUR_FILE_URL.txt).

Basic Text Request

This example shows how to analyze a local text file as your text source.

import os
from dotenv import load_dotenv

from deepgram import (
    DeepgramClient,
    AnalyzeOptions,
    TextSource,
)

load_dotenv()

# Path to the text file
TEXT_FILE = "conversation.txt"

API_KEY = os.getenv("DG_API_KEY")


def main():
    try:
        # STEP 1 Create a Deepgram client using the API key
        deepgram = DeepgramClient(API_KEY)

        with open(TEXT_FILE, "r") as file:
            buffer_data = file.read()

        payload: TextSource = {
            "buffer": buffer_data,
        }
				
        #STEP 2: Configure Deepgram options for text analysis
        options = AnalyzeOptions(
            language="en",
            sentiment=True,
            # intents=True,
            # summarize=True,
            # topics=True,
        )
				
        # STEP 3: Call the analyze_text method with the text payload and options
        response = deepgram.read.analyze.v("1").analyze_text(payload, options)

        print(response.to_json(indent=4))

    except Exception as e:
        print(f"Exception: {e}")


if __name__ == "__main__":
    main()
const { createClient } = require("@deepgram/sdk");
const fs = require("fs");

// path to text file
const text = fs.readFileSync("text.txt").toString();

const analyzeText = async () => {
  // STEP 1: Create a Deepgram client using the API key
  const deepgram = createClient(process.env.DEEPGRAM_API_KEY);

  // STEP 2: Call the analyzeText method with the text payload and options
  const { result, error } = await deepgram.read.analyzeText(
    {
      text,
    },
    // STEP 3: Configure Deepgram options for text analysis
    {
      language: "en",
      sentiment: true,
      // intents: true,
      // summarize: true,
      // topics: true,
    }
  );

  if (error) throw error;
  // STEP 4: Print the results
  if (!error) console.dir(result, { depth: null });
};

analyzeText();
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	prettyjson "github.com/hokaccha/go-prettyjson"

	analyze "github.com/deepgram/deepgram-go-sdk/pkg/api/analyze/v1"
	client "github.com/deepgram/deepgram-go-sdk/pkg/client/analyze"
	interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
)

// path to the text file to analyze
const (
	filePath string = "./conversation.txt"
)

func main() {
	// STEP 1: init Deepgram client library
	client.InitWithDefault()

	// STEP 2: define context to manage the lifecycle of the request
	ctx := context.Background()

	// STEP 3: define options for the request
	rOptions := interfaces.AnalyzeOptions{
		Language:  "en",
		Sentiment: true,
		// Summarize: true,
		// Topics: true,
		// Intents:  true,

	}

	// STEP 4: create a Deepgram client using default settings
	// NOTE: you can set your API KEY in your bash profile by typing the following line in your shell:
	// export DEEPGRAM_API_KEY = "YOUR_DEEPGRAM_API_KEY"
	c := client.NewWithDefaults()
	dg := analyze.New(c)

	// STEP 5: send/process file to Deepgram
	res, err := dg.FromFile(ctx, filePath, rOptions)
	if err != nil {
		fmt.Printf("FromFile failed. Err: %v\n", err)
		os.Exit(1)
	}

	// STEP 6: get the JSON response
	data, err := json.Marshal(res)
	if err != nil {
		fmt.Printf("json.Marshal failed. Err: %v\n", err)
		os.Exit(1)
	}

	// STEP 7: make the JSON pretty
	prettyJson, err := prettyjson.Format(data)
	if err != nil {
		fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}

Basic URL Request

This example shows how to analyze a hosted url file as your text source.

import os
from dotenv import load_dotenv

from deepgram import (
    DeepgramClient,
    AnalyzeOptions,
    UrlSource,
)

load_dotenv()

API_KEY = os.getenv("DG_API_KEY")

source: UrlSource = {
            "url": "https://static.deepgram.com/examples/aura.txt"
        } 

def main():
    try:
        # STEP 1 Create a Deepgram client using the API key
        deepgram = DeepgramClient(API_KEY)

        #STEP 2: Configure Deepgram options for text analysis
        options = AnalyzeOptions(
            language="en",
            sentiment=True,
            # intents=True,
            # summarize=True,
            # topics=True,
        )

        # STEP 3: Call the analyze_url method with the text source and options
        response = deepgram.read.analyze.v("1").analyze_url(source, options)

        print(response.to_json(indent=4))

    except Exception as e:
        print(f"Exception: {e}")


if __name__ == "__main__":
    main()

const { createClient } = require("@deepgram/sdk");

const analyzeUrl = async () => {
  // STEP 1: Create a Deepgram client using the API key
  const deepgram = createClient(process.env.DEEPGRAM_API_KEY);

  // STEP 2: Call the analyzeUrl method with the hosted url source and options
  const { result, error } = await deepgram.read.analyzeUrl(
    {
      url: "https://static.deepgram.com/examples/aura.txt",
    },
    // STEP 3: Configure Deepgram options for text analysis
    {
      language: "en",
      sentiment: true,
      // intents: true,
      // summarize: true,
      // topics: true,
    }
  );

  if (error) throw error;
  // STEP 4: Print the results
  if (!error) console.dir(result, { depth: null });
};

analyzeUrl();

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	prettyjson "github.com/hokaccha/go-prettyjson"

	analyze "github.com/deepgram/deepgram-go-sdk/pkg/api/analyze/v1"
	client "github.com/deepgram/deepgram-go-sdk/pkg/client/analyze"
	interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
)

// URL to the file to be sent to Deepgram
const (
	url string = "https://static.deepgram.com/examples/aura.txt"
)

func main() {
	// STEP 1: init Deepgram client library
	client.InitWithDefault()

	// STEP 2: define context to manage the lifecycle of the request
	ctx := context.Background()

	// STEP 3: define options for the request
	options := interfaces.AnalyzeOptions{
		Language:  "en",
		Sentiment: true,
		// Summarize: true,
		// Topics: true,
		// Intents:  true,

	}

	// STEP 4: create a Deepgram client using default settings
	// NOTE: you can set your API KEY in your bash profile by typing the following line in your shell:
	// export DEEPGRAM_API_KEY = "YOUR_DEEPGRAM_API_KEY"
	c := client.NewWithDefaults()
	dg := analyze.New(c)

	// STEP 5: send/process file to Deepgram
	res, err := dg.FromURL(ctx, url, options)
	if err != nil {
		fmt.Printf("FromFile failed. Err: %v\n", err)
		os.Exit(1)
	}

	// STEP 6: get the JSON response
	data, err := json.Marshal(res)
	if err != nil {
		fmt.Printf("json.Marshal failed. Err: %v\n", err)
		os.Exit(1)
	}

	// STEP 7: make the JSON pretty
	prettyJson, err := prettyjson.Format(data)
	if err != nil {
		fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}

Start the Application

Run your application from the terminal.

# Run your application using the file you created in the previous step
# Example: python deepgram_test.py
python YOUR_PROJECT_NAME.py
# Run your application using the file you created in the previous step
# Example: node index.js
node index.js
# Run your application using the file you created in the previous step
# Example: go run main.go

go run YOUR_PROJECT_NAME.go 

See Results

Your results will appear in your shell.

Analyze the Response

When the file is finished processing (often after only a few seconds), you’ll receive a JSON response:

{
  "metadata": {
    "request_id": "aff28024-3006-49e2-b70d-aabff2c23655",
    "created": "2024-01-30T15:22:33.604Z",
    "language": "en",
    "summary_info": {
      "model_uuid": "67875a7f-c9c4-48a0-aa55-5bdb8a91c34a",
      "input_tokens": 107,
      "output_tokens": 63
    }
  },
  "results": {
    "summary": {
      "text": "The speaker discusses the advances in speech recognition and spoken language understanding, citing examples such as the development of new transformer architectures for dealing with conversational audio and the use of model research for accurate transcriptions. They also mention the use of novel transformer architectures for handling conversational audio and the challenges of natural language understanding."
    }
  }
}
{
  "metadata": {
    "request_id": "6a0bdf68-ac01-47ae-96e3-b8fec7cb6477",
    "created": "2024-01-30T15:27:45.331Z",
    "language": "en",
    "topics_info": {
      "model_uuid": "ba5b22e4-b39a-4550-a4bc-d8655f5092bc",
      "input_tokens": 118,
      "output_tokens": 12
    }
  },
  "results": {
    "topics": {
      "segments": [
        {
          "text": "For nearly a decade, we’ve worked tirelessly to advance the art of the possible in speech recognition and spoken language understanding.",
          "start_word": 1,
          "end_word": 21,
          "topics": [
            {
              "topic": "Speech recognition",
              "confidence_score": 0.926069
            }
          ]
        },
        {
          "text": "Along the way, we’ve transcribed trillions of spoken words into highly accurate transcriptions.",
          "start_word": 21,
          "end_word": 33,
          "topics": [
            {
              "topic": "Transcripts",
              "confidence_score": 0.052929323
            }
          ]
        },
        {
          "text": "Our model research team has developed novel transformer architectures equipped to deal with the nuances of conversational audio–across different languages, accents, and dialects, while handling disfluencies and the changing rhythms, tones, cadences, and inflections that occur in natural, back-and-forth conversations.",
          "start_word": 34,
          "end_word": 73,
          "topics": [
            {
              "topic": "Conversational audio",
              "confidence_score": 0.63991606
            }
          ]
        }
      ]
    }
  }
}
{
  "metadata": {
    "request_id": "55dd2a7e-9fde-48c6-8db0-b00a98291c5a",
    "created": "2024-01-30T15:29:38.662Z",
    "language": "en",
    "intents_info": {
      "model_uuid": "80ab3179-d113-4254-bd6b-4a2f96498695",
      "input_tokens": 118,
      "output_tokens": 14
    }
  },
  "results": {
    "intents": {
      "segments": [
        {
          "text": "Our model research team has developed novel transformer architectures equipped to deal with the nuances of conversational audio–across different languages, accents, and dialects, while handling disfluencies and the changing rhythms, tones, cadences, and inflections that occur in natural, back-and-forth conversations.",
          "start_word": 34,
          "end_word": 73,
          "intents": [
            {
              "intent": "Propose novel transformer architectures",
              "confidence_score": 0.0000743953
            },
            {
              "intent": "Address conversational audio",
              "confidence_score": 0.07617511
            }
          ]
        }
      ]
    }
  }
}
{
  "metadata": {
    "request_id": "7c5516be-366c-499f-b0ff-11bc058f631a",
    "created": "2024-01-30T15:30:22.130Z",
    "language": "en",
    "sentiment_info": {
      "model_uuid": "ba5b22e4-b39a-4550-a4bc-d8655f5092bc",
      "input_tokens": 118,
      "output_tokens": 118
    }
  },
  "results": {
    "sentiments": {
      "segments": [
        {
          "text": "For nearly a decade, we’ve worked tirelessly to advance the art of the possible in speech recognition and spoken language understanding. Along the way, we’ve transcribed trillions of spoken words into highly accurate transcriptions.",
          "start_word": 0,
          "end_word": 33,
          "sentiment": "positive",
          "sentiment_score": 0.5421093702316284
        },
        {
          "text": "Our model research team has developed novel transformer architectures equipped to deal with the nuances of conversational audio–across different languages, accents, and dialects, while handling disfluencies and the changing rhythms, tones, cadences, and inflections that occur in natural, back-and-forth conversations.",
          "start_word": 34,
          "end_word": 73,
          "sentiment": "neutral",
          "sentiment_score": 0.27087897062301636
        }
      ],
      "average": {
        "sentiment": "positive",
        "sentiment_score": 0.3812099715410653
      }
    }
  }
}

Following are explanations of each of the example responses. Be sure to click the tabs in the code block above to view the example response for each text analysis feature.

Summarization

In the metadata object, we see:

  • summary_info: information about the model used and the input/output tokens. Summarization pricing is based on the number of input and output tokens. Read more at deepgram.com/pricing.

In the results object, we see:

  • summary: the text property in this object gives you the summary of the text you requested to be analyzed.

Topic Detection

In the metadata object, we see:

  • topics_info: information about the model used and the input/output tokens. Topic Detection pricing is based on the number of input and output tokens. Read more at deepgram.com/pricing.

In the results object, we see:

  • topics(object): contains the data about Topic Detection.
  • segments: each segment object contains a span of text taken from the input text; this text segment is analyzed for its topic.
  • topics(array): a list of topic objects, each containing the topic and a confidence_score.
    • topic: Deepgram analyzes the segmented text to identify the main topic of each.
    • confidence_score: a floating point value between 0 and 1 indicating the overall reliability of the analysis.

Intent Recognition

In the metadata object, we see:

  • intents_info: information about the model used and the input/output tokens. Intent Recognition pricing is based on the number of input and output tokens. Read more at deepgram.com/pricing.

In the results object, we see:

  • intents(object): contains the data about Intent Recognition.
  • segments: each segment object contains a span of text taken from the input text; this text segment is analyzed for its intent.
  • intents(array): a list of intent objects, each containing the intent and a confidence_score.
    • intent: Deepgram analyzes the segmented text to identify the intent of each.
    • confidence_score: a floating point value between 0 and 1 indicating the overall reliability of the analysis.

Sentiment Analysis

In the metadata object, we see:

  • sentiment_info: information about the model used and the input/output tokens. Sentiment Analysis pricing is based on the number of input and output tokens. Read more at deepgram.com/pricing.

In the results object, we see:

  • sentiments(object): contains the data about Sentiment Analysis.
  • segments: each segment object contains a span of text taken from the input text; these segments of text show when the sentiment shifts throughout the text, and each one is analyzed for its sentiment.
  • sentiment can be positive, negative, or neutral.
  • sentiment_score: a floating point value between -1 and 1 representing the sentiment of the associated span of text, with -1 being the most negative sentiment, and 1 being the most positive sentiment.
  • average: the average sentiment for the entire input text.

Constraints

Here are a few constraints to keep in mind when making your request.

Language

At this time, text analysis features only work for English language texts. You must add a language parameter and set it to English when you make a text analysis request.

options = AnalyzeOptions(
            language="en",
            summarize=True,
        )

Token Limit

The input token limit is 150K tokens. When that limit is exceeded, a 400 error will be thrown.

{
  "err_code": "TOKEN_LIMIT_EXCEEDED",
  "err_msg": "Text input currently supports up to 150K tokens. Please revise your text input to fit within the defined token limit. For more information, please visit our API documentation.",
  "request_id": "XXXX"
}