[-0.015540238, 0.0014074693, 0.009978753, -0.07941696, -0.027072648, 0.02588584, 0.0045492477, 0.050993927, 0.019187931, 0.0050778543]
[-0.015540238, 0.0014074693, 0.009978753, -0.07941696, -0.027072648, 0.02588584, 0.0045492477, 0.050993927, 0.019187931, 0.0050778543]
[-0.015540238, 0.0014074693, 0.009978753, -0.07941696, -0.027072648, 0.02588584, 0.0045492477, 0.050993927, 0.019187931, 0.0050778543]
cd /tmp
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
cd /tmp
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
cd /tmp
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
cd pgvector
make
cd pgvector
make
cd pgvector
make
sudo make install
sudo make install
sudo make install
sudo -u postgres psql
sudo -u postgres psql
sudo -u postgres psql
List of installed extensions Name | Version | Default version | Schema | Description ---------+---------+-----------------+------------+------------------------------------------------------ plpgsql | 1.0 | 1.0 | pg_catalog | PL/pgSQL procedural language vector | 0.8.2 | 0.8.2 | public | vector data type and ivfflat and hnsw access methods
List of installed extensions Name | Version | Default version | Schema | Description ---------+---------+-----------------+------------+------------------------------------------------------ plpgsql | 1.0 | 1.0 | pg_catalog | PL/pgSQL procedural language vector | 0.8.2 | 0.8.2 | public | vector data type and ivfflat and hnsw access methods
List of installed extensions Name | Version | Default version | Schema | Description ---------+---------+-----------------+------------+------------------------------------------------------ plpgsql | 1.0 | 1.0 | pg_catalog | PL/pgSQL procedural language vector | 0.8.2 | 0.8.2 | public | vector data type and ivfflat and hnsw access methods
CREATE DATABASE movies;
CREATE DATABASE movies;
CREATE DATABASE movies;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS vector;
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 pgvector/pgvector:pg17
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 pgvector/pgvector:pg17
docker run -d --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 pgvector/pgvector:pg17
docker exec -it postgres psql -U postgres
docker exec -it postgres psql -U postgres
docker exec -it postgres psql -U postgres
CREATE DATABASE movies;
CREATE DATABASE movies;
CREATE DATABASE movies;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS vector;
export GEMINI_API_KEY='YOUR_API_KEY'
export GEMINI_API_KEY='YOUR_API_KEY'
export GEMINI_API_KEY='YOUR_API_KEY'
pip install google-genai
pip install google-genai
pip install google-genai
from google import genai client = genai.Client() text = "A futuristic journey through the stars."
model_name = "gemini-embedding-001" response = client.models.embed_content( model=model_name, contents=text,
) vector = response.embeddings[0].values print(f"Dimension: {len(vector)}")
print(f"Vector preview: {vector[:5]}...")
from google import genai client = genai.Client() text = "A futuristic journey through the stars."
model_name = "gemini-embedding-001" response = client.models.embed_content( model=model_name, contents=text,
) vector = response.embeddings[0].values print(f"Dimension: {len(vector)}")
print(f"Vector preview: {vector[:5]}...")
from google import genai client = genai.Client() text = "A futuristic journey through the stars."
model_name = "gemini-embedding-001" response = client.models.embed_content( model=model_name, contents=text,
) vector = response.embeddings[0].values print(f"Dimension: {len(vector)}")
print(f"Vector preview: {vector[:5]}...")
python get_embeddings.py
python get_embeddings.py
python get_embeddings.py
Dimension: 3072
Vector preview: [-0.003496697, 0.004707519, 0.02058491, -0.0735851, 0.0041175582]...
Dimension: 3072
Vector preview: [-0.003496697, 0.004707519, 0.02058491, -0.0735851, 0.0041175582]...
Dimension: 3072
Vector preview: [-0.003496697, 0.004707519, 0.02058491, -0.0735851, 0.0041175582]...
pip install sentence-transformers
pip install sentence-transformers
pip install sentence-transformers
from sentence_transformers import SentenceTransformer movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] model = SentenceTransformer('all-MiniLM-L6-v2') embeddings = model.encode(movies) for i, embedding in enumerate(embeddings): print(f"Movie {i+1} dimension: {len(embedding)}") print(f"Vector preview: {embedding[:3]}...")
from sentence_transformers import SentenceTransformer movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] model = SentenceTransformer('all-MiniLM-L6-v2') embeddings = model.encode(movies) for i, embedding in enumerate(embeddings): print(f"Movie {i+1} dimension: {len(embedding)}") print(f"Vector preview: {embedding[:3]}...")
from sentence_transformers import SentenceTransformer movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] model = SentenceTransformer('all-MiniLM-L6-v2') embeddings = model.encode(movies) for i, embedding in enumerate(embeddings): print(f"Movie {i+1} dimension: {len(embedding)}") print(f"Vector preview: {embedding[:3]}...")
python get_embeddings.py
python get_embeddings.py
python get_embeddings.py
Movie 1 dimension: 384
Vector preview: [-0.04579255 0.01413548 -0.01935582]...
Movie 2 dimension: 384
Vector preview: [ 0.02027559 -0.03948853 0.06786963]...
Movie 3 dimension: 384
Vector preview: [-0.01461564 0.01758054 0.00982607]...
Movie 1 dimension: 384
Vector preview: [-0.04579255 0.01413548 -0.01935582]...
Movie 2 dimension: 384
Vector preview: [ 0.02027559 -0.03948853 0.06786963]...
Movie 3 dimension: 384
Vector preview: [-0.01461564 0.01758054 0.00982607]...
Movie 1 dimension: 384
Vector preview: [-0.04579255 0.01413548 -0.01935582]...
Movie 2 dimension: 384
Vector preview: [ 0.02027559 -0.03948853 0.06786963]...
Movie 3 dimension: 384
Vector preview: [-0.01461564 0.01758054 0.00982607]...
\c movies
CREATE TABLE synopses ( id SERIAL PRIMARY KEY, content TEXT, embedding VECTOR(3072)
);
\c movies
CREATE TABLE synopses ( id SERIAL PRIMARY KEY, content TEXT, embedding VECTOR(3072)
);
\c movies
CREATE TABLE synopses ( id SERIAL PRIMARY KEY, content TEXT, embedding VECTOR(3072)
);
pip install psycopg2 pgvector
pip install psycopg2 pgvector
pip install psycopg2 pgvector
import psycopg2
from google import genai
from pgvector.psycopg2 import register_vector movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] client = genai.Client()
response = client.models.embed_content( model="gemini-embedding-001", contents=movies,
) conn = psycopg2.connect("dbname=movies user=postgres password=secret")
register_vector(conn)
cur = conn.cursor() for i, text in enumerate(movies): embedding = response.embeddings[i].values cur.execute("INSERT INTO synopses (content, embedding) VALUES (%s, %s)", (text, embedding)) conn.commit()
print("Vectors from Gemini successfully stored!") cur.close()
conn.close()
import psycopg2
from google import genai
from pgvector.psycopg2 import register_vector movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] client = genai.Client()
response = client.models.embed_content( model="gemini-embedding-001", contents=movies,
) conn = psycopg2.connect("dbname=movies user=postgres password=secret")
register_vector(conn)
cur = conn.cursor() for i, text in enumerate(movies): embedding = response.embeddings[i].values cur.execute("INSERT INTO synopses (content, embedding) VALUES (%s, %s)", (text, embedding)) conn.commit()
print("Vectors from Gemini successfully stored!") cur.close()
conn.close()
import psycopg2
from google import genai
from pgvector.psycopg2 import register_vector movies = [ "A futuristic journey through the stars and the mysteries of the universe.", "A lighthearted story about two strangers falling in love in New York City.", "A gritty detective story set in a neon-lit city ruled by artificial intelligence."
] client = genai.Client()
response = client.models.embed_content( model="gemini-embedding-001", contents=movies,
) conn = psycopg2.connect("dbname=movies user=postgres password=secret")
register_vector(conn)
cur = conn.cursor() for i, text in enumerate(movies): embedding = response.embeddings[i].values cur.execute("INSERT INTO synopses (content, embedding) VALUES (%s, %s)", (text, embedding)) conn.commit()
print("Vectors from Gemini successfully stored!") cur.close()
conn.close()
python vectors-to-postgresql.py
python vectors-to-postgresql.py
python vectors-to-postgresql.py - Clone the repository: - Compile the source code: - Install the extension - Log in to your instance: - Verify the extension was installed correctly: - Set up a database to store movie synopses - Select the database: \c movies
- Enable the extension: - OpenAI Python library → https://github.com/openai/openai-python
- Google GenAI Python SDK → https://github.com/googleapis/python-genai - Go to the API Keys page in the Google AI Studio
- Click on Create API key
- Assign a name to the key
- Choose a project or create a new one
- Click on Create key
- Click on the created API key to see the details
- Copy the API key - Initialize the client
- Define the text to transform and the model to be used
- Generate the embedding
- Extract the vector, print the values, and the vector size - Define a list of texts to transform
- Load the model
- Generate the embeddings
- Extract the vectors, print the values, and vector size - Define a list of texts to transform
- Initialize the client
- Generate the embeddings
- Establish a connection to the database
- Insert each synopses with their respective vector
- Close the connection