Improve understand primer and load understand response as dict

This commit is contained in:
Debanjum Singh Solanky 2021-11-28 13:04:16 +05:30
parent 67c3cd7372
commit 1785047ea6
2 changed files with 5 additions and 5 deletions

View file

@ -95,12 +95,12 @@ def chat(q: str):
meta_log = processor_config.conversation.meta_log
# Converse with OpenAI GPT
user_message_metadata = understand(q, api_key=processor_config.conversation.openai_api_key)
gpt_response = converse(q, chat_log, api_key=processor_config.conversation.openai_api_key)
metadata = understand(q, api_key=processor_config.conversation.openai_api_key)
# Update Conversation History
processor_config.conversation.chat_log = message_to_prompt(q, chat_log, gpt_message=gpt_response)
processor_config.conversation.meta_log= message_to_log(q, user_message_metadata, gpt_response, meta_log)
processor_config.conversation.meta_log= message_to_log(q, metadata, gpt_response, meta_log)
return {'status': 'ok', 'response': gpt_response}

View file

@ -13,7 +13,7 @@ def understand(text, api_key=None, temperature=0.5, max_tokens=100):
"""
# Initialize Variables
openai.api_key = api_key or os.getenv("OPENAI_API_KEY")
understand_primer = "Extract intent, trigger emotion information as JSON from each chat message\n\n- intent\n - remember(memory-type, data);\n - memory-type=[\"companion\", \"notes\", \"ledger\", \"image\", \"music\"]\n - search(search-type, data);\n - search-type=[\"google\"]\n - generate(activity);\n - activity=[\"paint\",\"write\", \"chat\"]\n- trigger-emotion(emotion)\n - emotion=[\"happy\",\"confidence\",\"fear\",\"surprise\",\"sadness\",\"disgust\",\"anger\", \"curiosity\", \"calm\"]\n\nQ: How are you doing?\nA: { \"intent\": [\"activity\", \"chat\"], \"trigger-emotion\": \"surprise\" }\nQ: Do you remember what I told you about my brother Antoine when we were at the beach?\nA: { \"intent\": [\"remember\", \"notes\", \"Brother Antoine when we were at the beach\"], \"trigger-emotion\": \"curiosity\" }\nQ: what did we talk about last time?\nA: { \"intent\": [\"remember\", \"notes\", \"talk last time\"], \"trigger-emotion\": \"curiosity\" }\nQ: Let's make some drawings!\nA: { \"intent\": [\"generate\", \"paint\"], \"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\": [\"generate\", \"write\", \"A story about some adventure\"], \"trigger-emotion\": \"fear\" }\nQ: What fiction book was I reading last week about AI starship?\nA: { \"intent\": [\"remember\", \"notes\", \"fiction book about AI starship last week\"], \"trigger-emotion\": \"curiosity\" }\nQ: How much did I spend at Subway for dinner last time?\nA: { \"intent\": [\"remember\", \"ledger\", \"last Subway dinner\"], \"trigger-emotion\": \"curiosity\" }\nQ: I'm feeling sleepy\nA: { \"intent\": [\"activity\", \"chat\"], \"trigger-emotion\": \"calm\" }\nQ: What was that popular Sri lankan song that Alex had mentioned?\nA: { \"intent\": [\"remember\", \"music\", \"popular Sri lankan song mentioned by Alex\"], \"trigger-emotion\": \"curiosity\" } \nQ: You're pretty funny!\nA: { \"intent\": [\"activity\", \"chat\"], \"trigger-emotion\": \"pride\" }\nQ: Can you recommend a movie to watch from my notes?\nA: { \"intent\": [\"remember\", \"notes\", \"movie to watch\"], \"trigger-emotion\": \"curiosity\" }",
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\" }"
# Setup Prompt with Understand Primer
prompt = message_to_prompt(text, understand_primer, start_sequence="\nA:", restart_sequence="\nQ:")
@ -31,7 +31,7 @@ def understand(text, api_key=None, temperature=0.5, max_tokens=100):
# Extract, Clean Message from GPT's Response
story = response['choices'][0]['text']
return str(story)
return json.loads(story)
def converse(text, conversation_history=None, api_key=None, temperature=0.9, max_tokens=150):
@ -74,7 +74,7 @@ def message_to_prompt(user_message, conversation_history="", gpt_message=None, s
def message_to_log(user_message, user_message_metadata, gpt_message, conversation_log=[]):
"""Create json logs from messages, metadata for conversation log"""
# Create json log from Human's message
human_log = json.loads(user_message_metadata)
human_log = user_message_metadata
human_log["message"] = user_message
human_log["by"] = "Human"
human_log["created"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")