Pinecone
Querying Overview
Pinecone’s native query interface uses a programmatic interface to retrieve data. The general use case is to identify an index with the data of interest. Next a vector of values is required to indicate a point in n-space from which to locate similar data. It is recommended that the query set at most how many items to retrieve as well.
One way to specify a Pinecone query in Qarbine is to use a JSON-like structure which looks like the following.
{
topK: 2,
vector: [...],
filter: { …}
}
The number of values in the vector must correspond to the same number within the index’s underlying data. Qarbine only performs data retrieval. There is purposely no delete or update interactions provided.
The overall general form of the syntax used by Qarbine is shown below
{
index : "sample-movies", ← This is required.
nearText: "dracula", ← Set this or the vector field below.
returnNoValues: true, ← The default is true.
namespace: "my-namespace", ← Optional.
query: { ← This maps to a standard Pinecone query object.
topK: 2, ← At most how many matches to return.
No more than 100 will be returned.
includeMetadata: true, ← The default is true.
vector: [...], ← The list of n-space numbers.
filter: { … } ← Optional metadata filtering.
}
}
Either the outer useEmbedding field is defined or the inner vector field. If a useEmbedding value is set then the named Qarbine AI Assistant service is used to obtain the vector argument to use with the effective Pinecone query. This service is set up by the Qarbine administrator.
The returnNoValues field is a boolean indicating if the data’s “values” field should be returned. Usually this is not useful for Qarbine analytic and presentation purposes and it just adds bulk to the resulting reply. The default is true to not return them.
The namespace field is used when interacting with a Pinecone index that spans multiple namespaces. An example using JavaScript syntax is shown below,
const index = pinecone.index(“sample-movies);
const queryResponse = await index.namespace('example-namespace').
query({
topK: 10,
vector: [0.1, 0.2, 0.3, 0.4],
sparseVector: { indices: [3], values: [0.8] }
})
For more details on sparse and dense vectors see
https://docs.pinecone.io/docs/query-data#querying-vectors-with-sparse-and-dense-values
The query field general maps to the Pinecone query as described at
The optional Pinecone metadata filtering is described at
In brief, metadata field filtering offers a subset of operators similar to MongoDB. These operators can be combined with $and and $or.
Operator | Description |
---|---|
$eq | Equal to (number, string, boolean) |
$ne | Not equal to (number, string, boolean) |
$gt | Greater than (number) |
$gte | Greater than or equal to (number) |
$lt | Less than (number) |
$lte | Less than or equal to (number) |
$in | Less than (number) |
$nin | Not in array (string or number) |
Prerequisites
Prior to using Qarbine’s embeddings(...) macro function or the SQL-like query function nearText(...), the Qarbine Administrator must first configure “AI Assistant(s)”. The AI Assistants provide access to various popular Generative AI services and are referenced using an alias. Check with your Qarbine administrator for which ones are available and their proper use. For example, when using dynamic query vector embeddings, the model used by the AI Assistant must be compatible with the one used to generate the original embedding values in the database.
Qarbine SQL Interface
Qarbine provides a convenient SQL interface to retrieve Pinecone data and avoid the cumbersome manual authoring of Pinecone filters. The WHERE criteria is translated into the equivalent Pinecone filter and sent to Pinecone. The operators supported are listed in the section above. The metadata filtering limitation above still applies. The basic format is
FROM <index> ← Required.
WHERE … ← Required. See below.
ORDER BY <metadata field> [ASC | DESC] ← Optional.
LIMIT <how many> ← The maximum and default is 1000.
The ALIAS above refers to an AI Assistant alias as configured by the Qarbine administrator. This is important so that the model used to generate the embedding is compatible with that used to create the stored values.
The base WHERE clause can take several forms:
vector = (number 1, number n ...) ← A different way of expressing nearVector()
nearVector(number1, number n …)
nearText(aPhrase)
nearText(aPhrase, aiAssistantAlias)
The parameters are described below.
Parameter | Description |
---|---|
aPhrase | A quoted value for which a vector is first obtained by Qarbine and then passed along as the raw vector value. |
aiAssistantAlias | Refers to an AI Assistant alias as configured by the Qarbine administrator. This is important to consider so that the model used to generate the raw vector value is compatible with that used to create the stored values. |
Note that a SQL numeric list is enclosed in parentheses while one in the specification is enclosed in brackets. That is a subtle nuance across the SQL and JSON syntax standards.
The elements of the SQL statement are extracted and used as parameters to the standard native Pinecone interface previously discussed. The returnNoValues argument is always true. The index value may be of the form indexName.namespace. If so then the namespace value is sent to Pinecone. The WHERE clause may have additional criteria starting with “AND”. The default ordering is by score, highest first.
The SQL query below
select * from sample-movies
where nearText( 'two girls' ) and "box-office" > 102030405
order by summary desc
limit 2
is equivalent to
#pragma runPostQuery select * from data order by summary desc
{
"index": "sample-movies",
"returnNoValues": true,
"nearText": "two girls",
"query": {
"includeMetadata": true,
"filter" : {
"box-office":{ "$gt" : 102030405}
},
"topK": 2
}
}
Notice the dashed field names are enclosed in double quotes. Also, Qarbine data retrieval tools have a default LIMIT which can be overridden. IN this case by specifying topK.
Here is an example that uses SQL to generate the native Pinecone query which then performs post processing to sort the results by title.
select * from sample-movies
where embeddingNear('two girls')
and "box-office" > 102030405
and year in (2009, 2018)
order by title
limit 10
Reviewing the Generated Specification
You can enter criteria of the form “EXPLAIN SELECT ….” to have the SQL statement processed and have the returned answer set be the underlying query specification. When you want to see the effective Pinecone query specification simply precede your SELECT statement with the “explain ” text. A convenient way of specifying this is to have “explain” on the first line and the rest of your SQL on the next lines.
explain
select *
from "sample-movies"
where nearText( 'two girls')
and withOption('foo', 123)
and withOption('bar', (1,2,3) )
Then simply “comment out” the first line when not in use
// explain
select *
from "sample-movies"
where nearText( 'two girls')
and withOption('foo', 123)
and withOption('bar', (1,2,3) )
You can also use “explain: true” in the JSON query specification for similar information.
Another way to get the specification is to press ALT and click . Below is a sample result.
Any “explain SELECT” or “explain: true” takes precedence over the ALT-click interaction.
References
See the following pages for more information on querying Pinecone
https://docs.pinecone.io/guides/data/query-data
https://docs.pinecone.io/guides/data/filter-with-metadata