I use this assistant for not only vaadin but also for everything. You said, the code needs to be updated to use Serverless index and this is what I got from Pinecone AI support. Hope this helps you to quickly fix the issue
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import reactor.core.publisher.Mono;
import java.util.Map;
public class PineconeExample {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY";
String baseUrl = "https://api.pinecone.io";
String indexName = "serverless-index";
int dimension = 1536;
String metric = "cosine";
String cloud = "aws";
String region = "us-east-1";
// Create index
createServerlessIndex(apiKey, baseUrl, indexName, dimension, metric, cloud, region);
// Query index
float[] embedding = {/* Your embedding values */};
int maxResults = 10;
String namespace = "your-namespace";
var body = Map.of(
"vector", embedding,
"topK", maxResults,
"includeMetadata", true,
"namespace", namespace
);
queryIndex(apiKey, baseUrl, indexName, body);
}
private static void createServerlessIndex(String apiKey, String baseUrl, String indexName, int dimension, String metric, String cloud, String region) {
String url = baseUrl + "/indexes";
WebClient webClient = WebClient.builder()
.baseUrl(url)
.defaultHeader("Accept", "application/json")
.defaultHeader("Content-Type", "application/json")
.defaultHeader("Api-Key", apiKey)
.build();
String requestBody = String.format("{\n" +
" \"name\": \"%s\",\n" +
" \"dimension\": %d,\n" +
" \"metric\": \"%s\",\n" +
" \"spec\": {\n" +
" \"serverless\": {\n" +
" \"cloud\": \"%s\",\n" +
" \"region\": \"%s\"\n" +
" }\n" +
" }\n" +
"}", indexName, dimension, metric, cloud, region);
Mono<ClientResponse> response = webClient.post()
.body(BodyInserters.fromValue(requestBody))
.exchangeToMono(clientResponse -> {
if (clientResponse.statusCode().is2xxSuccessful()) {
return clientResponse.bodyToMono(String.class);
} else {
return clientResponse.createException().flatMap(Mono::error);
}
});
response.subscribe(
success -> System.out.println("Index created successfully: " + success),
error -> System.err.println("Error creating index: " + error.getMessage())
);
}
private static void queryIndex(String apiKey, String baseUrl, String indexName, Map<String, Object> body) {
String url = String.format("%s/indexes/%s/query", baseUrl, indexName);
WebClient webClient = WebClient.builder()
.baseUrl(url)
.defaultHeader("Accept", "application/json")
.defaultHeader("Content-Type", "application/json")
.defaultHeader("Api-Key", apiKey)
.build();
Mono<ClientResponse> response = webClient.post()
.body(BodyInserters.fromValue(body))
.exchangeToMono(clientResponse -> {
if (clientResponse.statusCode().is2xxSuccessful()) {
return clientResponse.bodyToMono(String.class);
} else {
return clientResponse.createException().flatMap(Mono::error);
}
});
response.subscribe(
success -> System.out.println("Query response: " + success),
error -> System.err.println("Error querying index: " + error.getMessage())
);
}
}