Fix the user intent extraction prompt for GPT. Clean up chatbot test

This commit is contained in:
Debanjum Singh Solanky 2022-01-12 10:36:01 -05:00
parent ea28897cdd
commit 2e53fbc844
2 changed files with 5 additions and 5 deletions

View file

@ -42,7 +42,7 @@ def understand(text, api_key=None, temperature=0.5, max_tokens=100, verbose=0):
"""
# Initialize Variables
openai.api_key = api_key or os.getenv("OPENAI_API_KEY")
understand_primer = "Objective: Extract intent and trigger emotion information as JSON from each chat message\n\nPotential intent types and valid argument values are listed below:\n- intent\n - remember(memory-type, query);\n - memory-type=[\"companion\", \"notes\", \"ledger\", \"image\", \"music\"]\n - search(search-type, query);\n - search-type=[\"google\"]\n - generate(activity, query);\n - activity=[\"paint\",\"write\", \"chat\"]\n- trigger-emotion(emotion)\n - emotion=[\"happy\",\"confidence\",\"fear\",\"surprise\",\"sadness\",\"disgust\",\"anger\", \"shy\", \"curiosity\", \"calm\"]\n\nSome examples are given below for reference:\nQ: How are you doing?\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"How are you doing?\"}, \"trigger-emotion\": \"happy\" }\nQ: Do you remember what I told you about my brother Antoine when we were at the beach?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"companion\", \"query\": \"Brother Antoine when we were at the beach\"}, \"trigger-emotion\": \"curiosity\" }\nQ: what was that fantasy story you told me last time?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"companion\", \"query\": \"fantasy story told last time\"}, \"trigger-emotion\": \"curiosity\" }\nQ: Let's make some drawings about the stars on a clear full moon night!\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"paint\", \"query\": \"stars on a clear full moon night\"}, \"trigger-emotion: \"happy\" }\nQ: Do you know anything about Lebanon cuisine in the 18th century?\nA: { \"intent\": [\"search\", \"google\", \"lebanon cusine in the 18th century\"], \"trigger-emotion; \"confidence\" }\nQ: Tell me a scary story\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"write\", \"query\": \"A scary story\"}, \"trigger-emotion\": \"fear\" }\nQ: What fiction book was I reading last week about AI starship?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"fiction book about AI starship last week\"}, \"trigger-emotion\": \"curiosity\" }\nQ: How much did I spend at Subway for dinner last time?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"ledger\", \"query\": \"last Subway dinner\"}, \"trigger-emotion\": \"calm\" }\nQ: I'm feeling sleepy\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"I'm feeling sleepy\"}, \"trigger-emotion\": \"calm\" }\nQ: What was that popular Sri lankan song that Alex had mentioned?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"music\", \"query\": \"popular Sri lankan song mentioned by Alex\"], \"trigger-emotion\": \"curiosity\" } \nQ: You're pretty funny!\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"You're pretty funny!\"}, \"trigger-emotion\": \"shy\" }\nQ: Can you recommend a movie to watch from my notes?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"recommend movie to watch\"], \"trigger-emotion\": \"curiosity\" }\nQ: When did I go surfing last?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"went surfing last\"], \"trigger-emotion\": \"calm\" }\nQ: Can you dance for me?\nA: { \"intent\": null, \"trigger-emotion\": \"sad\" }"
understand_primer = "Objective: Extract intent and trigger emotion information as JSON from each chat message\n\nPotential intent types and valid argument values are listed below:\n- intent\n - remember(memory-type, query);\n - memory-type=[\"companion\",\"notes\",\"ledger\",\"image\",\"music\"]\n - search(search-type, query);\n - search-type=[\"google\"]\n - generate(activity, query);\n - activity=[\"paint\",\"write\",\"chat\"]\n- trigger-emotion(emotion)\n - emotion=[\"happy\",\"confidence\",\"fear\",\"surprise\",\"sadness\",\"disgust\",\"anger\",\"shy\",\"curiosity\",\"calm\"]\n\nSome examples are given below for reference:\nQ: How are you doing?\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"How are you doing?\"}, \"trigger-emotion\": \"happy\" }\nQ: Do you remember what I told you about my brother Antoine when we were at the beach?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"companion\", \"query\": \"Brother Antoine when we were at the beach\"}, \"trigger-emotion\": \"curiosity\" }\nQ: what was that fantasy story you told me last time?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"companion\", \"query\": \"fantasy story told last time\"}, \"trigger-emotion\": \"curiosity\" }\nQ: Let's make some drawings about the stars on a clear full moon night!\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"paint\", \"query\": \"stars on a clear full moon night\"}, \"trigger-emotion: \"happy\" }\nQ: Do you know anything about Lebanon cuisine in the 18th century?\nA: { \"intent\": {\"type\": \"search\", \"search-type\": \"google\", \"query\": \"lebanon cusine in the 18th century\"}, \"trigger-emotion; \"confidence\" }\nQ: Tell me a scary story\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"write\", \"query\": \"A scary story\"}, \"trigger-emotion\": \"fear\" }\nQ: What fiction book was I reading last week about AI starship?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"fiction book about AI starship last week\"}, \"trigger-emotion\": \"curiosity\" }\nQ: How much did I spend at Subway for dinner last time?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"ledger\", \"query\": \"last Subway dinner\"}, \"trigger-emotion\": \"calm\" }\nQ: I'm feeling sleepy\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"I'm feeling sleepy\"}, \"trigger-emotion\": \"calm\" }\nQ: What was that popular Sri lankan song that Alex had mentioned?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"music\", \"query\": \"popular Sri lankan song mentioned by Alex\"], \"trigger-emotion\": \"curiosity\" } \nQ: You're pretty funny!\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"You're pretty funny!\"}, \"trigger-emotion\": \"shy\" }\nQ: Can you recommend a movie to watch from my notes?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"recommend movie to watch\"], \"trigger-emotion\": \"curiosity\" }\nQ: When did I go surfing last?\nA: { \"intent\": {\"type\": \"remember\", \"memory-type\": \"notes\", \"query\": \"When did I go surfing last\"], \"trigger-emotion\": \"calm\" }\nQ: Can you dance for me?\nA: { \"intent\": {\"type\": \"generate\", \"activity\": \"chat\", \"query\": \"Can you dance for me?\"}, \"trigger-emotion\": \"sad\" }"
# Setup Prompt with Understand Primer
prompt = message_to_prompt(text, understand_primer, start_sequence="\nA:", restart_sequence="\nQ:")

View file

@ -11,7 +11,7 @@ api_key = None
# Test
# ----------------------------------------------------------------------------------------------------
def test_message_to_understand_prompt():
# Setup
# Arrange
understand_primer = "Extract information from each chat message\n\nremember(memory-type, data);\nmemory-type=[\"companion\", \"notes\", \"ledger\", \"image\", \"music\"]\nsearch(search-type, data);\nsearch-type=[\"google\", \"youtube\"]\ngenerate(activity);\nactivity=[\"paint\",\"write\", \"chat\"]\ntrigger-emotion(emotion);\nemotion=[\"happy\",\"confidence\",\"fear\",\"surprise\",\"sadness\",\"disgust\",\"anger\", \"curiosity\", \"calm\"]\n\nQ: How are you doing?\nA: activity(\"chat\"); trigger-emotion(\"surprise\")\nQ: Do you remember what I told you about my brother Antoine when we were at the beach?\nA: remember(\"notes\", \"Brother Antoine when we were at the beach\"); trigger-emotion(\"curiosity\");\nQ: what did we talk about last time?\nA: remember(\"notes\", \"talk last time\"); trigger-emotion(\"curiosity\");\nQ: Let's make some drawings!\nA: generate(\"paint\"); trigger-emotion(\"happy\");\nQ: Do you know anything about Lebanon?\nA: search(\"google\", \"lebanon\"); trigger-emotion(\"confidence\");\nQ: Find a video about a panda rolling in the grass\nA: search(\"youtube\",\"panda rolling in the grass\"); trigger-emotion(\"happy\"); \nQ: Tell me a scary story\nA: generate(\"write\" \"A story about some adventure\"); trigger-emotion(\"fear\");\nQ: What fiction book was I reading last week about AI starship?\nA: remember(\"notes\", \"read fiction book about AI starship last week\"); trigger-emotion(\"curiosity\");\nQ: How much did I spend at Subway for dinner last time?\nA: remember(\"ledger\", \"last Subway dinner\"); trigger-emotion(\"curiosity\");\nQ: I'm feeling sleepy\nA: activity(\"chat\"); trigger-emotion(\"calm\")\nQ: What was that popular Sri lankan song that Alex showed me recently?\nA: remember(\"music\", \"popular Sri lankan song that Alex showed recently\"); trigger-emotion(\"curiosity\"); \nQ: You're pretty funny!\nA: activity(\"chat\"); trigger-emotion(\"pride\")"
expected_response = "Extract information from each chat message\n\nremember(memory-type, data);\nmemory-type=[\"companion\", \"notes\", \"ledger\", \"image\", \"music\"]\nsearch(search-type, data);\nsearch-type=[\"google\", \"youtube\"]\ngenerate(activity);\nactivity=[\"paint\",\"write\", \"chat\"]\ntrigger-emotion(emotion);\nemotion=[\"happy\",\"confidence\",\"fear\",\"surprise\",\"sadness\",\"disgust\",\"anger\", \"curiosity\", \"calm\"]\n\nQ: How are you doing?\nA: activity(\"chat\"); trigger-emotion(\"surprise\")\nQ: Do you remember what I told you about my brother Antoine when we were at the beach?\nA: remember(\"notes\", \"Brother Antoine when we were at the beach\"); trigger-emotion(\"curiosity\");\nQ: what did we talk about last time?\nA: remember(\"notes\", \"talk last time\"); trigger-emotion(\"curiosity\");\nQ: Let's make some drawings!\nA: generate(\"paint\"); trigger-emotion(\"happy\");\nQ: Do you know anything about Lebanon?\nA: search(\"google\", \"lebanon\"); trigger-emotion(\"confidence\");\nQ: Find a video about a panda rolling in the grass\nA: search(\"youtube\",\"panda rolling in the grass\"); trigger-emotion(\"happy\"); \nQ: Tell me a scary story\nA: generate(\"write\" \"A story about some adventure\"); trigger-emotion(\"fear\");\nQ: What fiction book was I reading last week about AI starship?\nA: remember(\"notes\", \"read fiction book about AI starship last week\"); trigger-emotion(\"curiosity\");\nQ: How much did I spend at Subway for dinner last time?\nA: remember(\"ledger\", \"last Subway dinner\"); trigger-emotion(\"curiosity\");\nQ: I'm feeling sleepy\nA: activity(\"chat\"); trigger-emotion(\"calm\")\nQ: What was that popular Sri lankan song that Alex showed me recently?\nA: remember(\"music\", \"popular Sri lankan song that Alex showed recently\"); trigger-emotion(\"curiosity\"); \nQ: You're pretty funny!\nA: activity(\"chat\"); trigger-emotion(\"pride\")\nQ: When did I last dine at Burger King?\nA:"
@ -37,14 +37,14 @@ def test_minimal_chat_with_gpt():
@pytest.mark.skipif(api_key is None,
reason="Set api_key variable to your OpenAI API key from https://beta.openai.com/account/api-keys")
def test_chat_with_history():
# Act
# Arrange
start_sequence="\nAI:"
restart_sequence="\nHuman:"
conversation_primer = f"The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly companion.\n{restart_sequence} Hello, I am testatron. Who are you?{start_sequence} Hi, I am an AI conversational companion created by OpenAI. How can I help you today?"
conversation_history = conversation_primer
response = converse("Can you tell me my name?", conversation_history=conversation_history, api_key=api_key, temperature=0, max_tokens=50)
# Act
response = converse("Can you tell me my name?", conversation_history=conversation_primer, api_key=api_key, temperature=0, max_tokens=50)
# Assert
assert len(response) > 0