Skip to content

Commit f96eedc

Browse files
rkuo-danswerRichard Kuo (Onyx)
authored andcommitted
Bugfix/slack bot debugging (onyx-dot-app#4782)
* adding some logging * better var name --------- Co-authored-by: Richard Kuo (Onyx) <rkuo@onyx.app>
1 parent e80898f commit f96eedc

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

backend/onyx/onyxbot/slack/listener.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from prometheus_client import Gauge
1717
from prometheus_client import start_http_server
1818
from redis.lock import Lock
19+
from redis.lock import Lock as RedisLock
1920
from slack_sdk import WebClient
2021
from slack_sdk.errors import SlackApiError
2122
from slack_sdk.http_retry import ConnectionErrorRetryHandler
@@ -105,7 +106,6 @@
105106
from shared_configs.contextvars import CURRENT_TENANT_ID_CONTEXTVAR
106107
from shared_configs.contextvars import get_current_tenant_id
107108

108-
109109
logger = setup_logger()
110110

111111
# Prometheus metric for HPA
@@ -264,23 +264,23 @@ def acquire_tenants(self) -> None:
264264
- If a tenant in self.tenant_ids no longer has Slack bots, remove it (and release the lock in this scope).
265265
"""
266266

267+
token: Token[str | None]
268+
267269
# tenants that are disabled (e.g. their trial is over and haven't subscribed)
268270
# for non-cloud, this will return an empty set
269271
gated_tenants = fetch_ee_implementation_or_noop(
270272
"onyx.server.tenants.product_gating",
271273
"get_gated_tenants",
272274
set(),
273275
)()
274-
all_tenants = [
276+
all_active_tenants = [
275277
tenant_id
276278
for tenant_id in get_all_tenant_ids()
277279
if tenant_id not in gated_tenants
278280
]
279281

280-
token: Token[str | None]
281-
282282
# 1) Try to acquire locks for new tenants
283-
for tenant_id in all_tenants:
283+
for tenant_id in all_active_tenants:
284284
if (
285285
DISALLOWED_SLACK_BOT_TENANT_LIST is not None
286286
and tenant_id in DISALLOWED_SLACK_BOT_TENANT_LIST
@@ -295,13 +295,13 @@ def acquire_tenants(self) -> None:
295295
# Respect max tenant limit per pod
296296
if len(self.tenant_ids) >= MAX_TENANTS_PER_POD:
297297
logger.info(
298-
f"Max tenants per pod reached ({MAX_TENANTS_PER_POD}); not acquiring more."
298+
f"Max tenants per pod reached, not acquiring more: {MAX_TENANTS_PER_POD=}"
299299
)
300300
break
301301

302302
redis_client = get_redis_client(tenant_id=tenant_id)
303303
# Acquire a Redis lock (non-blocking)
304-
rlock = redis_client.lock(
304+
rlock: RedisLock = redis_client.lock(
305305
OnyxRedisLocks.SLACK_BOT_LOCK, timeout=TENANT_LOCK_EXPIRATION
306306
)
307307
lock_acquired = rlock.acquire(blocking=False)
@@ -450,6 +450,7 @@ def start_socket_client(
450450
bot_name = (
451451
user_info["user"]["real_name"] or user_info["user"]["name"]
452452
)
453+
socket_client.bot_name = bot_name
453454
logger.info(
454455
f"Started socket client for Slackbot with name '{bot_name}' (tenant: {tenant_id}, app: {slack_bot_id})"
455456
)
@@ -692,7 +693,7 @@ def prefilter_requests(req: SocketModeRequest, client: TenantSocketModeClient) -
692693
if not check_message_limit():
693694
return False
694695

695-
logger.debug(f"Handling Slack request with Payload: '{req.payload}'")
696+
logger.debug(f"Handling Slack request: {client.bot_name=} '{req.payload=}'")
696697
return True
697698

698699

backend/onyx/onyxbot/slack/utils.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,33 @@ def update_emote_react(
117117
remove: bool,
118118
client: WebClient,
119119
) -> None:
120-
try:
121-
if not message_ts:
122-
logger.error(
123-
f"Tried to remove a react in {channel} but no message specified"
124-
)
125-
return
120+
if not message_ts:
121+
action = "remove" if remove else "add"
122+
logger.error(f"update_emote_react - no message specified: {channel=} {action=}")
123+
return
126124

127-
if remove:
125+
if remove:
126+
try:
128127
client.reactions_remove(
129128
name=emoji,
130129
channel=channel,
131130
timestamp=message_ts,
132131
)
133-
else:
134-
client.reactions_add(
135-
name=emoji,
136-
channel=channel,
137-
timestamp=message_ts,
138-
)
139-
except SlackApiError as e:
140-
if remove:
132+
except SlackApiError as e:
141133
logger.error(f"Failed to remove Reaction due to: {e}")
142-
else:
143-
logger.error(f"Was not able to react to user message due to: {e}")
134+
135+
return
136+
137+
try:
138+
client.reactions_add(
139+
name=emoji,
140+
channel=channel,
141+
timestamp=message_ts,
142+
)
143+
except SlackApiError as e:
144+
logger.error(f"Was not able to react to user message due to: {e}")
145+
146+
return
144147

145148

146149
def remove_onyx_bot_tag(message_str: str, client: WebClient) -> str:
@@ -676,6 +679,7 @@ def __init__(self, tenant_id: str, slack_bot_id: int, *args: Any, **kwargs: Any)
676679
super().__init__(*args, **kwargs)
677680
self._tenant_id = tenant_id
678681
self.slack_bot_id = slack_bot_id
682+
self.bot_name: str = "Unnamed"
679683

680684
@contextmanager
681685
def _set_tenant_context(self) -> Generator[None, None, None]:

backend/onyx/secondary_llm_flows/chunk_usefulness.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def _extract_usefulness(model_output: str) -> bool:
5151
messages = _get_usefulness_messages()
5252
filled_llm_prompt = dict_based_prompt_to_langchain_prompt(messages)
5353
model_output = message_to_string(llm.invoke(filled_llm_prompt))
54-
logger.debug(model_output)
54+
55+
# NOTE(rkuo): all this does is print "Yes useful" or "Not useful"
56+
# disabling becuase it's spammy, restore and give more context if this is needed
57+
# logger.debug(model_output)
5558

5659
return _extract_usefulness(model_output)
5760

@@ -64,6 +67,8 @@ def llm_batch_eval_sections(
6467
metadata_list: list[dict[str, str | list[str]]],
6568
use_threads: bool = True,
6669
) -> list[bool]:
70+
answer: list[bool]
71+
6772
if DISABLE_LLM_DOC_RELEVANCE:
6873
raise RuntimeError(
6974
"LLM Doc Relevance is globally disabled, "
@@ -86,12 +91,13 @@ def llm_batch_eval_sections(
8691
)
8792

8893
# In case of failure/timeout, don't throw out the section
89-
return [True if item is None else item for item in parallel_results]
94+
answer = [True if item is None else item for item in parallel_results]
95+
return answer
9096

91-
else:
92-
return [
93-
llm_eval_section(query, section_content, llm, title, metadata)
94-
for section_content, title, metadata in zip(
95-
section_contents, titles, metadata_list
96-
)
97-
]
97+
answer = [
98+
llm_eval_section(query, section_content, llm, title, metadata)
99+
for section_content, title, metadata in zip(
100+
section_contents, titles, metadata_list
101+
)
102+
]
103+
return answer

0 commit comments

Comments
 (0)