Skip to main content

Before you Begin

For this quickstart tutorial, make sure you have an Archetype API key. For support with API keys, please contact [email protected].

1. Setup

If you haven’t already done so, see these instructions to install the Archetype AI python client or directly run the following in your terminal:
pip install archetypeai
export ATAI_API_KEY="insert_your_key"
Note: Replace "insert_your_key" with your actual API key from Archetype AI.

2. Create and Run a Lens Session

Next, you will create a Python example that defines and runs a custom lens to analyze a pre-recorded video. A Lens defines how data flows into and out of Newton and how that data is interpreted for a given use case. Open the following code snippet in your text editor to get started:
import argparse
from archetypeai.api_client import ArchetypeAI

def main(args):
    # Create a new client using your unique API key.
    client = ArchetypeAI(args.api_key)

    # Upload the video file to the archetype platform.
    file_response = client.files.local.upload(args.filename)

    # Define a custom lens that reads from a video and writes to an SSE stream.
    client.lens.create_and_run_lens(f"""
       lens_name: Custom Activity Monitor
       lens_config:
        model_parameters:
            model_version: Newton::c2_3_7b_2508014e10af56
            instruction: Analyze the video with the following focus.
            focus: Delivery
            temporal_focus: 5
            max_new_tokens: 128
        input_streams:
            - stream_type: video_file_reader
              stream_config:
                file_id: {file_response['file_id']}
        output_streams:
            - stream_type: server_side_events_writer
    """, session_callback, client=client)

def session_callback(session_id: str, session_endpoint: str, client: ArchetypeAI):
    """Main function to run the logic of a custom lens session."""

    # Create a SSE reader to read the output of the lens.
    sse_reader = client.lens.sessions.create_sse_consumer(session_id)

    # Read events from the SSE stream until either the last message is received.
    for event in sse_reader.read(block=True):
        print(f"[sse_reader] {event}")

    # Close any active reader.
    sse_reader.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--api_key", required=True, type=str)
    parser.add_argument("--filename", required=True, type=str)
    args = parser.parse_args()
    main(args)
To run this example, save the code above as example.py, then run the following in your terminal in the same directory as the example:
python example.py --api_key=$ATAI_API_KEY --filename=your_test_video.mp4
Note: Replace your_test_video.mp4 with the actual name of your video file. Download our sample delivery video to follow along: Delivery.mp4

3. What you should see

After running the command, you should see the description of the video stream live in your terminal. Here’s an example response from the terminal: 'response': ['The video shows a FedEx Ground delivery truck parked on a residential driveway. A delivery person exits the truck carrying a package. The environment appears to be a suburban neighborhood with well-maintained lawns and a clear sky.
Lenses created via the API will appear in your Workbench where you can monitor sessions, view results, and manage your lens configurations.

Optional: Try Variations

Using the same video (or a starter video), experiment with how Lens parameters affect the output:
  • Change Focus to People or Vehicles and observe the difference.
  • Edit Instructions using the template below and see how behavior and output change (A prompt best practices section is coming soon):
    Role:
    You are a smart camera monitoring a conveyor belt in a manufacturing facility.
    
    Objective:
    Detect complete work stoppages that require human intervention.
    
    Output:
    Return alerts in the following format:
    ALERT: <reason in 5–7 words>
    
  • Adjust Temporal Focus to 2 or 20 to compare shorter vs. longer time windows.
Learn more in the Activity Monitor Lens section of Core Concepts.

Troubleshooting

If you’re getting errors in the quickstart, please ensure you have the latest version of the python client installed 25.12.15.
  • To show the latest version of the python client, please use pip show archetypeai
  • To upgrade the latest version of the python client, please use
    pip install --upgrade archetypeai
  • Request access
  • Support

Learn More