millions-greece-12478
08/01/2025, 4:22 AMtall-airline-16756
08/01/2025, 4:22 AMtall-airline-16756
08/01/2025, 4:23 AMImageContent from the LiveKit library to create an image object. Initialize it with a base64 data URL, an external URL, or a VideoFrame object.
from livekit.agents.llm import ImageContent, ChatContext
image_content = ImageContent(image="<your_image_url>")
2. Add Image to Chat Context: Add the ImageContent to the agent's chat context to make the image available for the LLM to process.
initial_ctx = ChatContext()
initial_ctx.add_message(
role="user",
content=["Here is the image:", image_content]
)
3. Upload Image from Frontend (Optional): Use the LiveKit SDK's sendFile method to upload images dynamically. Add a byte stream handler to your agent to receive the image data and add it to the chat context.
4. Implement Tool to Return Image: Define a tool in your agent to generate or return an image using the @function_tool decorator.
@function_tool()
async def return_image(self, context: RunContext, prompt: str) -> bytes:
# Generate or fetch the image based on the prompt
return image_bytes
For a complete example, you can define an agent that generates or fetches images dynamically and integrates them into the chat context. Let me know if you need further clarification or assistance!millions-greece-12478
08/01/2025, 2:38 PMtall-airline-16756
08/01/2025, 2:39 PM@function_tool decorator to create a tool that handles taking screenshots. This tool will manage the process of triggering the screenshot and returning the result.
from livekit.agents import function_tool, Agent, RunContext
class MyAgent(Agent):
@function_tool()
async def capture_screenshot(self, context: RunContext, name: str = "screenshot_1") -> dict:
"""Capture a screenshot of the screen."""
try:
# Platform-specific implementation here
screenshot = capture_screen()
return {"screenshot": screenshot}
except Exception as e:
return {"error": str(e)}
2. Platform-Specific Implementation: You’ll want to tailor the screenshot functionality depending on the platform you’re working on:
◦ iOS: Use RPScreenRecorder for capturing the screen within your app.
◦ Android: Use MediaProjectionManager to ask for permission to capture the screen.
◦ macOS: Use ScreenCaptureKit to record the screen.
◦ Unity: Use ScreenCapture.CaptureScreenshotIntoRenderTexture to grab screenshots.
1. Register the Tool: Finally, add your tool to the LiveKit Agent and make sure it works smoothly with the chat context so the captured screenshot shows up nicely.
If you want me to walk you through any part in more detail or need examples tailored to your platform, just let me know—I’m here to help!millions-greece-12478
08/14/2025, 4:41 PMtall-airline-16756
08/14/2025, 4:42 PMImageContent object. You can initialize it with a base64 data URL, an external URL, or a VideoFrame.
import base64
from livekit.agents.llm import ImageContent
# Convert image bytes to base64
base64_image = f"data:image/png;base64,{base64.b64encode(image_bytes).decode('utf-8')}"
# Create ImageContent
chat_image = ImageContent(image=base64_image)
2. Add ImageContent to ChatContext: Next, use the add_message method to add the image into the chat context.
from livekit.agents.llm import ChatContext
chat_ctx = ChatContext()
chat_ctx.add_message(
role="user",
content=[chat_image]
)
3. Return Value: Your tool should send back a confirmation message or the image data itself. Returning a dictionary is optional but can be handy if you want to include extra info like success status, image URL, or the base64 string for debugging or further steps.
By adding the ImageContent to the chat context, the LLM can easily work with the image alongside any text. If anything’s unclear or you want to chat more about this, just let me know—I’m here to help!
Sources: tests/test_chat_ctx.pymillions-greece-12478
08/14/2025, 4:47 PMtall-airline-16756
08/14/2025, 4:47 PMFunctionCall object with details like the tool's name and arguments.
2. Execute the Tool: The tool runs asynchronously, and the agent’s state switches to "thinking" to show it’s processing.
3. Handle the Tool Output: Once the tool finishes, it produces a FunctionCallOutput with the result or an error. This gets added to the chat context.
4. Update the Chat Context: Add both the FunctionCall and FunctionCallOutput to the chat context. This keeps the conversation history complete with the tool interaction.
5. Resume the Conversation: Use the updated chat context to generate the next response, so the conversation flows smoothly.
For example, if the agent calls a tool to fetch weather data, the chat context might look like this:
{
"items": [
{
"type": "function_call",
"name": "get_weather",
"arguments": "{\"location\": \"Tokyo\"}"
},
{
"type": "function_call_output",
"output": "The weather in Tokyo is sunny today."
}
]
}
This way, the conversation stays clear and informed by the tool’s output. If you want me to explain anything more, just let me know—I’m here to help!millions-greece-12478
08/14/2025, 5:10 PMtall-airline-16756
08/14/2025, 5:11 PMImageContent class to add that screenshot right into the chat context. Here’s a friendly tweak to your tool to make that happen:millions-greece-12478
08/14/2025, 9:07 PMtall-airline-16756
08/14/2025, 9:07 PM