marcushellberg / docs-assistant Goto Github PK
View Code? Open in Web Editor NEWdocs-assistant
License: The Unlicense
docs-assistant
License: The Unlicense
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())
);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.