Feature 1814 - Provide mechanism to read Pinecone api key from file

This commit is contained in:
Madhavan Sridharan 2024-02-09 13:56:56 -05:00
parent c2dde4bffd
commit d3c18a224c
3 changed files with 44 additions and 6 deletions

View File

@ -49,7 +49,7 @@
<dependency> <dependency>
<groupId>io.pinecone</groupId> <groupId>io.pinecone</groupId>
<artifactId>pinecone-client</artifactId> <artifactId>pinecone-client</artifactId>
<version>0.2.3</version> <version>0.7.4</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -27,13 +27,19 @@ import io.pinecone.PineconeConnectionConfig;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
public class PineconeSpace { public class PineconeSpace {
private final static Logger logger = LogManager.getLogger(PineconeSpace.class); private final static Logger logger = LogManager.getLogger(PineconeSpace.class);
private final String apiKey; private final String apiKey;
private final String apiKeyFile;
private final String environment; private final String environment;
private final String projectName; private final String projectName;
private final String name; private final String name;
@ -51,7 +57,23 @@ public class PineconeSpace {
* @param cfg The configuration ({@link NBConfiguration}) for this nb run * @param cfg The configuration ({@link NBConfiguration}) for this nb run
*/ */
public PineconeSpace(String name, NBConfiguration cfg) { public PineconeSpace(String name, NBConfiguration cfg) {
this.apiKey = cfg.get("apiKey"); String apiKeyFromFile = null;
this.apiKeyFile = cfg.get("apiKeyFile");
if(null != this.apiKeyFile && this.apiKeyFile.length() > 0) {
Optional<String> apiKeyFileOpt = cfg.getOptional("apiKeyFile");
if(apiKeyFileOpt.isPresent()) {
Path path = Paths.get(apiKeyFileOpt.get());
try {
apiKeyFromFile = Files.readAllLines(path).getFirst();
} catch (IOException e) {
String error = "Error while reading api key from file:" + path;
logger.error(error, e);
throw new RuntimeException(e);
}
}
}
this.apiKey = (apiKeyFromFile != null) ? apiKeyFromFile : cfg.get("apiKey");
this.environment = cfg.get("environment"); this.environment = cfg.get("environment");
this.projectName = cfg.get("projectName"); this.projectName = cfg.get("projectName");
this.name = name; this.name = name;
@ -59,7 +81,7 @@ public class PineconeSpace {
.withApiKey(apiKey) .withApiKey(apiKey)
.withEnvironment(environment) .withEnvironment(environment)
.withProjectName(projectName); .withProjectName(projectName);
logger.info(this.name + ": Creating new Pinecone Client with api key " + apiKey + ", environment " logger.info(this.name + ": Creating new Pinecone Client with api key " + maskDigits(apiKey) + ", environment "
+ environment + " and project name " + projectName); + environment + " and project name " + projectName);
this.client = new PineconeClient(config); this.client = new PineconeClient(config);
} }
@ -86,7 +108,10 @@ public class PineconeSpace {
return ConfigModel.of(PineconeSpace.class) return ConfigModel.of(PineconeSpace.class)
.add( .add(
Param.required("apiKey",String.class) Param.optional("apiKeyFile", String.class, "file to load the api key from")
)
.add(
Param.optional("apiKey",String.class)
.setDescription("the Pinecone API key to use to connect to the database") .setDescription("the Pinecone API key to use to connect to the database")
) )
.add( .add(
@ -100,4 +125,17 @@ public class PineconeSpace {
.asReadOnly(); .asReadOnly();
} }
private static String maskDigits(String unmasked) {
int inputLength = (null == unmasked) ? 0 : unmasked.length();
StringBuilder masked = new StringBuilder(inputLength);
for(char ch : unmasked.toCharArray()) {
if (Character.isDigit(ch)) {
masked.append("*");
} else {
masked.append(ch);
}
}
return masked.toString();
}
} }

View File

@ -7,8 +7,8 @@ https://github.com/pinecone-io/pinecone-java-client
The following parameters must be supplied to the adapter at runtime in order to successfully connect to an The following parameters must be supplied to the adapter at runtime in order to successfully connect to an
instance of the Pinecone database: instance of the Pinecone database:
* api key - In order to use the pinecone database you must have an account. Once the account is created you can request * api key - In order to use the pinecone database you must have an account. Once the account is created you can [request
an api key. This key will need to be provided any time a database connection is desired. an api key](https://docs.pinecone.io/docs/quickstart#2-get-your-api-key). This key will need to be provided any time a database connection is desired.
* environment - When an Index is created in the database the environment must be specified as well. The adapter will * environment - When an Index is created in the database the environment must be specified as well. The adapter will
use the default value of us-east-1 if none is provided at runtime. use the default value of us-east-1 if none is provided at runtime.
* project name - A project name must also be provided at time of index creation. This name then needs to be provided * project name - A project name must also be provided at time of index creation. This name then needs to be provided