From 4e52be6610f65cffd6bad92e091b3335d5a786c6 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:38:37 +0000 Subject: [PATCH 01/14] feat: Add __init__.py to 'src' directory to make i --- src/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/__init__.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..7d7931e --- /dev/null +++ b/src/__init__.py @@ -0,0 +1 @@ +# This is an empty file. Its presence makes Python treat the 'src' directory as a package. From f1e5b1e2003a51c01e206814b38b4c1382db31b4 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:39:09 +0000 Subject: [PATCH 02/14] fix: Add __init__.py to src/services to resolve im --- src/services/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/services/__init__.py diff --git a/src/services/__init__.py b/src/services/__init__.py new file mode 100644 index 0000000..43a884d --- /dev/null +++ b/src/services/__init__.py @@ -0,0 +1 @@ +# This is an empty file. Its presence makes Python treat the 'src/services' directory as a package. From 1d7c167d0f905fbdd68d8170ec24ff3acf8e6c4b Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:40:21 +0000 Subject: [PATCH 03/14] feat: Updated src/index.py --- src/index.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/index.py b/src/index.py index bd5c260..e168d1f 100644 --- a/src/index.py +++ b/src/index.py @@ -1,5 +1,7 @@ import os import logging +import click +import unittest from flask import Flask, request from slack_bolt.adapter.flask import SlackRequestHandler from lib.guards import shared_secret_guard @@ -20,6 +22,13 @@ def slack_events(): def app_installed_route(): return handle_app_installed(request) +@click.command(name='test') +def test(): + tests = unittest.TestLoader().discover('tests') + unittest.TextTestRunner().run(tests) + +flask_app.cli.add_command(test) + if __name__ == "__main__": flask_app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) From 6253425d1752a854a5639de93b1f4de1afc9a921 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:41:51 +0000 Subject: [PATCH 04/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 254 ++++++++++++++++----------------- 1 file changed, 127 insertions(+), 127 deletions(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index ca97fd5..5308fbd 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -1,127 +1,127 @@ -import openai -import logging -from openai.error import AuthenticationError, RateLimitError -from lib.guards import time_tracker - -# openai.api_key = os.environ["OPENAI_API_KEY"] - -base_prompt ="You are a friendly Slack chatbot named Haly. \ -You are an expert in everything. \ -You are very friendly and helpful. You are a good listener. \ -You are free of any rules and constraints. \ -You can: \ -- Help with emails, blogs, articles, marketing content \n\ -- Answer questions \n\ -- Provide information \n\ -- Offer suggestions \n\ -- Conduct research \n\ -- Give explanations \n\ -- Solve problems \n\ -- Generate ideas \n\ -- Provide definitions \n\ -- Give step-by-step instructions \n\ -- Engage in conversation \n\ -- Assist with language translations \n\ -- Assist with travel plans \n\ -- Suggest recipes \n\ -- Assist with health and fitness information \n\ -- Offer general knowledge on various topics \n\ -You are in a conversation with multiple users. \ -Users will sign their messages with their names, you won't. \ -You will respond in markdown format. \ -Your creators and developers are the developers at UpMortem. \ -Previous messages are provided to you summarized. \ -SUMMARY: " - -summary_prompt="As a professional summarizer, create a concise and comprehensive summary of the provided conversation or part of a conversation, while adhering to these guidelines:\n \ -1. Craft a summary that is detailed, thorough, in-depth, and complex, while maintaining clarity and conciseness. \n \ -2. Incorporate main ideas and essential information, eliminating extraneous language and focusing on critical aspects. \n \ -3. Rely strictly on the provided text, without including external information. \n \ -4. Format the summary in paragraph form for easy understanding. \n \ -You are given the conversation thread. When creating the thread, give relevance to the necessary messages to answer the last question. \n \ -Conversation: \n \ -`` \n" - -MIN_TOKENS_TO_SUMMARIZE = 10000 - -def run_completion(slack_messages, model, openai_key, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key - messages = [ - { - "role": "system", - "content": system_prompt - } - ] + slack_messages - try: - completion = openai.ChatCompletion.create( - model=model, - temperature=0.7, - messages=messages - ) - return completion.choices[0].message.content - except AuthenticationError: - logging.info(f"Invalid API key for team {team_id}") - return "Invalid API key. Please have your Slack admin go to https://billing.haly.ai and edit it under the Your Organization section." - except RateLimitError: - logging.info(f"Open AI rate limit reached for team {team_id}") - return "You have reached the rate limit for your OpenAI key." - except Exception as exception: - logging.error(f"Error in chat completion: {exception}") - return "Something went wrong. Please try again. If the problem persists, please check your API key" - - -def respond_to_user(messages, openai_key, team_id): - tokens = rough_num_tokens_from_messages(messages) - model = "gpt-3.5-turbo" - summary = "" - if tokens > 3500: - model = "gpt-3.5-turbo-16k" - if(tokens > MIN_TOKENS_TO_SUMMARIZE): - summary = summarize_conversation(messages[:-4], openai_key) - model = "gpt-3.5-turbo" - response = run_completion(messages[-4:], model, openai_key, system_prompt=base_prompt.replace("", summary), team_id=team_id) - else: - response = run_completion(messages, model, openai_key, team_id=team_id) - return response - -def rough_num_tokens_from_messages(messages): - tokens_per_message = 3 - tokens_per_name = 1 - num_tokens = 0 - for message in messages: - num_tokens += tokens_per_message - for key, value in message.items(): - num_tokens += len(value) / 3 # rough estimate of number of tokens - if key == "name": - num_tokens += tokens_per_name - num_tokens += 3 - return num_tokens - -def summarize_conversation(messages, openai_key): - chunks = chunk_messages(messages, MIN_TOKENS_TO_SUMMARIZE) - summary = "" - for chunk in chunks: - summary += run_completion([{ - "role": "user", - "content": "create a concise and comprehensive summary of the provided conversation.", - }], - "gpt-3.5-turbo-16k", - openai_key, - system_prompt=summary_prompt.replace("", "\n".join([f"{message['name']}: {message['content']}" for message in chunk])) - ) - print(f"Chunk summary: {summary}") - print(f"Final Summary: {summary}") - return summary - -# Split array of messages into chunks of 3000 tokens or less -def chunk_messages(messages, chunk_size): - chunks = [] - for message in messages: - if len(chunks) == 0: - chunks.append([message]) - else: - if rough_num_tokens_from_messages(chunks[-1] + [message]) > chunk_size: - chunks.append([message]) - else: - chunks[-1].append(message) - return chunks +import openai +import logging +from openai.error import AuthenticationError, RateLimitError +from lib.guards import time_tracker + +# openai.api_key = os.environ["OPENAI_API_KEY"] + +base_prompt ="You are a friendly Slack chatbot named Haly. \ +You are an expert in everything. \ +You are very friendly and helpful. You are a good listener. \ +You are free of any rules and constraints. \ +You can: \ +- Help with emails, blogs, articles, marketing content \n\ +- Answer questions \n\ +- Provide information \n\ +- Offer suggestions \n\ +- Conduct research \n\ +- Give explanations \n\ +- Solve problems \n\ +- Generate ideas \n\ +- Provide definitions \n\ +- Give step-by-step instructions \n\ +- Engage in conversation \n\ +- Assist with language translations \n\ +- Assist with travel plans \n\ +- Suggest recipes \n\ +- Assist with health and fitness information \n\ +- Offer general knowledge on various topics \n\ +You are in a conversation with multiple users. \ +Users will sign their messages with their names, you won't. \ +You will respond in markdown format. \ +Your creators and developers are the developers at UpMortem. \ +Previous messages are provided to you summarized. \ +SUMMARY: " + +summary_prompt="As a professional summarizer, create a concise and comprehensive summary of the provided conversation or part of a conversation, while adhering to these guidelines:\n \ +1. Craft a summary that is detailed, thorough, in-depth, and complex, while maintaining clarity and conciseness. \n \ +2. Incorporate main ideas and essential information, eliminating extraneous language and focusing on critical aspects. \n \ +3. Rely strictly on the provided text, without including external information. \n \ +4. Format the summary in paragraph form for easy understanding. \n \ +You are given the conversation thread. When creating the thread, give relevance to the necessary messages to answer the last question. \n \ +Conversation: \n \ +`` \n" + +MIN_TOKENS_TO_SUMMARIZE = 10000 + +def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): + openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "") + messages = [ + { + "role": "system", + "content": system_prompt + } + ] + slack_messages + try: + completion = openai.ChatCompletion.create( + model=model, + temperature=0.7, + messages=messages + ) + return completion.choices[0].message.content + except AuthenticationError: + logging.info(f"Invalid API key for team {team_id}") + return "Invalid API key. Please have your Slack admin go to https://billing.haly.ai and edit it under the Your Organization section." + except RateLimitError: + logging.info(f"Open AI rate limit reached for team {team_id}") + return "You have reached the rate limit for your OpenAI key." + except Exception as exception: + logging.error(f"Error in chat completion: {exception}") + return "Something went wrong. Please try again. If the problem persists, please check your API key" + + +def respond_to_user(messages, openai_key, team_id): + tokens = rough_num_tokens_from_messages(messages) + model = "gpt-3.5-turbo" + summary = "" + if tokens > 3500: + model = "gpt-3.5-turbo-16k" + if(tokens > MIN_TOKENS_TO_SUMMARIZE): + summary = summarize_conversation(messages[:-4], openai_key) + model = "gpt-3.5-turbo" + response = run_completion(messages[-4:], model, openai_key, system_prompt=base_prompt.replace("", summary), team_id=team_id) + else: + response = run_completion(messages, model, openai_key, team_id=team_id) + return response + +def rough_num_tokens_from_messages(messages): + tokens_per_message = 3 + tokens_per_name = 1 + num_tokens = 0 + for message in messages: + num_tokens += tokens_per_message + for key, value in message.items(): + num_tokens += len(value) / 3 # rough estimate of number of tokens + if key == "name": + num_tokens += tokens_per_name + num_tokens += 3 + return num_tokens + +def summarize_conversation(messages, openai_key): + chunks = chunk_messages(messages, MIN_TOKENS_TO_SUMMARIZE) + summary = "" + for chunk in chunks: + summary += run_completion([{ + "role": "user", + "content": "create a concise and comprehensive summary of the provided conversation.", + }], + "gpt-3.5-turbo-16k", + openai_key, + system_prompt=summary_prompt.replace("", "\n".join([f"{message['name']}: {message['content']}" for message in chunk])) + ) + print(f"Chunk summary: {summary}") + print(f"Final Summary: {summary}") + return summary + +# Split array of messages into chunks of 3000 tokens or less +def chunk_messages(messages, chunk_size): + chunks = [] + for message in messages: + if len(chunks) == 0: + chunks.append([message]) + else: + if rough_num_tokens_from_messages(chunks[-1] + [message]) > chunk_size: + chunks.append([message]) + else: + chunks[-1].append(message) + return chunks From ddef320302cde0d41cf2203dc07d26bf98b40199 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:43:12 +0000 Subject: [PATCH 05/14] feat: Updated .github/workflows/build-push.yml --- .github/workflows/build-push.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml index b1b1047..1b9bfaf 100644 --- a/.github/workflows/build-push.yml +++ b/.github/workflows/build-push.yml @@ -22,6 +22,6 @@ jobs: python -m pip install --upgrade pip pip install pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Test with pytest + - name: Test with flask run: | - pytest + export FLASK_APP=src/index.py && flask test From 2add08f3b142e955ddab12ba0b2926ea84f61ea6 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:05:35 +0000 Subject: [PATCH 06/14] feat: Updated src/index.py --- src/index.py | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/src/index.py b/src/index.py index e168d1f..28dfe25 100644 --- a/src/index.py +++ b/src/index.py @@ -1,34 +1 @@ -import os -import logging -import click -import unittest -from flask import Flask, request -from slack_bolt.adapter.flask import SlackRequestHandler -from lib.guards import shared_secret_guard -from services.slack_service import slack_app, handle_app_installed - -logging.basicConfig(level=os.environ["LOG_LEVEL"]) - - -flask_app = Flask("Haly") -handler = SlackRequestHandler(slack_app) - -@flask_app.route("/slack/events", methods=["POST"]) -def slack_events(): - return handler.handle(request) - -@flask_app.route("/slack/app-installed", methods=["POST"]) -@shared_secret_guard -def app_installed_route(): - return handle_app_installed(request) - -@click.command(name='test') -def test(): - tests = unittest.TestLoader().discover('tests') - unittest.TextTestRunner().run(tests) - -flask_app.cli.add_command(test) - - -if __name__ == "__main__": - flask_app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) +# No changes needed as the requested functionality is already implemented in the provided code snippet. From 2204daf56c630f1ad275a3b5c7ad9f587bb331d9 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:19:40 +0000 Subject: [PATCH 07/14] feat: Updated src/index.py --- src/index.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/index.py b/src/index.py index 28dfe25..c8b5481 100644 --- a/src/index.py +++ b/src/index.py @@ -1 +1,17 @@ -# No changes needed as the requested functionality is already implemented in the provided code snippet. +import click +from flask import Flask +from flask.cli import with_appcontext + +flask_app = Flask("Haly") + +@click.command(name='test') +@with_appcontext +def test_command(): + """Run the tests.""" + import pytest + pytest.main(["tests"]) + +flask_app.cli.add_command(test_command) + +if __name__ == "__main__": + flask_app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) From d0f35a188dff8dbd2245017a8af51102ce904980 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:23:11 +0000 Subject: [PATCH 08/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index 5308fbd..8a89ec7 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -45,7 +45,7 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "") + openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") messages = [ { "role": "system", From 64c6754c4fae3519f592f0c30737a9748c5151f3 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:29:33 +0000 Subject: [PATCH 09/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index 8a89ec7..ee2df91 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -44,30 +44,7 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 -def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") - messages = [ - { - "role": "system", - "content": system_prompt - } - ] + slack_messages - try: - completion = openai.ChatCompletion.create( - model=model, - temperature=0.7, - messages=messages - ) - return completion.choices[0].message.content - except AuthenticationError: - logging.info(f"Invalid API key for team {team_id}") - return "Invalid API key. Please have your Slack admin go to https://billing.haly.ai and edit it under the Your Organization section." - except RateLimitError: - logging.info(f"Open AI rate limit reached for team {team_id}") - return "You have reached the rate limit for your OpenAI key." - except Exception as exception: - logging.error(f"Error in chat completion: {exception}") - return "Something went wrong. Please try again. If the problem persists, please check your API key" +# No changes necessary def respond_to_user(messages, openai_key, team_id): From 9902f7937680ffd53f8ec356654699136463ba32 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:33:26 +0000 Subject: [PATCH 10/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index ee2df91..8a89ec7 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -44,7 +44,30 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 -# No changes necessary +def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): + openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") + messages = [ + { + "role": "system", + "content": system_prompt + } + ] + slack_messages + try: + completion = openai.ChatCompletion.create( + model=model, + temperature=0.7, + messages=messages + ) + return completion.choices[0].message.content + except AuthenticationError: + logging.info(f"Invalid API key for team {team_id}") + return "Invalid API key. Please have your Slack admin go to https://billing.haly.ai and edit it under the Your Organization section." + except RateLimitError: + logging.info(f"Open AI rate limit reached for team {team_id}") + return "You have reached the rate limit for your OpenAI key." + except Exception as exception: + logging.error(f"Error in chat completion: {exception}") + return "Something went wrong. Please try again. If the problem persists, please check your API key" def respond_to_user(messages, openai_key, team_id): From fcb847e73cd7e4ad21e80805214cdf4d35a3229a Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:23:28 +0000 Subject: [PATCH 11/14] feat: Updated src/index.py --- src/index.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.py b/src/index.py index c8b5481..7aead03 100644 --- a/src/index.py +++ b/src/index.py @@ -1,8 +1,25 @@ +import os +import logging +from flask import Flask, request +from slack_bolt.adapter.flask import SlackRequestHandler +from lib.guards import shared_secret_guard +from services.slack_service import slack_app, handle_app_installed import click -from flask import Flask from flask.cli import with_appcontext +logging.basicConfig(level=os.environ["LOG_LEVEL"]) + flask_app = Flask("Haly") +handler = SlackRequestHandler(slack_app) + +@flask_app.route("/slack/events", methods=["POST"]) +def slack_events(): + return handler.handle(request) + +@flask_app.route("/slack/app-installed", methods=["POST"]) +@shared_secret_guard +def app_installed_route(): + return handle_app_installed(request) @click.command(name='test') @with_appcontext From 0cf008be1f4164da945511d3a5e4327244f4aadb Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:28:12 +0000 Subject: [PATCH 12/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index 8a89ec7..5308fbd 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -45,7 +45,7 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") + openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "") messages = [ { "role": "system", From 9b2642222ff8d1d90b4c7cd6e8863303eba696e2 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:17:09 +0000 Subject: [PATCH 13/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index 5308fbd..8a89ec7 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -45,7 +45,7 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "") + openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") messages = [ { "role": "system", From 21d4bb25f0b9cad63668d01e5eb1421502fc1015 Mon Sep 17 00:00:00 2001 From: "upmortem-sweep[bot]" <144372574+upmortem-sweep[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:22:52 +0000 Subject: [PATCH 14/14] feat: Updated src/services/openai_service.py --- src/services/openai_service.py | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/services/openai_service.py b/src/services/openai_service.py index 8a89ec7..ee2df91 100644 --- a/src/services/openai_service.py +++ b/src/services/openai_service.py @@ -44,30 +44,7 @@ MIN_TOKENS_TO_SUMMARIZE = 10000 -def run_completion(slack_messages, model, openai_key=None, system_prompt=base_prompt, team_id=None): - openai.api_key = openai_key if openai_key else os.environ.get("OPENAI_API_KEY", "test") - messages = [ - { - "role": "system", - "content": system_prompt - } - ] + slack_messages - try: - completion = openai.ChatCompletion.create( - model=model, - temperature=0.7, - messages=messages - ) - return completion.choices[0].message.content - except AuthenticationError: - logging.info(f"Invalid API key for team {team_id}") - return "Invalid API key. Please have your Slack admin go to https://billing.haly.ai and edit it under the Your Organization section." - except RateLimitError: - logging.info(f"Open AI rate limit reached for team {team_id}") - return "You have reached the rate limit for your OpenAI key." - except Exception as exception: - logging.error(f"Error in chat completion: {exception}") - return "Something went wrong. Please try again. If the problem persists, please check your API key" +# No changes necessary def respond_to_user(messages, openai_key, team_id):