|
| 1 | +package net.b07z.sepia.sdk.services.uid1007; |
| 2 | + |
| 3 | +import java.util.TreeSet; |
| 4 | + |
| 5 | +import net.b07z.sepia.server.assist.answers.ServiceAnswers; |
| 6 | +import net.b07z.sepia.server.assist.assistant.LANGUAGES; |
| 7 | +import net.b07z.sepia.server.assist.data.Parameter; |
| 8 | +import net.b07z.sepia.server.assist.interpreters.NluResult; |
| 9 | +import net.b07z.sepia.server.assist.parameters.WebApiParameter; |
| 10 | +import net.b07z.sepia.server.assist.services.ServiceBuilder; |
| 11 | +import net.b07z.sepia.server.assist.services.ServiceInfo; |
| 12 | +import net.b07z.sepia.server.assist.services.ServiceInterface; |
| 13 | +import net.b07z.sepia.server.assist.services.ServiceResult; |
| 14 | +import net.b07z.sepia.server.assist.services.ServiceInfo.Content; |
| 15 | +import net.b07z.sepia.server.assist.services.ServiceInfo.Type; |
| 16 | +import net.b07z.sepia.server.core.data.Answer; |
| 17 | +import net.b07z.sepia.server.core.data.Language; |
| 18 | +import net.b07z.sepia.server.core.tools.Sdk; |
| 19 | + |
| 20 | +/** |
| 21 | + * Demonstration of how to use SEPIA Python-Bridge to enhance NLU and services. |
| 22 | + * |
| 23 | + * @author Florian Quirin |
| 24 | + * |
| 25 | + */ |
| 26 | +public class PythonBridgeDemo implements ServiceInterface { |
| 27 | + |
| 28 | + //Command name of your service (will be combined with userId to be unique, e.g. 'uid1007.python_bridge') |
| 29 | + private static final String CMD_NAME = "python_bridge"; |
| 30 | + |
| 31 | + //Define some sentences for testing: |
| 32 | + |
| 33 | + @Override |
| 34 | + public TreeSet<String> getSampleSentences(String lang) { |
| 35 | + TreeSet<String> samples = new TreeSet<>(); |
| 36 | + //GERMAN |
| 37 | + if (lang.equals(Language.DE.toValue())){ |
| 38 | + samples.add("Ich würde gerne die Python Brücke testen."); |
| 39 | + //OTHER |
| 40 | + }else{ |
| 41 | + samples.add("I'd like to test the Python bridge."); |
| 42 | + } |
| 43 | + return samples; |
| 44 | + } |
| 45 | + |
| 46 | + //Basic service setup: |
| 47 | + |
| 48 | + //Overriding the 'getAnswersPool' methods enables you to define custom answers with more complex features. |
| 49 | + //You can build a pool of answers that can have multiple versions of the same answer used for different |
| 50 | + //situations like a repeated question (what was the time? -> sorry say again, what was the time? -> ...). |
| 51 | + |
| 52 | + @Override |
| 53 | + public ServiceAnswers getAnswersPool(String language) { |
| 54 | + ServiceAnswers answerPool = new ServiceAnswers(language); |
| 55 | + |
| 56 | + //Build English answers |
| 57 | + if (language.equals(LANGUAGES.EN)){ |
| 58 | + answerPool |
| 59 | + //simple method to add answers |
| 60 | + .addAnswer(successAnswer, 0, "Test successful.") |
| 61 | + //complete method to add answers |
| 62 | + .addAnswer(new Answer( |
| 63 | + Language.from(language), okAnswer, |
| 64 | + "Message received but I could not fulfill your request.", |
| 65 | + Answer.Character.neutral, 0, 5 |
| 66 | + )) |
| 67 | + |
| 68 | + .addAnswer(askCodeWord, 0, "What's your code word?") |
| 69 | + .addAnswer(askCodeWord, 1, "Wrong, try again please. What's your code word?") |
| 70 | + .addAnswer(askCodeWord, 2, "Still wrong. Do you know the mines of Moria? Speak, 'friend', and enter.") |
| 71 | + ; |
| 72 | + return answerPool; |
| 73 | + |
| 74 | + //Other languages not yet supported |
| 75 | + }else{ |
| 76 | + answerPool |
| 77 | + .addAnswer(successAnswer, 0, "Test erfolgreich.") |
| 78 | + .addAnswer(okAnswer, 0, "Die Anfrage ist angekommen aber ich kann sie nicht bearbeiten.") |
| 79 | + |
| 80 | + .addAnswer(askCodeWord, 0, "Wie lautet das Schlüsselwort?") |
| 81 | + .addAnswer(askCodeWord, 1, "Falsch, versuch es noch einmal bitte. Wie lautet das Schlüsselwort?") |
| 82 | + .addAnswer(askCodeWord, 2, "Immer noch falsch. Kennst du die Minen von Moria? Sprich, 'Freund', und tritt ein.") |
| 83 | + ; |
| 84 | + return answerPool; |
| 85 | + } |
| 86 | + } |
| 87 | + //We keep a reference here for easy access in getResult - Note that custom answers need to start with a certain prefix |
| 88 | + private static final String failAnswer = "error_0a"; |
| 89 | + private static final String successAnswer = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_success_0a"; |
| 90 | + private static final String okAnswer = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_still_ok_0a"; |
| 91 | + private static final String askCodeWord = ServiceAnswers.ANS_PREFIX + CMD_NAME + "_ask_code_1a"; |
| 92 | + |
| 93 | + |
| 94 | + @Override |
| 95 | + public ServiceInfo getInfo(String language) { |
| 96 | + //Type of service (for descriptions, choose what you think fits best) |
| 97 | + ServiceInfo info = new ServiceInfo(Type.plain, Content.data, false); |
| 98 | + |
| 99 | + //Should be available publicly or only for the developer? Set this when you are done with testing and want to release |
| 100 | + //info.makePublic(); |
| 101 | + |
| 102 | + //Command |
| 103 | + info.setIntendedCommand(Sdk.getMyCommandName(this, CMD_NAME)); |
| 104 | + |
| 105 | + //Direct-match trigger sentences in different languages |
| 106 | + //NOTE: we use SEPIA internal NLU for direct match here and Python for more complex stuff to see how both work in parallel |
| 107 | + String EN = Language.EN.toValue(); |
| 108 | + info.addCustomTriggerSentence("Test Python bridge.", EN); |
| 109 | + String DE = Language.DE.toValue(); |
| 110 | + info.addCustomTriggerSentence("Python Brücke testen.", DE); |
| 111 | + |
| 112 | + //Regular expression triggers |
| 113 | + //NOTE: we don't use those here because we want to do complex NLU via the Python bridge |
| 114 | + //info.setCustomTriggerRegX(".*\\b(python bridge)\\b.*", EN); |
| 115 | + //info.setCustomTriggerRegXscoreBoost(5); //boost service a bit to increase priority over similar ones |
| 116 | + |
| 117 | + //Parameters: |
| 118 | + |
| 119 | + //This service has a one required parameter, the code word. |
| 120 | + //Required parameters will be asked automatically by SEPIA using the defined question. |
| 121 | + Parameter p1 = new Parameter(new CodeWord()) |
| 122 | + .setRequired(true) |
| 123 | + .setQuestion(askCodeWord); |
| 124 | + info.addParameter(p1); |
| 125 | + |
| 126 | + //Answers (these are the default answers, you can trigger a custom answer at any point in the module |
| 127 | + //with serviceBuilder.setCustomAnswer(..)): |
| 128 | + info.addSuccessAnswer(successAnswer) |
| 129 | + .addFailAnswer(failAnswer) |
| 130 | + .addOkayAnswer(okAnswer) |
| 131 | + .addCustomAnswer("askCodeWord", askCodeWord); //optional, just for info |
| 132 | + |
| 133 | + //Add answer parameters that are used to replace <1>, <2>, ... in your answers. |
| 134 | + //The name is arbitrary but you need to use the same one in getResult(...) later for api.resultInfoPut(...) |
| 135 | + info.addAnswerParameters("code"); //<1>=code, <2>=... |
| 136 | + |
| 137 | + return info; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public ServiceResult getResult(NluResult nluResult) { |
| 142 | + //initialize result |
| 143 | + ServiceBuilder api = new ServiceBuilder(nluResult, |
| 144 | + getInfoFreshOrCache(nluResult.input, this.getClass().getCanonicalName()), |
| 145 | + getAnswersPool(nluResult.language)); |
| 146 | + |
| 147 | + //get required parameters: |
| 148 | + |
| 149 | + //-code |
| 150 | + Parameter codeParameter = nluResult.getRequiredParameter(CodeWord.class.getName()); |
| 151 | + String code = codeParameter.getValueAsString(); |
| 152 | + |
| 153 | + //get optional parameters: |
| 154 | + //NONE |
| 155 | + |
| 156 | + //Set answer parameters as defined in getInfo(): |
| 157 | + api.resultInfoPut("code", code); |
| 158 | + |
| 159 | + // ... here you could put some code that runs after successful code word. |
| 160 | + // wrong code will automatically lead to rejection before reaching this part ... |
| 161 | + |
| 162 | + //all good |
| 163 | + api.setStatusSuccess(); |
| 164 | + |
| 165 | + //build the API_Result |
| 166 | + ServiceResult result = api.buildResult(); |
| 167 | + return result; |
| 168 | + } |
| 169 | + |
| 170 | + //----------------- custom parameters ------------------- |
| 171 | + |
| 172 | + /** |
| 173 | + * Parameter handler that tries to extract code word via Python bridge. |
| 174 | + */ |
| 175 | + public static class CodeWord extends WebApiParameter { |
| 176 | + |
| 177 | + @Override |
| 178 | + public String getApiUrl(){ |
| 179 | + //Enter the URL to your SEPIA Python-Bridge here including parameter path |
| 180 | + String nluBridgeUrl = "http://localhost:20731/nlu/"; |
| 181 | + String parameterPath = "get_parameter/code_word"; |
| 182 | + return nluBridgeUrl + parameterPath; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments