example of python livekit agent that looks at the ...
# ask-ai
n
example of python livekit agent that looks at the latest image frame, and analyzes it. But doesn't leave it within the context window
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you analyze the latest image frame without keeping it in the context window. A great way to do this is by using a vision model to process the frame and then include just the analysis results—like a textual description or metadata—in the chat context. Here’s a friendly approach you can try: 1. Use a vision model such as Moondream to create a text description of the image. This way, only the text description gets added to the chat context, not the actual image. 2. Alternatively, you might use a function tool to perform local or remote vision analysis that returns structured results (like JSON with detected objects), again without adding the image itself to the chat context. For example, the Moondream model processes the frame and adds a text caption to the chat message while clearing the frame afterwards:
Copy code
def _send_frame_to_moondream(self, frame):
    try:
        rgb_frame = frame.convert(proto_video.VideoBufferType.RGB24)
        image = Image.frombytes("RGB", (rgb_frame.width, rgb_frame.height), rgb_frame.data.tobytes())
        caption = self._md_model.caption(image).get("caption")
        return caption
    except Exception as exc:
        logger.error("Error sending frame to Moondream: %s", exc)
        return None

async def on_user_turn_completed(self, turn_ctx, new_message):
    if self._latest_frame:
        caption = self._send_frame_to_moondream(self._latest_frame)
        if caption:
            new_message.content.append(f"[Image description: {caption}]")
        self._latest_frame = None  # Clear frame after use
This way, the image won’t be stored in the context window at all. If you have any questions or would like me to walk you through more examples, just let me know—I’m here to help! Sources: complex-agents/vision/README.md | README.md