Monday, January 8, 2024

Use AI to Query AWS RDS Database with LlamaIndex

 This video shows step by step guide with code as how to integrate LlamaIndex with AWS RDS Postgresql database to query in natural language. Its AI and LLM at its best.




Commands Used:



sudo apt-get install libpq-dev

pip install llama-index sqlalchemy psycopg2

from sqlalchemy import create_engine, MetaData
from llama_index import SQLDatabase, VectorStoreIndex
from llama_index.indices.struct_store import SQLTableRetrieverQueryEngine
from llama_index.objects import SQLTableNodeMapping, ObjectIndex, SQLTableSchema

pg_uri = f"postgresql+psycopg2://postgres:test1234@<RDS Endpoint>:5432/testdb"

engine = create_engine(pg_uri)

metadata_obj = MetaData()
metadata_obj.reflect(engine)

sql_database = SQLDatabase(engine)

from llama_index.indices.struct_store import NLSQLTableQueryEngine


query_engine = NLSQLTableQueryEngine(
sql_database=sql_database,
tables=["companies","contacts"],
)

query_str = "who works in AWS?"

response = query_engine.query(query_str)

query_str = "How many people work in GCP and what are there names?"

response = query_engine.query(query_str)

     


No comments: