Skip to content

Instantly share code, notes, and snippets.

@himself65
Created September 20, 2024 03:58
Show Gist options
  • Save himself65/e4a40968a5dee38db50fc4324bc54101 to your computer and use it in GitHub Desktop.
Save himself65/e4a40968a5dee38db50fc4324bc54101 to your computer and use it in GitHub Desktop.
from llama_index.core import StorageContext
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.postgres import PGVectorStore
import openai
import os
openai.api_key = os.environ["OPENAI_API_KEY"]
from sqlalchemy import make_url
url = make_url("postgresql://user:password@localhost:5432/llamaindex_node_test")
vector_store = PGVectorStore.from_params(
database="llamaindex_node_test",
host=url.host,
password=url.password,
port=url.port,
user=url.username,
table_name="llamaindex_embedding",
embed_dim=1536, # openai embedding dimension
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_vector_store(
vector_store, storage_context=storage_context, show_progress=True
)
query_engine = index.as_query_engine()
res = query_engine.query("What is the meaning of life?")
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment