ai.api.model.AIResponse Java Examples

The following examples show how to use ai.api.model.AIResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: V20150415ProtocolTest.java    From dialogflow-android-client with Apache License 2.0 6 votes vote down vote up
@Test
public void legacyContextsWithoutParametersTest() throws AIServiceException {
    final AIConfiguration config = new AIConfiguration(
            "3485a96fb27744db83e78b8c4bc9e7b7",
            AIConfiguration.SupportedLanguages.English,
            AIConfiguration.RecognitionEngine.System);

    config.setProtocolVersion(PROTOCOL_VERSION);

    final AIDataService aiDataService = new AIDataService(RuntimeEnvironment.application, config);

    final AIContext weatherContext = new AIContext("weather");
    weatherContext.setParameters(Collections.singletonMap("location", "London"));

    final List<AIContext> contexts = Collections.singletonList(weatherContext);

    final AIRequest aiRequest = new AIRequest();
    aiRequest.setQuery("and for tomorrow");
    aiRequest.setContexts(contexts);

    final AIResponse aiResponse = aiDataService.request(aiRequest);

    // Old protocol doesn't support parameters, so response will not contains city name
    assertEquals("Weather in for tomorrow", aiResponse.getResult().getFulfillment().getSpeech());

}
 
Example #2
Source File: AIDataService.java    From dialogflow-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the specified context for a session
 * 
 * @param contextName The context name
 * @param serviceContext custom service context that should be used instead of the default context
 * @return <code>false</code> if context was not delete, <code>true</code> in otherwise case
 * @throws AIServiceException
 */
public boolean removeActiveContext(final String contextName,
    final AIServiceContext serviceContext) throws AIServiceException {

  try {
    doRequest(AIResponse.class, config.getContextsUrl(getSessionId(serviceContext), contextName),
        REQUEST_METHOD_DELETE);
    return true;
  } catch (BadResponseStatusException e) {
    if (e.response.getStatus().getCode() == 404) {
      return false;
    } else {
      throw new AIServiceException(e.response);
    }
  }
}
 
Example #3
Source File: MainActivity.java    From Krishi-Seva with MIT License 6 votes vote down vote up
public void onResult(final AIResponse response) {
    Result result = response.getResult();


    // Get parameters
    String parameterString = "";
    if (result.getParameters() != null && !result.getParameters().isEmpty()) {
        for (final Map.Entry<String, JsonElement> entry : result.getParameters().entrySet()) {
            parameterString += "(" + entry.getKey() + ", " + entry.getValue() + ") ";
        }
    }

    // Show results in TextView.
    resultTextView.setText(
           /* "\nAction: " + result.getAction() +*/
            "Solution To Your Problem:\n\n" + result.getFulfillment().getSpeech());
    Speech.getInstance().say(result.getFulfillment().getSpeech());
}
 
Example #4
Source File: GcpAIDataService.java    From dialogflow-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * @see AIDataService#voiceRequest(InputStream, RequestExtras, AIServiceContext)
 */
@Override
public AIResponse voiceRequest(InputStream voiceStream, RequestExtras requestExtras,
		AIServiceContext serviceContext) throws AIServiceException {
	
  RecognizeResponse response;
	try {
	    SpeechClient speechClient = SpeechClient.create();
		RecognitionAudio recognitionAudio = createRecognitionAudio(voiceStream);

        response = speechClient.recognize(config.getRecognitionConfig(), recognitionAudio);
	} catch (IOException | StatusRuntimeException e) {
		throw new AIServiceException("Failed to recognize speech", e);
	}
	if ((response.getResultsCount() == 0) || (response.getResults(0).getAlternativesCount() == 0)) {
		throw new AIServiceException("No speech");
	}
	String transcript = response.getResults(0).getAlternatives(0).getTranscript();
	AIRequest request = new AIRequest(transcript);
	return request(request, requestExtras, serviceContext);
}
 
