Last active
February 9, 2025 22:04
-
-
Save tomdevisser/c53ed228950e5c37b6465eca50b9e069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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