在Go语言中,如何使用discoveryengine来进行Gen App Builder搜索请求集成?

huangapple go评论176阅读模式
英文:

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

问题

我知道我可以使用API版本https://discoveryengine.googleapis.com,但我想尝试通过go包进行集成。有人可以帮我解决这个问题吗?
这个包还在测试阶段,网络上还没有示例。

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 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

得分: 2

注意:生成式 AI 应用构建器目前仅允许白名单访问,因此请确保您的项目和帐户具有适当的访问权限。

首先,请确保按照此指南中的说明在您的项目中创建了一个搜索引擎:

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

您需要按照生成式应用构建器客户端库中的说明安装和配置适用于 Go 的 discoveryengine 客户端库。

之前在该页面上的代码示例是自动生成的,并且不包含请求成功所需的所有参数。请按照代码示例中列出的注释获取完整的参数列表。

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

刚刚在客户端库文档页面中添加了一个新的 Go 代码示例,其中包含更多的示例参数。以下是示例代码:

import (
	"context"
	"fmt"

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

// search 在给定 Google Cloud 项目 ID、位置和搜索引擎 ID 的搜索引擎中搜索查询。
//
// 此示例使用默认搜索引擎。
func search(projectID, location, searchEngineID, query string) error {

	ctx := context.Background()

	// 创建客户端
	client, err := discoveryengine.NewSearchClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	// 搜索引擎服务配置的完整资源名称
	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
}
英文:

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
}

huangapple
  • 本文由 发表于 2023年7月25日 23:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76764155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定