Skip to content

Commit

Permalink
Chat with better date context (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin authored Dec 23, 2024
2 parents 5042cce + 2433112 commit 9317078
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backend/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ class TopicsContext(BaseModel):


class DatesContext(BaseModel):
dates_range: List[datetime] = Field(default=[], description="Dates range. (Optional)")
dates_range: List[datetime] = Field(default=[],
examples=[['2024-12-23T00:00:00+07:00', '2024-12-23T23:59:00+07:00']],
description="Dates range. (Optional)",)


def requires_context_v1(messages: List[Message]) -> bool:
Expand Down Expand Up @@ -406,14 +408,13 @@ def retrieve_context_dates_by_question(question: str, tz: str) -> List[datetime]
prompt = f'''
You MUST determine the appropriate date range in {tz} that provides context for answering the question provided.
Current date: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')} UTC.
Current date time in UTC: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')}
Question:
```
{question}
```
Date range:
'''.replace(' ', '').strip()

# print(prompt)
Expand Down Expand Up @@ -563,7 +564,7 @@ def answer_omi_question(messages: List[Message], context: str) -> str:
""".replace(' ', '').strip()
return llm_mini.invoke(prompt).content

def qa_rag(uid: str, question: str, context: str, plugin: Optional[Plugin] = None, cited: Optional[bool] = False, messages: List[Message] = []) -> str:
def qa_rag(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()

Expand Down Expand Up @@ -615,6 +616,10 @@ def qa_rag(uid: str, question: str, context: str, plugin: Optional[Plugin] = Non
{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)
Expand Down
1 change: 1 addition & 0 deletions backend/utils/retrieval/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def qa_handler(state: GraphState):
state.get("plugin_selected"),
cited=state.get("cited"),
messages=state.get("messages"),
tz=state.get("tz"),
)
return {"answer": response, "ask_for_nps": True}

Expand Down

0 comments on commit 9317078

Please sign in to comment.