Example #5
Source File: MainActivity.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    String address = settings.getString("pref_ipAddress", "");
    addressEdit.setText(address);

    final AIConfiguration config = new AIConfiguration(
            settings.getString("pref_apiKey", "a07bc2127c61497fad437dd8c197f951"),
            AIConfiguration.SupportedLanguages.English,
            AIConfiguration.RecognitionEngine.System);

    aiButton.initialize(config);
    aiButton.setResultsListener(new AIButton.AIButtonListener() {
        @Override
        public void onResult(AIResponse aiResponse) {
            if (!aiResponse.isError()) {
                Log.i(TAG, aiResponse.getResult().getResolvedQuery());
                commandText.setText(aiResponse.getResult().getAction());
                processResponse(aiResponse);
            } else {
                commandText.setText(aiResponse.getStatus().getErrorDetails());
            }
        }

        @Override
        public void onError(AIError aiError) {
            commandText.setText(aiError.getMessage());
        }

        @Override
        public void onCancelled() {

        }
    });
}
 
Example #6
Source File: ChatBot.java    From SensorsAndAi with MIT License 5 votes vote down vote up
@Override
public void onResult(final AIResponse response) {
    Result result = response.getResult();
    String parameterString = "";
    if (result.getParameters() != null && !result.getParameters().isEmpty()) {
        for (final Map.Entry<String, JsonElement> entry : result.getParameters().entrySet()) {
            parameterString += "(" + entry.getKey() + ", " + entry.getValue() + ") ";

        }}
    resultTextView.setText("Users Say:\n\n\n" + result.getResolvedQuery() +
            "\nAnswer:\n " + result.getAction() +
            "\n " + parameterString);}
 
Example #7
Source File: RemoteAPIAI.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
public Pair<Boolean, String> fetch() {

        final AIRequest aiRequest = new AIRequest();
        aiRequest.setQuery(utterance);

        try {

            final AIResponse response = aiDataService.request(aiRequest);

            if (response != null) {

                final String gsonString = new GsonBuilder().disableHtmlEscaping().create().toJson(response);

                if (DEBUG) {
                    MyLog.i(CLS_NAME, "gsonString: " + response.toString());
                }

                return new Pair<>(true, gsonString);
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "response null");
                }
            }

        } catch (final AIServiceException e) {
            if (DEBUG) {
                MyLog.e(CLS_NAME, "AIResponse AIServiceException");
                e.printStackTrace();
            }
        }

        return new Pair<>(false, null);
    }
 
Example #8
Source File: MainActivity.java    From android-dialogflow-chatbot-sample with MIT License 5 votes vote down vote up
@Override
protected void onPostExecute(final AIResponse response) {
    if (response != null) {
        onResult(response);
    } else {
        onError(aiError);
    }
}
 
Example #9
Source File: MainActivity.java    From PersonalAiChatBot with MIT License 5 votes vote down vote up
@Override
public void onResult(final AIResponse response) {
    Result result = response.getResult();
    String parameterString = "";
    if (result.getParameters() != null && !result.getParameters().isEmpty()) {
        for (final Map.Entry<String, JsonElement> entry : result.getParameters().entrySet()) {
            parameterString += "(" + entry.getKey() + ", " + entry.getValue() + ") ";

}}
    resultTextView.setText("Users Say:\n" + result.getResolvedQuery() +
            "\nAnswer:\n " + result.getAction() +
            "\n " + parameterString);}
 
Example #10
Source File: RequestTask.java    From DialogflowChat with Apache License 2.0 5 votes vote down vote up
@Override
protected AIResponse doInBackground(AIRequest... aiRequests) {
    final AIRequest request = aiRequests[0];
    try {
        return aiDataService.request(request, customAIServiceContext);
    } catch (AIServiceException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #11
Source File: MainActivity.java    From DialogflowChat with Apache License 2.0 5 votes vote down vote up
public void callback(AIResponse aiResponse) {
    if (aiResponse != null) {
        // process aiResponse here
        String botReply = aiResponse.getResult().getFulfillment().getSpeech();
        Log.d(TAG, "Bot Reply: " + botReply);
        showTextView(botReply, BOT);
    } else {
        Log.d(TAG, "Bot Reply: Null");
        showTextView("There was some communication issue. Please Try again!", BOT);
    }
}
 
Example #12
Source File: MainActivity.java    From Build-an-AI-Startup-with-PyTorch with MIT License 5 votes vote down vote up
@Override
protected void onPostExecute(final AIResponse response) {
    if (response != null) {
        onResult(response);
    } else {
        onError(aiError);
    }
}
 
Example #13
Source File: MainActivity.java    From Watch-Me-Build-a-Finance-Startup with MIT License 5 votes vote down vote up
@Override
protected void onPostExecute(final AIResponse response) {
    if (response != null) {
        onResult(response);
    } else {
        onError(aiError);
    }
}
 
Example #14
Source File: ResolveAPIAI.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void unpack(@NonNull final String gsonResponse) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "unpacking");
    }

    final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    final AIResponse response = gson.fromJson(gsonResponse, new TypeToken<AIResponse>() {
    }.getType());

    nluAPIAI = new NLUAPIAI(confidenceArray, resultsArray,
            response.getResult().getMetadata().getIntentName(), response.getResult().getParameters());

    new NLUCoerce(getNLUAPIAI(), getContext(), getSupportedLanguage(), getVRLocale(), getTTSLocale(),
            getConfidenceArray(), getResultsArray()).coerce();
}
 
Example #15
Source File: AIButton.java    From dialogflow-android-client with Apache License 2.0 5 votes vote down vote up
@Override
public void onResult(final AIResponse result) {
    post(new Runnable() {
        @Override
        public void run() {
            changeState(MicState.normal);
        }
    });

    if (resultsListener != null) {
        resultsListener.onResult(result);
    }
}
 
Example #16
Source File: SpeaktoitRecognitionServiceImpl.java    From dialogflow-android-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(final AIResponse aiResponse) {
    if (isCancelled()) {
        return;
    }
    if (aiResponse != null) {
        onResult(aiResponse);
    } else {
        SpeaktoitRecognitionServiceImpl.this.cancel();
        onError(aiError);
    }
}
 
Example #17
Source File: SpeaktoitRecognitionServiceImpl.java    From dialogflow-android-client with Apache License 2.0 5 votes vote down vote up
@Override
protected AIResponse doInBackground(final Void... params) {
    try {
        return aiDataService.voiceRequest(recorderStream, requestExtras);
    } catch (final AIServiceException e) {
        aiError = new AIError(e);
    }
    return null;
}
 
Example #18
Source File: GoogleRecognitionServiceImpl.java    From dialogflow-android-client with Apache License 2.0 5 votes vote down vote up
@TargetApi(14)
@Override
public void onResults(final Bundle results) {
    if (recognitionActive) {
        final ArrayList<String> recognitionResults = results
                .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        float[] rates = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            rates = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
        }

        if (recognitionResults == null || recognitionResults.isEmpty()) {
            // empty response
            GoogleRecognitionServiceImpl.this.onResult(new AIResponse());
        } else {
            final AIRequest aiRequest = new AIRequest();
            if (rates != null) {
                aiRequest.setQuery(recognitionResults.toArray(new String[recognitionResults.size()]), rates);
            } else {
                aiRequest.setQuery(recognitionResults.get(0));
            }

            // notify listeners about the last recogntion result for more accurate user feedback
            GoogleRecognitionServiceImpl.this.onPartialResults(recognitionResults);
            GoogleRecognitionServiceImpl.this.sendRequest(aiRequest, requestExtras);
        }
    }
    stopInternal();
}
 
Example #19
Source File: AIDataService.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Forget all old contexts
 *
 * @return true if operation succeed, false otherwise
 */
@Deprecated
public boolean resetContexts() {
  final AIRequest cleanRequest = new AIRequest();
  cleanRequest.setQuery("empty_query_for_resetting_contexts"); // TODO remove it after protocol
                                                               // fix
  cleanRequest.setResetContexts(true);
  try {
    final AIResponse response = request(cleanRequest);
    return !response.isError();
  } catch (final AIServiceException e) {
    logger.error("Exception while contexts clean.", e);
    return false;
  }
}
 
Example #20
Source File: AIDataService.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes all active contexts for a session
 * 
 * @param serviceContext custom service context that should be used instead of the default context
 * @throws AIServiceException
 */
public void resetActiveContexts(final AIServiceContext serviceContext) throws AIServiceException {
  try {
    doRequest(AIResponse.class, config.getContextsUrl(getSessionId(serviceContext)),
        REQUEST_METHOD_DELETE);
  } catch (BadResponseStatusException e) {
    throw new AIServiceException(e.response);
  }
}
 
Example #21
Source File: ServiceServletSample.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  try {
    AIResponse aiResponse = request(request.getParameter("query"), request.getSession());
    response.setContentType("text/plain");
    response.getWriter().append(aiResponse.getResult().getFulfillment().getSpeech());
  } catch (AIServiceException e) {
    e.printStackTrace();
  }
}
 
Example #22
Source File: VoiceClientApplication.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param args List of parameters:<br>
 *        First parameters should be valid api key<br>
 *        Second and the following args should be file names containing audio data.
 */
public static void main(String[] args) {
  if (args.length < 1) {
    showHelp("Please specify API key", ERROR_EXIT_CODE);
  }

  GcpAIConfiguration configuration = new GcpAIConfiguration(args[0]);

  GcpAIDataService dataService = new GcpAIDataService(configuration);

  for (int i = 1; i < args.length; ++i) {
    File file = new File(args[i]);
    try (FileInputStream inputStream = new FileInputStream(file)) {

      System.out.println(file);

      AIResponse response = dataService.voiceRequest(inputStream);

      if (response.getStatus().getCode() == 200) {
        System.out.println(response.getResult().getFulfillment().getSpeech());
      } else {
        System.err.println(response.getStatus().getErrorDetails());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
 
Example #23
Source File: AIDataService.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Add a bunch of new entity to an agent entity list
 * 
 * @param userEntities collection of a new entity data
 * @param serviceContext custom service context that should be used instead of the default context
 * @return response object from service. Never <code>null</code>
 * @throws AIServiceException
 */
public AIResponse uploadUserEntities(final Collection<Entity> userEntities,
    AIServiceContext serviceContext) throws AIServiceException {
  if (userEntities == null || userEntities.size() == 0) {
    throw new AIServiceException("Empty entities list");
  }

  final String requestData = GSON.toJson(userEntities);
  try {
    final String response =
        doTextRequest(config.getUserEntitiesEndpoint(getSessionId(serviceContext)), requestData);
    if (StringUtils.isEmpty(response)) {
      throw new AIServiceException(
          "Empty response from ai service. Please check configuration and Internet connection.");
    }
    logger.debug("Response json: " + response);

    final AIResponse aiResponse = GSON.fromJson(response, AIResponse.class);

    if (aiResponse == null) {
      throw new AIServiceException(
          "API.AI response parsed as null. Check debug log for details.");
    }

    if (aiResponse.isError()) {
      throw new AIServiceException(aiResponse);
    }

    aiResponse.cleanup();
    return aiResponse;

  } catch (final MalformedURLException e) {
    logger.error("Malformed url should not be raised", e);
    throw new AIServiceException("Wrong configuration. Please, connect to AI Service support", e);
  } catch (final JsonSyntaxException je) {
    throw new AIServiceException(
        "Wrong service answer format. Please, connect to API.AI Service support", je);
  }
}
 
Example #24
Source File: AIButton.java    From dialogflow-android-client with Apache License 2.0 5 votes vote down vote up
public AIResponse textRequest(final AIRequest request) throws AIServiceException {
    if (aiService != null) {
        return aiService.textRequest(request);
    } else {
        throw new IllegalStateException("Call initialize method before usage");
    }
}
 
Example #25
Source File: AIDialog.java    From dialogflow-android-client with Apache License 2.0 4 votes vote down vote up
public AIResponse textRequest(final AIRequest request) throws AIServiceException {
    return aiButton.textRequest(request);
}
 
Example #26
Source File: AIService.java    From dialogflow-android-client with Apache License 2.0 4 votes vote down vote up
public AIResponse textRequest(final AIRequest request) throws AIServiceException {
    return aiDataService.request(request);
}
 
Example #27
Source File: MainActivity.java    From Watch-Me-Build-a-Finance-Startup with MIT License 4 votes vote down vote up
private void onResult(final AIResponse response) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // Variables
            gson.toJson(response);
            final Status status = response.getStatus();
            final Result result = response.getResult();
            final String speech = result.getFulfillment().getSpeech();
            final Metadata metadata = result.getMetadata();
            final HashMap<String, JsonElement> params = result.getParameters();

            // Logging
            Log.d(TAG, "onResult");
            Log.i(TAG, "Received success response");
            Log.i(TAG, "Status code: " + status.getCode());
            Log.i(TAG, "Status type: " + status.getErrorType());
            Log.i(TAG, "Resolved query: " + result.getResolvedQuery());
            Log.i(TAG, "Action: " + result.getAction());
            Log.i(TAG, "Speech: " + speech);

            if (metadata != null) {
                Log.i(TAG, "Intent id: " + metadata.getIntentId());
                Log.i(TAG, "Intent name: " + metadata.getIntentName());
            }

            if (params != null && !params.isEmpty()) {
                Log.i(TAG, "Parameters: ");
                for (final Map.Entry<String, JsonElement> entry : params.entrySet()) {
                    Log.i(TAG, String.format("%s: %s",
                            entry.getKey(), entry.getValue().toString()));
                }
            }

            //Update view to bot says
            final Message receivedMessage = new Message.Builder()
                    .setUser(droidKaigiBot)
                    .setRightMessage(false)
                    .setMessageText(speech)
                    .build();
            chatView.receive(receivedMessage);
        }
    });
}
 
Example #28
Source File: AIButton.java    From dialogflow-android-client with Apache License 2.0 4 votes vote down vote up
public AIResponse textRequest(final String request) throws AIServiceException {
    return textRequest(new AIRequest(request));
}
 
Example #29
Source File: MainActivity.java    From dialogflow-java-client with Apache License 2.0 4 votes vote down vote up
protected Integer doInBackground(AIResponse... cmd) {

            Log.d(TAG, "Start response processing");

            if (ev3 == null) {
                return 2;
            }

            AIResponse command = cmd[0];

            if (command == null || command.isError()) {
                return 0;
            }

            try {
                ev3.getAudio().systemSound(1);

                if (!TextUtils.isEmpty(command.getResult().getFulfillment().getSpeech())) {
                    lcd.clear();
                    String speech = command.getResult().getFulfillment().getSpeech();

                    Log.d(TAG, "Write speech " + speech);

                    final int BASE_LINE = 4;
                    if (speech.length() > 15) {

                        lcd.drawString(speech.substring(0, 15), 0, BASE_LINE);

                        if (speech.length() > 15) {
                            lcd.drawString(speech.substring(15, Math.min(speech.length(), 30)), 0, BASE_LINE + 1);
                        }

                        if (speech.length() > 30) {
                            lcd.drawString(speech.substring(30, Math.min(speech.length(), 45)), 0, BASE_LINE + 2);
                        }

                        if (speech.length() > 45) {
                            lcd.drawString(speech.substring(45, Math.min(speech.length(), 60)), 0, BASE_LINE + 3);
                        }

                        if (speech.length() > 60) {
                            lcd.drawString(speech.substring(60, Math.min(speech.length(), 75)), 0, BASE_LINE + 4);
                        }

                        if (speech.length() > 75) {
                            lcd.drawString(speech.substring(75), 0, BASE_LINE + 5);
                        }

                    } else {
                        lcd.drawString(speech, 0, BASE_LINE);
                    }

                    lcd.refresh();

                    Log.d(TAG, "Speak " + speech);

                    if (ttsReady) {
                        ttsEngine.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                    }

                }

                switch (command.getResult().getAction().toLowerCase()) {
                    case "move":
                        doMove(command);
                        break;

                    default:
                        tryNextStep(command);
                        break;
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception while command execution", e);
                errorMessage = e.getMessage();
                return 3;
            }

            return 0;
        }
 
Example #30
Source File: MainActivity.java    From dialogflow-java-client with Apache License 2.0 4 votes vote down vote up
private void processResponse(final AIResponse aiResponse) {

        if (aiResponse != null) {
            new ControlTask().execute(aiResponse);
        }
    }