Pyvespa: Python API for Vespa.ai

1.1.2 · active · verified Tue Apr 14

Pyvespa provides a Python API to Vespa, the open-sourced serving engine for storing, computing, and ranking big data at user serving time. It enables users to create, modify, deploy, and interact with running Vespa instances, facilitating faster prototyping and familiarization with Vespa features. The current version is 1.1.2. Releases are generally frequent, with minor versions released multiple times a week for the main Vespa engine and `pyvespa` releases following to maintain compatibility and add features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple Vespa application schema, deploy it to a local Docker instance using `VespaDocker`, feed documents, and execute a basic query. It covers the core workflow for local development with pyvespa.

import time
from pyvespa.application import ApplicationPackage
from pyvespa.schema import Schema, Document, Field
from pyvespa.clients.vespa_docker import VespaDocker
from pyvespa.clients.vespa import Vespa

# 1. Define your application schema
app_package = ApplicationPackage(
    name='my_app',
    schema=Schema(
        name='my_document',
        document=Document(
            fields=[
                Field(name='id', type='string', indexing=['attribute', 'summary']),
                Field(name='title', type='string', indexing=['index', 'summary'], index='enable-bm25'),
                Field(name='body', type='string', indexing=['index', 'summary'], index='enable-bm25')
            ]
        )
    )
)

# 2. Deploy to local Docker instance
# Ensure Docker daemon is running and has at least 6GB memory allocated
vespa_docker = VespaDocker(port=8080, container_memory='6G')
try:
    app = vespa_docker.deploy(application_package=app_package)
    print("Vespa application deployed successfully to Docker.")

    # 3. Feed documents
    docs_to_feed = [
        {"id": "doc:1", "title": "The Quick Brown Fox", "body": "Jumps over the lazy dog."}, 
        {"id": "doc:2", "title": "Lazy Dog Sits", "body": "The quick brown fox watches."}
    ]
    app.feed_iterable(docs_to_feed)
    print("Documents fed.")

    # Wait a bit for indexing to complete
    time.sleep(5)

    # 4. Query data
    query_result = app.query(yql='select * from sources * where userQuery();', query='fox')
    print("Query Results:")
    for hit in query_result.hits:
        print(f"  ID: {hit['id']}, Title: {hit['fields']['title']}, Body: {hit['fields']['body']}")

finally:
    # 5. Shut down Vespa Docker instance
    vespa_docker.stop()
    print("Vespa Docker instance stopped.")

view raw JSON →