|
3 | 3 | from slack_bolt import Assistant, BoltContext, Say, SetSuggestedPrompts |
4 | 4 | from slack_bolt.context.get_thread_context import GetThreadContext |
5 | 5 | from slack_sdk import WebClient |
| 6 | +from slack_sdk.models.blocks import Block, ContextActionsBlock, FeedbackButtonsElement, FeedbackButtonObject |
6 | 7 |
|
7 | 8 | from ..llm_caller import call_llm |
8 | 9 |
|
9 | 10 | # Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details |
10 | 11 | assistant = Assistant() |
11 | 12 |
|
12 | 13 |
|
| 14 | +def create_feedback_block() -> List[Block]: |
| 15 | + """ |
| 16 | + Create feedback block with thumbs up/down buttons |
| 17 | +
|
| 18 | + Returns: |
| 19 | + Block Kit context_actions block |
| 20 | + """ |
| 21 | + blocks: List[Block] = [ |
| 22 | + ContextActionsBlock( |
| 23 | + elements=[ |
| 24 | + FeedbackButtonsElement( |
| 25 | + action_id="feedback", |
| 26 | + positive_button=FeedbackButtonObject( |
| 27 | + text="Good Response", |
| 28 | + accessibility_label="Submit positive feedback on this response", |
| 29 | + value="good-feedback", |
| 30 | + ), |
| 31 | + negative_button=FeedbackButtonObject( |
| 32 | + text="Bad Response", |
| 33 | + accessibility_label="Submit negative feedback on this response", |
| 34 | + value="bad-feedback", |
| 35 | + ), |
| 36 | + ) |
| 37 | + ] |
| 38 | + ) |
| 39 | + ] |
| 40 | + return blocks |
| 41 | + |
| 42 | + |
13 | 43 | # This listener is invoked when a human user opened an assistant thread |
14 | 44 | @assistant.thread_started |
15 | 45 | def start_assistant_thread( |
@@ -83,25 +113,26 @@ def respond_in_assistant_thread( |
83 | 113 | messages_in_thread.append({"role": role, "content": message["text"]}) |
84 | 114 |
|
85 | 115 | returned_message = call_llm(messages_in_thread) |
| 116 | + |
86 | 117 | client.assistant_threads_setStatus( |
87 | 118 | channel_id=channel_id, thread_ts=thread_ts, status="Bolt is typing", loading_messages=loading_messages |
88 | 119 | ) |
| 120 | + |
89 | 121 | stream_response = client.chat_startStream( |
90 | 122 | channel=channel_id, |
91 | 123 | thread_ts=thread_ts, |
92 | 124 | ) |
93 | 125 | stream_ts = stream_response["ts"] |
| 126 | + |
94 | 127 | # use of this for loop is specific to openai response method |
95 | 128 | for event in returned_message: |
96 | 129 | if event.type == "response.output_text.delta": |
97 | 130 | client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}") |
98 | 131 | else: |
99 | 132 | continue |
100 | 133 |
|
101 | | - client.chat_stopStream( |
102 | | - channel=channel_id, |
103 | | - ts=stream_ts, |
104 | | - ) |
| 134 | + feedback_block = create_feedback_block() |
| 135 | + client.chat_stopStream(channel=channel_id, ts=stream_ts, blocks=feedback_block) |
105 | 136 |
|
106 | 137 | except Exception as e: |
107 | 138 | logger.exception(f"Failed to handle a user message event: {e}") |
|
0 commit comments