Skip to content

Commit 51df3ec

Browse files
authored
adds example for pgvector search with metadata (#947)
1 parent 0c77913 commit 51df3ec

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
require "langchain"
4+
require "dotenv/load"
5+
require "ruby/openai"
6+
7+
# Initialize the Pgvector client
8+
pgvector = Langchain::Vectorsearch::Pgvector.new(
9+
url: ENV["POSTGRES_URL"],
10+
index_name: "documents",
11+
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
12+
)
13+
14+
# Create the default schema if it doesn't exist
15+
pgvector.create_default_schema
16+
17+
# Add documents with metadata
18+
documents = [
19+
{
20+
text: "The quick brown fox jumps over the lazy dog",
21+
metadata: {
22+
source: "fables",
23+
author: "Aesop",
24+
category: "animals"
25+
}
26+
},
27+
{
28+
text: "To be or not to be, that is the question",
29+
metadata: {
30+
source: "hamlet",
31+
author: "Shakespeare",
32+
category: "drama"
33+
}
34+
},
35+
{
36+
text: "It was the best of times, it was the worst of times",
37+
metadata: {
38+
source: "tale_of_two_cities",
39+
author: "Dickens",
40+
category: "novel"
41+
}
42+
}
43+
]
44+
45+
# Add texts with metadata
46+
ids = pgvector.add_texts(
47+
texts: documents.map { |doc| doc[:text] },
48+
metadata: documents.map { |doc| doc[:metadata] }
49+
)
50+
51+
puts "Added documents with IDs: #{ids.inspect}"
52+
53+
# Perform a similarity search
54+
puts "\nSearching for documents similar to 'fox':"
55+
results = pgvector.similarity_search(query: "fox", k: 2)
56+
results.each do |result|
57+
puts "Content: #{result.content}"
58+
puts "Metadata: #{JSON.parse(result.metadata)}"
59+
puts "---"
60+
end
61+
62+
# Search with specific metadata filter
63+
puts "\nSearching for Shakespeare's works:"
64+
results = pgvector.similarity_search(query: "drama", k: 1)
65+
results.each do |result|
66+
metadata = JSON.parse(result.metadata)
67+
if metadata["author"] == "Shakespeare"
68+
puts "Found Shakespeare's work:"
69+
puts "Content: #{result.content}"
70+
puts "Metadata: #{metadata}"
71+
end
72+
end
73+
74+
# Clean up (optional)
75+
# pgvector.destroy_default_schema

0 commit comments

Comments
 (0)