Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed May 22, 2024
1 parent c3f03f3 commit 88f4ed7
Show file tree
Hide file tree
Showing 11 changed files with 831 additions and 685 deletions.
551 changes: 54 additions & 497 deletions app/bolt_listeners.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/i18n.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from openai import OpenAI, AzureOpenAI
from openai import OpenAI
from openai.lib.azure import AzureOpenAI
from slack_bolt import BoltContext

from .openai_constants import GPT_3_5_TURBO_0613_MODEL
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions app/openai_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import time
import re
import json
from typing import List, Dict, Any, Tuple, Optional, Union
from typing import List, Dict, Tuple, Optional, Union
from importlib import import_module

from openai import OpenAI, AzureOpenAI
from openai import OpenAI, Stream
from openai.lib.azure import AzureOpenAI
from openai.types import Completion
from openai._streaming import Stream
import tiktoken

from slack_bolt import BoltContext
from slack_sdk.web import WebClient, SlackResponse

from app.markdown import slack_to_markdown, markdown_to_slack
from app.markdown_conversion import slack_to_markdown, markdown_to_slack
from app.openai_constants import (
MAX_TOKENS,
GPT_3_5_TURBO_MODEL,
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions app/slack_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from app.env import OPENAI_TIMEOUT_SECONDS

TIMEOUT_ERROR_MESSAGE = (
f":warning: Apologies! It seems that OpenAI didn't respond within the {OPENAI_TIMEOUT_SECONDS}-second timeframe. "
"Please try your request again later. "
"If you wish to extend the timeout limit, "
"you may consider deploying this app with customized settings on your infrastructure. :bow:"
)

DEFAULT_LOADING_TEXT = ":hourglass_flowing_sand: Wait a second, please ..."
107 changes: 1 addition & 106 deletions app/slack_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from slack_bolt import BoltContext

from app.env import IMAGE_FILE_ACCESS_ENABLED
from app.i18n import translate
from app.markdown import slack_to_markdown
from app.markdown_conversion import slack_to_markdown


# ----------------------------
Expand Down Expand Up @@ -110,110 +109,6 @@ def update_wip_message(
)


# ----------------------------
# Home tab
# ----------------------------

DEFAULT_HOME_TAB_MESSAGE = (
"To enable this app in this Slack workspace, you need to save your OpenAI API key. "
"Visit <https://platform.openai.com/account/api-keys|your developer page> to grap your key!"
)

DEFAULT_HOME_TAB_CONFIGURE_LABEL = "Configure"


def build_home_tab(
*,
openai_api_key: Optional[str],
context: BoltContext,
message: str = DEFAULT_HOME_TAB_MESSAGE,
single_workspace_mode: bool = False,
) -> dict:
original_sentences = "\n".join(
[
f"* {message}",
f"* {DEFAULT_HOME_TAB_CONFIGURE_LABEL}",
"* Can you proofread the following sentence without changing its meaning?",
"* (Start a chat from scratch)",
"* Start",
"* Chat Templates",
"* Configuration",
]
)
translated_sentences = list(
map(
lambda s: s.replace("* ", ""),
translate(
openai_api_key=openai_api_key,
context=context,
text=original_sentences,
).split("\n"),
)
)
message = translated_sentences[0]
configure_label = translated_sentences[1]
proofreading = translated_sentences[2]
from_scratch = translated_sentences[3]
start = translated_sentences[4]
chat_templates = translated_sentences[5]
configuration = translated_sentences[6]

blocks = []
if single_workspace_mode is False:
blocks.extend(
[
{
"type": "section",
"text": {"type": "mrkdwn", "text": f"*{configuration}*"},
},
{"type": "divider"},
{
"type": "section",
"text": {"type": "mrkdwn", "text": message},
"accessory": {
"action_id": "configure",
"type": "button",
"text": {"type": "plain_text", "text": configure_label},
"style": "primary",
"value": "api_key",
},
},
]
)
if openai_api_key is not None:
blocks.extend(
[
{
"type": "section",
"text": {"type": "mrkdwn", "text": f"*{chat_templates}*"},
},
{"type": "divider"},
{
"type": "section",
"text": {"type": "mrkdwn", "text": proofreading},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": start},
"value": proofreading,
"action_id": "templates-proofread",
},
},
{
"type": "section",
"text": {"type": "mrkdwn", "text": from_scratch},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": start},
"value": " ",
"action_id": "templates-from-scratch",
},
},
]
)

return {"type": "home", "blocks": blocks}


# ----------------------------
# Modals
# ----------------------------
Expand Down
Loading

0 comments on commit 88f4ed7

Please sign in to comment.