Skip to content

Commit bafd50e

Browse files
authored
Merge pull request #107 from watson-developer-cloud/feature-99-conversationAbstraction
Feature 99 conversation abstraction
2 parents b829edf + a65b31c commit bafd50e

19 files changed

+1205
-385
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ exclude-filter.txt.meta
6464
/Travis/installUnity.sh.meta
6565
/Travis/README.md.meta
6666
/Travis/runTests.sh.meta
67-
/Travis/TravisBuild.cs.meta
67+
/Travis/TravisBuild.cs.meta

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ _2016-07-15_
77
* New: Added `Document Conversion` abstraction
88
* New: Added `AlchemyData News` abstraction
99
* New: Added `Retrieve and Rank` abstraction
10+
* New: Added `Conversation` abstraction
11+
* Fix: Added `LanguageTranslation` and `LanguageTranslator`
1012

1113
## Version 0.5.0
1214

Config.json.enc

0 Bytes
Binary file not shown.

Examples/ServiceExamples/Scripts/ExampleConversation.cs

100644100755
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,28 @@
1818
using UnityEngine;
1919
using System.Collections;
2020
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
21+
using IBM.Watson.DeveloperCloud.Utilities;
22+
using IBM.Watson.DeveloperCloud.Logging;
2123

2224
public class ExampleConversation : MonoBehaviour
2325
{
2426
private Conversation m_Conversation = new Conversation();
25-
private string m_WorkspaceID = "25dfa8a0-0263-471b-8980-317e68c30488";
27+
private string m_WorkspaceID;
2628
private string m_Input = "Can you unlock the door?";
2729

2830
void Start () {
29-
Debug.Log("User: " + m_Input);
30-
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
31+
LogSystem.InstallDefaultReactors();
32+
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_WorkspaceID");
33+
Debug.Log("User: " + m_Input);
34+
35+
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
3136
}
3237

33-
void OnMessage (DataModels.MessageResponse resp)
38+
void OnMessage (MessageResponse resp)
3439
{
3540
if(resp != null)
3641
{
37-
foreach(DataModels.MessageIntent mi in resp.intents)
42+
foreach(MessageIntent mi in resp.intents)
3843
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
3944

4045
if(resp.output != null && !string.IsNullOrEmpty(resp.output.text))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using UnityEngine;
19+
using System.Collections;
20+
using IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1;
21+
using IBM.Watson.DeveloperCloud.Logging;
22+
using IBM.Watson.DeveloperCloud.Utilities;
23+
24+
public class ExampleConversationExperimental : MonoBehaviour
25+
{
26+
private ConversationExperimental m_Conversation = new ConversationExperimental();
27+
private string m_WorkspaceID;
28+
private string m_Input = "Can you unlock the door?";
29+
30+
void Start () {
31+
LogSystem.InstallDefaultReactors();
32+
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationExperimentalV1_ID");
33+
Debug.Log("User: " + m_Input);
34+
35+
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
36+
}
37+
38+
void OnMessage (MessageResponse resp)
39+
{
40+
if(resp != null)
41+
{
42+
foreach(MessageIntent mi in resp.intents)
43+
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
44+
45+
if(resp.output != null && !string.IsNullOrEmpty(resp.output.text))
46+
Debug.Log("response: " + resp.output.text);
47+
}
48+
else
49+
{
50+
Debug.Log("Failed to invoke Message();");
51+
}
52+
}
53+
}

Examples/ServiceExamples/Scripts/ExampleConversationExperimental.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,46 @@ Transform:
653653
m_Children: []
654654
m_Father: {fileID: 0}
655655
m_RootOrder: 1
656+
--- !u!1 &1937656527
657+
GameObject:
658+
m_ObjectHideFlags: 0
659+
m_PrefabParentObject: {fileID: 0}
660+
m_PrefabInternal: {fileID: 0}
661+
serializedVersion: 4
662+
m_Component:
663+
- 4: {fileID: 1937656529}
664+
- 114: {fileID: 1937656528}
665+
m_Layer: 0
666+
m_Name: ExampleConversation
667+
m_TagString: Untagged
668+
m_Icon: {fileID: 0}
669+
m_NavMeshLayer: 0
670+
m_StaticEditorFlags: 0
671+
m_IsActive: 1
672+
--- !u!114 &1937656528
673+
MonoBehaviour:
674+
m_ObjectHideFlags: 0
675+
m_PrefabParentObject: {fileID: 0}
676+
m_PrefabInternal: {fileID: 0}
677+
m_GameObject: {fileID: 1937656527}
678+
m_Enabled: 1
679+
m_EditorHideFlags: 0
680+
m_Script: {fileID: 11500000, guid: 87040e721f2394a4a9f444ecbaf9d991, type: 3}
681+
m_Name:
682+
m_EditorClassIdentifier:
683+
--- !u!4 &1937656529
684+
Transform:
685+
m_ObjectHideFlags: 0
686+
m_PrefabParentObject: {fileID: 0}
687+
m_PrefabInternal: {fileID: 0}
688+
m_GameObject: {fileID: 1937656527}
689+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
690+
m_LocalPosition: {x: 0, y: 0, z: 0}
691+
m_LocalScale: {x: 1, y: 1, z: 1}
692+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
693+
m_Children: []
694+
m_Father: {fileID: 0}
695+
m_RootOrder: 5
656696
--- !u!1 &1979050314
657697
GameObject:
658698
m_ObjectHideFlags: 0

Scripts/Editor/ConfigEditor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ private class ServiceSetup
6464
URL ="https://console.ng.bluemix.net/catalog/services/tradeoff-analytics/", ServiceID="TradeoffAnalyticsV1" },
6565
new ServiceSetup() { ServiceName = "Personality Insights", ServiceAPI = "personality-insights/api",
6666
URL ="https://console.ng.bluemix.net/catalog/services/personality-insights/", ServiceID="PersonalityInsightsV2" },
67-
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation-experimental/api",
67+
//new ServiceSetup() { ServiceName = "Conversation (Experimental)", ServiceAPI = "conversation-experimental/api",
68+
// URL ="https://console.ng.bluemix.net/catalog/services/conversation/", ServiceID="ConversationExperimentalV1" },
69+
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation/api",
6870
URL ="https://console.ng.bluemix.net/catalog/services/conversation/", ServiceID="ConversationV1" },
6971
new ServiceSetup() { ServiceName = "RetrieveAndRank", ServiceAPI = "retrieve-and-rank/api",
7072
URL ="https://console.ng.bluemix.net/catalog/services/retrieve-and-rank/", ServiceID="RetrieveAndRankV1" },

Scripts/Services/Conversation-Experimental.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using System;
19+
using System.Text;
20+
using System.Collections.Generic;
21+
using FullSerializer;
22+
using MiniJSON;
23+
using IBM.Watson.DeveloperCloud.Utilities;
24+
using IBM.Watson.DeveloperCloud.Connection;
25+
using IBM.Watson.DeveloperCloud.Logging;
26+
27+
namespace IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1
28+
{
29+
/// <summary>
30+
/// This class wraps the Watson Conversation service.
31+
/// <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/conversation.html">Conversation Service</a>
32+
/// </summary>
33+
public class ConversationExperimental : IWatsonService
34+
{
35+
#region Public Types
36+
/// <summary>
37+
/// The callback for GetWorkspaces().
38+
/// </summary>
39+
public delegate void OnGetWorkspaces(Workspaces workspaces);
40+
/// <summary>
41+
/// The callback for Message().
42+
/// </summary>
43+
/// <param name="success"></param>
44+
public delegate void OnMessageCallback(bool success);
45+
/// <summary>
46+
/// The callback delegate for the Converse() function.
47+
/// </summary>
48+
/// <param name="resp">The response object to a call to Converse().</param>
49+
public delegate void OnMessage(MessageResponse resp);
50+
51+
#endregion
52+
53+
#region Public Properties
54+
#endregion
55+
56+
#region Private Data
57+
private const string SERVICE_ID = "ConversationExperimentalV1";
58+
private static fsSerializer sm_Serializer = new fsSerializer();
59+
#endregion
60+
61+
#region Message
62+
private const string SERVICE_MESSAGE = "/v1/workspaces";
63+
/// <summary>
64+
/// Message the specified workspaceId, input and callback.
65+
/// </summary>
66+
/// <param name="workspaceId">Workspace identifier.</param>
67+
/// <param name="input">Input.</param>
68+
/// <param name="callback">Callback.</param>
69+
public bool Message(string workspaceId, string input, OnMessage callback)
70+
{
71+
if(string.IsNullOrEmpty(workspaceId))
72+
throw new ArgumentNullException("workspaceId");
73+
if(string.IsNullOrEmpty(input))
74+
throw new ArgumentNullException("input");
75+
if(callback == null)
76+
throw new ArgumentNullException("callback");
77+
78+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
79+
if(connector == null)
80+
return false;
81+
82+
string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}";
83+
string reqString = string.Format(reqJson, input);
84+
85+
MessageReq req = new MessageReq();
86+
req.Callback = callback;
87+
req.Headers["Content-Type"] = "application/json";
88+
req.Headers["Accept"] = "application/json";
89+
req.Parameters["version"] = Version.VERSION;
90+
req.Function = "/" + workspaceId + "/message";
91+
req.Send = Encoding.UTF8.GetBytes(reqString);
92+
req.OnResponse = MessageResp;
93+
94+
return connector.Send(req);
95+
}
96+
97+
private class MessageReq : RESTConnector.Request
98+
{
99+
public OnMessage Callback { get; set; }
100+
}
101+
102+
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
103+
{
104+
MessageResponse response = new MessageResponse();
105+
if (resp.Success)
106+
{
107+
try
108+
{
109+
fsData data = null;
110+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
111+
if (!r.Succeeded)
112+
throw new WatsonException(r.FormattedMessages);
113+
114+
object obj = response;
115+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
116+
if (!r.Succeeded)
117+
throw new WatsonException(r.FormattedMessages);
118+
}
119+
catch (Exception e)
120+
{
121+
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
122+
resp.Success = false;
123+
}
124+
}
125+
126+
if (((MessageReq)req).Callback != null)
127+
((MessageReq)req).Callback(resp.Success ? response : null);
128+
}
129+
#endregion
130+
131+
#region IWatsonService implementation
132+
133+
public string GetServiceID()
134+
{
135+
return SERVICE_ID;
136+
}
137+
138+
public void GetServiceStatus(ServiceStatus callback)
139+
{
140+
if (Config.Instance.FindCredentials(SERVICE_ID) != null)
141+
new CheckServiceStatus(this, callback);
142+
else
143+
{
144+
if (callback != null && callback.Target != null)
145+
{
146+
callback(SERVICE_ID, false);
147+
}
148+
}
149+
}
150+
151+
private class CheckServiceStatus
152+
{
153+
private ConversationExperimental m_Service = null;
154+
private ServiceStatus m_Callback = null;
155+
private int m_ConversationCount = 0;
156+
157+
public CheckServiceStatus(ConversationExperimental service, ServiceStatus callback)
158+
{
159+
m_Service = service;
160+
m_Callback = callback;
161+
162+
string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID");
163+
164+
//If custom classifierID is defined then we are using it to check the service health
165+
if (!string.IsNullOrEmpty(customServiceID))
166+
{
167+
168+
if (!m_Service.Message(customServiceID, "Ping", OnMessage))
169+
OnFailure("Failed to invoke Converse().");
170+
else
171+
m_ConversationCount += 1;
172+
}
173+
else
174+
{
175+
OnFailure("Please define a workspace variable in config.json (" + SERVICE_ID + "_ID)");
176+
}
177+
}
178+
179+
private void OnMessage(MessageResponse resp)
180+
{
181+
if (m_ConversationCount > 0)
182+
{
183+
m_ConversationCount -= 1;
184+
if (resp != null)
185+
{
186+
if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null)
187+
m_Callback(SERVICE_ID, true);
188+
}
189+
else
190+
OnFailure("ConverseResponse is null.");
191+
}
192+
}
193+
194+
private void OnFailure(string msg)
195+
{
196+
Log.Error("Dialog", msg);
197+
m_Callback(SERVICE_ID, false);
198+
m_ConversationCount = 0;
199+
}
200+
};
201+
#endregion
202+
}
203+
}

0 commit comments

Comments
 (0)