登录
首页 >  Golang >  Go问答

如何在 Go 中整合 Gen App Builder 的搜索请求与 discoveryengine

来源:stackoverflow

时间:2024-02-23 13:18:29 472浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《如何在 Go 中整合 Gen App Builder 的搜索请求与 discoveryengine》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我知道我可以使用 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
}

我总是收到空值


正确答案


注意:Generative AI App Builder 目前仅属于许可名单,因此请确保您的项目和帐户具有适当的访问权限。

首先,确保您已按照本指南中的概述在项目中创建了搜索引擎:

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

您需要按照 Gen App Builder client libraries 中的说明安装和配置适用于 go 的 discoveryengine 客户端库。

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

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

client library documentation page 中刚刚添加了一个新的 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 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
}

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>