Combining SQL Query Filtering with Vector Search in AI Agent Node

Is it possible to perform a regular SQL query search in a vector database table, and then, after retrieving the SQL results, use a prompt to search by vectors in the AI Agent node? If so, how can this be implemented?

Hi @walker,

Yes, it’s possible to perform a regular SQL query search in a vector database table and then use a prompt to search by vectors using the AI Agent node in n8n. Here’s how you can implement this:

  1. Execute the SQL Query:

    • Use the appropriate database node (e.g., PostgreSQL, MySQL) in n8n to connect to your vector database and execute the desired SQL query. This will retrieve the initial dataset based on your SQL conditions.
  2. Prepare Data for Vector Search:

    • Process the SQL query results to extract the relevant text or data fields that you intend to use for vector similarity search.
    • Ensure that this data is in a format suitable for embedding generation, as the AI Agent node will require embeddings to perform vector searches.
  3. Configure the AI Agent Node:

    • Add the AI Agent node to your workflow and configure it to use a vector store. n8n supports various vector stores, such as Pinecone, Qdrant, and Supabase.
    • Connect the processed data from the previous step to the AI Agent node as input.
    • In the AI Agent node settings, define the prompt that will guide the vector search. This prompt should specify the criteria or query for the vector similarity search.
  4. Perform the Vector Search:

    • The AI Agent node will generate embeddings for the input data and perform a similarity search within the connected vector store.
    • It will return results that are semantically similar to the prompt, based on the vector representations.

Example Workflow:

  • SQL Query Execution: Retrieve records from the vector database where certain conditions are met.
  • Data Processing: Extract and prepare text fields from the SQL results for embedding generation.
  • AI Agent Configuration: Set up the AI Agent node with a vector store tool and define a prompt like, “Find documents related to ‘machine learning algorithms’.”
  • Vector Search Execution: The AI Agent node searches the vector store for documents semantically related to the prompt.

For a practical example and detailed guidance, you can refer to the n8n workflow template titled “AI-Powered Chatbot Workflow with MySQL Database Integration” which demonstrates integrating SQL queries with AI-powered search functionalities.

By combining SQL queries with vector searches in the AI Agent node, you can effectively retrieve and process data from your vector database, leveraging both structured querying and semantic search capabilities.

I’m struggling to figure out how to take rows retrieved via SQL and then process them with a vector search using a prompt. For example, I want to use a Postgres Query to fetch 100 rows from a table containing vector data, and then, within those 100 rows, use a prompt to find specific information via vector search. How can I set up this workflow in n8n? Is there a way to pass the SQL results to a vector search tool and then use the AI Agent node for this? Any examples would be appreciated!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.