Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ exclude-filter.txt.meta
/Travis/installUnity.sh.meta
/Travis/README.md.meta
/Travis/runTests.sh.meta
/Travis/TravisBuild.cs.meta
/Travis/TravisBuild.cs.meta
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ _2016-07-15_
* New: Added `Document Conversion` abstraction
* New: Added `AlchemyData News` abstraction
* New: Added `Retrieve and Rank` abstraction
* New: Added `Conversation` abstraction
* Fix: Added `LanguageTranslation` and `LanguageTranslator`

## Version 0.5.0

Expand Down
Binary file modified Config.json.enc
Binary file not shown.
15 changes: 10 additions & 5 deletions Examples/ServiceExamples/Scripts/ExampleConversation.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@
using UnityEngine;
using System.Collections;
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;

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

void Start () {
Debug.Log("User: " + m_Input);
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
LogSystem.InstallDefaultReactors();
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_WorkspaceID");
Debug.Log("User: " + m_Input);

m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
}

void OnMessage (DataModels.MessageResponse resp)
void OnMessage (MessageResponse resp)
{
if(resp != null)
{
foreach(DataModels.MessageIntent mi in resp.intents)
foreach(MessageIntent mi in resp.intents)
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);

if(resp.output != null && !string.IsNullOrEmpty(resp.output.text))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using UnityEngine;
using System.Collections;
using IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;

public class ExampleConversationExperimental : MonoBehaviour
{
private ConversationExperimental m_Conversation = new ConversationExperimental();
private string m_WorkspaceID;
private string m_Input = "Can you unlock the door?";

void Start () {
LogSystem.InstallDefaultReactors();
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationExperimentalV1_ID");
Debug.Log("User: " + m_Input);

m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
}

void OnMessage (MessageResponse resp)
{
if(resp != null)
{
foreach(MessageIntent mi in resp.intents)
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);

if(resp.output != null && !string.IsNullOrEmpty(resp.output.text))
Debug.Log("response: " + resp.output.text);
}
else
{
Debug.Log("Failed to invoke Message();");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions Examples/ServiceExamples/ServiceExamples.unity
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,46 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
--- !u!1 &1937656527
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1937656529}
- 114: {fileID: 1937656528}
m_Layer: 0
m_Name: ExampleConversation
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1937656528
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1937656527}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 87040e721f2394a4a9f444ecbaf9d991, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1937656529
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1937656527}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
--- !u!1 &1979050314
GameObject:
m_ObjectHideFlags: 0
Expand Down
4 changes: 3 additions & 1 deletion Scripts/Editor/ConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ private class ServiceSetup
URL ="https://console.ng.bluemix.net/catalog/services/tradeoff-analytics/", ServiceID="TradeoffAnalyticsV1" },
new ServiceSetup() { ServiceName = "Personality Insights", ServiceAPI = "personality-insights/api",
URL ="https://console.ng.bluemix.net/catalog/services/personality-insights/", ServiceID="PersonalityInsightsV2" },
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation-experimental/api",
//new ServiceSetup() { ServiceName = "Conversation (Experimental)", ServiceAPI = "conversation-experimental/api",
// URL ="https://console.ng.bluemix.net/catalog/services/conversation/", ServiceID="ConversationExperimentalV1" },
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation/api",
URL ="https://console.ng.bluemix.net/catalog/services/conversation/", ServiceID="ConversationV1" },
new ServiceSetup() { ServiceName = "RetrieveAndRank", ServiceAPI = "retrieve-and-rank/api",
URL ="https://console.ng.bluemix.net/catalog/services/retrieve-and-rank/", ServiceID="RetrieveAndRankV1" },
Expand Down
9 changes: 9 additions & 0 deletions Scripts/Services/Conversation-Experimental.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System;
using System.Text;
using System.Collections.Generic;
using FullSerializer;
using MiniJSON;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Logging;

