简体   繁体   English

是否可以使用 API 到 AZURE bot 服务来创建聊天机器人模型传递意图、对话

[英]Is it possible to create chat bot model passing intents , dialog from java using an API to AZURE bot service

  1. below code is used to create intents and passing it to google.下面的代码用于创建意图并将其传递给谷歌。
  2. I want to achieve the same using java + azure bot platform我想使用 java + azure bot 平台实现相同的目标
  3. I want to framework in which user will be able to pass intents to chat model present on azure.我想构建一个框架,用户可以在其中将意图传递给 azure 上的聊天模型。
  4. that frame work will be created in java.该框架将在 Java 中创建。
  5. For now i am working on POC , in which i will pass intents as below code to azure.现在我正在研究 POC,其中我将以下代码的意图传递给 azure。
  6. i want to if its possible , is there any api same as google or amazon in azure as well.我想如果可能的话,是否也有与 google 或 amazon 相同的 azure api。
import com.google.api.gax.paging.Page;
import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.dialogflow.v2.Intent;
import com.google.cloud.dialogflow.v2.Intent.Message;
import com.google.cloud.dialogflow.v2.Intent.Message.Text;
import com.google.cloud.dialogflow.v2.IntentsClient;
import com.google.cloud.dialogflow.v2.ProjectAgentName;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SankalpGCPChatBot {

    public static void main(String[] args) {

        List<String> trainingPhrasesParts = new ArrayList<>();
        List<String> messageTexts = new ArrayList<>();

        trainingPhrasesParts.add("What is your name?");
        messageTexts.add("My name is Sankalp Bot.");
        String displayName = "SankalpTestIntent";
        String projectId = "newagent-257c8";

        try {
            createIntent(displayName, projectId, trainingPhrasesParts, messageTexts);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void authCompute() {
        GoogleCredentials credentials = ComputeEngineCredentials.create();
        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

        System.out.println("Buckets:");
        Page<Bucket> buckets = storage.list();
        for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
        }
    }

    static void authImplicit() {
        Storage storage = StorageOptions.getDefaultInstance().getService();

        System.out.println("Buckets:");
        Page<Bucket> buckets = storage.list();
        for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
        }
    }

    static void authExplicit(String jsonPath) throws IOException {
        GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
                .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

        System.out.println("Buckets:");
        Page<Bucket> buckets = storage.list();
        for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
        }
    }

    public static Intent createIntent(
            String displayName,
            String projectId,
            List<String> trainingPhrasesParts,
            List<String> messageTexts) throws Exception {

        try (IntentsClient intentsClient = IntentsClient.create()) {
            ProjectAgentName parent = ProjectAgentName.of(projectId);

            List<Intent.TrainingPhrase> trainingPhrases = new ArrayList<>();
            for (String trainingPhrase : trainingPhrasesParts) {
                trainingPhrases.add(
                        Intent.TrainingPhrase.newBuilder().addParts(
                                Intent.TrainingPhrase.Part.newBuilder().setText(trainingPhrase).build())
                                .build());
            }

            Message message = Message.newBuilder()
                    .setText(
                            Text.newBuilder()
                                    .addAllText(messageTexts).build()
                    ).build();

            Intent intent = Intent.newBuilder()
                    .setDisplayName(displayName)
                    .addMessages(message)
                    .addAllTrainingPhrases(trainingPhrases)
                    .build();

            Intent response = intentsClient.createIntent(parent, intent);
            System.out.format("Intent created: %s\n", response);

            return response;
        }
    }
}

The Java version of the Microsoft Bot Framework is currently in preview (and is being actively worked on) as of this answer.截至本答案,Microsoft Bot Framework 的 Java 版本目前处于预览状态(并且正在积极开发中)。 You can track changes by monitoring the Botbuilder-Java repo on Github.您可以通过监控 Github 上的Botbuilder-Java 存储库来跟踪更改。

The repo also includes a small number of samples, however none (AFAIK) have been built to show how to connect a Java bot to a Language understanding service such as LUIS.该存储库还包括少量示例,但没有构建 (AFAIK) 来展示如何将 Java 机器人连接到语言理解服务,例如 LUIS。 Also, bear in mind that as it is still in preview, some of the samples might not behave as expected.此外,请记住,由于它仍处于预览阶段,因此某些示例可能不会按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM