<p align="center" width="100%">
  <img height="250" src="https://github.com/city-of-san-francisco/.github/blob/main/assets/logo.svg" />
</p>

<p align="center" width="100%">
   <a href="https://github.com/city-of-san-francisco/license"><img height="20" src="https://img.shields.io/badge/license-SF-blue.svg"/></a>
   <a href="https://github.com/city-of-san-francisco/sf-rag-utils/actions/workflows/ci.yml"><img height="20" src="https://github.com/city-of-san-francisco/sf-rag-utils/actions/workflows/ci.yml/badge.svg" /></a>
   <a href="https://www.npmjs.com/package/@sf-ai/rag-functions"><img height="20" src="https://img.shields.io/github/package-json/v/city-of-san-francisco/sf-rag-utils?filename=packages%2Frag-functions%2Fpackage.json"/></a>
</p>

<p align="center" width="100%">
  <a href="https://github.com/city-of-san-francisco/sf-rag-utils">A pgpm workspace for building agentic, database-driven AI applications for the City of San Francisco.</a>
</p>

# @sf-ai/rag-functions

RAG functions for import, export, and vector search.

## Overview

This package provides SQL functions for working with RAG collections:

- **Import/Export**: JSON format for portable collection data
- **Search**: Cosine similarity search using pgvector
- **CSV Export**: Flat format for embeddings

## Functions

### `rag.export_collection_json(collection_id uuid)`

Export a complete collection with all documents, chunks, and embeddings as JSON.

```sql
SELECT rag.export_collection_json('collection-uuid');
```

### `rag.import_collection_json(data jsonb, overwrite boolean)`

Import a collection from JSON format.

```sql
SELECT rag.import_collection_json('{...}'::jsonb, false);
```

### `rag.search_similar(collection_id uuid, query_vector vector, ...)`

Search for similar chunks using cosine distance.

```sql
SELECT * FROM rag.search_similar(
  'collection-uuid',
  '[0.1, 0.2, ...]'::vector,
  'text-embedding-3-small',  -- model name (optional)
  10,                         -- limit
  0.5                         -- distance threshold (optional)
);
```

Also supports searching by collection name:

```sql
SELECT * FROM rag.search_similar(
  'my-collection',
  '[0.1, 0.2, ...]'::vector
);
```

### `rag.export_embeddings_csv(collection_id uuid, model_name text)`

Export embeddings in CSV-friendly format.

```sql
SELECT * FROM rag.export_embeddings_csv('collection-uuid', 'text-embedding-3-small');
```

## Dependencies

- `@sf-ai/rag-core`

## License

[SF License](https://github.com/city-of-san-francisco/license)