namespace IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1
{
/// <summary>
/// This class wraps the Watson Conversation service.
/// <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/conversation.html">Conversation Service</a>
/// </summary>
public class ConversationExperimental : IWatsonService
{
#region Public Types
/// <summary>
/// The callback for GetWorkspaces().
/// </summary>
public delegate void OnGetWorkspaces(Workspaces workspaces);
/// <summary>
/// The callback for Message().
/// </summary>
/// <param name="success"></param>
public delegate void OnMessageCallback(bool success);
/// <summary>
/// The callback delegate for the Converse() function.
/// </summary>
/// <param name="resp">The response object to a call to Converse().</param>
public delegate void OnMessage(MessageResponse resp);

#endregion

#region Public Properties
#endregion

#region Private Data
private const string SERVICE_ID = "ConversationExperimentalV1";
private static fsSerializer sm_Serializer = new fsSerializer();
#endregion

#region Message
private const string SERVICE_MESSAGE = "/v1/workspaces";
/// <summary>
/// Message the specified workspaceId, input and callback.
/// </summary>
/// <param name="workspaceId">Workspace identifier.</param>
/// <param name="input">Input.</param>
/// <param name="callback">Callback.</param>
public bool Message(string workspaceId, string input, OnMessage callback)
{
if(string.IsNullOrEmpty(workspaceId))
throw new ArgumentNullException("workspaceId");
if(string.IsNullOrEmpty(input))
throw new ArgumentNullException("input");
if(callback == null)
throw new ArgumentNullException("callback");

RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
if(connector == null)
return false;

string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}";
string reqString = string.Format(reqJson, input);

MessageReq req = new MessageReq();
req.Callback = callback;
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
req.Parameters["version"] = Version.VERSION;
req.Function = "/" + workspaceId + "/message";
req.Send = Encoding.UTF8.GetBytes(reqString);
req.OnResponse = MessageResp;

return connector.Send(req);
}

private class MessageReq : RESTConnector.Request
{
public OnMessage Callback { get; set; }
}

private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
{
MessageResponse response = new MessageResponse();
if (resp.Success)
{
try
{
fsData data = null;
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

object obj = response;
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
}
catch (Exception e)
{
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
resp.Success = false;
}
}

if (((MessageReq)req).Callback != null)
((MessageReq)req).Callback(resp.Success ? response : null);
}
#endregion

#region IWatsonService implementation

public string GetServiceID()
{
return SERVICE_ID;
}

public void GetServiceStatus(ServiceStatus callback)
{
if (Config.Instance.FindCredentials(SERVICE_ID) != null)
new CheckServiceStatus(this, callback);
else
{
if (callback != null && callback.Target != null)
{
callback(SERVICE_ID, false);
}
}
}

private class CheckServiceStatus
{
private ConversationExperimental m_Service = null;
private ServiceStatus m_Callback = null;
private int m_ConversationCount = 0;

public CheckServiceStatus(ConversationExperimental service, ServiceStatus callback)
{
m_Service = service;
m_Callback = callback;

string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID");

//If custom classifierID is defined then we are using it to check the service health
if (!string.IsNullOrEmpty(customServiceID))
{

if (!m_Service.Message(customServiceID, "Ping", OnMessage))
OnFailure("Failed to invoke Converse().");
else
m_ConversationCount += 1;
}
else
{
OnFailure("Please define a workspace variable in config.json (" + SERVICE_ID + "_ID)");
}
}

private void OnMessage(MessageResponse resp)
{
if (m_ConversationCount > 0)
{
m_ConversationCount -= 1;
if (resp != null)
{
if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null)
m_Callback(SERVICE_ID, true);
}
else
OnFailure("ConverseResponse is null.");
}
}

private void OnFailure(string msg)
{
Log.Error("Dialog", msg);
m_Callback(SERVICE_ID, false);
m_ConversationCount = 0;
}
};
#endregion
}
}
Loading