Giter Site home page Giter Site logo

docs-assistant's Issues

Link is down. Not getting any responses

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())
        );
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.