Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhongsun96 committed Dec 15, 2024
1 parent ada81a6 commit c7dd6c6
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 5 deletions.
6 changes: 4 additions & 2 deletions backend/onyx/auth/noauth_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from onyx.auth.schemas import UserRole
from onyx.configs.constants import KV_NO_AUTH_USER_PREFERENCES_KEY
from onyx.configs.constants import NO_AUTH_USER_EMAIL
from onyx.configs.constants import NO_AUTH_USER_ID
from onyx.key_value_store.store import KeyValueStore
from onyx.key_value_store.store import KvKeyNotFoundError
from onyx.server.manage.models import UserInfo
Expand All @@ -30,8 +32,8 @@ def load_no_auth_user_preferences(store: KeyValueStore) -> UserPreferences:

def fetch_no_auth_user(store: KeyValueStore) -> UserInfo:
return UserInfo(
id="__no_auth_user__",
email="[email protected]",
id=NO_AUTH_USER_ID,
email=NO_AUTH_USER_EMAIL,
is_active=True,
is_superuser=False,
is_verified=True,
Expand Down
32 changes: 32 additions & 0 deletions backend/onyx/chat/process_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from onyx.configs.chat_configs import DISABLE_LLM_CHOOSE_SEARCH
from onyx.configs.chat_configs import MAX_CHUNKS_FED_TO_CHAT
from onyx.configs.constants import MessageType
from onyx.configs.constants import MilestoneRecordType
from onyx.configs.constants import NO_AUTH_USER_ID
from onyx.context.search.enums import OptionalSearchSetting
from onyx.context.search.enums import QueryFlow
from onyx.context.search.enums import SearchType
Expand All @@ -53,6 +55,9 @@
from onyx.db.chat import translate_db_message_to_chat_message_detail
from onyx.db.chat import translate_db_search_doc_to_server_search_doc
from onyx.db.engine import get_session_context_manager
from onyx.db.milestone import check_multi_assistant_milestone
from onyx.db.milestone import create_milestone_if_not_exists
from onyx.db.milestone import update_user_assistant_milestone
from onyx.db.models import SearchDoc as DbSearchDoc
from onyx.db.models import ToolCall
from onyx.db.models import User
Expand Down Expand Up @@ -117,6 +122,7 @@
from onyx.tools.tool_runner import ToolCallFinalResult
from onyx.utils.logger import setup_logger
from onyx.utils.long_term_log import LongTermLogger
from onyx.utils.telemetry import mt_cloud_telemetry
from onyx.utils.timing import log_function_time
from onyx.utils.timing import log_generator_function_time
from shared_configs.contextvars import CURRENT_TENANT_ID_CONTEXTVAR
Expand Down Expand Up @@ -356,6 +362,32 @@ def stream_chat_message_objects(
if not persona:
raise RuntimeError("No persona specified or found for chat session")

multi_assistant_milestone, _is_new = create_milestone_if_not_exists(
user=user,
tenant_id=tenant_id,
event_type=MilestoneRecordType.MULTIPLE_ASSISTANTS,
db_session=db_session,
)

update_user_assistant_milestone(
milestone=multi_assistant_milestone,
user_id=str(user.id) if user else NO_AUTH_USER_ID,
assistant_id=persona.id,
db_session=db_session,
)

_, just_hit_multi_assistant_milestone = check_multi_assistant_milestone(
milestone=multi_assistant_milestone,
db_session=db_session,
)

if just_hit_multi_assistant_milestone:
mt_cloud_telemetry(
distinct_id=tenant_id,
event=MilestoneRecordType.MULTIPLE_ASSISTANTS,
properties=None,
)

# If a prompt override is specified via the API, use that with highest priority
# but for saving it, we are just mapping it to an existing prompt
prompt_id = new_msg_req.prompt_id
Expand Down
3 changes: 3 additions & 0 deletions backend/onyx/configs/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
DEFAULT_BOOST = 0
SESSION_KEY = "session"

NO_AUTH_USER_ID = "__no_auth_user__"
NO_AUTH_USER_EMAIL = "[email protected]"

# For chunking/processing chunks
RETURN_SEPARATOR = "\n\r\n"
SECTION_SEPARATOR = "\n\n"
Expand Down
57 changes: 57 additions & 0 deletions backend/onyx/db/milestone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from onyx.db.models import User


USER_ASSISTANT_PREFIX = "user_assistants_used_"
MULTI_ASSISTANT_USED = "multi_assistant_used"


def create_milestone(
user: User | None,
tenant_id: str | None,
Expand Down Expand Up @@ -50,3 +54,56 @@ def create_milestone_if_not_exists(
return create_milestone(user, tenant_id, event_type, db_session), True

return milestones[0], False


def update_milestone_event_tracker(
milestone: Milestone,
event_update: dict,
db_session: Session,
) -> None:
if milestone.event_tracker is None:
milestone.event_tracker = event_update
else:
milestone.event_tracker.update(event_update)
db_session.commit()


def update_user_assistant_milestone(
milestone: Milestone,
user_id: str | None,
assistant_id: int,
db_session: Session,
) -> None:
event_tracker = milestone.event_tracker
if event_tracker is None:
milestone.event_tracker = event_tracker = {}

user_key = f"{USER_ASSISTANT_PREFIX}{user_id}"

if event_tracker.get(user_key) is None:
event_tracker[user_key] = [assistant_id]
else:
event_tracker[user_key].append(assistant_id)

db_session.commit()


def check_multi_assistant_milestone(
milestone: Milestone,
db_session: Session,
) -> tuple[bool, bool]:
"""Returns if the milestone was hit and if it was just hit for the first time"""
event_tracker = milestone.event_tracker
if event_tracker is None:
return False, False

if event_tracker.get(MULTI_ASSISTANT_USED):
return True, False

for key, value in event_tracker.items():
if key.startswith(USER_ASSISTANT_PREFIX) and len(value) > 1:
event_tracker[MULTI_ASSISTANT_USED] = True
db_session.commit()
return True, True

return False, False
2 changes: 1 addition & 1 deletion backend/onyx/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ class Milestone(Base):
)
event_type: Mapped[MilestoneRecordType] = mapped_column(String)
# Need to track counts and specific ids of certain events to know if the Milestone has been reached
event_tracker: Mapped[list[int] | None] = mapped_column(
event_tracker: Mapped[dict | None] = mapped_column(
postgresql.JSONB(), nullable=True
)
time_created: Mapped[datetime.datetime] = mapped_column(
Expand Down
17 changes: 16 additions & 1 deletion backend/onyx/server/features/persona/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from onyx.auth.users import current_user
from onyx.chat.prompt_builder.utils import build_dummy_prompt
from onyx.configs.constants import FileOrigin
from onyx.configs.constants import MilestoneRecordType
from onyx.configs.constants import NotificationType
from onyx.db.engine import get_current_tenant_id
from onyx.db.engine import get_session
from onyx.db.models import User
from onyx.db.notification import create_notification
Expand Down Expand Up @@ -44,6 +46,7 @@
from onyx.server.models import DisplayPriorityRequest
from onyx.tools.utils import is_image_generation_available
from onyx.utils.logger import setup_logger
from onyx.utils.telemetry import create_milestone_and_report


logger = setup_logger()
Expand Down Expand Up @@ -167,14 +170,26 @@ def create_persona(
create_persona_request: CreatePersonaRequest,
user: User | None = Depends(current_user),
db_session: Session = Depends(get_session),
tenant_id: str | None = Depends(get_current_tenant_id),
) -> PersonaSnapshot:
return create_update_persona(
persona_snapshot = create_update_persona(
persona_id=None,
create_persona_request=create_persona_request,
user=user,
db_session=db_session,
)

create_milestone_and_report(
user=user,
tenant_id=tenant_id,
distinct_id=tenant_id or "N/A",
event_type=MilestoneRecordType.CREATED_ASSISTANT,
properties=None,
db_session=db_session,
)

return persona_snapshot


# NOTE: This endpoint cannot update persona configuration options that
# are core to the persona, such as its display priority and
Expand Down
14 changes: 14 additions & 0 deletions backend/onyx/server/manage/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from sqlalchemy.orm import Session

from onyx.auth.users import current_admin_user
from onyx.configs.constants import MilestoneRecordType
from onyx.db.constants import SLACK_BOT_PERSONA_PREFIX
from onyx.db.engine import get_current_tenant_id
from onyx.db.engine import get_session
from onyx.db.models import ChannelConfig
from onyx.db.models import User
Expand All @@ -25,6 +27,7 @@
from onyx.server.manage.models import SlackBotCreationRequest
from onyx.server.manage.models import SlackChannelConfig
from onyx.server.manage.models import SlackChannelConfigCreationRequest
from onyx.utils.telemetry import create_milestone_and_report


router = APIRouter(prefix="/manage")
Expand Down Expand Up @@ -217,6 +220,7 @@ def create_bot(
slack_bot_creation_request: SlackBotCreationRequest,
db_session: Session = Depends(get_session),
_: User | None = Depends(current_admin_user),
tenant_id: str | None = Depends(get_current_tenant_id),
) -> SlackBot:
slack_bot_model = insert_slack_bot(
db_session=db_session,
Expand All @@ -225,6 +229,16 @@ def create_bot(
bot_token=slack_bot_creation_request.bot_token,
app_token=slack_bot_creation_request.app_token,
)

create_milestone_and_report(
user=None,
tenant_id=tenant_id,
distinct_id=tenant_id or "N/A",
event_type=MilestoneRecordType.CREATED_ONYX_BOT,
properties=None,
db_session=db_session,
)

return SlackBot.from_model(slack_bot_model)


Expand Down
2 changes: 1 addition & 1 deletion backend/onyx/server/query_and_chat/chat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def handle_new_chat_message(
chat_message_req: CreateChatMessageRequest,
request: Request,
user: User | None = Depends(current_limited_user),
_: None = Depends(check_token_rate_limits),
_rate_limit_check: None = Depends(check_token_rate_limits),
is_connected_func: Callable[[], bool] = Depends(is_connected),
tenant_id: str = Depends(get_current_tenant_id),
) -> StreamingResponse:
Expand Down

0 comments on commit c7dd6c6

Please sign in to comment.