A simple interface for making and querying Pinecone vector databases. Use OpenAI embeddings to vectorize and search
Create keys for Pinecone and OpenAI, and store then in your environment variables.
import PineconeIndex from "https://esm.town/v/peterqliu/PineconeIndex/main.tsx";
// set up your environment variables
const pineconeKey = Deno.env.get("PINECONE_KEY");
const modelToken = Deno.env.get("OPENAI_KEY");
const index = new PineconeIndex({
name: "2025-all-docs",
model: "text-embedding-ada-002",
dimensions: 1536,
pineconeKey,
modelToken,
});
await index.create();
// Using the methods directly (see bottom of README for HTTP access)
const results = await index.query("machine learning applications");
await index.upsertRecords(["Document 1", "Document 2"]);
await index.empty();
Find the most relevant documents for your query.
const results = await index.query("your search text");
Returns:
{ "matches": [ { "id": "doc-123", "score": 0.95, "metadata": { "text": "Machine learning is transforming..." } } ] }
Upload new documents to your index.
await index.upsertRecords([
"First document content",
"Second document content",
]);
Remove all documents from your index.
await index.empty();
Create a new Pinecone index (if it doesn't exist).
await index.create();
PineconeIndex also provides handleRequest as a convenience method to access your indices via HTTP. This is useful when accessing the index from other vals, especially without sharing Pinecone and OpenAI credentials.
Instantiate your index in one val:
const index = new PinconeIndex({...});
export default async function (req: Request): Promise<Response> {
return await index.handleRequest(req);
}
In vals that need to Operations are determined by the URL path. The first segment after the domain specifies the operation to perform.
const API_URL = "https://index-val-url.web.val.run";
// Search for documents: append the desired operation to the url
const searchResponse = await fetch(`${API_URL}/query/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify("artificial intelligence trends"),
});