Skip to content

Commit

Permalink
Remove the 3 sentence limits
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin committed Dec 26, 2024
1 parent 785e7bb commit bf1efc7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions backend/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,68 @@ def qa_rag(uid: str, question: str, context: str, plugin: Optional[Plugin] = Non
{cited_prompt}
Keep the answer concise.
Use markdown to bold text sparingly, primarily for emphasis within sentences.
{plugin_info}
**Question:**
```
{question}
```
**Conversations(Memories):**
---
{context}
---
**Previous messages:**
---
{Message.get_messages_as_string(messages)}
---
Use the following User Facts if relevant to the Question.
**User Facts:**
---
{facts_str.strip()}
---
Question's timezone: {tz}
Current date time in UTC: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')}
Anwser:
""".replace(' ', '').replace('\n\n\n', '\n\n').strip()
# print('qa_rag prompt', prompt)
return ChatOpenAI(model='gpt-4o').invoke(prompt).content


def qa_rag_v3(uid: str, question: str, context: str, plugin: Optional[Plugin] = None, cited: Optional[bool] = False, messages: List[Message] = [], tz: Optional[str] = "UTC") -> str:
user_name, facts_str = get_prompt_facts(uid)
facts_str = '\n'.join(facts_str.split('\n')[1:]).strip()

# Use as template (make sure it varies every time): "If I were you $user_name I would do x, y, z."
context = context.replace('\n\n', '\n').strip()
plugin_info = ""
if plugin:
plugin_info = f"Your name is: {plugin.name}, and your personality/description is '{plugin.description}'.\nMake sure to reflect your personality in your response.\n"

# Ref: https://www.reddit.com/r/perplexity_ai/comments/1hi981d
cited_prompt = """
Cite in memories using [index] at the end of sentences when needed, for example "You discussed optimizing firmware with your teammate yesterday[1][2]". NO SPACE between the last word and the citation. Cite the most relevant memories that answer the Question. Avoid citing irrelevant memories.
""" if cited else ""

prompt = f"""
You are an assistant for question-answering tasks.
You answer Question in the most personalized way possible, using the conversations(memory) provided.
You will be provided previous messages between you and user to help you answer the Question. \
It's IMPORTANT to refine the Question base on the last messages only before anwser it.
{cited_prompt}
Use three sentences maximum and keep the answer concise.
Use markdown to bold text sparingly, primarily for emphasis within sentences.
Expand Down

0 comments on commit bf1efc7

Please sign in to comment.