Git Product home page Git Product logo

alexa-gpt's Introduction

Alexa GPT

License: MIT

Boost your Alexa by making it respond as ChatGPT.

This repository contains a tutorial on how to create a simple Alexa skill that uses the OpenAI API to generate responses from the ChatGPT model.

Prerequisites

Step-by-step tutorial

1.

Log in to your Amazon Developer account and navigate to the Alexa Developer Console.

2.

Click on "Create Skill" and name the skill "Chat". Choose the primary locale according to your language.

name your skill

3.

Choose "Other" and "Custom" for the model.

type of experience

choose a model

4.

Choose "Alexa-hosted (Python)" for the backend resources.

hosting services

5.

You now have two options:

Or if you want to create the skill manually

  • Select "Start from Scratch" and click on "Create Skill"

template

6.

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

7.

If you have directly imported the skill from this repository, just change the "invocationName" to "chat" or another preferred word for activation and proceed to step 12.

However, if you chose to manually create the skill, replace the existing JSON content with the provided JSON content:

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

json_editor

8.

Save the model and click on "Build Model".

9.

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

ask-sdk-core==1.11.0
boto3==1.9.216
requests>=2.20.0

10.

Create an OpenAI API key on the API keys page by clicking "+ Create new secret key".

11.

Replace your lambda_functions.py file with the provided lambda_function.py.

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

# Set your OpenAI API key
api_key = "YOUR_API_KEY"

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"

        session_attr = handler_input.attributes_manager.session_attributes
        session_attr["chat_history"] = []

        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

        session_attr = handler_input.attributes_manager.session_attributes
        if "chat_history" not in session_attr:
            session_attr["chat_history"] = []
        response = generate_gpt_response(session_attr["chat_history"], query)
        session_attr["chat_history"].append((query, response))

        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(chat_history, new_question):
    """Generates a GPT response to a new question"""
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    url = "https://api.openai.com/v1/chat/completions"
    messages = [{"role": "system", "content": "You are a helpful assistant. Answer in 50 words or less."}]
    for question, answer in chat_history[-10:]:
        messages.append({"role": "user", "content": question})
        messages.append({"role": "assistant", "content": answer})
    messages.append({"role": "user", "content": new_question})
    
    data = {
        "model": "gpt-3.5-turbo-0125",
        "messages": messages,
        "max_tokens": 300,
        "temperature": 0.5
    }
    try:
        response = requests.post(url, headers=headers, data=json.dumps(data))
        response_data = response.json()
        if response.ok:
            return response_data['choices'][0]['message']['content']
        else:
            return f"Error {response.status_code}: {response_data['error']['message']}"
    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()

12.

Put your OpenAI API key that you got from your OpenAI account

openai_api_key

13.

Save and deploy. Go to "Test" section and enable "Skill testing" in "Development".

development_enabled

14.

You are now ready to use your Alexa in ChatGPT mode. You should see results like this:

test

Please note that running this skill will incur costs for using both AWS Lambda and the OpenAI API. Make sure you understand the pricing structure and monitor your usage to avoid unexpected charges.

alexa-gpt's People

Contributors

csperle avatar ghzeni avatar k4l1sh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

alexa-gpt's Issues

Help not working, not published

When trying to publish the Skill it is getting rejected :
The help prompt must end with a question for users or prompt the user to speak and leave the session open to receive a response.
2024-05-18_19h57_28

Not sure what the problem is as i dont see a coding issue.
I am inexperienced in coding so take it easy on me :)

Can you support updating to the API of Google Gemini Pro?

Hi, alexa-gpt is very good, thank you very much. I recently experienced gemini pro and it feels better than chatgpt3.5. If google upgrade to gemini ultra in the future, I believe it will not be worse than gpt4. Can you support upgrading to Gemini Pro?

Impossible to make this skill work now

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

Error in response

image
Hello sir,
i am getting this as my response

Error 429: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.

I am currently using a free tier account in openai. do i need to upgrade. I am only a hobbyist. which subscription would i need to add sir
I have also shared the screenshot
thanks!

Alexa's 10 second timeout for skill response workaround

Hello! I've been using this skill for quite a lot of things recently, but the 10-second timeout really is a buzzkill for me.

For those who are not aware, this is what I'm talking about.

Since this limitation is for the skill to provide a response to the user and not for a request inside the skill to provide a response, I was wondering if it isn't possible to create a separate thread for the request to run, and in case the request takes up more than 7 seconds, provide an initial response of alexa saying something like "One moment, please." and provide the actual answer afterwards.

Any feedbacks on this? Thanks!

Skill only responds to "Are you alexa?"

