Skip to content

Instantly share code, notes, and snippets.

@tomdevisser
Last active February 9, 2025 22:04
Show Gist options
  • Save tomdevisser/c53ed228950e5c37b6465eca50b9e069 to your computer and use it in GitHub Desktop.
Save tomdevisser/c53ed228950e5c37b6465eca50b9e069 to your computer and use it in GitHub Desktop.
const { Pinecone: PineconeClient } = require("@pinecone-database/pinecone");
const { config } = require("dotenv");
config();
/**
* Class for interacting with Pinecone API.
*/
class Pinecone {
constructor() {
try {
this.client = new PineconeClient({
apiKey: process.env.PINECONE_API_KEY,
});
} catch (error) {
console.error("Failed to initialize Pinecone client:", error);
throw error;
}
}
async getIndex(indexName) {
try {
return await this.client.index(indexName);
} catch (error) {
console.error("Failed to get Pinecone index:", error);
throw error;
}
}
async search(indexName, embedding, topK = 3) {
try {
const index = await this.getIndex(indexName);
const searchResults = await index.query({
vector: embedding,
topK,
includeMetadata: true,
});
return searchResults.matches;
} catch (error) {
console.error("Failed to search Pinecone index:", error);
throw error;
}
}
}
module.exports = new Pinecone();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment