Created
February 11, 2023 03:18
-
-
Save tiago-lp/4a4ae13664dd96a2ff6127c6f041763b to your computer and use it in GitHub Desktop.
Alexa GPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# colocar nos requirements.txt o seguinte: openai==0.26.1 | |
import openai | |
openai.api_key = "SUA CHAVE AQUI" | |
def get_chat_gtp_response(prompt): | |
response = openai.Completion.create( | |
engine="text-davinci-002", | |
prompt=prompt, | |
temperature=0.5, | |
max_tokens=500, | |
top_p=1, | |
frequency_penalty=1, | |
presence_penalty=1 | |
)['choices'][0]['text'] | |
return response | |
class AskGptIntentHandler(AbstractRequestHandler): | |
def can_handle(self, handler_input): | |
return ask_utils.is_request_type("IntentRequest")(handler_input) and ask_utils.is_intent_name("AskGptIntent")(handler_input) | |
def handle(self, handler_input): | |
query = ask_utils.request_util.get_slot(handler_input, "question").value + "?" | |
try: | |
if query == "sair": | |
return ( | |
handler_input.response_builder.speak("Até a próxima.").response | |
) | |
gpt_response = get_chat_gtp_response(query) | |
except Exception: | |
gpt_response = "Tive algum problema para me comunicar com o chat GPT." | |
return ( | |
handler_input.response_builder | |
.speak(gpt_response) | |
.ask("Pergunte novamente.") | |
.response | |
) | |
# la no final lembrar de sibstituir o HelloWorldIntentHandler por essa classe nova acima AskGptIntentHandler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment