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 start, you’ll need to follow the steps in the Make Your First API Request guide to obtain a Deepgram API key, and configure your environment if you are choosing to use a Deepgram SDK.

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:

API Playground

First, quickly explore Deepgram Text Intelligence in our API Playground.

Deepgram API Playground Try this feature out in our API Playground!

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.

1const { createClient } = require("@deepgram/sdk");
2const fs = require("fs");
3
4// path to text file
5const text = fs.readFileSync("text.txt").toString();
6
7const analyzeText = async () => {
8 // STEP 1: Create a Deepgram client using the API key
9 const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
10
11 // STEP 2: Call the analyzeText method with the text payload and options
12 const { result, error } = await deepgram.read.analyzeText(
13 {
14 text,
15 },
16 // STEP 3: Configure Deepgram options for text analysis
17 {
18 language: "en",
19 sentiment: true,
20 // intents: true,
21 // summarize: true,
22 // topics: true,
23 }
24 );
25
26 if (error) throw error;
27 // STEP 4: Print the results
28 if (!error) console.dir(result, { depth: null });
29};
30
31analyzeText();

Basic URL Request

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

1const { createClient } = require("@deepgram/sdk");
2
3const analyzeUrl = async () => {
4 // STEP 1: Create a Deepgram client using the API key
5 const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
6
7 // STEP 2: Call the analyzeUrl method with the hosted url source and options
8 const { result, error } = await deepgram.read.analyzeUrl(
9 {
10 url: "https://static.deepgram.com/examples/aura.txt",
11 },
12 // STEP 3: Configure Deepgram options for text analysis
13 {
14 language: "en",
15 sentiment: true,
16 // intents: true,
17 // summarize: true,
18 // topics: true,
19 }
20 );
21
22 if (error) throw error;
23 // STEP 4: Print the results
24 if (!error) console.dir(result, { depth: null });
25};
26
27analyzeUrl();

Start the Application

Run your application from the terminal.

1# Run your application using the file you created in the previous step
2# Example: node index.js
3node index.js

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:

1{
2 "metadata": {
3 "request_id": "aff28024-3006-49e2-b70d-aabff2c23655",
4 "created": "2024-01-30T15:22:33.604Z",
5 "language": "en",
6 "summary_info": {
7 "model_uuid": "67875a7f-c9c4-48a0-aa55-5bdb8a91c34a",
8 "input_tokens": 107,
9 "output_tokens": 63
10 }
11 },
12 "results": {
13 "summary": {
14 "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."
15 }
16 }
17}

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.

Python
1options = AnalyzeOptions(
2 language="en",
3 summarize=True,
4 )

Token Limit

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

JSON
1{
2 "err_code": "TOKEN_LIMIT_EXCEEDED",
3 "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.",
4 "request_id": "XXXX"
5}
Built with