private string FetchSuggestedIntent(string assistantId, string text, AssistantService service) { var input = new MessageInputStateless() { MessageType = "text", Text = text }; var response = service.MessageStateless(assistantId, input); var intents = response.Result.Output.Intents; return(intents.Count > 0 ? intents[0].Intent : null); }
private IDictionary <string, double> FetchIntentConfidencies(string assistantId, string text, AssistantService service) { var input = new MessageInputStateless() { MessageType = "text", Text = text, Options = new MessageInputOptionsStateless() { AlternateIntents = true } }; var response = service.MessageStateless(assistantId, input); var intents = response.Result.Output.Intents; return(intents.ToDictionary(it => it.Intent, it => it.Confidence.GetValueOrDefault(0))); }
public IEnumerator TestRuntimeResponseGenericRuntimeResponseTypeChannelTransfer() { assistantId = Environment.GetEnvironmentVariable("ASSISTANT_ASSISTANT_ID"); MessageResponseStateless messageResponse = null; string conversationId = null; service.WithHeader("X-Watson-Test", "1"); MessageInputStateless input = new MessageInputStateless(); input.Text = "test sdk"; input.MessageType = MessageInputStateless.MessageTypeValue.TEXT; Log.Debug("AssistantV1IntegrationTests", "Attempting to Message...test sdk"); service.MessageStateless( callback: (DetailedResponse <MessageResponseStateless> response, IBMError error) => { messageResponse = response.Result; RuntimeResponseGenericRuntimeResponseTypeChannelTransfer runtimeResponseGenericRuntimeResponseTypeChannelTransfer = (RuntimeResponseGenericRuntimeResponseTypeChannelTransfer)messageResponse.Output.Generic[0]; ChannelTransferInfo channelTransferInfo = runtimeResponseGenericRuntimeResponseTypeChannelTransfer.TransferInfo; Assert.IsNotNull(channelTransferInfo); Assert.IsNull(error); }, assistantId: assistantId, input: input ); while (messageResponse == null) { yield return(null); } }
public void StatelessMessage_Success() { MessageInputStateless input = new MessageInputStateless() { MessageType = MessageInput.MessageTypeEnumValue.TEXT, Text = inputString, Options = new MessageInputOptionsStateless() { AlternateIntents = true } }; service.WithHeader("X-Watson-Test", "1"); var messageResult = service.MessageStateless( assistantId: assistantId, input: input ); Assert.IsNotNull(messageResult.Response); Assert.IsTrue(messageResult.Result.Output.Generic[0].ResponseType == "text"); Assert.IsTrue(messageResult.Result.Output.Generic[0].Text.Contains("Hello")); Assert.IsTrue(messageResult.Result.Output.Intents[0].Intent == "General_Greetings"); Assert.IsNotNull(messageResult.Result.Context.Global.SessionId); }
/// <summary> /// Send user input to assistant (stateless). /// /// Send user input to an assistant and receive a response, with conversation state (including context data) /// managed by your application. /// </summary> /// <param name="callback">The callback function that is invoked when the operation completes.</param> /// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson /// Assistant user interface, open the assistant settings and click **API Details**. For information about /// creating assistants, see the /// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task). /// /// **Note:** Currently, the v2 API does not support creating assistants.</param> /// <param name="input">An input object that includes the input text. (optional)</param> /// <param name="context">Context data for the conversation. You can use this property to set or modify context /// variables, which can also be accessed by dialog nodes. The context is not stored by the assistant. To /// maintain session state, include the context from the previous response. /// /// **Note:** The total size of the context data for a stateless session cannot exceed 250KB. (optional)</param> /// <returns><see cref="MessageResponseStateless" />MessageResponseStateless</returns> public bool MessageStateless(Callback <MessageResponseStateless> callback, string assistantId, MessageInputStateless input = null, MessageContextStateless context = null) { if (callback == null) { throw new ArgumentNullException("`callback` is required for `MessageStateless`"); } if (string.IsNullOrEmpty(assistantId)) { throw new ArgumentNullException("`assistantId` is required for `MessageStateless`"); } if (string.IsNullOrEmpty(Version)) { throw new ArgumentNullException("`Version` is required"); } RequestObject <MessageResponseStateless> req = new RequestObject <MessageResponseStateless> { Callback = callback, HttpMethod = UnityWebRequest.kHttpVerbPOST, DisableSslVerification = DisableSslVerification }; foreach (KeyValuePair <string, string> kvp in customRequestHeaders) { req.Headers.Add(kvp.Key, kvp.Value); } ClearCustomRequestHeaders(); foreach (KeyValuePair <string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "MessageStateless")) { req.Headers.Add(kvp.Key, kvp.Value); } if (!string.IsNullOrEmpty(Version)) { req.Parameters["version"] = Version; } req.Headers["Content-Type"] = "application/json"; req.Headers["Accept"] = "application/json"; JObject bodyObject = new JObject(); if (input != null) { bodyObject["input"] = JToken.FromObject(input); } if (context != null) { bodyObject["context"] = JToken.FromObject(context); } req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject)); req.OnResponse = OnMessageStatelessResponse; Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/message", assistantId); Authenticator.Authenticate(Connector); return(Connector.Send(req)); }
/// <summary> /// Send user input to assistant (stateless). /// /// Send user input to an assistant and receive a response, with conversation state (including context data) /// managed by your application. /// </summary> /// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson /// Assistant user interface, open the assistant settings and click **API Details**. For information about /// creating assistants, see the /// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task). /// /// **Note:** Currently, the v2 API does not support creating assistants.</param> /// <param name="request">The message to be sent. This includes the user's input, context data, and optional /// content such as intents and entities. (optional)</param> /// <returns><see cref="MessageResponseStateless" />MessageResponseStateless</returns> public DetailedResponse <MessageResponseStateless> MessageStateless(string assistantId, MessageInputStateless input = null, MessageContextStateless context = null) { if (string.IsNullOrEmpty(assistantId)) { throw new ArgumentNullException("`assistantId` is required for `MessageStateless`"); } else { assistantId = Uri.EscapeDataString(assistantId); } if (string.IsNullOrEmpty(VersionDate)) { throw new ArgumentNullException("versionDate cannot be null."); } DetailedResponse <MessageResponseStateless> result = null; try { IClient client = this.Client; SetAuthentication(); var restRequest = client.PostAsync($"{this.Endpoint}/v2/assistants/{assistantId}/message"); restRequest.WithArgument("version", VersionDate); restRequest.WithHeader("Accept", "application/json"); restRequest.WithHeader("Content-Type", "application/json"); JObject bodyObject = new JObject(); if (input != null) { bodyObject["input"] = JToken.FromObject(input); } if (context != null) { bodyObject["context"] = JToken.FromObject(context); } var httpContent = new StringContent(JsonConvert.SerializeObject(bodyObject), Encoding.UTF8, HttpMediaType.APPLICATION_JSON); restRequest.WithBodyContent(httpContent); restRequest.WithHeaders(Common.GetSdkHeaders("conversation", "v2", "MessageStateless")); restRequest.WithHeaders(customRequestHeaders); ClearCustomRequestHeaders(); result = restRequest.As <MessageResponseStateless>().Result; if (result == null) { result = new DetailedResponse <MessageResponseStateless>(); } } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }