k4l1sh / alexa-gpt

A tutorial on how to use ChatGPT in Alexa
MIT License
177 stars 38 forks source link

Impossible to make this skill work now #2

Closed imoforte closed 1 year ago

imoforte commented 1 year ago

Good morning. Before it worked fine. Still the same problem in Alexa. Impossible to make this skill work now. I'm sorry because before it was perfect

k4l1sh commented 1 year ago

what's the problem? everything seems to be working following the step-by-step tutorial

imoforte commented 1 year ago

Good morning. I'll send some screenshots to show what's going on.

https://github.com/kalishcode/alexa-gpt

Following the step by step tutorial...

[image: image.png]

[image: image.png] [image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

1.

In the "Build" section, navigate to the "JSON Editor" tab. 2. 1.

  Replace the existing JSON content with the provided JSON content
  <https://github.com/kalishcode/alexa-gpt/blob/main/json_editor.json>:
  2. [image: image.png]

  Changed in Editor Json.
  3.
     1.

     Save the model and click on "Build Model".
     2.

     Go to "Code" section and add "openai" to requirements.txt. Your
     requirements.txt should look like this:

  ask-sdk-core==1.11.0
  openai

  4.

  [image: image.png]

  5.

  1.

     Create an OpenAI API key by signing up

https://beta.openai.com/signup/ and clicking in "+ Create new secret key" in the API keys page https://platform.openai.com/account/api-keys. 2.

     Replace your lambda_functions.py file with the provided

lambda_functions.py https://github.com/kalishcode/alexa-gpt/blob/main/lambda/lambda_functions.py. Remember to put your OpenAI API key:

  import logging
  import ask_sdk_core.utils as ask_utils
  import openai
  from ask_sdk_core.skill_builder import SkillBuilder
  from ask_sdk_core.dispatch_components import AbstractRequestHandler
  from ask_sdk_core.dispatch_components import AbstractExceptionHandler
  from ask_sdk_core.handler_input import HandlerInput
  from ask_sdk_model import Response

  # Set your OpenAI API key
  openai.api_key = "PUT YOUR OPENAI API KEY HERE"

  logger = logging.getLogger(__name__)
  logger.setLevel(logging.INFO)

  class LaunchRequestHandler(AbstractRequestHandler):
      """Handler for Skill Launch."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool

          return ask_utils.is_request_type("LaunchRequest")(handler_input)

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          speak_output = "Chat G.P.T. mode activated"

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .ask(speak_output)
                  .response
          )

  class GptQueryIntentHandler(AbstractRequestHandler):
      """Handler for Gpt Query Intent."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool
          return ask_utils.is_intent_name("GptQueryIntent")(handler_input)

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          query =

handler_input.request_envelope.request.intent.slots["query"].value response = generate_gpt_response(query)

          return (
                  handler_input.response_builder
                      .speak(response)
                      .ask("Any other questions?")
                      .response
              )

  class CatchAllExceptionHandler(AbstractExceptionHandler):
      """Generic error handling to capture any syntax or routing errors."""
      def can_handle(self, handler_input, exception):
          # type: (HandlerInput, Exception) -> bool
          return True

      def handle(self, handler_input, exception):
          # type: (HandlerInput, Exception) -> Response
          logger.error(exception, exc_info=True)

          speak_output = "Sorry, I had trouble doing what you

asked. Please try again."

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .ask(speak_output)
                  .response
          )

  class CancelOrStopIntentHandler(AbstractRequestHandler):
      """Single handler for Cancel and Stop Intent."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool
          return

(ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or

ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input))

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          speak_output = "Leaving Chat G.P.T. mode"

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .response
          )

  def generate_gpt_response(query):
      try:
          messages = [{"role": "system", "content": "You are a

helpful assistant."}, {"role": "user", "content": query}] response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, max_tokens=100, n=1, stop=None, temperature=0.5 ) return response['choices'][0]['message']['content'].strip() except Exception as e: return f"Error generating response: {str(e)}"

  sb = SkillBuilder()

  sb.add_request_handler(LaunchRequestHandler())
  sb.add_request_handler(GptQueryIntentHandler())
  sb.add_request_handler(CancelOrStopIntentHandler())
  sb.add_exception_handler(CatchAllExceptionHandler())

  lambda_handler = sb.lambda_handler()

  6.

  The key was placed

  7.

  1. Save and deploy. Go to "Test" section and enable "Skill

testing" in "Development". [image: image.png]

  3.

4.

  1. [image: image.png]

        Following the tutorial's detailed step-by-step we arrived at

    this error.

        Before the code worked very well.
        Now it stops there and I don't know what it could be.
        I would appreciate it if you can help me please.

Em ter., 9 de mai. de 2023 às 06:15, kalish @.***> escreveu:

what's the problem? everything seems to be working following the step-by-step tutorial

— Reply to this email directly, view it on GitHub https://github.com/kalishcode/alexa-gpt/issues/2#issuecomment-1539450976, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEB7ECMYLYYLVZ7DPDLVGTXFIDMLANCNFSM6AAAAAAXZ6HRF4 . You are receiving this because you authored the thread.Message ID: @.***>

Utterance Conflicts ... Based on your last successful build, you have 1 Utterance Conflicts in your Skill Model.

UTTERANCE CONFLICT .... hush

SAMPLE UTTERANCES INTENTS CURRENT BEHAVIOR (built-in utterance) AMAZON.StopIntent Alexa will ignore built-in intents {query} GptQueryIntent not resolved. These are the conflicts found by the Alexa dev console. Kindly help me with this problem, please.
imoforte commented 1 year ago

[image: image.png]

Em ter., 9 de mai. de 2023 às 07:29, Emilio Terol Fortuny < @.***> escreveu:

Good morning. I'll send some screenshots to show what's going on.

https://github.com/kalishcode/alexa-gpt

Following the step by step tutorial...

[image: image.png]

[image: image.png] [image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

[image: image.png]

1.

In the "Build" section, navigate to the "JSON Editor" tab. 2. 1.

  Replace the existing JSON content with the provided JSON content
  <https://github.com/kalishcode/alexa-gpt/blob/main/json_editor.json>
  :
  2. [image: image.png]

  Changed in Editor Json.
  3.
     1.

     Save the model and click on "Build Model".
     2.

     Go to "Code" section and add "openai" to requirements.txt. Your
     requirements.txt should look like this:

  ask-sdk-core==1.11.0
  openai

  4.

  [image: image.png]

  5.

  1.

     Create an OpenAI API key by signing up <https://beta.openai.com/signup/> and clicking in "+ Create new secret key" in the API keys page <https://platform.openai.com/account/api-keys>.
     2.

     Replace your lambda_functions.py file with the provided lambda_functions.py <https://github.com/kalishcode/alexa-gpt/blob/main/lambda/lambda_functions.py>. Remember to put your OpenAI API key:

  import logging
  import ask_sdk_core.utils as ask_utils
  import openai
  from ask_sdk_core.skill_builder import SkillBuilder
  from ask_sdk_core.dispatch_components import AbstractRequestHandler
  from ask_sdk_core.dispatch_components import AbstractExceptionHandler
  from ask_sdk_core.handler_input import HandlerInput
  from ask_sdk_model import Response

  # Set your OpenAI API key
  openai.api_key = "PUT YOUR OPENAI API KEY HERE"

  logger = logging.getLogger(__name__)
  logger.setLevel(logging.INFO)

  class LaunchRequestHandler(AbstractRequestHandler):
      """Handler for Skill Launch."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool

          return ask_utils.is_request_type("LaunchRequest")(handler_input)

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          speak_output = "Chat G.P.T. mode activated"

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .ask(speak_output)
                  .response
          )

  class GptQueryIntentHandler(AbstractRequestHandler):
      """Handler for Gpt Query Intent."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool
          return ask_utils.is_intent_name("GptQueryIntent")(handler_input)

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          query = handler_input.request_envelope.request.intent.slots["query"].value
          response = generate_gpt_response(query)

          return (
                  handler_input.response_builder
                      .speak(response)
                      .ask("Any other questions?")
                      .response
              )

  class CatchAllExceptionHandler(AbstractExceptionHandler):
      """Generic error handling to capture any syntax or routing errors."""
      def can_handle(self, handler_input, exception):
          # type: (HandlerInput, Exception) -> bool
          return True

      def handle(self, handler_input, exception):
          # type: (HandlerInput, Exception) -> Response
          logger.error(exception, exc_info=True)

          speak_output = "Sorry, I had trouble doing what you asked. Please try again."

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .ask(speak_output)
                  .response
          )

  class CancelOrStopIntentHandler(AbstractRequestHandler):
      """Single handler for Cancel and Stop Intent."""
      def can_handle(self, handler_input):
          # type: (HandlerInput) -> bool
          return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or
                  ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input))

      def handle(self, handler_input):
          # type: (HandlerInput) -> Response
          speak_output = "Leaving Chat G.P.T. mode"

          return (
              handler_input.response_builder
                  .speak(speak_output)
                  .response
          )

  def generate_gpt_response(query):
      try:
          messages = [{"role": "system", "content": "You are a helpful assistant."},
                      {"role": "user", "content": query}]
          response = openai.ChatCompletion.create(
              model="gpt-3.5-turbo",
              messages=messages,
              max_tokens=100,
              n=1,
              stop=None,
              temperature=0.5
          )
          return response['choices'][0]['message']['content'].strip()
      except Exception as e:
          return f"Error generating response: {str(e)}"

  sb = SkillBuilder()

  sb.add_request_handler(LaunchRequestHandler())
  sb.add_request_handler(GptQueryIntentHandler())
  sb.add_request_handler(CancelOrStopIntentHandler())
  sb.add_exception_handler(CatchAllExceptionHandler())

  lambda_handler = sb.lambda_handler()

  6.

  The key was placed

  7.

  1. Save and deploy. Go to "Test" section and enable "Skill testing" in "Development".
  [image: image.png]

  3.

4.

  1. [image: image.png]

        Following the tutorial's detailed step-by-step we arrived at

    this error.

        Before the code worked very well.
        Now it stops there and I don't know what it could be.
        I would appreciate it if you can help me please.

Em ter., 9 de mai. de 2023 às 06:15, kalish @.***> escreveu:

what's the problem? everything seems to be working following the step-by-step tutorial

— Reply to this email directly, view it on GitHub https://github.com/kalishcode/alexa-gpt/issues/2#issuecomment-1539450976, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEB7ECMYLYYLVZ7DPDLVGTXFIDMLANCNFSM6AAAAAAXZ6HRF4 . You are receiving this because you authored the thread.Message ID: @.***>

image

k4l1sh commented 1 year ago

Looks like the stop intent is conflicting with the gpt intent. These conflicts will no longer be available from May 17, 2023.

You can edit JSON Editor to AMAZON.SearchQuery (the way it was before)

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "chat",
            "intents": [
                {
                    "name": "GptQueryIntent",
                    "slots": [
                        {
                            "name": "query",
                            "type": "AMAZON.SearchQuery"
                        }
                    ],
                    "samples": [
                        "chat {query}"
                    ]
                },
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                }
            ],
            "types": []
        }
    }
}

The downside of this is that you will need to say "chat" before each interaction.

imoforte commented 1 year ago

I will see it now

imoforte commented 1 year ago

still not working. If .... These conflicts will no longer be available from May 17, 2023 ... I believe that the best thing will be to wait after that date ... to be able to test the possible solutions. Thanks

imoforte commented 1 year ago

Good morning. Note: Invocation name in Alexa must be a minimum of two words. I'm doing ALL what your manual says, step by step. Alexa's response: There was a problem with the requested skill's response

k4l1sh commented 1 year ago

Fixed in the issue #3