In Go, how to use discoveryengine for Gen App Builder Search Request Integration

395 views Asked by At

I know I can use the API version https://discoveryengine.googleapis.com but I want to try integration via the go package. Can anyone please help me solve the issue? The package is in beta .. no example from the net yet

package gcp

import (
    "context"
    "fmt"
    "log"
    "virtual-idol-api/config"

    "cloud.google.com/go/storage"
    "google.golang.org/api/option"

    discoveryengine "cloud.google.com/go/discoveryengine/apiv1beta"
    discoveryenginepb "cloud.google.com/go/discoveryengine/apiv1beta/discoveryenginepb"
)

// NewGCPStorageClient creates a new GCPClient and initializes its dependencies based on the environment.
func NewGCPClient(cfg *config.Config) (*GCPClient, error) {
    client := GCPClient{}
    ctx := context.Background()

    var discoveryClient *discoveryengine.SearchClient
    var err error
    discoveryClient, err = discoveryengine.NewSearchClient(ctx)
    if err != nil {
        log.Fatalf("failed to create Discovery client: %s", err)
        return nil, err
    }
    client.DiscoveryClient = discoveryClient

    defer discoveryClient.Close()
    req := &discoveryenginepb.SearchRequest{
        ServingConfig: "projects/12345/locations/global/collections/default_collection/dataStores/test-unstructured_12456/servingConfigs/default_search:search",
        //Branch:        "default_search",

        Query: "who is the president of America",

        // TODO: Fill request struct fields.
        // See https://pkg.go.dev/cloud.google.com/go/discoveryengine/apiv1beta/discoveryenginepb#SearchRequest.
    }
    res := discoveryClient.Search(ctx, req)
    if res == nil {
        log.Println("Search result is nil")
    } else {
        fmt.Printf("%+v\n", *res)
    }

    return &client, nil
}

I always receive null value

1

There are 1 answers

2
Holt Skinner On BEST ANSWER

NOTE: Generative AI App Builder is currently allowlist only, so make sure your project and account have the appropriate access.

First, ensure that you've created a search engine in your project as outlined in this guide:

https://cloud.google.com/generative-ai-app-builder/docs/try-enterprise-search

You'll need to follow the instructions at Gen App Builder client libraries to install and configure the discoveryengine client library for Go.

The code sample that was on that page previously is autogenerated and doesn't include all of the parameters needed for the requests to be successful. Follow the comment listed in the code sample for the full list of parameters.

https://pkg.go.dev/cloud.google.com/go/discoveryengine/apiv1beta/discoveryenginepb#SearchRequest

A new go code sample was just added to the client library documentation page that includes more sample parameters. Here's what it is so you can follow it.

import (
    "context"
    "fmt"

    discoveryengine "cloud.google.com/go/discoveryengine/apiv1beta"
    discoveryenginepb "cloud.google.com/go/discoveryengine/apiv1beta/discoveryenginepb"
    "google.golang.org/api/iterator"
)

// search searches for a query in a search engine given the Google Cloud Project ID,
// Location, and Search Engine ID.
//
// This example uses the default search engine.
func search(projectID, location, searchEngineID, query string) error {

    ctx := context.Background()

    // Create a client
    client, err := discoveryengine.NewSearchClient(ctx)
    if err != nil {
        return err
    }
    defer client.Close()

    // Full resource name of search engine serving config
    servingConfig := fmt.Sprintf("projects/%s/locations/%s/collections/default_collection/dataStores/%s/servingConfigs/default_serving_config",
        projectID, location, searchEngineID)

    searchRequest := &discoveryenginepb.SearchRequest{
        ServingConfig: servingConfig,
        Query:         query,
    }

    it := client.Search(ctx, searchRequest)
    for {
        resp, err := it.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            return err
        }
        fmt.Printf("%+v\n", resp)
    }

    return nil
}