Skip to content

Commit f2e821c

Browse files
authored
Merge pull request #5 from SEPIA-Framework/dev
Added 'PythonBridgeDemo' service; Updated 'WorkoutHelperDemo';
2 parents 92e0fe2 + ee4da68 commit f2e821c

File tree

4 files changed

+208
-5
lines changed

4 files changed

+208
-5
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}

smart-services/java/RestaurantDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public ServiceAnswers getAnswersPool(String language) {
9797
;
9898
return answerPool;
9999

100-
//Other languages not yet supported
100+
//Other languages are not used in this demo yet
101101
}else{
102102
return null;
103103
}

smart-services/java/WorkoutHelperDemo.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.b07z.sepia.server.assist.assistant.LANGUAGES;
1111
import net.b07z.sepia.server.assist.data.Parameter;
1212
import net.b07z.sepia.server.assist.interpreters.NluResult;
13+
import net.b07z.sepia.server.assist.interviews.Interview;
1314
import net.b07z.sepia.server.assist.interviews.InterviewData;
1415
import net.b07z.sepia.server.assist.services.ServiceAccessManager;
1516
import net.b07z.sepia.server.assist.services.ServiceBuilder;
@@ -20,6 +21,7 @@
2021
import net.b07z.sepia.server.assist.services.ServiceInfo.Type;
2122
import net.b07z.sepia.server.core.assistant.PARAMETERS;
2223
import net.b07z.sepia.server.core.data.Language;
24+
import net.b07z.sepia.server.core.tools.Debugger;
2325
import net.b07z.sepia.server.core.tools.JSON;
2426
import net.b07z.sepia.server.core.tools.Sdk;
2527

@@ -108,9 +110,17 @@ public ServiceInfo getInfo(String language) {
108110

109111
//Direct-match trigger sentences in different languages:
110112
String EN = Language.EN.toValue();
111-
info.addCustomTriggerSentence("Start a simple workout.", EN);
113+
info.addCustomTriggerSentence("Start a simple workout.", EN)
114+
.addCustomTriggerSentenceWithExtractedParameters("7 minutes workout.", EN, JSON.make(
115+
PARAMETERS.TIME, Interview.INPUT_RAW, //This will tell the 'Interview' module to extract the time from the raw text
116+
PARAMETERS.ACTION, "<on>" //This is what the Action parameter handler usually extracts for ON/START/ACTIVATE etc.
117+
));
112118
String DE = Language.DE.toValue();
113-
info.addCustomTriggerSentence("Ein einfaches Workout starten.", DE);
119+
info.addCustomTriggerSentence("Ein einfaches Workout starten.", DE)
120+
.addCustomTriggerSentenceWithRawParameters("7 Minuten Workout.", DE, JSON.make(
121+
PARAMETERS.TIME, "7 Minuten", //Here we use raw text, ...
122+
PARAMETERS.ACTION, "starten" //... and let the 'Interview' module normalize and extract the parameter
123+
));
114124

115125
//Regular expression triggers:
116126
info.setCustomTriggerRegX(".*\\b("
@@ -129,7 +139,9 @@ public ServiceInfo getInfo(String language) {
129139

130140
Parameter p1 = new Parameter(PARAMETERS.TIME)
131141
.setRequired(false);
132-
info.addParameter(p1);
142+
Parameter p2 = new Parameter(PARAMETERS.ACTION)
143+
.setRequired(false);
144+
info.addParameter(p1).addParameter(p2);
133145

134146
//Answers (these are the default answers, you can trigger a custom answer at any point in the module
135147
//with serviceBuilder.setCustomAnswer(..)):
@@ -155,7 +167,7 @@ public ServiceResult getResult(NluResult nluResult) {
155167
//test-load stored data
156168
ServiceAccessManager sam0 = new ServiceAccessManager("demokey");
157169
JSONObject userData = api.readServiceDataForUser(sam0, "tasks");
158-
System.out.println("result: " + userData);
170+
Debugger.println(CMD_NAME + " result: " + userData, 3); //DEBUG
159171

160172
//get optional parameters:
161173

smart-services/smart-services.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"name": "WorkoutHelperDemo",
1515
"cmd": "workout_helper",
1616
"path": "java/WorkoutHelperDemo.java",
17+
"version": "0.9.1"
18+
},{
19+
"name": "PythonBridgeDemo",
20+
"cmd": "python_bridge",
21+
"path": "java/PythonBridgeDemo.java",
1722
"version": "0.9.0"
1823
}
1924
]

0 commit comments

Comments
 (0)