Skip to content

Commit

Permalink
Merge pull request #14 from khoj-ai/features/hook-up-khoj-and-whatsapp
Browse files Browse the repository at this point in the history
Have Flint talk to the Khoj backend
  • Loading branch information
sabaimran authored Jan 23, 2024
2 parents 5aa179b + 8c83e84 commit 04d403e
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 402 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ dependencies = [
"schedule == 1.1.0",
"python-multipart >= 0.0.6",
"openai >= 0.27.8",
"langchain >= 0.0.187",
"django == 4.2.4",
"psycopg2-binary == 2.9.6",
"pgvector >= 0.2.1",
"django-phonenumber-field == 7.1.0",
"phonenumbers == 8.13.17",
"tiktoken >= 0.3.0",
"gunicorn == 21.2.0",
]
dynamic = ["version"]
Expand Down
71 changes: 2 additions & 69 deletions src/flint/configure.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
# Standard Packages
from collections import defaultdict
import logging
from datetime import timedelta, datetime
from datetime import datetime

# External Packages
from fastapi import FastAPI
from asgiref.sync import sync_to_async
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
HumanMessagePromptTemplate,
)
from langchain.memory import ConversationBufferMemory
from langchain.schema import SystemMessage
import requests
import schedule

# Internal Packages
from flint.db.models import Conversation
from flint.state import llm, telemetry
from flint.state import telemetry
from flint.constants import telemetry_server
from flint.helpers import log_telemetry
from flint.prompt import system_prompt

# Keep Django module import here to avoid import ordering errors
from django.contrib.auth.models import User
Expand All @@ -30,54 +21,6 @@
logger = logging.getLogger(__name__)


def configure_chat_prompt():
return ChatPromptTemplate(
messages=[
SystemMessage(content=system_prompt.format()),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{question}"),
]
)


def initialize_conversation_sessions() -> defaultdict[str, ConversationBufferMemory]:
"Initialize the Conversation Sessions"
logger.info("Initializing Conversation Sessions")
conversation_sessions = defaultdict(
lambda: ConversationBufferMemory(memory_key="chat_history", return_messages=True, llm=llm)
)
# Get Conversations from the database within the last 24 hours
conversations = Conversation.objects.filter(created_at__gte=datetime.now() - timedelta(hours=24))
users = User.objects.filter(conversations__in=conversations).distinct()

for user in users:
conversations = Conversation.objects.filter(user=user)[:10]
conversations = conversations[::-1]
# Reconstruct the conversation sessions from the database
for conversation in conversations:
conversation_sessions[conversation.user.khojuser.uuid].chat_memory.add_user_message(
conversation.user_message
)
conversation_sessions[conversation.user.khojuser.uuid].chat_memory.add_ai_message(conversation.bot_message)

return conversation_sessions


async def get_recent_conversations(user: User, uuid: str) -> ConversationBufferMemory:
"Get the recent conversations for the user and construct a new ConversationBufferMemory object"
conversation_buffer_memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, llm=llm)
conversations = Conversation.objects.filter(user=user)[:10]
conversations = await sync_to_async(list)(conversations.all())
# Reconstruct the conversation sessions from the database
for conversation in conversations[::-1]:
if len(conversation.user_message) > 0:
conversation_buffer_memory.chat_memory.add_user_message(conversation.user_message)
if len(conversation.bot_message) > 0:
conversation_buffer_memory.chat_memory.add_ai_message(conversation.bot_message)

return conversation_buffer_memory


async def save_conversation(user, message, response, user_message_type="text"):
"Save the conversation to the database"

Expand Down Expand Up @@ -124,13 +67,3 @@ def upload_telemetry():
logger.error(f"📡 Error uploading telemetry: {e}", exc_info=True)
finally:
telemetry.clear()


@schedule.repeat(schedule.every(1439).minutes)
def clear_conversations():
from flint import state

start_time = datetime.now()
logger.info("Re-initializing conversation sessions")
state.conversation_sessions = initialize_conversation_sessions()
logger.info(f"Re-initializing conversation sessions took {datetime.now() - start_time}")
21 changes: 11 additions & 10 deletions src/flint/constants.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import os

telemetry_server = "https://khoj.beta.haletic.com/v1/telemetry"
KHOJ_WHATSAPP_PROD = "whatsapp:+18488004242"
KHOJ_WHATSAPP_DEBUG = "whatsapp:+14155238886"
KHOJ_INTRO_MESSAGE = f"""
Nice to meet you! I am Khoj, your dedicated personal AI 👋🏽. I can help you with:
- 📜 Free-form journaling and note-taking
- 🧠 Answering general knowledge questions
- 💡 Ideating over new ideas
- 🖊️ Being a scratchpad for your thoughts, links, concepts that you want to remember
- 🌄 Making images from your own words (try typing `/dream`)
- 📝 Retrieving information from your own knowledge base shared with Khoj on the cloud. See https://khoj.dev for more info
You can also send me voice messages in your native language 🎙️.
I'm constantly learning and improving. I'm still in beta, so please report any bugs or issues to my creators on GitHub: https://github.com/khoj-ai/flint/issues
I'm constantly learning and improving, so please report any issues to my creators on GitHub: https://github.com/khoj-ai/flint/issues
""".strip()
KHOJ_PROMPT_EXCEEDED_MESSAGE = f"""
I'm sorry, I can't read messages that are so long. Could you please shorten your message and try again? I'm constantly learning and improving, so I'll be able to read longer messages soon.
"""
MAX_TOKEN_LIMIT_PROMPT = 12000

KHOJ_FAILED_AUDIO_TRANSCRIPTION_MESSAGE = f"""
Sorry, I wasn't able to understand your voice message this time. Could you please try typing out your message or send a shorter audio file? If you'd like to help me improve, email my creators at [email protected].
""".strip()
KHOJ_UNSUPPORTED_MEDIA_TYPE_MESSAGE = f"""
Sorry, I'm still learning and can only understand text or voice message right now. Maybe share the content as a text message with me? Or if you really need this feature, email my creators at [email protected].
Sorry, I wasn't able to understand your voice message this time. Could you please try typing out your message or send a shorter message? If you'd like to help me improve, email my creators at [email protected].
""".strip()

KHOJ_API_URL = os.getenv("KHOJ_API_URL", "https://khoj.dev")
KHOJ_API_CLIENT_ID = os.getenv("KHOJ_API_CLIENT_ID")
KHOJ_API_CLIENT_SECRET = os.getenv("KHOJ_API_CLIENT_SECRET")
86 changes: 0 additions & 86 deletions src/flint/db/management/commands/notify_users.py

This file was deleted.

Loading

0 comments on commit 04d403e

Please sign in to comment.