新编程语言的客户端#

本节中描述的所有 Riva 语音 AI 技能均使用 gRPC 公开,以最大限度地提高与现有软件基础设施的兼容性并简化集成。gRPC 官方支持 12 种语言,包括 C++、Java、Python 和 Golang,并对其他许多语言提供非官方支持。

gRPC 服务和消息/数据结构使用协议缓冲区定义文件定义,这些文件可在 GitHub 上找到。使用这些文件,您可以为您选择的任何受支持语言生成 Riva 语音 AI 服务绑定。生成的代码可以编译到您的应用程序中,唯一的额外依赖项是 gRPC 库。

下载 Riva API#

生成绑定的过程取决于您打算用于 Riva 开发的目标语言。但是,高级步骤是相同的。

  1. 下载、构建(如果需要)并安装适用于您的目标平台和语言的 gRPC。

  2. 从 GitHub 或 NGC 获取 Riva 的 gRPC 服务和协议缓冲区定义

    nvidia-riva/common

    git clone https://github.com/nvidia-riva/common.git
    cd common/riva/proto
    
  3. 使用 protoc 工具生成特定于语言的绑定(请参阅 gRPC 文档)。

  4. 使用上一步中生成的绑定,以您所需的语言编写客户端。

生成语言绑定#

在本教程中,我们将构建一个使用 Golang 编写的 Riva 问答应用程序。

  1. 如果您尚未安装 Golang 编译器,请下载并安装。确保您的环境 $PATH 中包含 $GOPATH/bin,以便您可以使用 Golang 安装的工具(如 protoc)。

  2. 为 Golang 安装 Protocol Buffers 和 gRPC 库。

    export GO111MODULE=on
    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
    
  3. 为您的项目创建一个目录。使用 go mod 初始化一个模块。

    mkdir riva-qa-golang-client
    cd riva-qa-golang-client
    go mod init riva-qa
    
  4. 从 GitHub 或 NGC 下载并复制 Riva API 定义。

    wget -O riva-common.zip https://github.com/nvidia-riva/common/archive/main.zip
    unzip -j -d riva_speech riva-common.zip '*.proto'
    rm riva-common.zip
    
  5. 为 Riva NLP 服务生成 Golang 绑定。

    protoc --go_out=. --go_opt=paths=source_relative \
       --go-grpc_out=. --go-grpc_opt=paths=source_relative \
       riva_speech/riva_nlp.proto
    

编写客户端#

  1. 编写一个简单的同步客户端。使用您选择的文本编辑器或 IDE,为我们的可执行文件 qa.go 创建一个源文件,其中包含以下代码。

    package main
    
    import (
        "context"
        "flag"
        "log"
        "time"
    
        "google.golang.org/grpc"
        pb "riva-qa/riva_speech"
    )
    
    func main() {
        serverFlag := flag.String("server", "localhost:50051", "Riva server to connect to")
        queryFlag := flag.String("query", "", "Question to ask")
        contextFlag := flag.String("context", "", "Context to search for answer")
    
        flag.Parse()
    
        if *queryFlag == "" || *contextFlag == "" {
            log.Fatal("-query and -context must both be set.")
        }
    
        // Set up a connection to the server.
        conn, err := grpc.Dial(*serverFlag, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second))
        if err != nil {
            log.Fatalf("Unable to connect to server: %v", err)
        }
        defer conn.Close()
        c := pb.NewRivaLanguageUnderstandingClient(conn)
    
        // Contact the server and print out its response.
        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()
    
        r, err := c.NaturalQuery(ctx, &pb.NaturalQueryRequest{
            Query:   *queryFlag,
            Context: *contextFlag,
            TopN:    1,
        })
    
        if err != nil {
            log.Fatalf("Issue encountered with Riva Request: %v", err)
        }
    
        log.Printf("Question: %s", *queryFlag)
        log.Printf("Answer:   %s", r.GetResults()[0].GetAnswer())
    }
    
  2. 编译并运行新客户端。

    go mod tidy
    go build qa.go
    ./qa -server <riva_uri> \
         -query "Which founder of NVIDIA previously worked at Sun?" \
         -context "Nvidia was founded on April 5, 1993, by Jensen Huang (CEO as of
                   2020), a Taiwanese American, previously director of CoreWare at LSI
                   Logic and a microprocessor designer at Advanced Micro Devices (AMD),
                   Chris Malachowsky, an electrical engineer who worked at Sun
                   Microsystems, and Curtis Priem, previously a senior staff engineer
                   and graphics chip designer at Sun Microsystems."
    
    2021/02/04 13:17:51 Question: Which founder of NVIDIA previously worked at Sun?
    2021/02/04 13:17:51 Answer:   Chris Malachowsky,