Skip to content

Commit

Permalink
Merge pull request #535 from elnoro/pgvector-docs
Browse files Browse the repository at this point in the history
docs: add pgvector page
  • Loading branch information
tmc authored Jan 21, 2024
2 parents 19b5f8e + 1cd9a21 commit c3c9d57
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/docs/modules/data_connection/vector_stores/pgvector.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
sidebar_label: pgvector
sidebar_position: 1
draft: true
---

import CodeBlock from "@theme/CodeBlock";
import ExamplePGVector from "@examples/pgvector-vectorstore-example/pgvector_vectorstore_example.go";

# Getting Started: pgvector

[PGVector](https://github.com/pgvector/pgvector) is an open-source vector similarity search for Postgres

PGVector supports:
* exact and approximate nearest neighbor search
* L2 distance, inner product, and cosine distance
* IVFFlat and HNSW index types

See the [installation instructions](https://github.com/pgvector/pgvector#installation-notes).

## Usage with Langchain Go

In code, create an embedder based on an LLM (OpenAI, Ollama, etc.):
```go
llm, _:= openai.New()
emb, _ := embeddings.NewEmbedder(llm)
```

For OpenAI embeddings, you will need obtain an API key and provide as an environment variable to the program:

```bash
export OPENAI_API_KEY=your_openai_api_key_here
```

Create a vector store:
```go
ctx := context.Background()
store, err := pgvector.New(
ctx,
pgvector.WithConnectionURL("postgres://testuser:testpass@localhost:5432/testdb?sslmode=disable"),
pgvector.WithEmbedder(emb),
)
```

Document tables will be created automatically.

Add documents:
```go
_, err = store.AddDocuments(context.Background(), []schema.Document{
{
PageContent: "Tokyo",
Metadata: map[string]any{
"population": 38,
"area": 2190,
},
},
{
PageContent: "Sao Paulo",
Metadata: map[string]any{
"population": 22.6,
"area": 1523,
},
},
})
```

Run a similarity search using cosine distance (`<=>`):

```go
filter := map[string]any{"area": "1523"}

docs, err = store.SimilaritySearch(ctx, "only cities in south america",
10,
vectorstores.WithScoreThreshold(0.80),
vectorstores.WithFilters(filter),
)
```

For now, pgvector integration only supports simple key-value filters and cosine distance search.

## Full example

Here is the entire program (from [pgvector-vectorstore-example](https://github.com/tmc/langchaingo/blob/main/examples/pgvector-vectorstore-example/pgvector_vectorstore_example.go)):
<CodeBlock language="go">{ExamplePGVector}</CodeBlock>

0 comments on commit c3c9d57

Please sign in to comment.