forked from inovasolutions-io/slack_ai_agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (37 loc) · 1.45 KB
/
main.py
File metadata and controls
51 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from dotenv import load_dotenv
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from langgraph.prebuilt import create_react_agent
from langchain.tools.retriever import create_retriever_tool
from tools.search import get_vectorstore
import os
load_dotenv()
bot_token = os.environ.get("SLACK_BOT_TOKEN")
app_token = os.environ.get("SLACK_APP_TOKEN")
if not bot_token or not app_token:
raise ValueError(
"Missing required environment variables: SLACK_BOT_TOKEN and/or SLACK_APP_TOKEN"
)
app = App(token=bot_token)
vectorstore = get_vectorstore()
retriever_tool = create_retriever_tool(
vectorstore.as_retriever(),
name="search",
description="Retrieve information about the company. You will call this tool when you need to answer a question that you do not know the answer to.",
)
agent = create_react_agent(model="openai:gpt-4o-mini", tools=[retriever_tool])
@app.event("message")
def handle_message_events(body, logger):
"""Handle general message events."""
logger.info(body)
@app.event("app_mention")
def handle_hello(body, say):
event = body["event"]
message = event["text"]
thread_ts = event.get("thread_ts", event["ts"])
response = agent.invoke({"messages": [{"role": "user", "content": message}]})
text = response["messages"][-1].content
say(text=text, thread_ts=thread_ts)
if __name__ == "__main__":
handler = SocketModeHandler(app, app_token)
handler.start()