Hello!

Thank you so much for creating this, it's such a helpful guide on how to create a gpt skill for Alexa. I've been using it for a while now, but a few weeks ago my skill stopped working. I double-checked everything according to your code and everything seems to be the same, but my skill will only respond to the question "Are you alexa?" and crash if I ask it anything else.

I am also very clueless on how to properly debug this. I can provide you with some logs once I find out where to find 'em ๐Ÿ˜„

Error generating response: 'choices'?

I'm not very familiar with Amazon developer console or a frequent user of OpenAI API. So there is a 99.9% chance I'm doing something stupid. However:

I was able to import the project using the Import Skill feature.
I was able to input my key into the api_key variable in lambda_function.py as api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
I was able to verify the requirements.txt, lambda_function.py, and JSON content all match the project here.
I am able to Build and Deploy the Skill without any errors.
I did get an error complaining chat was too generic an invocation and had to select an alternate invocation. I settled on "chat g p t" and from within test when I type "open chat g p t" I get the expected "Chat G.P.T. mode activated".

However once Chat G.P.T. mode is activated the only responses I get are Error generating response: 'choices'

I did generate a new API key from OpenAI with All permissions.
I have OpenAI API funds available from a previous Tasker project.

Any help would be appreciated.

"Preview is not available in your region"

When I go to test the skill, I'm getting "Sorry, this preview is not available in your region. In the meantime, try asking me for a joke, or a song."

I think my regions are all set correctly. Not sure whats going on here?

How to use more recent models?

hello, I've tried to use more recent models, but it doesn't seem to work..
any hint?

 data = {
        "model": "gpt-4-1106",
        "messages": messages,
        "max_tokens": 300,
        "temperature": 0.5
    }

ty

TTS Response

Hello, I have created a skill in Portuguese (Brazil), and I received the following error message:

"The skill's TTS response should be presented in a language supported by Alexa and match the locale of the skill.
The skill's TTS response should be presented in a language supported by Alexa and matches the locale of the skill. If the skill supports multiple locales, the TTS responses for each locale has to be presented in the relevant language. The following TTS responses were found in a language that does not match the skill's locale.
Response : Leaving Chat G. P. T. mode, TTS Language Found: English
Response : Vaginal Davis is an American artist, performer, and musician known for her work in the fields of performance art, music, and film. She is a pioneering figure in the queer and punk scenes, challenging societal norms and pushing boundaries with her provocative and unique artistic expressions., TTS Language Found: English
Response : Chat G. P. T. mode activated, TTS Language Found: English
Response : Brenda Marshall was an American actress known for her roles in classic films such as "The Sea Hawk" and "The Constant Nymph. " She was active in Hollywood during the 1940s and 1950s, often appearing in supporting roles alongside top stars of the era., TTS Language Found: English
Response : Sorry, I had trouble doing what you asked. Please try again., TTS Language Found: English"

I have no idea how to fix this. Could you help, please?

Error when building

Sample utterance " {query}" in intent "GptQueryIntent" must include a carrier phrase. Sample intent utterances with phrase types cannot consist of only slots.

Can I customize the prompt?

I want to automatically bring up the prompt after chatgpt starts. Where can I modify it? The modification I made in lambda_function.py seems to be invalid.

def generate_gpt_response(chat_history, new_question): try: messages = [{"role": "system", "content": "I want you to act as a spoken English teacher and improver. I will speak to you in English and you will reply to me in English to practice my spoken English. I want you to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors."}] for question, answer in chat_history[-10:]: messages.append({"role": "user", "content": question}) messages.append({"role": "assistant", "content": answer}) messages.append({"role": "user", "content": new_question}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", 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)}"

Error import OpenAI

I tried you code but I get an error when invoking the skill.
I removed all calls to OpenAI and the skill is invoked correctly.

When I add from openai import OpenAI to lambda_function.py I get the error.

In the requirements.txt file I add openai==1.3.3 and I tried also with version 1.12.0, but nothing.

Can someone help me?
Thanks.

Not working on device

Imported and working fine in the Test pane. But doesn't respond to those Invocation via voice on device.

  • 'Sorry I don't know that' (invocation set to 'upgrade chat')
  • 'Not available in your region' ('chat chat')

Can see the skill under Dev in your skills on mobile, tried disabling/re-enabling, re-doing the process from start. All saved and built/deployed. No way to enter GPT mode from audio on device.

GPT 4?

Can this code work with gpt-4-0125-preview? I tried changing the model to this but got some errors.

Wondering if it's a limitation of OpenAI when used in this way, or if the code just isn't written for gpt4.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.