Pinecone Plugin Interface

0.0.7 · active · verified Thu Apr 09

The Pinecone Plugin Interface provides base classes and models for developing custom plugins that extend the functionality of the Pinecone Python client. This allows users to implement custom logic for tasks like query pre-processing (text plugins) or result post-processing (filter plugins). The current version is 0.0.7, indicating an early-stage library with potential for frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple `FilterPlugin` that intercepts and modifies a filter parameter. The core idea is to subclass `FilterPlugin` (or `TextPlugin`) and implement the `run` asynchronous method. The example shows capitalizing a 'category' filter value. To activate such a plugin, you must instantiate it and then register it with a specific `Pinecone` index instance using `index.register_plugin(plugin_instance)`.

from pinecone_plugin_interface.plugin import FilterPlugin
from pinecone_plugin_interface.models import FilterPluginRequest, FilterPluginResponse
from typing import Dict, Any
import os

class CapitalizeCategoryFilterPlugin(FilterPlugin):
    """
    An example filter plugin that adds a simple metadata filter to capitalize
    the 'category' field for demonstration purposes, effectively applying
    a filter like {'category': {'$eq': 'DOCS'}}.
    """
    def __init__(self, name: str = "capitalize-category-filter", description: str = "Capitalizes category for filtering"):
        super().__init__(name=name, description=description)

    async def run(self, request: FilterPluginRequest) -> FilterPluginResponse:
        # Assume a 'category' key exists in request.metadata or is expected in the filter
        if request.filter and 'category' in request.filter:
            # This example demonstrates modification; in a real scenario,
            # you might look for specific filter patterns to modify.
            # Here, we just illustrate returning a modified filter.
            modified_filter = {"category": {"$eq": request.filter['category'].upper()}}
        else:
            # If no category filter is present, we could add a default or pass through
            modified_filter = request.filter

        return FilterPluginResponse(filter=modified_filter)

# To use this plugin, you would typically register it with a Pinecone index:
# from pinecone import Pinecone
# # Ensure PINECONE_API_KEY and PINECONE_ENVIRONMENT are set in your environment
# pc = Pinecone(api_key=os.environ.get('PINECONE_API_KEY', 'TEST_API_KEY'),
#               environment=os.environ.get('PINECONE_ENVIRONMENT', 'us-west-2'))
# index = pc.Index("my-index") # Replace with your index name
# 
# plugin_instance = CapitalizeCategoryFilterPlugin()
# index.register_plugin(plugin_instance)
# 
# print(f"Plugin '{plugin_instance.name}' registered. Its description is: '{plugin_instance.description}'")
# # You can then call index.query with the plugin applied through a 'plugin_params' argument:
# # query_results = index.query(vector=[0.1, ...], top_k=5, plugin_params={'my-plugin-name': {'category': 'docs'}})

view raw JSON →