Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions src/content/docs/go/docs/plugins/postgresql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
title: Postgresql plugin
description: Learn how to configure and use the Genkit Postgresql plugin for Go to integrate with pgvector extension.
---

The Postgresql plugin provides indexer and retriever implementatons that use the [Google Cloud SQL for Postgresql](https://cloud.google.com/sql/docs/postgres) and [pgvector](https://github.com/pgvector/pgvector) extension.

## Configuration

To use this plugin, follow these steps:

1. Import the plugin

```go
import "github.com/firebase/genkit/go/plugins/postgresql"
```

2. Create a `PostgresEngine` instance:

- Using basic authentication
```go
pEngine, err := NewPostgresEngine(ctx,
WithUser('user'),
WithPassword('password'),
WithCloudSQLInstance('my-project', 'us-central1', 'my-instance'),
WithDatabase('my-database')
```
- Using email authentication
```go
pEngine, err := NewPostgresEngine(ctx,
WithCloudSQLInstance('my-project', 'us-central1', 'my-instance'),
WithDatabase('my-database'),
WithIAMAccountEmail('[email protected]'))
```
- Using custom pool
```go
pool, err := pgxpool.New(ctx, "add_your_connection_string")
if err != nil {
return err
}

pEngine, err := NewPostgresEngine(ctx,
WithDatabase("db_test"),
WithPool(pool))

```

3. Create the Postgres plugin
- Using plugin method Init


```go
postgres := &postgresql.Postgres{
engine: pEngine,
}

if err := (postgres).Init(ctx, g); err != nil {
return err
}
```

- Using the genkit method init

```go
postgres := &postgresql.Postgres{
engine: pEngine,
}

g, err := genkit.Init(ctx, genkit.WithPlugins(postgres))

if err != nil {
return err
}

```

## Usage

To add documents to a Postgresql index, first create a retrieve definition that specifies the features of the table:

```go
cfg := &postgresql.Config{
TableName: 'documents',
SchemaName: 'public',
ContentColumn: "content",
EmbeddingColumn: "embedding",
MetadataColumns: []string{"source", "category"},
IDColumn: "custom_id",
MetadataJSONColumn: "custom_metadata",
Embedder: embedder,
EmbedderOptions: nil,
}

doc, retriever, err := postgresql.DefineRetriever(ctx, g, postgres, cfg)
if err != nil {
retrun err
}

docs := []*ai.Document{{
Content: []*ai.Part{{
Kind: ai.PartText,
ContentType: "text/plain",
Text: "The product features include...",
}},
Metadata: map[string]any{"source": "website", "category": "product-docs", "custom_id": "doc-123"},
}}

if err := doc.Index(ctx, docs); err != nil {
return err
}

```

Similarly, to retrieve documents from an index, use the retrieve method:

```go
d2 := ai.DocumentFromText( "The product features include..." , nil)

resp, err := retriever.Retrieve(ctx, &ai.RetrieverRequest{
Query: d2,
k:5,
filter: "source='website' AND category='product-docs'"
})

if err != nil {
retrun err
}
```

It's also possible to use the Retrieve method from Retriever

```go
_, retriever, err := postgresql.DefineRetriever(ctx, g, postgres, cfg)
if err != nil {
retrun err
}

d2 := ai.DocumentFromText( "The product features include..." , nil)

retrieverOptions := &postgresql.RetrieverOptions{
k:5,
filter: "source='website' AND category='product-docs'"
}

resp, err := ai.Retrieve(ctx, retriever,ai.WithDocs(d2), &ai.WithConfig(retrieverOptions))
if err != nil {
retrun err
}
```


See the [Retrieval-augmented generation](/go/docs/rag) page for a general
discussion on using indexers and retrievers for RAG.