Skip to content

Commit

Permalink
Refactor slack setup
Browse files Browse the repository at this point in the history
Signed-off-by: Di Wang <[email protected]>
  • Loading branch information
hemslo committed Feb 15, 2024
1 parent 5e628db commit 3e697eb
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ OLLAMA_CHAT_MODEL=mistral
OLLAMA_URL=http://host.docker.internal:11434
OPENAI_API_KEY=
OPENAI_CHAT_MODEL='gpt-3.5-turbo-0125'
SLACK_APP_TOKEN=
SLACK_BOT_TOKEN=
SLACK_SIGNING_SECRET=
VERBOSE=0
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ langchain serve
```shell
docker compose up --build
```

## Start Slack socket mode

```shell
slack_socket_mode
```
5 changes: 4 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
load_dotenv()

DEBUG = os.getenv("DEBUG", "0") == "1"
VERBOSE = os.getenv("VERBOSE", "0") == "1"
OLLAMA_CHAT_MODEL = os.getenv("OLLAMA_CHAT_MODEL", "mistral")
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
OPENAI_CHAT_MODEL = os.getenv("OPENAI_CHAT_MODEL", "gpt-3.5-turbo")
SLACK_APP_TOKEN = os.getenv("SLACK_APP_TOKEN")
SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN")
SLACK_SIGNING_SECRET = os.getenv("SLACK_SIGNING_SECRET")
VERBOSE = os.getenv("VERBOSE", "0") == "1"

set_debug(DEBUG)
set_verbose(VERBOSE)
38 changes: 38 additions & 0 deletions app/dependencies/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Annotated

from fastapi import Depends
from slack_bolt import App
from slack_bolt.adapter.fastapi import SlackRequestHandler
from slack_bolt.adapter.socket_mode import SocketModeHandler

from app import config

app = App(
token=config.SLACK_BOT_TOKEN,
signing_secret=config.SLACK_SIGNING_SECRET
)

slack_request_handler = SlackRequestHandler(app)

slack_socket_model_handler = SocketModeHandler(app, config.SLACK_APP_TOKEN)


def get_slack_request_handler() -> SlackRequestHandler:
return slack_request_handler


SlackRequestHandlerDep = Annotated[SlackRequestHandler, Depends(get_slack_request_handler)]

@app.event("message")
def handle_message_events(body, say, logger):
logger.info(body)
text = body.get('event', {}).get('text', '')
if 'hello' in text.lower():
say("Hello there! :wave:")


# Define an event listener for "app_mention" events
@app.event("app_mention")
def handle_app_mention_events(body, say, logger):
logger.info(body)
say("Hello SRE slackers! :blush:")
Empty file added app/routers/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions app/routers/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from fastapi import APIRouter, Request

from app.dependencies.slack import SlackRequestHandlerDep

router = APIRouter()


@router.post("/slack/events")
async def slack_events(request: Request, handler: SlackRequestHandlerDep):
return await handler.handle(request)
3 changes: 3 additions & 0 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from app.chains.extraction import extraction_chain
from app.chains.supervisor import build_supervisor_chain
from app.dependencies.ollama_chat_model import ollama_chat_model
from app.routers import slack

app = FastAPI()

Expand Down Expand Up @@ -39,6 +40,8 @@ async def redirect_root_to_docs():
path="/supervisor",
)

app.include_router(slack.router)

if __name__ == "__main__":
import uvicorn

Expand Down
15 changes: 15 additions & 0 deletions app/slack_socket_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3

from app import config
from app.dependencies.slack import slack_socket_model_handler


def run():
if config.SLACK_APP_TOKEN:
slack_socket_model_handler.start()
else:
print("SLACK_APP_TOKEN not found.")


if __name__ == '__main__':
run()
5 changes: 0 additions & 5 deletions app/tools/slack_bolt_env/requirements.txt

This file was deleted.

19 changes: 0 additions & 19 deletions app/tools/slack_bolt_env/setup.sh

This file was deleted.

47 changes: 0 additions & 47 deletions app/tools/slack_bolt_env/sre-ascent-dev.py

This file was deleted.

45 changes: 40 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ langserve = {extras = ["server"], version = ">=0.0.41"}
pydantic = "<2"
python = "^3.11"
python-dotenv = "^1.0.1"
slack-bolt = "^1.18.1"
uvicorn = "^0.23.2"

[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.15"
ruff = "^0.2.1"

[tool.poetry.scripts]
slack_socket_mode = "app.slack_socket_mode:run"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 3e697eb

Please sign in to comment.