Skip to Content

Vector Selector

Introduction

The Vector Selector is a powerful tool designed to optimize function selection in Agentica by significantly reducing token consumption. By leveraging Vector Semantic Search technology, it embeds text into a high-dimensional vector space, enabling efficient semantic similarity-based searches.

Key benefits (based on 100 trials from vector-selector-benchmarkย ):

  • Token Efficiency: Reduces token consumption by approximately 70% compared to plain function calling (2.9M โ†’ 0.9M tokens)
  • Improved Accuracy: Increases success rate from 75% to 91-93% compared to plain function calling
  • Performance:
    • PostgreSQL: ~19.4s average response time
    • SQLite: ~14.3s average response time (faster than PostgreSQL)
    • Both options are slightly slower than plain function calling (~10s) but provide better accuracy and token efficiency

Installation

npm install @agentica/vector-selector

Usage

Simply replace the existing executor.select with the selectorExecute function.

import { Agentica } from "@agentica/core"; import { AgenticaVectorSelector } from "@agentica/vector-selector"; import typia from "typia"; // Initialize the vector selector const selectorExecute = AgenticaVectorSelector.boot({ // Configuration options }); const agent = new Agentica({ vendor: { model: "gpt-4o-mini", api: new OpenAI({ apiKey: process.env.CHATGPT_API_KEY, }), }, controllers: [ await fetch( "https://shopping-be.wrtn.ai/editor/swagger.json", ).then(r => r.json()), typia.llm.application<ShoppingCounselor>(), typia.llm.application<ShoppingPolicy>(), typia.llm.application<ShoppingSearchRag>(), ], config: { executor: { select: selectorExecute, // Replace the existing selector } } }); await agent.conversate("I wanna buy MacBook Pro");

Setup

Basic Setup

For basic usage, you can initialize the vector selector with minimal configuration:

const selectorExecute = AgenticaVectorSelector.boot({ // Basic configuration options });

Advanced Setup

For more advanced use cases, you can configure additional options:

const selectorExecute = AgenticaVectorSelector.boot({ // Advanced configuration options experimental: { select_prompt: "Custom prompt for function selection", // Additional experimental features } });

Vector Store Setup

To use PostgreSQL with vector extension for enhanced performance, youโ€™ll need to set up both PostgreSQL and the connector-hive server:

  1. First, set up PostgreSQL with vector extension:
docker run -d \ --name postgres-vector \ -e POSTGRES_USER=your_user \ -e POSTGRES_PASSWORD=your_password \ -e POSTGRES_DB=your_database \ -p 5432:5432 \ pgvector/pgvector
  1. Set up the environment variables for connector-hive:
PROJECT_API_PORT=37001 DATABASE_URL=postgresql://your_user:your_password@host.docker.internal:5432/your_database COHERE_API_KEY=your_cohere_api_key API_KEY=your_optional_api_key # Optional: If set, all requests except GET /health must include this key
  1. Run the connector-hive server:
docker pull ghcr.io/wrtnlabs/connector-hive:latest && \ docker run -d \ --name connector-hive \ --env-file .env \ -p 37001:37001 \ ghcr.io/wrtnlabs/connector-hive:latest
  1. Configure the vector selector with PostgreSQL:
import { BootAgenticaVectorSelector } from "@agentica/vector-selector"; import { configurePostgresStrategy } from "@agentica/vector-selector/strategy"; // Check if connector-hive is running if (!(await fetch(`${connectorHiveUrl}/health`).catch(() => ({ ok: false }))).ok) { throw new Error("Connector Hive is not running"); } const selectorExecute = BootAgenticaVectorSelector({ strategy: configurePostgresStrategy({ host: connectorHiveUrl, }), }); const agent = new Agentica({ // ... other configurations config: { executor: { select: selectorExecute, } } });

How It Works

The Vector Selector operates through the following process:

  1. Converts input text into high-dimensional vectors
  2. Performs semantic similarity search in the vector space
  3. Selects and executes the most appropriate function based on similarity scores
Last updated on