Open-ended text
Use the /generate
endpoint for generating open-ended texts from videos that are more customizable and tailor-made than the results provided by the /summarize
endpoint. This endpoint can generate diverse results based on your prompts, including, but not limited to, tables of content, action items, memos, reports, and comprehensive analyses.
Below are examples of prompts designed to generate specific content types. To view them in the Playground, ensure you’re logged in, then open the URLs in the Playground column.
For a description of each field in the request and response, see the API Reference > Generate open-ended texts page.
Prerequisites
The examples in this guide assume the following:
- You’re familiar with the concepts that are described on the Platform overview page.
- You’ve already created an index and the Pegasus video understanding engine is enabled for this index.
- You’ve uploaded a video, and the platform has finished indexing it.
Examples
When generating open-ended texts, the default behavior of the platform is to stream responses. This enables real-time processing of partial results, enhances the user experience with immediate feedback and significantly reduces the perceived latency.
You can choose whether the model generates streaming responses or non-streaming responses. For details, see one of the sections below:
Streaming responses
For streaming responses, you must invoke the text_stream
method of the generate
object. The response consists of a stream of JSON objects, each on its own line, following the NDJSON format. Each object represents an event in the generation process, with three event types:
stream_start
: Indicates the beginning of the stream. When you receive this event, initialize your processing logic.
Example:text_generation
: Contains a fragment of generated text. Astext_generation
events arrive, handle the text fragments based on your application’s needs. This might involve displaying the text in real-time, analyzing it, or storing it for later use. Note that these fragments may be of varying lengths and are not guaranteed to align with word or sentence boundaries.
Example:stream_end
: Indicates the end of the stream. When you receive this event, finalize your processing logic.
Example:
To use streaming responses in your application:
- Start a stream by invoking the
textStream
method of thegenerate
object with the following parameters:video_id
: A string representing the unique identifier of your videoprompt
: A string that guides the model on the desired format or content.
- Use a loop to iterate over the stream.
- Inside the loop, handle each text fragment as it arrives. This example prints each fragment to the standard output.
- (Optional) After the stream ends, use the
textStream.aggregatedText
field if you need the full generated text.
The example code below demonstrates using the SDKs to generate and process a streaming response. It starts a stream for a specified video and prompt, prints each text fragment as it arrives, and prints the complete aggregated text. Ensure you replace the placeholders surrounded by <>
with your values.
The output should look similar to the following:
Non-streaming responses
For streaming responses, you must invoke the text
method of the generate
object. The following example generates a brief summary with a specific format by invoking the text
method of the generate
object with the following parameters:
video_id
: A string representing the unique identifier of the video for which you want to generate a title.prompt
: A string that guides the model on the desired format or content.
The output should be similar to the following one:
The following example generates a police report based on the provided template:
The output should be similar to the following one:
The following example displays the key takeaways of a video:
The output should be similar to the following one:
The following example identifies the creative approach of a video:
The output should be similar to the following one: