Skip to content

Commit

Permalink
Removed ChatBot Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarthak5598 committed Jul 26, 2024
1 parent a468f13 commit c3c3fc4
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
client = WebClient(token=os.environ["SLACK_TOKEN"])
client.chat_postMessage(channel=DEPLOYS_CHANNEL_NAME, text="bot started v1.9 240611-1 top")

template = """
You're a Software Engineer (Mentor) at OWASP,
Your job is to provide help to contributors with a short message.
Contributor' Question :{Doubt}
"""
# template = """
# You're a Software Engineer (Mentor) at OWASP,
# Your job is to provide help to contributors with a short message.
# Contributor' Question :{Doubt}
# """


openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

cache = TTLCache(maxsize=100, ttl=86400)
# cache = TTLCache(maxsize=100, ttl=86400)


@app.route("/slack/events", methods=["POST"])
Expand Down Expand Up @@ -153,61 +153,61 @@ def handle_message(payload):
text=f"Error sending message: {response['error']}",
)
logging.error(f"Error sending message: {response['error']}")
# if message.get("channel_type") == "im":
# user = message["user"] # The user ID of the person who sent the message
# text = message.get("text", "") # The text of the message
# try:
# if message.get("user") != bot_user_id:
# client.chat_postMessage(channel=JOINS_CHANNEL_ID, text=f"<@{user}> said {text}")
# # Respond to the direct message
# client.chat_postMessage(channel=user, text=f"Hello <@{user}>, you said: {text}")
# except SlackApiError as e:
# print(f"Error sending response: {e.response['error']}")


@slack_events_adapter.on("message")
def gpt_bot(payload):
token_limit = 1000
token_per_prompt = 100
user = "D078YQ93TSL"
message = payload.get("event", {})

if message.get("channel_type") == "im":
doubt = message.get("text", "")
prompt = template.format(doubt=doubt)

today = datetime.now(timezone.utc).date()
rate_limit_key = f"global_daily_request_{today}"
total_token_used = cache.get(rate_limit_key, 0)
doubt_limit = 50
if len(doubt) >= doubt_limit:
client.chat_postMessage(channel=user, text="Please enter less than 50 characters")
return

if total_token_used + token_per_prompt > token_limit:
client.chat_postMessage(channel=user, text="Exceeds Token Limit")
return

try:
response = openai_client.Completion.create(
messages=[{"role": "user", "content": prompt}],
model="gpt-3.5-turbo-0125",
max_tokens=20,
)
answer = response.choices[0].message.content
except Exception as e:
logging.error(f"OpenAI API request failed: {e}")
client.chat_postMessage(
channel=user, text="An error occurred while processing your request."
)
return

user = message["user"] # The user ID of the person who sent the message
text = message.get("text", "") # The text of the message
try:
client.chat_postMessage(channel=user, text=f"{answer}")
cache[rate_limit_key] = total_token_used + token_per_prompt

# Log the user's question and GPT's answer
logging.info(f"User's Question: {doubt}")
logging.info(f"GPT's Answer: {answer}")
if message.get("user") != bot_user_id:
client.chat_postMessage(channel=JOINS_CHANNEL_ID, text=f"<@{user}> said {text}")
# Respond to the direct message
client.chat_postMessage(channel=user, text=f"Hello <@{user}>, you said: {text}")
except SlackApiError as e:
logging.error(f"Error sending message to Slack: {e.response['error']}")
print(f"Error sending response: {e.response['error']}")


# @slack_events_adapter.on("message")
# def gpt_bot(payload):
# token_limit = 1000
# token_per_prompt = 100
# user = "D078YQ93TSL"
# message = payload.get("event", {})

# if message.get("channel_type") == "im":
# doubt = message.get("text", "")
# prompt = template.format(doubt=doubt)

# today = datetime.now(timezone.utc).date()
# rate_limit_key = f"global_daily_request_{today}"
# total_token_used = cache.get(rate_limit_key, 0)
# doubt_limit = 50
# if len(doubt) >= doubt_limit:
# client.chat_postMessage(channel=user, text="Please enter less than 50 characters")
# return

# if total_token_used + token_per_prompt > token_limit:
# client.chat_postMessage(channel=user, text="Exceeds Token Limit")
# return

# try:
# response = openai_client.Completion.create(
# messages=[{"role": "user", "content": prompt}],
# model="gpt-3.5-turbo-0125",
# max_tokens=20,
# )
# answer = response.choices[0].message.content
# except Exception as e:
# logging.error(f"OpenAI API request failed: {e}")
# client.chat_postMessage(
# channel=user, text="An error occurred while processing your request."
# )
# return

# try:
# client.chat_postMessage(channel=user, text=f"{answer}")
# cache[rate_limit_key] = total_token_used + token_per_prompt

# # Log the user's question and GPT's answer
# logging.info(f"User's Question: {doubt}")
# logging.info(f"GPT's Answer: {answer}")
# except SlackApiError as e:
# logging.error(f"Error sending message to Slack: {e.response['error']}")

0 comments on commit c3c3fc4

Please sign in to comment.