PGVector with Agents for querying a DB #25

Open
opened 2026-02-16 05:16:08 -05:00 by yindo · 1 comment
Owner

Originally created by @feddionisio on GitHub (Jun 27, 2024).

langchain Documentation
https://python.langchain.com/v0.1/docs/use_cases/sql/agents/

CODE
from langchain_community.utilities import SQLDatabase
from langchain_community.agent_toolkits import create_sql_agent
from langchain_openai import AzureChatOpenAI
import os
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import AzureOpenAIEmbeddings

from langchain_postgres import PGVector
from langchain_postgres.vectorstores import PGVector
import os
import psycopg2

os.environ["AZURE_OPENAI_API_KEY"] = "api_key"
os.environ["AZURE_OPENAI_API_TYPE"] = "ap_type"
os.environ["AZURE_OPENAI_ENDPOINT"] = "azure_openai_endpoint"
os.environ["AZURE_OPENAI_API_VERSION"] = "2024-02-01"
os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"] = "deployment_model_name"

examples = [
{"input": "Describe article name ...", "query": "SELECT compatibility FROM article ...';"}
]

AzureEmbeddings = AzureOpenAIEmbeddings(model="embedding-model-name")
connection = "postgresql+psycopg://YYY:XXXX@localhost/ZZZ"
collection_name = "collection_name"

vectorstore = PGVector(
embeddings=AzureEmbeddings,
collection_name=collection_name,
connection=connection,
use_jsonb=True,
)

example_selector = SemanticSimilarityExampleSelector.from_examples(
examples,
AzureEmbeddings,
vectorstore,
k=5,
input_keys=["input"],
)

ERROR INFO
File "C:\Users\user\Projects\project\langchain_sql.py", line 53, in
example_selector = SemanticSimilarityExampleSelector.from_examples(
File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_core\example_selectors\semantic_similarity.py", line 133, in from_examples
vectorstore = vectorstore_cls.from_texts(
File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 1496, in from_texts
return cls.__from(
File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 660, in __from
store = cls(
File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 414, in init
raise ValueError(
ValueError: connection should be a connection string or an instance of sqlalchemy.engine.Engine or sqlalchemy.ext.asyncio.engine.AsyncEngine

SYSTEM INFO
langchain 0.2.6
langchain-community 0.2.5
langchain-core 0.2.10
langchain-experimental 0.0.61
langchain-openai 0.1.8
langchain-postgres 0.0.9
langchain-text-splitters 0.2.1
psycopg 3.1.19
psycopg-binary 3.1.19
psycopg-pool 3.2.2
psycopg2-binary 2.9.6

COMMENTS
Already tried to include or not psycopg within the connection string "postgresql+psycopg:..." the error doesn't change

Originally created by @feddionisio on GitHub (Jun 27, 2024). **langchain Documentation** https://python.langchain.com/v0.1/docs/use_cases/sql/agents/ **CODE** from langchain_community.utilities import SQLDatabase from langchain_community.agent_toolkits import create_sql_agent from langchain_openai import AzureChatOpenAI import os from langchain_core.example_selectors import SemanticSimilarityExampleSelector from langchain_openai import AzureOpenAIEmbeddings from langchain_postgres import PGVector from langchain_postgres.vectorstores import PGVector import os import psycopg2 os.environ["AZURE_OPENAI_API_KEY"] = "api_key" os.environ["AZURE_OPENAI_API_TYPE"] = "ap_type" os.environ["AZURE_OPENAI_ENDPOINT"] = "azure_openai_endpoint" os.environ["AZURE_OPENAI_API_VERSION"] = "2024-02-01" os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"] = "deployment_model_name" examples = [ {"input": "Describe article name ...", "query": "SELECT compatibility FROM article ...';"} ] AzureEmbeddings = AzureOpenAIEmbeddings(model="embedding-model-name") connection = "postgresql+psycopg://YYY:XXXX@localhost/ZZZ" collection_name = "collection_name" vectorstore = PGVector( embeddings=AzureEmbeddings, collection_name=collection_name, connection=connection, use_jsonb=True, ) example_selector = SemanticSimilarityExampleSelector.from_examples( examples, AzureEmbeddings, vectorstore, k=5, input_keys=["input"], ) **ERROR INFO** File "C:\Users\user\Projects\project\langchain_sql.py", line 53, in <module> example_selector = SemanticSimilarityExampleSelector.from_examples( File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_core\example_selectors\semantic_similarity.py", line 133, in from_examples vectorstore = vectorstore_cls.from_texts( File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 1496, in from_texts return cls.__from( File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 660, in __from store = cls( File "C:\Users\user\Projects\project\venv\lib\site-packages\langchain_postgres\vectorstores.py", line 414, in __init__ raise ValueError( ValueError: connection should be a connection string or an instance of sqlalchemy.engine.Engine or sqlalchemy.ext.asyncio.engine.AsyncEngine **SYSTEM INFO** langchain 0.2.6 langchain-community 0.2.5 langchain-core 0.2.10 langchain-experimental 0.0.61 langchain-openai 0.1.8 langchain-postgres 0.0.9 langchain-text-splitters 0.2.1 psycopg 3.1.19 psycopg-binary 3.1.19 psycopg-pool 3.2.2 psycopg2-binary 2.9.6 **COMMENTS** Already tried to include or not psycopg within the connection string "postgresql+psycopg:..." the error doesn't change
Author
Owner

@pprados commented on GitHub (Jul 10, 2024):

The package is compatible only with psycogp3.

@pprados commented on GitHub (Jul 10, 2024): The package is compatible only with [psycogp3](https://www.psycopg.org/psycopg3/).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-postgres#25