|
| 1 | +# TwelveLabs Plugin |
| 2 | + |
| 3 | +This plugin brings [TwelveLabs](https://twelvelabs.io) **Pegasus** video |
| 4 | +understanding to Vision Agents as a first-class `VideoLLM`. |
| 5 | + |
| 6 | +Unlike frame-by-frame VLMs, Pegasus analyzes a short **video clip**, so it can |
| 7 | +reason about motion and events over time ("what just happened?") rather than a |
| 8 | +single still frame. Recent frames from the watched track are buffered, encoded |
| 9 | +into a short MP4 clip on demand, uploaded to the TwelveLabs Assets API, and |
| 10 | +analyzed with your prompt. The streamed answer is vocalized by the agent's TTS |
| 11 | +service. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +uv add "vision-agents[twelvelabs]" |
| 17 | +# or directly |
| 18 | +uv add vision-agents-plugins-twelvelabs |
| 19 | +``` |
| 20 | + |
| 21 | +You can grab a free API key at https://twelvelabs.io — there's a generous free |
| 22 | +tier. |
| 23 | + |
| 24 | +## Quick Start |
| 25 | + |
| 26 | +```python |
| 27 | +import asyncio |
| 28 | +import os |
| 29 | +from dotenv import load_dotenv |
| 30 | +from vision_agents.core import User, Agent, Runner |
| 31 | +from vision_agents.core.agents import AgentLauncher |
| 32 | +from vision_agents.plugins import deepgram, getstream, elevenlabs, twelvelabs |
| 33 | +from vision_agents.plugins.getstream import CallSessionParticipantJoinedEvent |
| 34 | + |
| 35 | +load_dotenv() |
| 36 | + |
| 37 | + |
| 38 | +async def create_agent(**kwargs) -> Agent: |
| 39 | + llm = twelvelabs.PegasusVLM( |
| 40 | + api_key=os.getenv("TWELVELABS_API_KEY"), # or set TWELVELABS_API_KEY |
| 41 | + ) |
| 42 | + |
| 43 | + agent = Agent( |
| 44 | + edge=getstream.Edge(), |
| 45 | + agent_user=User(name="My happy AI friend", id="agent"), |
| 46 | + llm=llm, |
| 47 | + tts=elevenlabs.TTS(), |
| 48 | + stt=deepgram.STT(), |
| 49 | + ) |
| 50 | + return agent |
| 51 | + |
| 52 | + |
| 53 | +async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None: |
| 54 | + call = await agent.create_call(call_type, call_id) |
| 55 | + |
| 56 | + @agent.events.subscribe |
| 57 | + async def on_participant_joined(event: CallSessionParticipantJoinedEvent): |
| 58 | + if event.participant.user.id != "agent": |
| 59 | + await asyncio.sleep(5) # let a few seconds of video buffer |
| 60 | + await agent.simple_response("Describe what just happened in the video") |
| 61 | + |
| 62 | + async with agent.join(call): |
| 63 | + await agent.finish() |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + Runner(AgentLauncher(create_agent=create_agent, join_call=join_call)).cli() |
| 68 | +``` |
| 69 | + |
| 70 | +## Configuration |
| 71 | + |
| 72 | +### PegasusVLM Parameters |
| 73 | + |
| 74 | +- `api_key`: str - TwelveLabs API key. If not provided, read from the |
| 75 | + `TWELVELABS_API_KEY` environment variable. |
| 76 | +- `model_name`: str - Pegasus model identifier (default: `"pegasus1.5"`). |
| 77 | +- `fps`: float - Frame sampling rate for the buffered clip (default: `1.0`). |
| 78 | +- `clip_seconds`: int - Length of the clip analyzed per request. Pegasus |
| 79 | + requires at least 4 seconds of video (default: `5`). |
| 80 | +- `max_tokens`: int - Maximum tokens in the response. Pegasus requires at least |
| 81 | + `512` (default: `512`). |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +- Pegasus requires a minimum resolution of 360x360; lower-resolution frames are |
| 86 | + scaled up to that floor on encode. |
| 87 | +- Pegasus requires the analyzed clip to be at least 4 seconds long, so |
| 88 | + `clip_seconds` must be `>= 4`. |
| 89 | +- Each request uploads a clip and runs server-side analysis, so latency is |
| 90 | + higher than single-frame VLMs. Tune `fps` and `clip_seconds` for your use |
| 91 | + case. |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +```bash |
| 96 | +# Unit tests (no API key needed) |
| 97 | +uv run pytest plugins/twelvelabs/tests -m "not integration" |
| 98 | + |
| 99 | +# Integration tests (needs TWELVELABS_API_KEY) |
| 100 | +export TWELVELABS_API_KEY="your-key-here" |
| 101 | +uv run pytest plugins/twelvelabs/tests -m integration |
| 102 | +``` |
| 103 | + |
| 104 | +## Links |
| 105 | + |
| 106 | +- [TwelveLabs Documentation](https://docs.twelvelabs.io/) |
| 107 | +- [Vision Agents Documentation](https://visionagents.ai/) |
| 108 | +- [GitHub Repository](https://github.com/GetStream/Vision-Agents) |
0 commit comments