A simple interface for querying and managing your Pinecone vector database. Search through your documents using natural language queries.
To use PineconeIndex in your own Val Town project:
import PineconeIndex from "https://esm.town/v/peterqliu/PineconeIndex/PineconeIndex";
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,
});
// Use the methods directly
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();
import PineconeIndex from "https://esm.town/v/peterqliu/PineconeIndex/PineconeIndex";
const index = new PineconeIndex({
name: "my-documents",
model: "text-embedding-ada-002",
dimensions: 1536,
pineconeKey: Deno.env.get("PINECONE_KEY"),
modelToken: Deno.env.get("OPENAI_KEY"),
});
// Add some documents
await index.upsertRecords([
"AI is revolutionizing healthcare",
"Machine learning enables automation",
"Solar energy is becoming more efficient"
]);
// Search for relevant documents
const results = await index.query("renewable energy technologies");
console.log(results.matches);
Create an HTTP endpoint that uses your index:
// query index with string for nearest neighbors
export default async function (req: Request): Promise<Response> {
return await index.handleRequest(req);
}
PineconeIndex also provides convenience methods to access your indices via HTTP. This is useful when multiple other vals need to access the index, or when we need access without sharing Pinecone or OpenAI keys.
When using the HTTP server, operations are determined by the URL path. The first segment after the domain specifies the operation to perform.
const API_URL = "https://your-val-url.web.val.run";
// Search for documents
const searchResponse = await fetch(`${API_URL}/query/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify("artificial intelligence trends")
});
The HTTP API returns standard status codes:
200- Success400- Bad Request (missing required fields)405- Method Not Allowed (wrong HTTP method)500- Server Error (API key issues, service problems)
Error responses include helpful messages:
{ "error": "Query text is required", "hint": "Make sure PINECONE_KEY and OPENAI_KEY environment variables are set" }