使用GRPC仅连接到对话框流StreamingDetectIntent,停留在等待responseStream.MoveNext [英] Using GRPC only connecting to dialog flow StreamingDetectIntent, stuck at await responseStream.MoveNext

查看:389
本文介绍了使用GRPC仅连接到对话框流StreamingDetectIntent,停留在等待responseStream.MoveNext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将DialogFlow API v2与Unity一起使用.

I'm trying to use DialogFlow API v2 with Unity.

由于没有用于Unity的正式SDK,我使用了Grpc beta unity SDK和通过Protobuf和grpc工具的protoc创建的生成的C#代码

Since there's no official SDK for Unity yet I used the Grpc beta unity SDK and the generated C# code I created with Protobuf and protoc from Grpc tools

此链接隐藏了Grpc beta unity sdk. https://packages.grpc.io/只需单击构建ID,您将找到一个构建的统一软件包

The Grpc beta unity sdk is hidden in this link. https://packages.grpc.io/ just click a build ID and you will find a built unity package.

我导入了Google.Apis.Auth.OAuth2和Grpc.Auth,它们没有包含在官方的Grpc unity beta sdk中.

I imported Google.Apis.Auth.OAuth2 and Grpc.Auth which weren't included in the official Grpc unity beta sdk.

然后我写了这段代码,除了等待responseStream.MoveNext()被卡住之外,这段代码看起来还不错.

Then I wrote this code which seems to work fine except that await responseStream.MoveNext() is stuck.

我相信主要原因是我不确定在哪里将终点设置为'/v2/projects/project-id/agent/intents'

I believe the main reason is I'm not sure where to set the path to the end point which is '/v2/projects/project-id/agent/intents'

    GoogleCredential credential = GoogleCredential.FromJson(privateKey);

    Grpc.Core.Channel channel = new Grpc.Core.Channel("dialogflow.googleapis.com", credential.ToChannelCredentials());

    var client = new SessionsClient(channel);

    CallOptions options = new CallOptions();
    var duplexStream = client.StreamingDetectIntent();

    var responseHandlerTask = System.Threading.Tasks.Task.Run(async () =>
    {
        IAsyncEnumerator<StreamingDetectIntentResponse> responseStream = duplexStream.ResponseStream;
        while (await responseStream.MoveNext())//stuck here
        {
            StreamingDetectIntentResponse response = responseStream.Current;
        }
        // The response stream has completed
    });

    // Send requests to the server
    bool done = false;
    while (!done)
    {
        // Initialize a request
        var queryInput = new QueryInput();
        queryInput.AudioConfig = new InputAudioConfig();
        queryInput.AudioConfig.LanguageCode = "ja";
        queryInput.AudioConfig.SampleRateHertz = 141000;
        queryInput.AudioConfig.AudioEncoding = AudioEncoding.Linear16;

        StreamingDetectIntentRequest request = new StreamingDetectIntentRequest
        {
            Session = "",
            QueryInput = queryInput,
        };
        var bytes = File.ReadAllBytes("test.wav");
        request.InputAudio = Google.Protobuf.ByteString.CopyFrom(bytes);
        try
        {
            await duplexStream.RequestStream.WriteAsync(request);
        }
        catch (System.Exception e)
        {
            context.Post(state =>
            {
                Debug.LogErrorFormat("{0}\n{1}\n{2}\n{3}", e.Message, e.HelpLink, e.Source, e.StackTrace);
            }, null);
        }

        done = true;
    }

    await duplexStream.RequestStream.CompleteAsync();
    await responseHandlerTask;

感谢前进.

推荐答案

我没有在请求中添加更正会话.以下内容对其进行了修复.

I didn't add correction session to the request. The following fixed it.

StreamingDetectIntentRequest request = new StreamingDetectIntentRequest
{
    Session = "projects/project-id/agent/sessions/sessionid",
    QueryInput = queryInput,
};

这篇关于使用GRPC仅连接到对话框流StreamingDetectIntent,停留在等待responseStream.MoveNext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