k4l1sh / alexa-gpt

A tutorial on how to use ChatGPT in Alexa
MIT License
204 stars 44 forks source link

Can I customize the prompt? #6

Closed xinyonghu2015 closed 11 months ago

xinyonghu2015 commented 11 months ago

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)}"

k4l1sh commented 11 months ago

Yes, you can modify to make it start requesting the ChatGPT API. To do that you will need to modify the LaunchRequestHandler

Instead of:

speak_output = "Chat G.P.T. mode activated"

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

You can change for something like:

prompt = "YOUR PROMPT HERE"
session_attr = handler_input.attributes_manager.session_attributes
session_attr["chat_history"] = []
speak_output = generate_gpt_response(chat_history, prompt)
session_attr["chat_history"].append((prompt, speak_output))
xinyonghu2015 commented 11 months ago

Thank you very much k4l1sh, the problem is solved. But I modified the next part of the code.

def handle(self, handler_input):
    # type: (HandlerInput) -> Response
    prompt = "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.."
    session_attr = handler_input.attributes_manager.session_attributes  
    session_attr["chat_history"] = []
    speak_output = generate_gpt_response(session_attr["chat_history"], prompt)
    session_attr["chat_history"].append((prompt, speak_output))