mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 21:19:12 +00:00
Wrap prompts for GPT in triple quotes to improve prompt readability
To prompt improve readability: - Remove newline escape sequence and use actual newline directly - This avoids one long line of text as prompt and - Remove escaping of double quotes
This commit is contained in:
@@ -45,7 +45,49 @@ 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\": {\"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\" }"
|
||||
understand_primer = '''
|
||||
Objective: Extract intent and trigger emotion information as JSON from each chat message
|
||||
|
||||
Potential intent types and valid argument values are listed below:
|
||||
- intent
|
||||
- remember(memory-type, query);
|
||||
- memory-type=["companion","notes","ledger","image","music"]
|
||||
- search(search-type, query);
|
||||
- search-type=["google"]
|
||||
- generate(activity, query);
|
||||
- activity=["paint","write","chat"]
|
||||
- trigger-emotion(emotion)
|
||||
- emotion=["happy","confidence","fear","surprise","sadness","disgust","anger","shy","curiosity","calm"]
|
||||
|
||||
Some examples are given below for reference:
|
||||
Q: How are you doing?
|
||||
A: { "intent": {"type": "generate", "activity": "chat", "query": "How are you doing?"}, "trigger-emotion": "happy" }
|
||||
Q: Do you remember what I told you about my brother Antoine when we were at the beach?
|
||||
A: { "intent": {"type": "remember", "memory-type": "companion", "query": "Brother Antoine when we were at the beach"}, "trigger-emotion": "curiosity" }
|
||||
Q: what was that fantasy story you told me last time?
|
||||
A: { "intent": {"type": "remember", "memory-type": "companion", "query": "fantasy story told last time"}, "trigger-emotion": "curiosity" }
|
||||
Q: Let's make some drawings about the stars on a clear full moon night!
|
||||
A: { "intent": {"type": "generate", "activity": "paint", "query": "stars on a clear full moon night"}, "trigger-emotion: "happy" }
|
||||
Q: Do you know anything about Lebanon cuisine in the 18th century?
|
||||
A: { "intent": {"type": "search", "search-type": "google", "query": "lebanon cusine in the 18th century"}, "trigger-emotion; "confidence" }
|
||||
Q: Tell me a scary story
|
||||
A: { "intent": {"type": "generate", "activity": "write", "query": "A scary story"}, "trigger-emotion": "fear" }
|
||||
Q: What fiction book was I reading last week about AI starship?
|
||||
A: { "intent": {"type": "remember", "memory-type": "notes", "query": "fiction book about AI starship last week"}, "trigger-emotion": "curiosity" }
|
||||
Q: How much did I spend at Subway for dinner last time?
|
||||
A: { "intent": {"type": "remember", "memory-type": "ledger", "query": "last Subway dinner"}, "trigger-emotion": "calm" }
|
||||
Q: I'm feeling sleepy
|
||||
A: { "intent": {"type": "generate", "activity": "chat", "query": "I'm feeling sleepy"}, "trigger-emotion": "calm" }
|
||||
Q: What was that popular Sri lankan song that Alex had mentioned?
|
||||
A: { "intent": {"type": "remember", "memory-type": "music", "query": "popular Sri lankan song mentioned by Alex"], "trigger-emotion": "curiosity" }
|
||||
Q: You're pretty funny!
|
||||
A: { "intent": {"type": "generate", "activity": "chat", "query": "You're pretty funny!"}, "trigger-emotion": "shy" }
|
||||
Q: Can you recommend a movie to watch from my notes?
|
||||
A: { "intent": {"type": "remember", "memory-type": "notes", "query": "recommend movie to watch"], "trigger-emotion": "curiosity" }
|
||||
Q: When did I go surfing last?
|
||||
A: { "intent": {"type": "remember", "memory-type": "notes", "query": "When did I go surfing last"], "trigger-emotion": "calm" }
|
||||
Q: Can you dance for me?
|
||||
A: { "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:")
|
||||
@@ -75,12 +117,16 @@ def converse(text, conversation_history=None, api_key=None, temperature=0.9, max
|
||||
# Initialize Variables
|
||||
openai.api_key = api_key or os.getenv("OPENAI_API_KEY")
|
||||
|
||||
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, who are you?{start_sequence} Hi, I am an AI conversational companion created by OpenAI. How can I help you today?"
|
||||
ai_prompt = "AI:"
|
||||
human_prompt = "Human:"
|
||||
conversation_primer = f'''
|
||||
The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and a very friendly companion.
|
||||
|
||||
{human_prompt} Hello, who are you?
|
||||
{ai_prompt} Hi, I am an AI conversational companion created by OpenAI. How can I help you today?'''
|
||||
|
||||
# Setup Prompt with Primer or Conversation History
|
||||
prompt = message_to_prompt(text, conversation_history or conversation_primer, start_sequence=start_sequence, restart_sequence=restart_sequence)
|
||||
prompt = message_to_prompt(text, conversation_history or conversation_primer, start_sequence=ai_prompt, restart_sequence=human_prompt)
|
||||
|
||||
# Get Response from GPT
|
||||
response = openai.Completion.create(
|
||||
|
||||
Reference in New Issue
Block a user